Advanced Settings Panel
[pierogi] / macros / pirmacromanager.cpp
1 #include "pirmacromanager.h"
2
3 #include "pirmacropack.h"
4 #include "pirreversemultitap.h"
5 #include "pirmacro.h"
6 #include "pirmacrocommanditem.h"
7 #include "mainwindow.h"
8
9 #include <QSettings>
10 #include <iostream>
11
12 PIRMacroManager::PIRMacroManager(
13   MainWindow *mw)
14   : userPack(0),
15     multitapPack(0),
16     keyboardController(0),
17     buttonsController(0),
18     mainWindow(mw)
19 {
20   userPack = new PIRMacroPack("User Defined Macros");
21   multitapPack = new PIRReverseMultitap(mw);
22
23   retrieveSettings();
24
25   // For testing, set the multitap pack as the keyboard controller:
26 //  keyboardController = multitapPack;
27
28   // And more testing, userpack as button controller:
29 //  buttonsController = userPack;
30 }
31
32
33 PIRMacroManager::~PIRMacroManager()
34 {
35 // The UI currently owns the macro packs...
36 //  delete userPack;
37 //  delete multitapPack;
38 }
39
40
41 PIRMacroPack *PIRMacroManager::getUserPack()
42 {
43   return userPack;
44 }
45
46
47 PIRMacroPack *PIRMacroManager::getMultitapPack()
48 {
49   return multitapPack;
50 }
51
52
53 void PIRMacroManager::setKeyboardController(
54   PIRMacroPack *pack)
55 {
56   keyboardController = pack;
57 }
58
59
60 void PIRMacroManager::setButtonsController(
61   PIRMacroPack *pack)
62 {
63   buttonsController = pack;
64 }
65
66
67 void PIRMacroManager::handleKeypress(
68   char key)
69 {
70   if (keyboardController)
71   {
72     keyboardController->executeKey(key);
73   }
74   else
75   {
76     executeStandardKey(key);
77   }
78 }
79
80
81 bool PIRMacroManager::hasMacroButton(
82   unsigned int buttonID)
83 {
84   if (buttonsController)
85     return buttonsController->hasButton(buttonID);
86
87   return false;
88 }
89
90
91 QString PIRMacroManager::getMacroButtonText(
92   unsigned int buttonID)
93 {
94   if (buttonsController)
95     return buttonsController->buttonText(buttonID);
96
97   return "No Macro Found";
98 }
99
100
101 void PIRMacroManager::executeMacroButton(
102   unsigned int buttonID)
103 {
104   if (buttonsController)
105     buttonsController->executeButton(buttonID);
106 }
107
108
109 void PIRMacroManager::storeSettings()
110 {
111   if (userPack) userPack->storeSettings();
112 }
113
114
115 void PIRMacroManager::retrieveSettings()
116 {
117   // Pull in any user-defined macros:
118   QSettings settings("pietrzak.org", "Pierogi");
119   int size = settings.beginReadArray("userMacros");
120   int index = 0;
121   QString macroName;
122   char macroKey;
123   unsigned int macroButtonID;
124   PIRMacro *macro;
125   PIRMacroCommandItem *macroCommand;
126   int commandIndex;
127   QString commandName;
128   int commandType;
129   unsigned int keysetID;
130   QString commandKeysetName;
131   QString commandKeysetMakeStr;
132   QString commandKeysetDisplayName;
133   PIRKeyName commandKeyID;
134   unsigned int commandPause;
135
136   while (index < size)
137   {
138     settings.setArrayIndex(index);
139     macroName = settings.value("macroName").toString();
140     macroKey = settings.value("macroKey").toInt();
141     macroButtonID = settings.value("macroButtonID").toInt();
142
143     macro = new PIRMacro(
144       userPack, macroName, macroKey, macroButtonID, mainWindow);
145
146     commandIndex = 0;
147     commandName = "commandType";
148     commandName.append(QString::number(commandIndex));
149     while (settings.contains(commandName))
150     {
151       commandType = settings.value(commandName).toInt();
152       switch(CommandItemType(commandType))
153       {
154       case KeysetCommand_Type:
155         commandName = "commandKeysetName";
156         commandName.append(QString::number(commandIndex));
157         commandKeysetName = settings.value(commandName).toString();
158
159         commandName = "commandKeysetMake";
160         commandName.append(QString::number(commandIndex));
161         commandKeysetMakeStr = settings.value(commandName).toString();
162
163         commandName = "commandKeysetDisplayName";
164         commandName.append(QString::number(commandIndex));
165         commandKeysetDisplayName = settings.value(commandName).toString();
166
167         if (mainWindow->findKeysetID(
168             commandKeysetMakeStr,
169             commandKeysetName,
170             keysetID))
171         {
172           macroCommand = new PIRKeysetCommandItem(
173             commandKeysetDisplayName,
174             keysetID,
175             mainWindow);
176
177           macro->appendCommand(macroCommand);
178         }
179
180         break;
181
182       case KeyCommand_Type:
183         commandName = "commandKeyID";
184         commandName.append(QString::number(commandIndex));
185         commandKeyID = PIRKeyName(settings.value(commandName).toInt());
186
187         macroCommand = new PIRKeyCommandItem(commandKeyID, mainWindow);
188
189         macro->appendCommand(macroCommand);
190
191         break;
192
193       case PauseCommand_Type:
194         commandName = "commandPause";
195         commandName.append(QString::number(commandIndex));
196         commandPause = settings.value(commandName).toInt();
197
198         macroCommand = new PIRPauseCommandItem(commandPause);
199
200         macro->appendCommand(macroCommand);
201
202         break;
203
204       default:
205         break;
206       }
207
208       ++commandIndex;
209       commandName = "commandType";
210       commandName.append(QString::number(commandIndex));
211     }
212
213     ++index;
214   }
215
216   settings.endArray();
217 }
218
219
220 // This needs to be done differently!!!
221 void PIRMacroManager::setKbdFocus(
222   int index)
223 {
224   if (index == 0)
225   {
226     keyboardController = userPack;
227   }
228   else
229   {
230     keyboardController = multitapPack;
231   }
232 }
233
234
235 void PIRMacroManager::setBtnFocus(
236   int index)
237 {
238   if (index == 0)
239   {
240     buttonsController = userPack;
241   }
242   else
243   {
244     buttonsController = multitapPack;
245   }
246 }
247
248
249 void PIRMacroManager::executeStandardKey(
250   char key)
251 {
252   switch (key)
253   {
254   case 'Q':
255     mainWindow->switchToTab(0);
256     break;
257
258   case 'W':
259     mainWindow->switchToTab(1);
260     break;
261
262   case 'E':
263     mainWindow->switchToTab(2);
264     break;
265
266   case 'R':
267     mainWindow->switchToTab(3);
268     break;
269
270   case 'T':
271     mainWindow->switchToTab(4);
272     break;
273
274   case 'Y':
275     mainWindow->switchToTab(5);
276     break;
277
278   case 'U':
279     mainWindow->switchToTab(6);
280     break;
281
282   case 'I':
283     mainWindow->switchToTab(7);
284     break;
285
286   case 'O':
287     mainWindow->switchToTab(8);
288     break;
289
290   case 'P':
291     mainWindow->switchToTab(9);
292
293   default:
294     break;
295   }
296 }