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 "rs_variabledict.h"
22 RS_VariableDict::RS_VariableDict()
24 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
25 // variables.setAutoDelete(true);
28 /*virtual*/ RS_VariableDict::~RS_VariableDict()
33 * Removes all variables in the blocklist.
35 void RS_VariableDict::clear()
41 * @return Number of variables available.
43 int RS_VariableDict::count()
45 return variables.count();
49 * Activates the given block.
50 * Listeners are notified.
52 //void RS_VariableDict::activateBlock(const QString& name) {
53 // activateBlock(findBlock(name));
57 * Activates the given block.
58 * Listeners are notified.
60 /*void RS_VariableDict::activateBlock(RS_Block* block) {
63 for (uint i=0; i<blockListListeners.count(); ++i) {
64 RS_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 RS_VariableDict::add(const QString & key, const QString & value, int code)
76 RS_DEBUG->print("RS_VariableDict::addVariable()");
80 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
84 variables.replace(key, new RS_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 RS_VariableDict::add(const QString & key, int value, int code)
93 RS_DEBUG->print("RS_VariableDict::addVariable()");
97 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
101 variables.replace(key, new RS_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 RS_VariableDict::add(const QString & key, double value, int code)
110 RS_DEBUG->print("RS_VariableDict::addVariable()");
114 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
118 variables.replace(key, new RS_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 RS_VariableDict::add(const QString & key, const Vector & value, int code)
127 RS_DEBUG->print("RS_VariableDict::addVariable()");
131 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
135 variables.replace(key, new RS_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 RS_VariableDict::getVector(const QString & key, const Vector & def)
150 // RS_Variable * ptr = variables.find(key);
151 RS_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 RS_VariableDict::getString(const QString & key, const QString & def)
174 RS_DEBUG->print("RS_VariableDict::getString: 001");
175 RS_DEBUG->print("RS_VariableDict::getString: key: '%s'", key.toLatin1().data());
177 // RS_Variable * ptr = variables.find(key);
178 RS_Variable * ptr = variables.value(key);
179 RS_DEBUG->print("RS_VariableDict::getString: 002");
183 RS_DEBUG->print("RS_VariableDict::getString: 003");
186 else if (ptr->getType() != RS2::VariableString)
188 RS_DEBUG->print("RS_VariableDict::getString: 004");
193 RS_DEBUG->print("RS_VariableDict::getString: 005");
194 ret = ptr->getString();
197 RS_DEBUG->print("RS_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 RS_VariableDict::getInt(const QString & key, int def)
214 // RS_Variable * ptr = variables.find(key);
215 RS_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 RS_VariableDict::getDouble(const QString & key, double def)
237 // RS_Variable * ptr = variables.find(key);
238 RS_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 RS_VariableDict::addBlockNotification() {
254 for (uint i=0; i<blockListListeners.count(); ++i) {
255 RS_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 RS_VariableDict::remove(const QString & key)
268 RS_DEBUG->print("RS_VariableDict::removeVariable()");
270 // here the block is removed from the list but not deleted
271 variables.remove(key);
274 //Q3Dict<RS_Variable> & RS_VariableDict::getVariableDict()
275 QMultiHash<QString, RS_Variable *> & RS_VariableDict::getVariableDict()
281 * Dumps the variables to stdout.
283 std::ostream & operator<<(std::ostream & os, RS_VariableDict & d)
285 os << "Variables: \n";
286 // Q3DictIterator<RS_Variable> it(d.variables);
287 QHashIterator<QString, RS_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";