Bugfixes and Keysets
[pierogi] / macros / pirmacro.cpp
1 #include "pirmacro.h"
2
3 #include "macros/pirmacrocommanditem.h"
4
5 bool PIRMacro::macroRunning = false;
6
7 PIRMacro::PIRMacro(
8   QTreeWidgetItem *parent,
9   QString n,
10   char k)
11   : QTreeWidgetItem(parent),
12     keyMapping(k)
13 {
14   setText(0, n);
15 }
16
17
18 QString PIRMacro::getName()
19 {
20   return text(0);
21 }
22
23
24 char PIRMacro::getKeyMapping()
25 {
26   return keyMapping;
27 }
28
29
30 void PIRMacro::appendCommand(
31   PIRMacroCommandItem *c)
32 {
33   commands.push_back(c);
34 }
35
36
37 void PIRMacro::populateList(
38   QListWidget *lw)
39 {
40   lw->clear();
41
42   CommandSequence::const_iterator i = commands.begin();
43
44   while (i != commands.end())
45   {
46     lw->addItem(*i);
47     ++i;
48   }
49 }
50
51
52 // executeMacro() returns false if it was unable to start the macro running:
53 bool PIRMacro::executeMacro()
54 {
55   // Don't start a new macro if one is already running:
56   if (macroRunning) return false;
57
58   currentCommand = commands.begin();
59
60   // If this macro is empty, just return:
61   if (currentCommand == commands.end())
62   {
63     emit macroCompleted();
64     return true;
65   }
66
67   // Start running the list of commands:
68   macroRunning = true;
69
70   connect(
71     *currentCommand,
72     SIGNAL(commandCompleted()),
73     this,
74     SLOT(startNextCommand()));
75
76   (*currentCommand)->executeCommand();
77
78   ++currentCommand;
79
80   return true;
81 }
82
83
84 void PIRMacro::startNextCommand()
85 {
86   // Are we done?
87   if (currentCommand == commands.end())
88   {
89     // We are done.
90     macroRunning = false;
91     emit macroCompleted();
92     return;
93   }
94
95   // We are not done; run the next command.
96   connect(
97     *currentCommand,
98     SIGNAL(commandCompleted()),
99     this,
100     SLOT(startNextCommand()));
101
102   (*currentCommand)->executeCommand();
103
104   ++currentCommand;
105 }