]> Shamusworld >> Repos - architektonas/blob - src/base/variabledict.cpp
Initial removal of unnecessary rs_ prefixes from files.
[architektonas] / src / base / variabledict.cpp
1 // variabledict.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/12/2010  Created this file. :-)
15 //
16
17 #include "variabledict.h"
18
19 /**
20  * Constructor.
21  */
22 RS_VariableDict::RS_VariableDict()
23 {
24 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
25 //      variables.setAutoDelete(true);
26 }
27
28 /*virtual*/ RS_VariableDict::~RS_VariableDict()
29 {
30 }
31
32 /**
33  * Removes all variables in the blocklist.
34  */
35 void RS_VariableDict::clear()
36 {
37         variables.clear();
38 }
39
40 /**
41  * @return Number of variables available.
42  */
43 int RS_VariableDict::count()
44 {
45         return variables.count();
46 }
47
48 /**
49  * Activates the given block.
50  * Listeners are notified.
51  */
52 //void RS_VariableDict::activateBlock(const QString& name) {
53 //      activateBlock(findBlock(name));
54 //}
55
56 /**
57  * Activates the given block.
58  * Listeners are notified.
59  */
60 /*void RS_VariableDict::activateBlock(RS_Block* block) {
61         activeBlock = block;
62
63         for (uint i=0; i<blockListListeners.count(); ++i) {
64                 RS_VariableDictListener* l = blockListListeners.at(i);
65
66                 l->blockActivated(activeBlock);
67         }
68 }*/
69
70 /**
71  * Adds a variable to the variable dictionary. If a variable with the
72  * same name already exists, is will be overwritten.
73  */
74 void RS_VariableDict::add(const QString & key, const QString & value, int code)
75 {
76         RS_DEBUG->print("RS_VariableDict::addVariable()");
77
78         if (key.isEmpty())
79         {
80                 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
81                 return;
82         }
83
84         variables.replace(key, new RS_Variable(value, code));
85 }
86
87 /**
88  * Adds a variable to the variable dictionary. If a variable with the
89  * same name already exists, is will be overwritten.
90  */
91 void RS_VariableDict::add(const QString & key, int value, int code)
92 {
93         RS_DEBUG->print("RS_VariableDict::addVariable()");
94
95         if (key.isEmpty())
96         {
97                 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
98                 return;
99         }
100
101         variables.replace(key, new RS_Variable(value, code));
102 }
103
104 /**
105  * Adds a variable to the variable dictionary. If a variable with the
106  * same name already exists, is will be overwritten.
107  */
108 void RS_VariableDict::add(const QString & key, double value, int code)
109 {
110         RS_DEBUG->print("RS_VariableDict::addVariable()");
111
112         if (key.isEmpty())
113         {
114                 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
115                 return;
116         }
117
118         variables.replace(key, new RS_Variable(value, code));
119 }
120
121 /**
122  * Adds a variable to the variable dictionary. If a variable with the
123  * same name already exists, is will be overwritten.
124  */
125 void RS_VariableDict::add(const QString & key, const Vector & value, int code)
126 {
127         RS_DEBUG->print("RS_VariableDict::addVariable()");
128
129         if (key.isEmpty())
130         {
131                 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
132                 return;
133         }
134
135         variables.replace(key, new RS_Variable(value, code));
136 }
137
138 /**
139  * Gets the value for the given variable.
140  *
141  * @param key Key of the variable.
142  * @param def Default value.
143  *
144  * @return The value for the given variable or the given default value
145  * if the variable couldn't be found.
146  */
147 Vector RS_VariableDict::getVector(const QString & key, const Vector & def)
148 {
149         Vector ret;
150 //      RS_Variable * ptr = variables.find(key);
151         RS_Variable * ptr = variables.value(key);
152
153         if (ptr == NULL || ptr->getType() != RS2::VariableVector)
154                 ret = def;
155         else
156                 ret = ptr->getVector();
157
158         return ret;
159 }
160
161 /**
162  * Gets the value for the given variable.
163  *
164  * @param key Key of the variable.
165  * @param def Default value.
166  *
167  * @return The value for the given variable or the given default value
168  * if the variable couldn't be found.
169  */
170 QString RS_VariableDict::getString(const QString & key, const QString & def)
171 {
172         QString ret;
173
174         RS_DEBUG->print("RS_VariableDict::getString: 001");
175         RS_DEBUG->print("RS_VariableDict::getString: key: '%s'", key.toLatin1().data());
176
177 //      RS_Variable * ptr = variables.find(key);
178         RS_Variable * ptr = variables.value(key);
179         RS_DEBUG->print("RS_VariableDict::getString: 002");
180
181         if (ptr == NULL)
182         {
183                 RS_DEBUG->print("RS_VariableDict::getString: 003");
184                 ret = def;
185         }
186         else if (ptr->getType() != RS2::VariableString)
187         {
188                 RS_DEBUG->print("RS_VariableDict::getString: 004");
189                 ret = def;
190         }
191         else
192         {
193                 RS_DEBUG->print("RS_VariableDict::getString: 005");
194                 ret = ptr->getString();
195         }
196
197         RS_DEBUG->print("RS_VariableDict::getString: 006");
198
199         return ret;
200 }
201
202 /**
203  * Gets the value as int for the given variable.
204  *
205  * @param key Key of the variable.
206  * @param def Default value.
207  *
208  * @return The value for the given variable or the given default value
209  * if the variable couldn't be found.
210  */
211 int RS_VariableDict::getInt(const QString & key, int def)
212 {
213         int ret;
214 //      RS_Variable * ptr = variables.find(key);
215         RS_Variable * ptr = variables.value(key);
216
217         if (ptr == NULL || ptr->getType() != RS2::VariableInt)
218                 ret = def;
219         else
220                 ret = ptr->getInt();
221
222         return ret;
223 }
224
225 /**
226  * Gets the value as double for the given variable.
227  *
228  * @param key Key of the variable.
229  * @param def Default value.
230  *
231  * @return The value for the given variable or the given default value
232  * if the variable couldn't be found.
233  */
234 double RS_VariableDict::getDouble(const QString & key, double def)
235 {
236         double ret;
237 //      RS_Variable * ptr = variables.find(key);
238         RS_Variable * ptr = variables.value(key);
239
240         if (ptr == NULL || ptr->getType() != RS2::VariableDouble)
241                 ret = def;
242         else
243                 ret = ptr->getDouble();
244
245         return ret;
246 }
247
248 /**
249  * Notifies the listeners about layers that were added. This can be
250  * used after adding a lot of variables without auto-update.
251  */
252 /*
253 void RS_VariableDict::addBlockNotification() {
254     for (uint i=0; i<blockListListeners.count(); ++i) {
255         RS_VariableDictListener* l = blockListListeners.at(i);
256         l->blockAdded(NULL);
257     }
258 }
259 */
260
261 /**
262  * Removes a variable from the list.
263  * TODO: Listeners are notified after the block was removed from
264  * the list but before it gets deleted.
265  */
266 void RS_VariableDict::remove(const QString & key)
267 {
268         RS_DEBUG->print("RS_VariableDict::removeVariable()");
269
270         // here the block is removed from the list but not deleted
271         variables.remove(key);
272 }
273
274 //Q3Dict<RS_Variable> & RS_VariableDict::getVariableDict()
275 QMultiHash<QString, RS_Variable *> & RS_VariableDict::getVariableDict()
276 {
277         return variables;
278 }
279
280 /**
281  * Dumps the variables to stdout.
282  */
283 std::ostream & operator<<(std::ostream & os, RS_VariableDict & d)
284 {
285         os << "Variables: \n";
286 //      Q3DictIterator<RS_Variable> it(d.variables);
287         QHashIterator<QString, RS_Variable *> it(d.variables);
288
289         while (it.hasNext())
290         {
291                 it.next();
292                 os << it.key().toAscii().data() << ": ";
293
294                 switch (it.value()->getType())
295                 {
296                 case RS2::VariableVoid:
297                         os << "void\n";
298                         break;
299                 case RS2::VariableInt:
300                         os << "int " << it.value()->getInt() << "\n";
301                         break;
302                 case RS2::VariableDouble:
303                         os << "double " << it.value()->getDouble() << "\n";
304                         break;
305                 case RS2::VariableVector:
306                         os << "vector " << it.value()->getVector() << "\n";
307                         break;
308                 case RS2::VariableString:
309                         os << "string " << it.value()->getString().toAscii().data() << "\n";
310                         break;
311                 }
312         }
313         os << std::endl;
314
315         return os;
316 }