File size: 10,101 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | /****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2015 A. Stebich (librecad@mail.lordofbikes.de)
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include<iostream>
#include "rs_variabledict.h"
#include "rs_debug.h"
/**
* Removes all variables in the blocklist.
*/
void RS_VariableDict::clear() {
variables.clear();
}
/**
* Activates the given block.
* Listeners are notified.
*/
//void RS_VariableDict::activateBlock(const QString& name) {
// activateBlock(findBlock(name));
//}
/**
* Activates the given block.
* Listeners are notified.
*/
/*void RS_VariableDict::activateBlock(RS_Block* block)
{
activeBlock = block;
for (unsigned i=0; i<blockListListeners.count(); ++i) {
RS_VariableDictListener* l = blockListListeners.at(i);
l->blockActivated(activeBlock);
}
}*/
/**
* Adds a variable to the variable dictionary. If a variable with the
* same name already exists, is will be overwritten.
*/
void RS_VariableDict::add(const QString& key,
const QString& value, int code) {
RS_DEBUG->print("RS_VariableDict::addVariable()");
if (key.isEmpty()) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_VariableDict::addVariable(): No empty keys allowed.");
return;
}
variables.insert(key, RS_Variable(value, code));
}
void RS_VariableDict::add(const QString& key,
const QString& value, int code, int type) {
RS_DEBUG->print("RS_VariableDict::addVariable()");
if (key.isEmpty()) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_VariableDict::addVariable(): No empty keys allowed.");
return;
}
RS_Variable result;
bool ok {false};
switch (type) {
case RS2::VariableString: {
ok = true;
result = RS_Variable(value, code);
break;
}
case RS2::VariableInt: {
int val = value.toInt(&ok);
if (ok) {
result = RS_Variable(val, code);
}
break;
}
case RS2::VariableDouble: {
double val = value.toDouble(&ok);
if (ok) {
result = RS_Variable(val, code);
}
break;
}
case RS2::VariableVector: {
int separatorPos = value.trimmed().indexOf(' ');
if (separatorPos == -1) {
break;
}
QString left = value.left(separatorPos);
QString right = value.right(separatorPos);
double x = left.toDouble(&ok);
if (!ok) {
break;
}
double y = right.toDouble(&ok);
if (!ok) {
break;
}
RS_Vector vect(x, y);
result = RS_Variable(vect, code);
break;
}
default:
ok = false;
}
if (ok) {
variables.insert(key, result);
}
else {
RS_DEBUG->print(QString("RS_VariableDict::addVariable(): Cant convert var from string. Name: %1, value: %2.").arg(key).arg(value));
}
}
/**
* Adds a variable to the variable dictionary. If a variable with the
* same name already exists, is will be overwritten.
*/
void RS_VariableDict::add(const QString& key, int value, int code) {
RS_DEBUG->print("RS_VariableDict::addVariable()");
if (key.isEmpty()) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_VariableDict::addVariable(): No empty keys allowed.");
return;
}
variables.insert(key, RS_Variable(value, code));
}
void RS_VariableDict::add(const QString& key, bool value, int code) {
RS_DEBUG->print("RS_VariableDict::addVariable()");
if (key.isEmpty()) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_VariableDict::addVariable(): No empty keys allowed.");
return;
}
variables.insert(key, RS_Variable(value ? 1: 0, code));
}
/**
* Adds a variable to the variable dictionary. If a variable with the
* same name already exists, is will be overwritten.
*/
void RS_VariableDict::add(const QString& key, double value, int code) {
RS_DEBUG->print("RS_VariableDict::addVariable()");
if (key.isEmpty()) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_VariableDict::addVariable(): No empty keys allowed.");
return;
}
variables.insert(key, RS_Variable(value, code));
}
/**
* Adds a variable to the variable dictionary. If a variable with the
* same name already exists, is will be overwritten.
*/
void RS_VariableDict::add(const QString& key,
const RS_Vector& value, int code) {
RS_DEBUG->print("RS_VariableDict::addVariable()");
if (key.isEmpty()) {
RS_DEBUG->print(RS_Debug::D_WARNING,
"RS_VariableDict::addVariable(): No empty keys allowed.");
return;
}
variables.insert(key, RS_Variable(value, code));
}
/**
* Gets the value for the given variable.
*
* @param key Key of the variable.
* @param def Default value.
*
* @return The value for the given variable or the given default value
* if the variable couldn't be found.
*/
RS_Vector RS_VariableDict::getVector(const QString& key, const RS_Vector& def) const {
RS_Vector ret;
auto i = variables.find(key);
if (variables.end() != i && RS2::VariableVector == i.value().getType()) {
ret = i.value().getVector();
}
else {
ret = def;
}
return ret;
}
/**
* Gets the value for the given variable.
*
* @param key Key of the variable.
* @param def Default value.
*
* @return The value for the given variable or the given default value
* if the variable couldn't be found.
*/
QString RS_VariableDict::getString(const QString& key, const QString& def) const {
QString ret;
RS_DEBUG->print("RS_VariableDict::getString: key: '%s'", key.toLatin1().data());
auto i = variables.find(key);
if (variables.end() != i && RS2::VariableString == i.value().getType()) {
ret = i.value().getString();
}
else {
ret = def;
}
return ret;
}
/**
* Gets the value as int for the given variable.
*
* @param key Key of the variable.
* @param def Default value.
*
* @return The value for the given variable or the given default value
* if the variable couldn't be found.
*/
int RS_VariableDict::getInt(const QString& key, int def) const {
int ret = 0;
auto i = variables.find(key);
if (variables.end() != i && RS2::VariableInt == i.value().getType()) {
ret = i.value().getInt();
}
else {
ret = def;
}
return ret;
}
bool RS_VariableDict::getBool(const QString& key, bool def) const {
bool defValue = def ? 1 : 0;
return getInt(key, defValue) != 0;
}
/**
* Gets the value as double for the given variable.
*
* @param key Key of the variable.
* @param def Default value.
*
* @return The value for the given variable or the given default value
* if the variable couldn't be found.
*/
double RS_VariableDict::getDouble(const QString& key, double def) const {
double ret = 0.0;
auto i = variables.find(key);
if (variables.end() != i && RS2::VariableDouble == i.value().getType()) {
ret = i.value().getDouble();
}
else {
ret = def;
}
return ret;
}
/**
* Notifies the listeners about layers that were added. This can be
* used after adding a lot of variables without auto-update.
*/
/*
void RS_VariableDict::addBlockNotification()
{
for (unsigned i=0; i<blockListListeners.count(); ++i) {
RS_VariableDictListener* l = blockListListeners.at(i);
l->blockAdded(NULL);
}
}
*/
/**
* Removes a variable from the list.
* TODO: Listeners are notified after the block was removed from
* the list but before it gets deleted.
*/
void RS_VariableDict::remove(const QString& key) {
RS_DEBUG->print("RS_VariableDict::removeVariable()");
// here the block is removed from the list but not deleted
variables.remove(key);
}
/**
* Dumps the variables to stdout.
*/
std::ostream& operator <<(std::ostream& os, RS_VariableDict& d) {
os << "Variables: \n";
auto it = d.variables.begin();
while (it != d.variables.end()) {
os << it.key().toLatin1().data() << ": ";
switch (it.value().getType()) {
case RS2::VariableVoid:
os << "void\n";
break;
case RS2::VariableInt:
os << "int " << it.value().getInt() << "\n";
break;
case RS2::VariableDouble:
os << "double " << it.value().getDouble() << "\n";
break;
case RS2::VariableVector:
os << "vector " << it.value().getVector() << "\n";
break;
case RS2::VariableString:
os << "string " << it.value().getString().toLatin1().data() << "\n";
break;
}
++it;
}
os << std::endl;
return os;
}
|