3c8db07471ddc1b01db02473112b7eac46b4051e
[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 }
75
76
77 bool PIRMacroManager::hasMacroButton(
78   unsigned int buttonID)
79 {
80   if (buttonsController)
81     return buttonsController->hasButton(buttonID);
82
83   return false;
84 }
85
86
87 QString PIRMacroManager::getMacroButtonText(
88   unsigned int buttonID)
89 {
90   if (buttonsController)
91     return buttonsController->buttonText(buttonID);
92
93   return "No Macro Found";
94 }
95
96
97 void PIRMacroManager::executeMacroButton(
98   unsigned int buttonID)
99 {
100   if (buttonsController)
101     buttonsController->executeButton(buttonID);
102 }
103
104
105 void PIRMacroManager::storeSettings()
106 {
107   if (userPack) userPack->storeSettings();
108 }
109
110
111 void PIRMacroManager::retrieveSettings()
112 {
113   // Pull in any user-defined macros:
114   QSettings settings("pietrzak.org", "Pierogi");
115   int size = settings.beginReadArray("userMacros");
116   int index = 0;
117   QString macroName;
118   char macroKey;
119   unsigned int macroButtonID;
120   PIRMacro *macro;
121   PIRMacroCommandItem *macroCommand;
122   int commandIndex;
123   QString commandName;
124   int commandType;
125   unsigned int keysetID;
126   QString commandKeysetName;
127   QString commandKeysetMakeStr;
128   QString commandKeysetDisplayName;
129   PIRKeyName commandKeyID;
130   unsigned int commandPause;
131
132   while (index < size)
133   {
134     settings.setArrayIndex(index);
135     macroName = settings.value("macroName").toString();
136     macroKey = settings.value("macroKey").toInt();
137     macroButtonID = settings.value("macroButtonID").toInt();
138
139     macro = new PIRMacro(
140       userPack, macroName, macroKey, macroButtonID, mainWindow);
141
142     commandIndex = 0;
143     commandName = "commandType";
144     commandName.append(QString::number(commandIndex));
145     while (settings.contains(commandName))
146     {
147       commandType = settings.value(commandName).toInt();
148       switch(CommandItemType(commandType))
149       {
150       case KeysetCommand_Type:
151         commandName = "commandKeysetName";
152         commandName.append(QString::number(commandIndex));
153         commandKeysetName = settings.value(commandName).toString();
154
155         commandName = "commandKeysetMake";
156         commandName.append(QString::number(commandIndex));
157         commandKeysetMakeStr = settings.value(commandName).toString();
158
159         commandName = "commandKeysetDisplayName";
160         commandName.append(QString::number(commandIndex));
161         commandKeysetDisplayName = settings.value(commandName).toString();
162
163         if (mainWindow->findKeysetID(
164             commandKeysetMakeStr,
165             commandKeysetName,
166             keysetID))
167         {
168           macroCommand = new PIRKeysetCommandItem(
169             commandKeysetDisplayName,
170             keysetID,
171             mainWindow);
172
173           macro->appendCommand(macroCommand);
174         }
175
176         break;
177
178       case KeyCommand_Type:
179         commandName = "commandKeyID";
180         commandName.append(QString::number(commandIndex));
181         commandKeyID = PIRKeyName(settings.value(commandName).toInt());
182
183         macroCommand = new PIRKeyCommandItem(commandKeyID, mainWindow);
184
185         macro->appendCommand(macroCommand);
186
187         break;
188
189       case PauseCommand_Type:
190         commandName = "commandPause";
191         commandName.append(QString::number(commandIndex));
192         commandPause = settings.value(commandName).toInt();
193
194         macroCommand = new PIRPauseCommandItem(commandPause);
195
196         macro->appendCommand(macroCommand);
197
198         break;
199
200       default:
201         break;
202       }
203
204       ++commandIndex;
205       commandName = "commandType";
206       commandName.append(QString::number(commandIndex));
207     }
208
209     ++index;
210   }
211
212   settings.endArray();
213 }
214
215
216 // This needs to be done differently!!!
217 void PIRMacroManager::setKbdFocus(
218   int index)
219 {
220   if (index == 0)
221   {
222     keyboardController = userPack;
223   }
224   else
225   {
226     keyboardController = multitapPack;
227   }
228 }
229
230
231 void PIRMacroManager::setBtnFocus(
232   int index)
233 {
234   if (index == 0)
235   {
236     buttonsController = userPack;
237   }
238   else
239   {
240     buttonsController = multitapPack;
241   }
242 }