Adding Macros!
[pierogi] / macros / pirmacropack.cpp
1 #include "pirmacropack.h"
2
3 #include "pirmacro.h"
4
5 #include <QSettings>
6
7 #include <iostream>
8
9 PIRMacroPack::PIRMacroPack(
10   QString name)
11 {
12   setText(0, name);
13 }
14
15
16 bool PIRMacroPack::hasButton(
17   unsigned int buttonID)
18 {
19   ButtonCollection::const_iterator i = buttons.find(buttonID);
20
21   if (i != buttons.end())
22   {
23     return true;
24   }
25   else
26   {
27     return false;
28   }
29 }
30
31
32 void PIRMacroPack::registerButton(
33   unsigned int buttonID,
34   PIRMacro *macro)
35 {
36   buttons[buttonID] = macro;
37 }
38
39
40 void PIRMacroPack::eraseButton(
41   unsigned int buttonID,
42   PIRMacro *macro)
43 {
44   ButtonCollection::iterator i = buttons.find(buttonID);
45
46   if (i != buttons.end() && (*i).second == macro)
47   {
48     buttons.erase(i);
49   }
50 }
51
52
53 QString PIRMacroPack::buttonText(
54   unsigned int buttonID)
55 {
56   ButtonCollection::const_iterator i = buttons.find(buttonID);
57
58   if (i != buttons.end())
59   {
60     return i->second->getName();
61   }
62   else
63   {
64     return "Error: Macro not found";
65   }
66 }
67
68
69 void PIRMacroPack::executeButton(
70   unsigned int buttonID)
71 {
72   ButtonCollection::const_iterator i = buttons.find(buttonID);
73
74   if (i != buttons.end())
75   {
76     i->second->executeMacro();
77   }
78 }
79
80
81 bool PIRMacroPack::hasKey(
82   char key)
83 {
84   KeyboardCollection::const_iterator i = keymaps.find(key);
85
86   if (i != keymaps.end())
87   {
88     return true;
89   }
90   else
91   {
92     return false;
93   }
94 }
95
96
97 void PIRMacroPack::registerKey(
98   char key,
99   PIRMacro *macro)
100 {
101   keymaps[key] = macro;
102 }
103
104
105 void PIRMacroPack::eraseKey(
106   char key,
107   PIRMacro *macro)
108 {
109   KeyboardCollection::iterator i = keymaps.find(key);
110
111   if (i != keymaps.end() && (*i).second == macro)
112   {
113     keymaps.erase(i);
114   }
115 }
116
117
118 void PIRMacroPack::executeKey(
119   char key)
120 {
121   KeyboardCollection::const_iterator i = keymaps.find(key);
122
123   if (i != keymaps.end())
124   {
125     i->second->executeMacro();
126   }
127 }
128
129
130 void PIRMacroPack::storeSettings()
131 {
132   QSettings settings("pietrzak.org", "Pierogi");
133
134   // Erase any existing macros:
135   settings.remove("userMacros");
136
137   // First, do we even have any macros?  If not, quit.
138   int macroCount = childCount();
139   if (macroCount == 0) return;
140
141   // (Re-) Construct the macros array:
142
143   int index = 0;
144   PIRMacro *macro;
145
146   settings.beginWriteArray("userMacros");
147
148   while (index < macroCount)
149   {
150     settings.setArrayIndex(index);
151     macro = dynamic_cast<PIRMacro *> (child(index));
152
153     macro->storeSettings(settings);
154
155     ++index;
156   }
157
158   settings.endArray();
159 }