f78deb31ca4439dd2532893096e2949e5d10d1c9
[pierogi] / macros / pirmacrocommanditem.cpp
1 #include "pirmacrocommanditem.h"
2
3 #include "mainwindow.h"
4 #include "pirkeynames.h"
5
6 #include <QSettings>
7 #include <QTimer>
8
9 #include <iostream>
10
11 PIRKeynameMgr keynameMgr;
12
13
14 PIRMacroCommandItem::PIRMacroCommandItem()
15   : name("Unnamed")
16 {
17 }
18
19
20 PIRMacroCommandItem::PIRMacroCommandItem(
21   QString displayName)
22   : name(displayName)
23 {
24 }
25
26
27 QString PIRMacroCommandItem::getName() const
28 {
29   return name;
30 }
31
32
33 void PIRMacroCommandItem::setName(
34   QString n)
35 {
36   name = n;
37 }
38
39
40 PIRKeyCommandItem::PIRKeyCommandItem(
41   PIRKeyName keyToExecute,
42   MainWindow *mw)
43   : PIRMacroCommandItem(keynameMgr.getKeynameString(keyToExecute)),
44     key(keyToExecute),
45     advanceTimer(0),
46     mainWindow(mw)
47 {
48 }
49
50
51 PIRKeyCommandItem::~PIRKeyCommandItem()
52 {
53   if (advanceTimer) delete advanceTimer;
54 }
55
56
57 void PIRKeyCommandItem::executeCommand()
58 {
59   // First, make sure no other macro is running:
60   if (advanceTimer) delete advanceTimer;
61
62   // Start a new timer:
63   advanceTimer = new QTimer();
64   connect(advanceTimer, SIGNAL(timeout()), this, SLOT(startRunningCommand()));
65   advanceTimer->start(50);
66 }
67
68
69 void PIRKeyCommandItem::startRunningCommand()
70 {
71   // Try to start running the command:
72   if (mainWindow->startRepeating(key))
73   {
74     // The command has successfully started, now wait for it to stop:
75     if (advanceTimer) delete advanceTimer;
76     advanceTimer = new QTimer();
77     connect(advanceTimer, SIGNAL(timeout()), this, SLOT(stopRunningCommand()));
78     advanceTimer->start(50);
79   }
80 }
81
82
83 void PIRKeyCommandItem::stopRunningCommand()
84 {
85   mainWindow->stopRepeating();
86
87   if (advanceTimer) delete advanceTimer;
88   advanceTimer = 0;
89
90   emit commandCompleted();
91 }
92
93
94 void PIRKeyCommandItem::storeSettings(
95   QSettings &settings,
96   int index)
97 {
98   QString commandName = "commandType";
99   commandName.append(QString::number(index));
100   settings.setValue(commandName, KeyCommand_Type);
101
102   commandName = "commandKeyID";
103   commandName.append(QString::number(index));
104   settings.setValue(commandName, key);
105 }
106
107
108 QString PIRKeyCommandItem::getTypeString() const
109 {
110   return "Execute Keypress: ";
111 }
112
113
114 PIRKeysetCommandItem::PIRKeysetCommandItem(
115   QString displayName,
116   unsigned int keysetToChoose,
117   MainWindow *mw)
118   : PIRMacroCommandItem(displayName),
119     id(keysetToChoose),
120     mainWindow(mw)
121 {
122 }
123
124
125 PIRKeysetCommandItem::PIRKeysetCommandItem(
126   unsigned int keysetToChoose,
127   MainWindow *mw)
128   : PIRMacroCommandItem(),
129     id(keysetToChoose),
130     mainWindow(mw)
131 {
132   setName(mainWindow->getFullKeysetName(keysetToChoose));
133 }
134
135
136 void PIRKeysetCommandItem::executeCommand()
137 {
138   mainWindow->updateKeysetSelection(id);
139   emit commandCompleted();
140 }
141
142
143 void PIRKeysetCommandItem::storeSettings(
144   QSettings &settings,
145   int index)
146 {
147   QString commandName = "commandType";
148   commandName.append(QString::number(index));
149   settings.setValue(commandName, KeysetCommand_Type);
150
151   commandName = "commandKeysetMake";
152   commandName.append(QString::number(index));
153   settings.setValue(commandName, mainWindow->getKeysetMake(id));
154
155   commandName = "commandKeysetName";
156   commandName.append(QString::number(index));
157   settings.setValue(commandName, mainWindow->getKeysetName(id));
158
159   commandName = "commandKeysetDisplayName";
160   commandName.append(QString::number(index));
161   settings.setValue(commandName, mainWindow->getFullKeysetName(id));
162 }
163
164
165 QString PIRKeysetCommandItem::getTypeString() const
166 {
167   return "Choose Keyset: ";
168 }
169
170
171 PIRPauseCommandItem::PIRPauseCommandItem(
172   unsigned int timeToWait)
173   : timeInSeconds(timeToWait),
174     advanceTimer(0)
175 {
176   QString pauseName = "Pause for ";
177   pauseName.append(QString::number(timeToWait));
178   pauseName.append(" seconds");
179
180   setName(pauseName);
181 }
182
183
184 PIRPauseCommandItem::~PIRPauseCommandItem()
185 {
186   if (advanceTimer) delete advanceTimer;
187 }
188
189
190 void PIRPauseCommandItem::executeCommand()
191 {
192   if (advanceTimer) delete advanceTimer;
193
194   advanceTimer = new QTimer();
195   connect(advanceTimer, SIGNAL(timeout()), this, SLOT(finishedWaiting()));
196   advanceTimer->start(timeInSeconds * 1000);
197 }
198
199
200 void PIRPauseCommandItem::finishedWaiting()
201 {
202   if (advanceTimer)
203   {
204     delete advanceTimer;
205     advanceTimer = 0;
206   }
207
208   emit commandCompleted();
209 }
210
211
212 void PIRPauseCommandItem::storeSettings(
213   QSettings &settings,
214   int index)
215 {
216   QString commandName = "commandType";
217   commandName.append(QString::number(index));
218   settings.setValue(commandName, PauseCommand_Type);
219
220   commandName = "commandPause";
221   commandName.append(QString::number(index));
222   settings.setValue(commandName, timeInSeconds);
223 }
224
225
226 QString PIRPauseCommandItem::getTypeString() const
227 {
228   return "Pause (in seconds): ";
229 }