Mercurial > hg > svcore
comparison base/PropertyContainer.cpp @ 199:1d789d688f59
* When adding a layer, make it the selected layer on that pane
* More OSC support, including transforms
author | Chris Cannam |
---|---|
date | Fri, 10 Nov 2006 17:45:26 +0000 |
parents | 60ba218a54bb |
children | 91fdc752e540 |
comparison
equal
deleted
inserted
replaced
198:4c5c62784211 | 199:1d789d688f59 |
---|---|
13 COPYING included with this distribution for more information. | 13 COPYING included with this distribution for more information. |
14 */ | 14 */ |
15 | 15 |
16 #include "PropertyContainer.h" | 16 #include "PropertyContainer.h" |
17 #include "CommandHistory.h" | 17 #include "CommandHistory.h" |
18 #include "RangeMapper.h" | |
19 #include "UnitDatabase.h" | |
20 | |
21 #include <QColor> | |
18 | 22 |
19 #include <iostream> | 23 #include <iostream> |
20 | 24 |
21 PropertyContainer::PropertyList | 25 PropertyContainer::PropertyList |
22 PropertyContainer::getProperties() const | 26 PropertyContainer::getProperties() const |
74 int currentValue = getPropertyRangeAndValue(name, 0, 0); | 78 int currentValue = getPropertyRangeAndValue(name, 0, 0); |
75 if (value == currentValue) return; | 79 if (value == currentValue) return; |
76 | 80 |
77 CommandHistory::getInstance()->addCommand | 81 CommandHistory::getInstance()->addCommand |
78 (new SetPropertyCommand(this, name, value), true, true); // bundled | 82 (new SetPropertyCommand(this, name, value), true, true); // bundled |
83 } | |
84 | |
85 void | |
86 PropertyContainer::setProperty(QString nameString, QString valueString) | |
87 { | |
88 PropertyName name; | |
89 int value; | |
90 if (!convertPropertyStrings(nameString, valueString, name, value)) { | |
91 std::cerr << "WARNING: PropertyContainer::setProperty(\"" | |
92 << nameString.toStdString() << "\", \"" | |
93 << valueString.toStdString() | |
94 << "\"): Name and value conversion failed" << std::endl; | |
95 return; | |
96 } | |
97 setProperty(name, value); | |
98 } | |
99 | |
100 void | |
101 PropertyContainer::setPropertyWithCommand(QString nameString, QString valueString) | |
102 { | |
103 PropertyName name; | |
104 int value; | |
105 if (!convertPropertyStrings(nameString, valueString, name, value)) { | |
106 std::cerr << "WARNING: PropertyContainer::setPropertyWithCommand(\"" | |
107 << nameString.toStdString() << "\", \"" | |
108 << valueString.toStdString() | |
109 << "\"): Name and value conversion failed" << std::endl; | |
110 return; | |
111 } | |
112 setPropertyWithCommand(name, value); | |
113 } | |
114 | |
115 bool | |
116 PropertyContainer::convertPropertyStrings(QString nameString, QString valueString, | |
117 PropertyName &name, int &value) | |
118 { | |
119 PropertyList pl = getProperties(); | |
120 | |
121 QString adjusted = nameString.trimmed(); | |
122 adjusted.replace('_', ' '); | |
123 adjusted.replace('-', ' '); | |
124 | |
125 name = ""; | |
126 | |
127 for (PropertyList::iterator pli = pl.begin(); pli != pl.end(); ++pli) { | |
128 | |
129 QString label = getPropertyLabel(*pli); | |
130 | |
131 if (label != "" && (nameString == label || adjusted == label)) { | |
132 name = *pli; | |
133 break; | |
134 } else if (nameString == *pli) { | |
135 name = *pli; | |
136 break; | |
137 } | |
138 } | |
139 | |
140 if (name == "") { | |
141 std::cerr << "PropertyContainer::convertPropertyStrings: Unable to match name string \"" << nameString.toStdString() << "\"" << std::endl; | |
142 return false; | |
143 } | |
144 | |
145 value = 0; | |
146 bool success = false; | |
147 | |
148 bool isDouble = false; | |
149 double dval = valueString.toDouble(&isDouble); | |
150 | |
151 switch (getPropertyType(name)) { | |
152 | |
153 case ToggleProperty: | |
154 if (valueString == tr("yes") || | |
155 valueString == tr("on") || | |
156 valueString == tr("true")) { | |
157 value = 1; success = true; | |
158 } else if (valueString == tr("no") || | |
159 valueString == tr("off") || | |
160 valueString == tr("false")) { | |
161 value = 0; success = true; | |
162 } | |
163 break; | |
164 | |
165 case RangeProperty: | |
166 if (isDouble) { | |
167 RangeMapper *mapper = getNewPropertyRangeMapper(name); | |
168 if (mapper) { | |
169 value = mapper->getPositionForValue(dval); | |
170 delete mapper; | |
171 success = true; | |
172 } | |
173 } | |
174 break; | |
175 | |
176 case ValueProperty: | |
177 { | |
178 int min, max; | |
179 getPropertyRangeAndValue(name, &min, &max); | |
180 for (int i = min; i <= max; ++i) { | |
181 if (valueString == getPropertyValueLabel(name, i)) { | |
182 value = i; | |
183 success = true; | |
184 break; | |
185 } | |
186 } | |
187 break; | |
188 } | |
189 | |
190 case ColourProperty: | |
191 { | |
192 QColor c(valueString); | |
193 if (c.isValid()) { | |
194 value = c.rgb(); | |
195 success = true; | |
196 } | |
197 break; | |
198 } | |
199 | |
200 case UnitsProperty: | |
201 value = UnitDatabase::getInstance()->getUnitId(valueString, false); | |
202 if (value >= 0) success = true; | |
203 else value = 0; | |
204 break; | |
205 | |
206 case InvalidProperty: | |
207 std::cerr << "PropertyContainer::convertPropertyStrings: Invalid property name \"" << name.toStdString() << "\"" << std::endl; | |
208 return false; | |
209 } | |
210 | |
211 if (success) return true; | |
212 | |
213 int min, max; | |
214 getPropertyRangeAndValue(name, &min, &max); | |
215 | |
216 bool ok = false; | |
217 int i = valueString.toInt(&ok); | |
218 if (!ok) { | |
219 std::cerr << "PropertyContainer::convertPropertyStrings: Unable to parse value string \"" << valueString.toStdString() << "\"" << std::endl; | |
220 return false; | |
221 } else if (i < min || i > max) { | |
222 std::cerr << "PropertyContainer::convertPropertyStrings: Property value \"" << i << "\" outside valid range " << min << " to " << max << std::endl; | |
223 return false; | |
224 } | |
225 | |
226 value = i; | |
227 return true; | |
79 } | 228 } |
80 | 229 |
81 PropertyContainer::SetPropertyCommand::SetPropertyCommand(PropertyContainer *pc, | 230 PropertyContainer::SetPropertyCommand::SetPropertyCommand(PropertyContainer *pc, |
82 const PropertyName &pn, | 231 const PropertyName &pn, |
83 int value) : | 232 int value) : |