1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Fritzing is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 6141 $:
22 $Author: cohen@irascible.com $:
23 $Date: 2012-07-04 21:20:05 +0200 (Mi, 04. Jul 2012) $
24 
25 ********************************************************************/
26 
27 #include "propertydef.h"
28 #include "../debugdialog.h"
29 #include "../model/modelpart.h"
30 #include "../utils/textutils.h"
31 
32 #include <QDomNodeList>
33 #include <QDomDocument>
34 #include <QDomElement>
35 #include <QFile>
36 
37 QList <PropertyDef *> PropertyDefMaster::PropertyDefs;
38 
loadPropertyDefs()39 void PropertyDefMaster::loadPropertyDefs() {
40 	QFile file(":/resources/properties.xml");
41 
42 	QString errorStr;
43 	int errorLine;
44 	int errorColumn;
45 
46 	QDomDocument domDocument;
47 	if (!domDocument.setContent(&file, true, &errorStr, &errorLine, &errorColumn)) {
48 		DebugDialog::debug(QString("failed loading properties %1 line:%2 col:%3").arg(errorStr).arg(errorLine).arg(errorColumn));
49 		return;
50 	}
51 
52 	QDomElement root = domDocument.documentElement();
53 	if (root.isNull()) return;
54 	if (root.tagName() != "properties") return;
55 
56 	QDomElement propertyElement = root.firstChildElement("property");
57 	while (!propertyElement.isNull()) {
58 		PropertyDef * propertyDef = new PropertyDef;
59 		propertyDef->name = propertyElement.attribute("name");
60 
61 		PropertyDefs.append(propertyDef);
62 		propertyDef->symbol = propertyElement.attribute("symbol");
63 		propertyDef->minValue = propertyElement.attribute("minValue").toDouble();
64 		propertyDef->maxValue = propertyElement.attribute("maxValue").toDouble();
65 		propertyDef->defaultValue = propertyElement.attribute("defaultValue");
66 		propertyDef->editable = propertyElement.attribute("editable", "").compare("yes") == 0;
67 		propertyDef->numeric = propertyElement.attribute("numeric", "").compare("yes") == 0;
68 		QDomElement menuItem = propertyElement.firstChildElement("menuItem");
69 		while (!menuItem.isNull()) {
70 			QString val = menuItem.attribute("value");
71 			if (propertyDef->numeric) {
72 				propertyDef->menuItems.append(val.toDouble());
73 			}
74 			else {
75 				propertyDef->sMenuItems.append(val);
76 			}
77 			QString adjunct = menuItem.attribute("adjunct");
78 			if (!adjunct.isEmpty()) {
79 				propertyDef->adjuncts.insert(val, adjunct);
80 			}
81 			menuItem = menuItem.nextSiblingElement("menuItem");
82 		}
83 		QDomElement suffixElement = propertyElement.firstChildElement("suffix");
84 		while (!suffixElement.isNull()) {
85 			QString suffix = suffixElement.attribute("suffix");
86 			propertyDef->suffixes.append(suffix);
87 			suffixElement = suffixElement.nextSiblingElement("suffix");
88 		}
89 
90 		propertyElement = propertyElement.nextSiblingElement("property");
91 	}
92 
93 }
94 
cleanup()95 void PropertyDefMaster::cleanup() {
96     foreach (PropertyDef * propertyDef, PropertyDefs) {
97         delete propertyDef;
98     }
99 
100     PropertyDefs.clear();
101 }
102 
initPropertyDefs(ModelPart * modelPart,QHash<PropertyDef *,QString> & propertyDefs)103 void PropertyDefMaster::initPropertyDefs(ModelPart * modelPart, QHash<PropertyDef *, QString> & propertyDefs)
104 {
105 	if (PropertyDefs.count() == 0) {
106 		loadPropertyDefs();
107 	}
108 
109 	foreach (PropertyDef * propertyDef, PropertyDefs) {
110 		foreach (QString suffix, propertyDef->suffixes) {
111 			if (!modelPart->moduleID().endsWith(suffix, Qt::CaseInsensitive)) continue;
112 
113 			//DebugDialog::debug(QString("%1 %2").arg(suffix).arg(modelPart->moduleID()));
114 			QString defaultValue;
115 			if (propertyDef->numeric) {
116 				if (!propertyDef->defaultValue.isEmpty()) {
117 					defaultValue = TextUtils::convertToPowerPrefix(propertyDef->defaultValue.toDouble()) + propertyDef->symbol;
118 				}
119 			}
120 			else {
121 				defaultValue = propertyDef->defaultValue;
122 			}
123 			QString savedValue = modelPart->localProp(propertyDef->name).toString();
124 			if (savedValue.isEmpty()) {
125 				savedValue = modelPart->properties().value(propertyDef->name.toLower(), defaultValue);
126 				if (!savedValue.isEmpty()) {
127 					modelPart->setLocalProp(propertyDef->name, savedValue);
128 				}
129 			}
130 			// caches the current value
131 			propertyDefs.insert(propertyDef, savedValue);
132 		}
133 	}
134 }
135 
136 
137 
138