Bugfixes and Keysets
[pierogi] / macros / pirmacrocommanditem.cpp
1 #include "pirmacrocommanditem.h"
2
3 #include "mainwindow.h"
4 #include "pirkeynames.h"
5
6 #include <QTimer>
7
8 PIRKeynameMgr keynameMgr;
9
10
11 PIRMacroCommandItem::PIRMacroCommandItem(
12   QString displayName)
13   : QListWidgetItem(displayName)
14 {
15 }
16
17
18 PIRKeyCommandItem::PIRKeyCommandItem(
19   PIRKeyName keyToExecute,
20   MainWindow *mw)
21   : PIRMacroCommandItem(keynameMgr.getKeynameString(keyToExecute)),
22     key(keyToExecute),
23     advanceTimer(0),
24     mainWindow(mw)
25 {
26 }
27
28
29 void PIRKeyCommandItem::executeCommand()
30 {
31   // First, make sure no other macro is running:
32   if (advanceTimer) delete advanceTimer;
33
34   // Start a new timer:
35   advanceTimer = new QTimer();
36   connect(advanceTimer, SIGNAL(timeout()), this, SLOT(startRunningCommand()));
37   advanceTimer->start(50);
38 }
39
40
41 void PIRKeyCommandItem::startRunningCommand()
42 {
43   // Try to start running the command:
44   if (mainWindow->startRepeating(key))
45   {
46     // The command has successfully started, now wait for it to stop:
47     if (advanceTimer) delete advanceTimer;
48     advanceTimer = new QTimer();
49     connect(advanceTimer, SIGNAL(timeout()), this, SLOT(stopRunningCommand()));
50     advanceTimer->start(50);
51   }
52 }
53
54
55 void PIRKeyCommandItem::stopRunningCommand()
56 {
57   mainWindow->stopRepeating();
58
59   if (advanceTimer) delete advanceTimer;
60   advanceTimer = 0;
61
62   emit commandCompleted();
63 }
64
65
66 PIRKeysetCommandItem::PIRKeysetCommandItem(
67   QString displayName,
68   unsigned int keysetToChoose,
69   MainWindow *mw)
70   : PIRMacroCommandItem(displayName),
71     id(keysetToChoose),
72     mainWindow(mw)
73 {
74 }
75
76
77 void PIRKeysetCommandItem::executeCommand()
78 {
79   mainWindow->updateKeysetSelection(id);
80   emit commandCompleted();
81 }
82
83
84 PIRPauseCommandItem::PIRPauseCommandItem(
85   QString displayName,
86   unsigned int timeToWait)
87   : PIRMacroCommandItem(displayName),
88     timeInSeconds(timeToWait),
89     advanceTimer(0)
90 {
91 }
92
93
94 void PIRPauseCommandItem::executeCommand()
95 {
96   if (advanceTimer) delete advanceTimer;
97
98   advanceTimer = new QTimer();
99   connect(advanceTimer, SIGNAL(timeout()), this, SLOT(finishedWaiting()));
100   advanceTimer->start(timeInSeconds * 1000);
101 }
102
103
104 void PIRPauseCommandItem::finishedWaiting()
105 {
106   if (advanceTimer)
107   {
108     delete advanceTimer;
109     advanceTimer = 0;
110   }
111
112   emit commandCompleted();
113 }