46463204f20460d56c295427b83a70976971a1eb
[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   connect(
78     &ecd,
79     SIGNAL(deleteCommand()),
80     this,
81     SLOT(deleteCurrentCommand()));
82
83   connect(
84     &ecd,
85     SIGNAL(moveCommandUp()),
86     this,
87     SLOT(moveCurrentCommandUp()));
88
89   connect(
90     &ecd,
91     SIGNAL(moveCommandDown()),
92     this,
93     SLOT(moveCurrentCommandDown()));
94 }
95
96
97 PIRMacroForm::~PIRMacroForm()
98 {
99   delete ui;
100 }
101
102
103 void PIRMacroForm::on_chooseMacroButton_clicked()
104 {
105   request = No_Request;
106   smd.exec();
107
108   // Loop until the user either selects a macro to display,
109   // or makes no request:
110   while (request != No_Request)
111   {
112     switch (request)
113     {
114     case DisplayMacro_Request:
115       currentMacro = userRequestMacro;
116       userRequestMacro = 0;
117
118       ui->macroNameLabel->setText(currentMacro->getName());
119
120       // Turn on the "new" button
121       ui->addButton->setEnabled(true);
122
123       currentMacro->populateList(ui->macroListWidget);
124
125       return;
126
127     case NewMacro_Request:
128       emd.clear();
129       if (emd.exec() == QDialog::Accepted)
130       {
131         // Create the new macro:
132         currentMacro = new PIRMacro(
133           mainWindow->getUserPack(),
134           emd.getText(),
135           emd.getKey(),
136           emd.getButtonID(),
137           mainWindow);
138
139         ui->macroNameLabel->setText(emd.getText());
140
141         // Turn on the "new" button
142         ui->addButton->setEnabled(true);
143
144         mainWindow->updateUserButtons();
145
146         ui->macroListWidget->clear();
147
148         mainWindow->storeMacros();
149
150         return;
151       }
152       break;
153
154     case EditMacro_Request:
155       emd.setup(userRequestMacro);
156       if (emd.exec() == QDialog::Accepted)
157       {
158         // Store the changes:
159         userRequestMacro->setName(emd.getText());
160         userRequestMacro->setKey(emd.getKey());
161         userRequestMacro->setButtonID(emd.getButtonID());
162
163         mainWindow->updateUserButtons();
164
165         mainWindow->storeMacros();
166       }
167       userRequestMacro = 0;
168       break;
169
170     case DeleteMacro_Request:
171       dmd.setup(userRequestMacro);
172       if (dmd.exec() == QDialog::Accepted)
173       {
174         if (currentMacro == userRequestMacro)
175         {
176           currentMacro = 0;
177           ui->macroNameLabel->setText("No Macro Selected");
178           ui->addButton->setEnabled(false);
179           ui->macroListWidget->clear();
180         }
181
182         // Delete the macro:
183         delete userRequestMacro;
184
185         // Make sure no dangling buttons exist:
186         mainWindow->updateUserButtons();
187
188         mainWindow->storeMacros();
189
190         smd.resetIndices();
191       }
192       userRequestMacro = 0;
193       break;
194
195     default:
196       break;
197     }
198
199     // Return to the selector dialog:
200     request = No_Request;
201     smd.exec();
202   }
203 }
204
205
206 void PIRMacroForm::on_runButton_clicked()
207 {
208   if (currentMacro) currentMacro->executeMacro();
209 }
210
211
212 void PIRMacroForm::displayMacro(
213   QTreeWidgetItem *item)
214 {
215   if (!item) return;
216
217   PIRMacro *m = dynamic_cast<PIRMacro *> (item);
218
219   if (!m) return;
220
221   userRequestMacro = m;
222   request = DisplayMacro_Request;
223 }
224
225
226 QComboBox *PIRMacroForm::getKeysetComboBox()
227 {
228   return ccd.getKeysetComboBox();
229 }
230
231
232 void PIRMacroForm::createNewMacro()
233 {
234   request = NewMacro_Request;
235 }
236
237
238 void PIRMacroForm::editMacro(
239   QTreeWidgetItem *item)
240 {
241   if (!item) return;
242   userRequestMacro = dynamic_cast<PIRMacro *> (item);
243   request = EditMacro_Request;
244 }
245
246
247 void PIRMacroForm::deleteMacro(
248   QTreeWidgetItem *item)
249 {
250   if (!item) return;
251   userRequestMacro = dynamic_cast<PIRMacro *> (item);
252   request = DeleteMacro_Request;
253 }
254
255
256 void PIRMacroForm::on_addButton_clicked()
257 {
258   // Sanity check:
259   if (!currentMacro) return;
260
261   // Initialize the choose command dialog:
262   ccd.init();
263
264   // Check whether the user has actually created a command:
265   if (ccd.exec() != QDialog::Accepted) return;
266
267   int index = ui->macroListWidget->currentRow();
268
269   switch (ccd.getCommandType())
270   {
271   case Keyset_Command:
272     currentMacro->appendCommand(
273       new PIRKeysetCommandItem(
274         ccd.getKeysetID(),
275         mainWindow));
276
277     currentMacro->populateList(ui->macroListWidget);
278     ui->macroListWidget->setCurrentRow(index);
279
280     mainWindow->storeMacros();
281
282     break;
283
284   case Key_Command:
285     currentMacro->appendCommand(
286       new PIRKeyCommandItem(
287         ccd.getKeyName(),
288         mainWindow));
289
290     currentMacro->populateList(ui->macroListWidget);
291     ui->macroListWidget->setCurrentRow(index);
292
293     mainWindow->storeMacros();
294
295     break;
296
297   case Pause_Command:
298     currentMacro->appendCommand(
299       new PIRPauseCommandItem(
300         ccd.getTimeToWait()));
301
302     currentMacro->populateList(ui->macroListWidget);
303     ui->macroListWidget->setCurrentRow(index);
304
305     mainWindow->storeMacros();
306
307     break;
308
309   case No_Command:
310   default:
311     break;
312   }
313 }
314
315
316 void PIRMacroForm::deleteCurrentCommand()
317 {
318   int index = ui->macroListWidget->currentRow();
319   if (currentMacro->deleteCommand(index))
320   {
321     currentMacro->populateList(ui->macroListWidget);
322
323     if (index == ui->macroListWidget->count()) --index;
324
325     if (index)
326     {
327       ui->macroListWidget->setCurrentRow(index);
328     }
329
330     mainWindow->storeMacros();
331   }
332 }
333
334
335 void PIRMacroForm::moveCurrentCommandUp()
336 {
337   int index = ui->macroListWidget->currentRow();
338   if (currentMacro->moveUp(index))
339   {
340     currentMacro->populateList(ui->macroListWidget);
341     ui->macroListWidget->setCurrentRow(index - 1);
342
343     mainWindow->storeMacros();
344   }
345 }
346
347
348 void PIRMacroForm::moveCurrentCommandDown()
349 {
350   int index = ui->macroListWidget->currentRow();
351
352   if (currentMacro->moveDown(index))
353   {
354     currentMacro->populateList(ui->macroListWidget);
355     ui->macroListWidget->setCurrentRow(index + 1);
356
357     mainWindow->storeMacros();
358   }
359 }
360
361
362 //void PIRMacroForm::on_macroListWidget_itemClicked(QListWidgetItem *item)
363 void PIRMacroForm::on_macroListWidget_itemClicked()
364 {
365   ecd.setup(
366     currentMacro->getCommandType(ui->macroListWidget->currentRow()),
367     currentMacro->getCommandName(ui->macroListWidget->currentRow()));
368
369   ecd.exec();
370 }
371
372
373 /*
374 void PIRMacroForm::on_newMacroButton_clicked()
375 {
376   emd.clear();
377
378   // If no new macro, just give up:
379   if (emd.exec() != QDialog::Accepted) return;
380
381   // Create the new macro:
382   currentMacro = new PIRMacro(
383     mainWindow->getUserPack(),
384     emd.getText(),
385     emd.getKey(),
386     emd.getButtonID());
387
388   ui->macroNameLabel->setText(emd.getText());
389
390   // Turn on the "new" button
391   ui->addButton->setEnabled(true);
392
393   if (emd.getButtonID())
394   {
395     mainWindow->updateUserButtons();
396   }
397
398   ui->macroListWidget->clear();
399
400   mainWindow->storeMacros();
401 }
402 */
403