b1a947505012319a8de2862c6e8aa109d0f734cb
[pierogi] / macros / pirmacro.cpp
1 #include "pirmacro.h"
2
3 #include "pirmacrocommanditem.h"
4 #include "pirmacropack.h"
5 #include "mainwindow.h"
6
7 #include <QSettings>
8
9 #include <iostream>
10
11 bool PIRMacro::macroRunning = false;
12
13 PIRMacro::PIRMacro(
14   PIRMacroPack *parent,
15   QString name,
16   char k,
17   unsigned int b,
18   MainWindow *mw)
19   : QTreeWidgetItem(parent),
20     key(k),
21     buttonID(b),
22     mainWindow(mw)
23 {
24   setText(0, name);
25
26   if (key)
27   {
28     parent->registerKey(key, this);
29   }
30
31   if (buttonID)
32   {
33     parent->registerButton(buttonID, this);
34   }
35 }
36
37
38 PIRMacro::~PIRMacro()
39 {
40   PIRMacroPack *mp = dynamic_cast<PIRMacroPack*> (QTreeWidgetItem::parent());
41
42   if (key)
43   {
44     mp->eraseKey(key, this);
45   }
46
47   if (buttonID)
48   {
49     mp->eraseButton(buttonID, this);
50   }
51
52   currentCommand = commands.begin();
53   while (currentCommand != commands.end())
54   {
55     delete *currentCommand;
56     commands.pop_front();
57     currentCommand = commands.begin();
58   }
59 }
60
61
62 QString PIRMacro::getName()
63 {
64   return text(0);
65 }
66
67
68 void PIRMacro::setName(
69   QString name)
70 {
71   setText(0, name);
72 }
73
74
75 char PIRMacro::getKey()
76 {
77   return key;
78 }
79
80
81 void PIRMacro::setKey(
82   char k)
83 {
84   key = k;
85 }
86
87
88 unsigned int PIRMacro::getButtonID()
89 {
90   return buttonID;
91 }
92
93
94 void PIRMacro::setButtonID(
95   unsigned int id)
96 {
97   buttonID = id;
98 }
99
100 void PIRMacro::appendCommand(
101   PIRMacroCommandItem *c)
102 {
103   commands.push_back(c);
104 }
105
106
107 bool PIRMacro::deleteCommand(
108   int index)
109 {
110   CommandSequence::iterator i = commands.begin();
111   int count = 0;
112
113   while (i != commands.end())
114   {
115     if (index == count)
116     {
117       delete (*i);
118       commands.erase(i);
119       return true;
120     }
121     ++count;
122     ++i;
123   }
124
125   return false;
126 }
127
128
129 bool PIRMacro::moveUp(
130   int index)
131 {
132   CommandSequence::iterator i = commands.begin();
133   int count = 0;
134   CommandSequence::iterator previ = commands.begin();
135
136   while (i != commands.end())
137   {
138     if (index == count)
139     {
140       PIRMacroCommandItem *mci = *i;
141       commands.erase(i);
142       commands.insert(previ, mci);
143       return true;
144     }
145
146     previ = i;
147     ++count;
148     ++i;
149   }
150
151   return false;
152 }
153
154
155 bool PIRMacro::moveDown(
156   int index)
157 {
158   CommandSequence::iterator i = commands.begin();
159   CommandSequence::iterator nexti = commands.begin();
160   int count = 0;
161
162   while (i != commands.end())
163   {
164     ++nexti;
165     if ((index == count) && nexti != commands.end())
166     {
167       PIRMacroCommandItem *mci = *i;
168       commands.erase(i);
169       ++nexti;
170       commands.insert(nexti, mci);
171       return true;
172     }
173
174     ++count;
175     ++i;
176   }
177
178   return false;
179 }
180
181
182 void PIRMacro::populateList(
183   QListWidget *lw)
184 {
185   lw->clear();
186
187   CommandSequence::const_iterator i = commands.begin();
188
189   while (i != commands.end())
190   {
191     lw->addItem(new QListWidgetItem((*i)->getName()));
192     ++i;
193   }
194 }
195
196
197 // executeMacro() returns false if it was unable to start the macro running:
198 bool PIRMacro::executeMacro()
199 {
200   // Don't start a new macro if one is already running:
201   if (macroRunning) return false;
202
203   currentCommand = commands.begin();
204
205   // If this macro is empty, just return:
206   if (currentCommand == commands.end())
207   {
208     emit macroCompleted();
209     return true;
210   }
211
212   // Start running the list of commands:
213   macroRunning = true;
214
215   // Take note of the current keyset id:
216   preMacroKeysetID = mainWindow->getCurrentKeyset();
217
218   connect(
219     *currentCommand,
220     SIGNAL(commandCompleted()),
221     this,
222     SLOT(startNextCommand()));
223
224   (*currentCommand)->executeCommand();
225
226   return true;
227 }
228
229
230 void PIRMacro::startNextCommand()
231 {
232   disconnect(
233     *currentCommand,
234     SIGNAL(commandCompleted()),
235     0,
236     0);
237
238   ++currentCommand;
239
240   // Are we done?
241   if (currentCommand == commands.end())
242   {
243     // We are done.
244     macroRunning = false;
245     mainWindow->updateKeysetSelection(preMacroKeysetID);
246     emit macroCompleted();
247     return;
248   }
249
250   // We are not done; run the next command.
251   connect(
252     *currentCommand,
253     SIGNAL(commandCompleted()),
254     this,
255     SLOT(startNextCommand()));
256
257   (*currentCommand)->executeCommand();
258 }
259
260
261 void PIRMacro::storeSettings(
262   QSettings &settings)
263 {
264   settings.setValue("macroName", text(0));
265   settings.setValue("macroKey", key);
266   settings.setValue("macroButtonID", buttonID);
267
268   CommandSequence::const_iterator i = commands.begin();
269
270   int index = 0;
271   while (i != commands.end())
272   {
273     (*i)->storeSettings(settings, index);
274     ++index;
275     ++i;
276   }
277 }
278
279
280 QString PIRMacro::getCommandType(
281   int index)
282 {
283   CommandSequence::iterator i = commands.begin();
284
285   // Advance 'index' steps into the sequence:
286   while (i != commands.end() && index)
287   {
288     ++i;
289     --index;
290   }
291
292   if (i != commands.end())
293   {
294     return (*i)->getTypeString();
295   }
296
297   return "";
298 }
299
300
301 QString PIRMacro::getCommandName(
302   int index)
303 {
304   CommandSequence::iterator i = commands.begin();
305
306   while (i != commands.end() && index)
307   {
308     ++i;
309     --index;
310   }
311
312   if (i != commands.end())
313   {
314     return (*i)->getName();
315   }
316
317   return "";
318 }