New Keysets
[pierogi] / forms / pirmacroform.cpp
1 #include "pirmacroform.h"
2 #include "ui_pirmacroform.h"
3
4 #include "mainwindow.h"
5 #include "macros/pirmacro.h"
6 #include "macros/pirmacropack.h"
7 #include "macros/pirmacrocommanditem.h"
8
9 #include <iostream>
10
11 class QTreeWidgetItem;
12
13 /*
14 PIRMacroForm::PIRMacroForm(QWidget *parent) :
15   QWidget(parent),
16   ui(new Ui::PIRMacroForm)
17 {
18   ui->setupUi(this);
19 }
20 */
21
22 PIRMacroForm::PIRMacroForm(
23   MainWindow *mw)
24   : QWidget(0),
25     ui(new Ui::PIRMacroForm),
26     currentMacro(0),
27     userRequestMacro(0),
28     request(No_Request),
29     mainWindow(mw)
30 {
31   ui->setupUi(this);
32
33   PIRMacroPack *userPack = mainWindow->getUserPack();
34   if (userPack)
35   {
36     if (userPack->childCount())
37     {
38       currentMacro = dynamic_cast<PIRMacro *> (userPack->child(0));
39       if (currentMacro)
40       {
41         ui->macroNameLabel->setText(currentMacro->getName());
42         currentMacro->populateList(ui->macroListWidget);
43         ui->addButton->setEnabled(true);
44       }
45     }
46   }
47
48   connect(
49     &smd,
50     SIGNAL(macroChosen(QTreeWidgetItem *)),
51     this,
52     SLOT(displayMacro(QTreeWidgetItem *)));
53
54   connect(
55     &smd,
56     SIGNAL(newMacroRequested()),
57     this,
58     SLOT(createNewMacro()));
59
60   connect(
61     &smd,
62     SIGNAL(editMacroRequested(QTreeWidgetItem *)),
63     this,
64     SLOT(editMacro(QTreeWidgetItem *)));
65
66   connect(
67     &smd,
68     SIGNAL(deleteMacroRequested(QTreeWidgetItem *)),
69     this,
70     SLOT(deleteMacro(QTreeWidgetItem *)));
71
72   // Set up the select macro dialog:
73   smd.addPack(mw->getUserPack());
74   smd.addPack(mw->getMultitapPack());
75
76   // Connect to edit command dialog signals:
77 /*
78   connect(
79     &ecd,
80     SIGNAL(deleteCommand()),
81     this,
82     SLOT(deleteCurrentCommand()));
83
84   connect(
85     &ecd,
86     SIGNAL(copyCommand()),
87     this,
88     SLOT(copyCurrentCommand()));
89 */
90
91 /*
92   connect(
93     &ecd,
94     SIGNAL(moveCommandUp()),
95     this,
96     SLOT(moveCurrentCommandUp()));
97
98   connect(
99     &ecd,
100     SIGNAL(moveCommandDown()),
101     this,
102     SLOT(moveCurrentCommandDown()));
103 */
104 }
105
106
107 PIRMacroForm::~PIRMacroForm()
108 {
109   delete ui;
110 }
111
112
113 void PIRMacroForm::on_chooseMacroButton_clicked()
114 {
115   request = No_Request;
116   smd.exec();
117
118   // Loop until the user either selects a macro to display,
119   // or makes no request:
120   while (request != No_Request)
121   {
122     switch (request)
123     {
124     case DisplayMacro_Request:
125       currentMacro = userRequestMacro;
126       userRequestMacro = 0;
127
128       ui->macroNameLabel->setText(currentMacro->getName());
129
130       // Turn on the "new" button
131       ui->addButton->setEnabled(true);
132
133       currentMacro->populateList(ui->macroListWidget);
134
135       return;
136
137     case NewMacro_Request:
138       emd.clear();
139       if (emd.exec() == QDialog::Accepted)
140       {
141         // Create the new macro:
142         currentMacro = new PIRMacro(
143           mainWindow->getUserPack(),
144           emd.getText(),
145           emd.getKey(),
146           emd.getButtonID(),
147           mainWindow);
148
149         ui->macroNameLabel->setText(emd.getText());
150
151         // Turn on the "new" button
152         ui->addButton->setEnabled(true);
153
154         mainWindow->updateUserButtons();
155
156         ui->macroListWidget->clear();
157
158         mainWindow->storeMacros();
159
160         return;
161       }
162       break;
163
164     case EditMacro_Request:
165       emd.setup(userRequestMacro);
166       if (emd.exec() == QDialog::Accepted)
167       {
168         // Store the changes:
169         userRequestMacro->setName(emd.getText());
170         userRequestMacro->setKey(emd.getKey());
171         userRequestMacro->setButtonID(emd.getButtonID());
172
173         mainWindow->updateUserButtons();
174
175         mainWindow->storeMacros();
176       }
177       userRequestMacro = 0;
178       break;
179
180     case DeleteMacro_Request:
181       dmd.setup(userRequestMacro);
182       if (dmd.exec() == QDialog::Accepted)
183       {
184         if (currentMacro == userRequestMacro)
185         {
186           currentMacro = 0;
187           ui->macroNameLabel->setText("No Macro Selected");
188           ui->addButton->setEnabled(false);
189           ui->macroListWidget->clear();
190         }
191
192         // Delete the macro:
193         delete userRequestMacro;
194
195         // Make sure no dangling buttons exist:
196         mainWindow->updateUserButtons();
197
198         mainWindow->storeMacros();
199
200         smd.resetIndices();
201       }
202       userRequestMacro = 0;
203       break;
204
205     default:
206       break;
207     }
208
209     // Return to the selector dialog:
210     request = No_Request;
211     smd.exec();
212   }
213 }
214
215
216 void PIRMacroForm::on_runButton_clicked()
217 {
218   if (currentMacro) currentMacro->executeMacro();
219 }
220
221
222 void PIRMacroForm::displayMacro(
223   QTreeWidgetItem *item)
224 {
225   if (!item) return;
226
227   PIRMacro *m = dynamic_cast<PIRMacro *> (item);
228
229   if (!m) return;
230
231   userRequestMacro = m;
232   request = DisplayMacro_Request;
233 }
234
235
236 QComboBox *PIRMacroForm::getKeysetComboBox()
237 {
238   return ccd.getKeysetComboBox();
239 }
240
241
242 void PIRMacroForm::createNewMacro()
243 {
244   request = NewMacro_Request;
245 }
246
247
248 void PIRMacroForm::editMacro(
249   QTreeWidgetItem *item)
250 {
251   if (!item) return;
252   userRequestMacro = dynamic_cast<PIRMacro *> (item);
253   request = EditMacro_Request;
254 }
255
256
257 void PIRMacroForm::deleteMacro(
258   QTreeWidgetItem *item)
259 {
260   if (!item) return;
261   userRequestMacro = dynamic_cast<PIRMacro *> (item);
262   request = DeleteMacro_Request;
263 }
264
265
266 void PIRMacroForm::on_addButton_clicked()
267 {
268   // Sanity check:
269   if (!currentMacro) return;
270
271   // Initialize the choose command dialog:
272   ccd.init();
273
274   // Check whether the user has actually created a command:
275   if (ccd.exec() != QDialog::Accepted) return;
276
277   int index = ui->macroListWidget->currentRow();
278
279   switch (ccd.getCommandType())
280   {
281   case Keyset_Command:
282     currentMacro->appendCommand(
283       new PIRKeysetCommandItem(
284         ccd.getKeysetID(),
285         mainWindow));
286
287     currentMacro->populateList(ui->macroListWidget);
288     ui->macroListWidget->setCurrentRow(index);
289
290     mainWindow->storeMacros();
291
292     break;
293
294   case Key_Command:
295     currentMacro->appendCommand(
296       new PIRKeyCommandItem(
297         ccd.getKeyName(),
298         mainWindow));
299
300     currentMacro->populateList(ui->macroListWidget);
301     ui->macroListWidget->setCurrentRow(index);
302
303     mainWindow->storeMacros();
304
305     break;
306
307   case Pause_Command:
308     currentMacro->appendCommand(
309       new PIRPauseCommandItem(
310         ccd.getTimeToWait()));
311
312     currentMacro->populateList(ui->macroListWidget);
313     ui->macroListWidget->setCurrentRow(index);
314
315     mainWindow->storeMacros();
316
317     break;
318
319   case No_Command:
320   default:
321     break;
322   }
323 }
324
325
326 //void PIRMacroForm::deleteCurrentCommand()
327 void PIRMacroForm::on_deleteButton_clicked()
328 {
329   if (!currentMacro) return;
330
331   int index = ui->macroListWidget->currentRow();
332
333   if (currentMacro->deleteCommand(index))
334   {
335     currentMacro->populateList(ui->macroListWidget);
336
337     if (index == ui->macroListWidget->count()) --index;
338
339     if (index)
340     {
341       ui->macroListWidget->setCurrentRow(index);
342     }
343
344     mainWindow->storeMacros();
345   }
346 }
347
348
349 //void PIRMacroForm::copyCurrentCommand()
350 void PIRMacroForm::on_copyButton_clicked()
351 {
352   if (!currentMacro) return;
353
354   // copy constructor dealing with inheritance issues?
355 }
356
357
358 //void PIRMacroForm::moveCurrentCommandUp()
359 void PIRMacroForm::on_commandUpButton_clicked()
360 {
361   if (!currentMacro) return;
362
363   int index = ui->macroListWidget->currentRow();
364
365   if ((index > 0) && currentMacro->moveUp(index))
366   {
367     currentMacro->populateList(ui->macroListWidget);
368     ui->macroListWidget->setCurrentRow(index - 1);
369
370     mainWindow->storeMacros();
371   }
372 }
373
374
375 //void PIRMacroForm::moveCurrentCommandDown()
376 void PIRMacroForm::on_commandDownIcon_clicked()
377 {
378   if (!currentMacro) return;
379
380   int index = ui->macroListWidget->currentRow();
381
382   if ((index < (ui->macroListWidget->count() - 1) &&
383       currentMacro->moveDown(index)))
384   {
385     currentMacro->populateList(ui->macroListWidget);
386     ui->macroListWidget->setCurrentRow(index + 1);
387
388     mainWindow->storeMacros();
389   }
390 }
391
392
393 void PIRMacroForm::on_macroListWidget_currentRowChanged(int currentRow)
394 {
395   if (currentRow < 0)
396   {
397     ui->deleteButton->setEnabled(false);
398   }
399   else
400   {
401     ui->deleteButton->setEnabled(true);
402   }
403 }
404
405
406 //void PIRMacroForm::on_macroListWidget_itemClicked(QListWidgetItem *item)
407 /*
408 void PIRMacroForm::on_macroListWidget_itemClicked()
409 {
410   ecd.setup(
411     currentMacro->getCommandType(ui->macroListWidget->currentRow()),
412     currentMacro->getCommandName(ui->macroListWidget->currentRow()));
413
414   ecd.exec();
415 }
416 */
417
418
419 /*
420 void PIRMacroForm::on_newMacroButton_clicked()
421 {
422   emd.clear();
423
424   // If no new macro, just give up:
425   if (emd.exec() != QDialog::Accepted) return;
426
427   // Create the new macro:
428   currentMacro = new PIRMacro(
429     mainWindow->getUserPack(),
430     emd.getText(),
431     emd.getKey(),
432     emd.getButtonID());
433
434   ui->macroNameLabel->setText(emd.getText());
435
436   // Turn on the "new" button
437   ui->addButton->setEnabled(true);
438
439   if (emd.getButtonID())
440   {
441     mainWindow->updateUserButtons();
442   }
443
444   ui->macroListWidget->clear();
445
446   mainWindow->storeMacros();
447 }
448 */