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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/12/2010 Created this file. :-)
17 #include "variabledict.h"
22 VariableDict::VariableDict()
24 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
25 // variables.setAutoDelete(true);
28 /*virtual*/ VariableDict::~VariableDict()
33 * Removes all variables in the blocklist.
35 void VariableDict::clear()
41 * @return Number of variables available.
43 int VariableDict::count()
45 return variables.count();
49 * Activates the given block.
50 * Listeners are notified.
52 //void VariableDict::activateBlock(const QString& name) {
53 // activateBlock(findBlock(name));
57 * Activates the given block.
58 * Listeners are notified.
60 /*void VariableDict::activateBlock(Block* block) {
63 for (uint i=0; i<blockListListeners.count(); ++i) {
64 VariableDictListener* l = blockListListeners.at(i);
66 l->blockActivated(activeBlock);
71 * Adds a variable to the variable dictionary. If a variable with the
72 * same name already exists, is will be overwritten.
74 void VariableDict::add(const QString & key, const QString & value, int code)
76 DEBUG->print("VariableDict::addVariable()");
80 DEBUG->print("VariableDict::addVariable(): No empty keys allowed.", Debug::D_WARNING);
84 variables.replace(key, new Variable(value, code));
88 * Adds a variable to the variable dictionary. If a variable with the
89 * same name already exists, is will be overwritten.
91 void VariableDict::add(const QString & key, int value, int code)
93 DEBUG->print("VariableDict::addVariable()");
97 DEBUG->print("VariableDict::addVariable(): No empty keys allowed.", Debug::D_WARNING);
101 variables.replace(key, new Variable(value, code));
105 * Adds a variable to the variable dictionary. If a variable with the
106 * same name already exists, is will be overwritten.
108 void VariableDict::add(const QString & key, double value, int code)
110 DEBUG->print("VariableDict::addVariable()");
114 DEBUG->print("VariableDict::addVariable(): No empty keys allowed.", Debug::D_WARNING);
118 variables.replace(key, new Variable(value, code));
122 * Adds a variable to the variable dictionary. If a variable with the
123 * same name already exists, is will be overwritten.
125 void VariableDict::add(const QString & key, const Vector & value, int code)
127 DEBUG->print("VariableDict::addVariable()");
131 DEBUG->print("VariableDict::addVariable(): No empty keys allowed.", Debug::D_WARNING);
135 variables.replace(key, new Variable(value, code));
139 * Gets the value for the given variable.
141 * @param key Key of the variable.
142 * @param def Default value.
144 * @return The value for the given variable or the given default value
145 * if the variable couldn't be found.
147 Vector VariableDict::getVector(const QString & key, const Vector & def)
150 // Variable * ptr = variables.find(key);
151 Variable * ptr = variables.value(key);
153 if (ptr == NULL || ptr->getType() != RS2::VariableVector)
156 ret = ptr->getVector();
162 * Gets the value for the given variable.
164 * @param key Key of the variable.
165 * @param def Default value.
167 * @return The value for the given variable or the given default value
168 * if the variable couldn't be found.
170 QString VariableDict::getString(const QString & key, const QString & def)
174 DEBUG->print("VariableDict::getString: 001");
175 DEBUG->print("VariableDict::getString: key: '%s'", key.toLatin1().data());
177 // Variable * ptr = variables.find(key);
178 Variable * ptr = variables.value(key);
179 DEBUG->print("VariableDict::getString: 002");
183 DEBUG->print("VariableDict::getString: 003");
186 else if (ptr->getType() != RS2::VariableString)
188 DEBUG->print("VariableDict::getString: 004");
193 DEBUG->print("VariableDict::getString: 005");
194 ret = ptr->getString();
197 DEBUG->print("VariableDict::getString: 006");
203 * Gets the value as int for the given variable.
205 * @param key Key of the variable.
206 * @param def Default value.
208 * @return The value for the given variable or the given default value
209 * if the variable couldn't be found.
211 int VariableDict::getInt(const QString & key, int def)
214 // Variable * ptr = variables.find(key);
215 Variable * ptr = variables.value(key);
217 if (ptr == NULL || ptr->getType() != RS2::VariableInt)
226 * Gets the value as double for the given variable.
228 * @param key Key of the variable.
229 * @param def Default value.
231 * @return The value for the given variable or the given default value
232 * if the variable couldn't be found.
234 double VariableDict::getDouble(const QString & key, double def)
237 // Variable * ptr = variables.find(key);
238 Variable * ptr = variables.value(key);
240 if (ptr == NULL || ptr->getType() != RS2::VariableDouble)
243 ret = ptr->getDouble();
249 * Notifies the listeners about layers that were added. This can be
250 * used after adding a lot of variables without auto-update.
253 void VariableDict::addBlockNotification() {
254 for (uint i=0; i<blockListListeners.count(); ++i) {
255 VariableDictListener* l = blockListListeners.at(i);
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.
266 void VariableDict::remove(const QString & key)
268 DEBUG->print("VariableDict::removeVariable()");
270 // here the block is removed from the list but not deleted
271 variables.remove(key);
274 //Q3Dict<Variable> & VariableDict::getVariableDict()
275 QMultiHash<QString, Variable *> & VariableDict::getVariableDict()
281 * Dumps the variables to stdout.
283 std::ostream & operator<<(std::ostream & os, VariableDict & d)
285 os << "Variables: \n";
286 // Q3DictIterator<Variable> it(d.variables);
287 QHashIterator<QString, Variable *> it(d.variables);
292 os << it.key().toAscii().data() << ": ";
294 switch (it.value()->getType())
296 case RS2::VariableVoid:
299 case RS2::VariableInt:
300 os << "int " << it.value()->getInt() << "\n";
302 case RS2::VariableDouble:
303 os << "double " << it.value()->getDouble() << "\n";
305 case RS2::VariableVector:
306 os << "vector " << it.value()->getVector() << "\n";
308 case RS2::VariableString:
309 os << "string " << it.value()->getString().toAscii().data() << "\n";