Advanced Settings Panel
[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   advanceTimer = 0;
55 }
56
57
58 void PIRKeyCommandItem::executeCommand()
59 {
60   // First, make sure no other macro is running:
61   if (advanceTimer) delete advanceTimer;
62
63   // Start a new timer:
64   advanceTimer = new QTimer();
65   connect(advanceTimer, SIGNAL(timeout()), this, SLOT(startRunningCommand()));
66   advanceTimer->start(50);
67 }
68
69
70 void PIRKeyCommandItem::startRunningCommand()
71 {
72   // Try to start running the command:
73   if (mainWindow->startRepeating(key))
74   {
75     // The command has successfully started, now wait for it to stop:
76     if (advanceTimer) delete advanceTimer;
77     advanceTimer = new QTimer();
78     connect(advanceTimer, SIGNAL(timeout()), this, SLOT(stopRunningCommand()));
79     advanceTimer->start(50);
80   }
81 }
82
83
84 void PIRKeyCommandItem::stopRunningCommand()
85 {
86   mainWindow->stopRepeating();
87
88   if (advanceTimer) delete advanceTimer;
89   advanceTimer = 0;
90
91   emit commandCompleted();
92 }
93
94
95 void PIRKeyCommandItem::storeSettings(
96   QSettings &settings,
97   int index)
98 {
99   QString commandName = "commandType";
100   commandName.append(QString::number(index));
101   settings.setValue(commandName, KeyCommand_Type);
102
103   commandName = "commandKeyID";
104   commandName.append(QString::number(index));
105   settings.setValue(commandName, key);
106 }
107
108
109 QString PIRKeyCommandItem::getTypeString() const
110 {
111   return "Execute Keypress: ";
112 }
113
114
115 PIRKeysetCommandItem::PIRKeysetCommandItem(
116   QString displayName,
117   unsigned int keysetToChoose,
118   MainWindow *mw)
119   : PIRMacroCommandItem(displayName),
120     id(keysetToChoose),
121     mainWindow(mw)
122 {
123 }
124
125
126 PIRKeysetCommandItem::PIRKeysetCommandItem(
127   unsigned int keysetToChoose,
128   MainWindow *mw)
129   : PIRMacroCommandItem(),
130     id(keysetToChoose),
131     mainWindow(mw)
132 {
133   setName(mainWindow->getFullKeysetName(keysetToChoose));
134 }
135
136
137 void PIRKeysetCommandItem::executeCommand()
138 {
139   mainWindow->updateKeysetSelection(id);
140   emit commandCompleted();
141 }
142
143
144 void PIRKeysetCommandItem::storeSettings(
145   QSettings &settings,
146   int index)
147 {
148   QString commandName = "commandType";
149   commandName.append(QString::number(index));
150   settings.setValue(commandName, KeysetCommand_Type);
151
152   commandName = "commandKeysetMake";
153   commandName.append(QString::number(index));
154   settings.setValue(commandName, mainWindow->getKeysetMake(id));
155
156   commandName = "commandKeysetName";
157   commandName.append(QString::number(index));
158   settings.setValue(commandName, mainWindow->getKeysetName(id));
159
160   commandName = "commandKeysetDisplayName";
161   commandName.append(QString::number(index));
162   settings.setValue(commandName, mainWindow->getFullKeysetName(id));
163 }
164
165
166 QString PIRKeysetCommandItem::getTypeString() const
167 {
168   return "Choose Keyset: ";
169 }
170
171
172 PIRPauseCommandItem::PIRPauseCommandItem(
173   unsigned int timeToWait)
174   : timeInSeconds(timeToWait),
175     advanceTimer(0)
176 {
177   QString pauseName = "Pause for ";
178   pauseName.append(QString::number(timeToWait));
179   pauseName.append(" seconds");
180
181   setName(pauseName);
182 }
183
184
185 PIRPauseCommandItem::~PIRPauseCommandItem()
186 {
187   if (advanceTimer) delete advanceTimer;
188 }
189
190
191 void PIRPauseCommandItem::executeCommand()
192 {
193   if (advanceTimer) delete advanceTimer;
194
195   advanceTimer = new QTimer();
196   connect(advanceTimer, SIGNAL(timeout()), this, SLOT(finishedWaiting()));
197   advanceTimer->start(timeInSeconds * 1000);
198 }
199
200
201 void PIRPauseCommandItem::finishedWaiting()
202 {
203   if (advanceTimer)
204   {
205     delete advanceTimer;
206     advanceTimer = 0;
207   }
208
209   emit commandCompleted();
210 }
211
212
213 void PIRPauseCommandItem::storeSettings(
214   QSettings &settings,
215   int index)
216 {
217   QString commandName = "commandType";
218   commandName.append(QString::number(index));
219   settings.setValue(commandName, PauseCommand_Type);
220
221   commandName = "commandPause";
222   commandName.append(QString::number(index));
223   settings.setValue(commandName, timeInSeconds);
224 }
225
226
227 QString PIRPauseCommandItem::getTypeString() const
228 {
229   return "Pause (in seconds): ";
230 }