1 |
#include "manualcommands.h" |
2 |
#include "ui_manualcommands.h" |
3 |
|
4 |
ManualCommands::ManualCommands(QWidget *parent) : |
5 |
QMainWindow(parent), |
6 |
ui(new Ui::ManualCommands) |
7 |
{ |
8 |
ui->setupUi(this); |
9 |
this->setAttribute(Qt::WA_DeleteOnClose,true); //destroy itself once finished. |
10 |
this->myProcess = new QProcess(); |
11 |
this->myProcess->setProcessChannelMode(QProcess::MergedChannels); |
12 |
ui->leManualCommand->installEventFilter(this); |
13 |
|
14 |
this->nextInsertHistoryIdx=0; |
15 |
this->searchHistoryIdx=0; |
16 |
for(int i=0; i<this->limHistory; i++){ |
17 |
this->history[i]=""; //clean array |
18 |
} |
19 |
} |
20 |
|
21 |
ManualCommands::~ManualCommands() |
22 |
{ |
23 |
delete myProcess; |
24 |
delete ui; |
25 |
} |
26 |
|
27 |
void ManualCommands::on_pbInput_clicked() |
28 |
{ |
29 |
executeCommand(); |
30 |
} |
31 |
|
32 |
void ManualCommands::executeCommand(){ |
33 |
|
34 |
QString command=ui->leManualCommand->text().trimmed(); |
35 |
|
36 |
if(command.isEmpty()){ |
37 |
Util::showErrorPopUp("Please input a command first."); |
38 |
return; |
39 |
} |
40 |
|
41 |
//Only add to the history if the last command is different |
42 |
bool different=false; |
43 |
if(this->nextInsertHistoryIdx==0){ //at the limit |
44 |
if(this->history[this->limHistory-1]!=command){ |
45 |
different=true; |
46 |
} |
47 |
} |
48 |
else{ |
49 |
if(this->history[this->nextInsertHistoryIdx-1]!=command){ |
50 |
different=true; |
51 |
} |
52 |
} |
53 |
|
54 |
if(different){ |
55 |
this->history[this->nextInsertHistoryIdx++]=command; //assign and increment |
56 |
|
57 |
if(this->nextInsertHistoryIdx==this->limHistory){ //if we are at the limit begin override |
58 |
this->nextInsertHistoryIdx=0; |
59 |
} |
60 |
} |
61 |
|
62 |
this->myProcess->start(GlobalVars::OniSplitExeName+" "+ui->leManualCommand->text()); |
63 |
this->myProcess->waitForFinished(120000); //wait 2 minutes at maximum |
64 |
ui->ptOutput->appendPlainText("> "+command); |
65 |
ui->ptOutput->appendPlainText(this->myProcess->readAll()); |
66 |
ui->ptOutput->ensureCursorVisible(); |
67 |
ui->ptOutput->verticalScrollBar()->setValue( ui->ptOutput->verticalScrollBar()->maximum() ); |
68 |
ui->leManualCommand->clear(); |
69 |
} |
70 |
|
71 |
void ManualCommands::on_pcCopyClipboard_clicked() |
72 |
{ |
73 |
QApplication::clipboard()->setText(ui->ptOutput->toPlainText()); |
74 |
} |
75 |
|
76 |
void ManualCommands::on_pbClear_clicked() |
77 |
{ |
78 |
if(Util::showQuestionPopUp(this,"Clear the output?")){ |
79 |
ui->ptOutput->clear(); |
80 |
} |
81 |
} |
82 |
|
83 |
//Allows detecting arrows press for history without subclassing lineedit. |
84 |
bool ManualCommands::eventFilter(QObject* obj, QEvent *event) |
85 |
{ |
86 |
if (obj == ui->leManualCommand) |
87 |
{ |
88 |
if (event->type() == QEvent::KeyPress) |
89 |
{ |
90 |
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); |
91 |
if (keyEvent->key() == Qt::Key_Up) //UP ARROW |
92 |
{ |
93 |
|
94 |
int oldValue=this->searchHistoryIdx; |
95 |
|
96 |
//if it isn't the first member of history continue decrementing |
97 |
if(this->searchHistoryIdx!=this->nextInsertHistoryIdx){ //for when it didn't the round |
98 |
this->searchHistoryIdx--; |
99 |
} |
100 |
|
101 |
//start with the last elemented inputted |
102 |
if(ui->leManualCommand->text().trimmed().isEmpty()){ |
103 |
this->searchHistoryIdx=this->nextInsertHistoryIdx-1; |
104 |
} |
105 |
|
106 |
//rotate |
107 |
if(this->searchHistoryIdx < 0){ |
108 |
this->searchHistoryIdx=this->limHistory-1; //start from behind (49) |
109 |
} |
110 |
else if(this->searchHistoryIdx == this->limHistory){ |
111 |
this->searchHistoryIdx=0; //start from 0 again |
112 |
} |
113 |
|
114 |
//not filled yet value? Stop. |
115 |
if(this->history[this->searchHistoryIdx].isEmpty()){ |
116 |
this->searchHistoryIdx=oldValue; |
117 |
return true; |
118 |
} |
119 |
|
120 |
ui->leManualCommand->setText(this->history[this->searchHistoryIdx]); |
121 |
|
122 |
return true; |
123 |
|
124 |
} |
125 |
else if(keyEvent->key() == Qt::Key_Down) //DOWN ARROW |
126 |
{ |
127 |
if(ui->leManualCommand->text().trimmed().isEmpty()){ |
128 |
return true; |
129 |
} |
130 |
|
131 |
int oldValue=this->searchHistoryIdx; |
132 |
|
133 |
//Continue incrementing if it isnt the last member of history |
134 |
if(this->searchHistoryIdx!=this->nextInsertHistoryIdx-1 && //for when it didn't the round |
135 |
!(this->nextInsertHistoryIdx==0 && this->searchHistoryIdx==this->limHistory-1)){ //for when it did the round |
136 |
this->searchHistoryIdx++; |
137 |
} |
138 |
|
139 |
//rotate |
140 |
if(this->searchHistoryIdx < 0){ |
141 |
this->searchHistoryIdx=this->limHistory-1; //start from behind (49) |
142 |
} |
143 |
else if(this->searchHistoryIdx == this->limHistory){ |
144 |
this->searchHistoryIdx=0; //start from 0 again |
145 |
} |
146 |
|
147 |
//not filled yet value? Stop. |
148 |
if(this->history[this->searchHistoryIdx].isEmpty()){ |
149 |
this->searchHistoryIdx=oldValue; |
150 |
return true; |
151 |
} |
152 |
|
153 |
ui->leManualCommand->setText(this->history[this->searchHistoryIdx]); |
154 |
|
155 |
return true; |
156 |
} |
157 |
} |
158 |
return false; |
159 |
} |
160 |
return QMainWindow::eventFilter(obj, event); |
161 |
} |