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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/12/2010 Created this file. :-)
15 #include "rs_variabledict.h"
20 RS_VariableDict::RS_VariableDict()
22 #warning "!!! Need to deal with setAutoDelete() Qt3->Qt4 !!!"
23 // variables.setAutoDelete(true);
26 /*virtual*/ RS_VariableDict::~RS_VariableDict()
31 * Removes all variables in the blocklist.
33 void RS_VariableDict::clear()
39 * @return Number of variables available.
41 int RS_VariableDict::count()
43 return variables.count();
47 * Activates the given block.
48 * Listeners are notified.
50 //void RS_VariableDict::activateBlock(const QString& name) {
51 // activateBlock(findBlock(name));
55 * Activates the given block.
56 * Listeners are notified.
58 /*void RS_VariableDict::activateBlock(RS_Block* block) {
61 for (uint i=0; i<blockListListeners.count(); ++i) {
62 RS_VariableDictListener* l = blockListListeners.at(i);
64 l->blockActivated(activeBlock);
69 * Adds a variable to the variable dictionary. If a variable with the
70 * same name already exists, is will be overwritten.
72 void RS_VariableDict::add(const QString & key, const QString & value, int code)
74 RS_DEBUG->print("RS_VariableDict::addVariable()");
78 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
82 variables.replace(key, new RS_Variable(value, code));
86 * Adds a variable to the variable dictionary. If a variable with the
87 * same name already exists, is will be overwritten.
89 void RS_VariableDict::add(const QString & key, int value, int code)
91 RS_DEBUG->print("RS_VariableDict::addVariable()");
95 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
99 variables.replace(key, new RS_Variable(value, code));
103 * Adds a variable to the variable dictionary. If a variable with the
104 * same name already exists, is will be overwritten.
106 void RS_VariableDict::add(const QString & key, double value, int code)
108 RS_DEBUG->print("RS_VariableDict::addVariable()");
112 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
116 variables.replace(key, new RS_Variable(value, code));
120 * Adds a variable to the variable dictionary. If a variable with the
121 * same name already exists, is will be overwritten.
123 void RS_VariableDict::add(const QString & key, const Vector & value, int code)
125 RS_DEBUG->print("RS_VariableDict::addVariable()");
129 RS_DEBUG->print("RS_VariableDict::addVariable(): No empty keys allowed.", RS_Debug::D_WARNING);
133 variables.replace(key, new RS_Variable(value, code));
137 * Gets the value for the given variable.
139 * @param key Key of the variable.
140 * @param def Default value.
142 * @return The value for the given variable or the given default value
143 * if the variable couldn't be found.
145 Vector RS_VariableDict::getVector(const QString & key, const Vector & def)
148 // RS_Variable * ptr = variables.find(key);
149 RS_Variable * ptr = variables.value(key);
151 if (ptr == NULL || ptr->getType() != RS2::VariableVector)
154 ret = ptr->getVector();
160 * Gets the value for the given variable.
162 * @param key Key of the variable.
163 * @param def Default value.
165 * @return The value for the given variable or the given default value
166 * if the variable couldn't be found.
168 QString RS_VariableDict::getString(const QString & key, const QString & def)
172 RS_DEBUG->print("RS_VariableDict::getString: 001");
173 RS_DEBUG->print("RS_VariableDict::getString: key: '%s'", key.toLatin1().data());
175 // RS_Variable * ptr = variables.find(key);
176 RS_Variable * ptr = variables.value(key);
177 RS_DEBUG->print("RS_VariableDict::getString: 002");
181 RS_DEBUG->print("RS_VariableDict::getString: 003");
184 else if (ptr->getType() != RS2::VariableString)
186 RS_DEBUG->print("RS_VariableDict::getString: 004");
191 RS_DEBUG->print("RS_VariableDict::getString: 005");
192 ret = ptr->getString();
195 RS_DEBUG->print("RS_VariableDict::getString: 006");
201 * Gets the value as int for the given variable.
203 * @param key Key of the variable.
204 * @param def Default value.
206 * @return The value for the given variable or the given default value
207 * if the variable couldn't be found.
209 int RS_VariableDict::getInt(const QString & key, int def)
212 // RS_Variable * ptr = variables.find(key);
213 RS_Variable * ptr = variables.value(key);
215 if (ptr == NULL || ptr->getType() != RS2::VariableInt)
224 * Gets the value as double for the given variable.
226 * @param key Key of the variable.
227 * @param def Default value.
229 * @return The value for the given variable or the given default value
230 * if the variable couldn't be found.
232 double RS_VariableDict::getDouble(const QString & key, double def)
235 // RS_Variable * ptr = variables.find(key);
236 RS_Variable * ptr = variables.value(key);
238 if (ptr == NULL || ptr->getType() != RS2::VariableDouble)
241 ret = ptr->getDouble();
247 * Notifies the listeners about layers that were added. This can be
248 * used after adding a lot of variables without auto-update.
251 void RS_VariableDict::addBlockNotification() {
252 for (uint i=0; i<blockListListeners.count(); ++i) {
253 RS_VariableDictListener* l = blockListListeners.at(i);
260 * Removes a variable from the list.
261 * TODO: Listeners are notified after the block was removed from
262 * the list but before it gets deleted.
264 void RS_VariableDict::remove(const QString & key)
266 RS_DEBUG->print("RS_VariableDict::removeVariable()");
268 // here the block is removed from the list but not deleted
269 variables.remove(key);
272 //Q3Dict<RS_Variable> & RS_VariableDict::getVariableDict()
273 QMultiHash<QString, RS_Variable *> & RS_VariableDict::getVariableDict()
279 * Dumps the variables to stdout.
281 std::ostream & operator<<(std::ostream & os, RS_VariableDict & d)
283 os << "Variables: \n";
284 // Q3DictIterator<RS_Variable> it(d.variables);
285 QHashIterator<QString, RS_Variable *> it(d.variables);
287 // for(; it.current(); ++it)
291 //Hmm, not sure about this...
292 #warning "Not sure if this is correct usage (.toAscii().data())... !!! FIX !!!"
293 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";