Commit bd688ffcf0df6a11d19f3f46018ae62ce3ba20c9

Authored by Charles Otto
1 parent fac7a232

Rework implementation of setPropertyRecursive

Introduce QList<Object*> Object::getChildren(), which returns the
(immeidate) child objects of a given transform/distance. The default
implementation looks for properties of the appropriate type.

Implment setPropertyRecursive leveraging getChildren (this reduces the
amount of special casing needed to implement spr).

Special casing for stream, independent, and LoadStore due to various
oddities in their handling of child transforms
openbr/openbr_plugin.cpp
... ... @@ -786,7 +786,7 @@ void Object::load(QDataStream &amp;stream)
786 786 init();
787 787 }
788 788  
789   -bool Object::setPropertyRecursive(const QString &name, QVariant value)
  789 +bool Object::setExistingProperty(const QString &name, QVariant value)
790 790 {
791 791 if (this->metaObject()->indexOfProperty(qPrintable(name)) == -1)
792 792 return false;
... ... @@ -795,6 +795,56 @@ bool Object::setPropertyRecursive(const QString &amp;name, QVariant value)
795 795 return true;
796 796 }
797 797  
  798 +QList<Object *> Object::getChildren() const
  799 +{
  800 + QList<Object *> output;
  801 + for (int i=0; i < metaObject()->propertyCount(); i++) {
  802 + const char *prop_name = metaObject()->property(i).name();
  803 + const QVariant &variant = this->property(prop_name);
  804 +
  805 + if (variant.canConvert<Transform *>()) {
  806 + Transform *tform = variant.value<Transform *>();
  807 + if (tform)
  808 + output.append((Object* ) variant.value<Transform *>());
  809 + }
  810 + else if (variant.canConvert<QList<Transform *> >()) {
  811 + foreach (const Transform *tform, variant.value<QList<Transform *> >()) {
  812 + if (tform)
  813 + output.append((Object *) tform);
  814 + }
  815 + }
  816 + else if (variant.canConvert<Distance *>()) {
  817 + Distance *dist = variant.value<Distance *>();
  818 + if (dist)
  819 + output.append((Object* ) variant.value<Distance *>());
  820 + }
  821 + else if (variant.canConvert<QList<Distance *> >()) {
  822 + foreach (const Distance *dist, variant.value<QList<Distance *> >()) {
  823 + if (dist)
  824 + output.append((Object *) dist);
  825 + }
  826 + }
  827 + }
  828 + return output;
  829 +}
  830 +
  831 +bool Object::setPropertyRecursive(const QString &name, QVariant value)
  832 +{
  833 + // collect children
  834 + bool res = setExistingProperty(name, value);
  835 + if (res)
  836 + return true;
  837 +
  838 + QList<Object *> children = getChildren();
  839 + foreach (Object *obj, children) {
  840 + if (obj->setPropertyRecursive(name, value)) {
  841 + init();
  842 + return true;
  843 + }
  844 + }
  845 + return false;
  846 +}
  847 +
798 848 void Object::setProperty(const QString &name, QVariant value)
799 849 {
800 850 QString type;
... ... @@ -1408,24 +1458,9 @@ void Transform::project(const TemplateList &amp;src, TemplateList &amp;dst) const
1408 1458 futures.waitForFinished();
1409 1459 }
1410 1460  
1411   -QList<Transform *> Transform::getChildren() const
1412   -{
1413   - QList<Transform *> output;
1414   - for (int i=0; i < metaObject()->propertyCount(); i++) {
1415   - const char *prop_name = metaObject()->property(i).name();
1416   - const QVariant &variant = this->property(prop_name);
1417   -
1418   - if (variant.canConvert<Transform *>())
1419   - output.append(variant.value<Transform *>());
1420   - if (variant.canConvert<QList<Transform *> >())
1421   - output.append(variant.value<QList<Transform *> >());
1422   - }
1423   - return output;
1424   -}
1425   -
1426 1461 TemplateEvent *Transform::getEvent(const QString &name)
1427 1462 {
1428   - foreach (Transform *child, getChildren()) {
  1463 + foreach (Transform *child, getChildren<Transform>()) {
1429 1464 TemplateEvent *probe = child->getEvent(name);
1430 1465 if (probe)
1431 1466 return probe;
... ...
openbr/openbr_plugin.h
... ... @@ -603,6 +603,22 @@ public:
603 603  
604 604 void setProperty(const QString &name, QVariant value); /*!< \brief Overload of QObject::setProperty to handle OpenBR data types. */
605 605 virtual bool setPropertyRecursive(const QString &name, QVariant value); /*!< \brief Recursive version of setProperty, try to set the property on this object, or its children, returns true if successful. */
  606 + bool setExistingProperty(const QString &name, QVariant value); /*! <\brief attempt to set property 'name' on this object. If name is not a pre-declared property, return false */
  607 +
  608 + virtual QList<Object *> getChildren() const; /*!< \brief retrieve children of this object, default version scans properties, subclasses which do not sotre their children as properties must overload. */
  609 +
  610 + template<typename T>
  611 + QList<T *> getChildren() const
  612 + {
  613 + QList<Object *> children = getChildren();
  614 + QList<T *> output;
  615 + foreach(Object *obj, children) {
  616 + T *temp = dynamic_cast<T *>(obj);
  617 + if (temp != NULL)
  618 + output.append(temp);
  619 + }
  620 + return output;
  621 + }
606 622  
607 623 static QStringList parse(const QString &string, char split = ','); /*!< \brief Splits the string while respecting lexical scoping of <tt>()</tt>, <tt>[]</tt>, <tt>\<\></tt>, and <tt>{}</tt>. */
608 624  
... ... @@ -1276,12 +1292,6 @@ public:
1276 1292 */
1277 1293 virtual TemplateEvent *getEvent(const QString &name);
1278 1294  
1279   - /*!
1280   - * \brief Get a list of child transforms of this transform, child transforms are considered to be
1281   - * any transforms stored as properties of this transform.
1282   - */
1283   - QList<Transform *> getChildren() const;
1284   -
1285 1295 static Transform *deserialize(QDataStream &stream)
1286 1296 {
1287 1297 QString desc;
... ...
openbr/plugins/independent.cpp
... ... @@ -138,9 +138,10 @@ class IndependentTransform : public MetaTransform
138 138 return transform->description(expanded);
139 139 }
140 140  
  141 + // can't use general setPropertyRecursive because of transforms oddness
141 142 bool setPropertyRecursive(const QString &name, QVariant value)
142 143 {
143   - if (br::Object::setPropertyRecursive(name, value))
  144 + if (br::Object::setExistingProperty(name, value))
144 145 return true;
145 146  
146 147 if (!transform->setPropertyRecursive(name, value))
... ...
openbr/plugins/meta.cpp
... ... @@ -529,11 +529,11 @@ public:
529 529 return res;
530 530 }
531 531  
532   - bool setPropertyRecursive(const QString &name, QVariant value)
  532 + QList<Object *> getChildren() const
533 533 {
534   - if (br::Object::setPropertyRecursive(name, value))
535   - return true;
536   - return transform->setPropertyRecursive(name, value);
  534 + QList<Object *> rval;
  535 + rval.append(transform);
  536 + return rval;
537 537 }
538 538 private:
539 539  
... ...
openbr/plugins/openbr_internal.h
... ... @@ -227,19 +227,6 @@ public:
227 227 return output;
228 228 }
229 229  
230   -
231   - bool setPropertyRecursive(const QString &name, QVariant value)
232   - {
233   - if (br::Object::setPropertyRecursive(name, value))
234   - return true;
235   -
236   - if (transform->setPropertyRecursive(name, value)) {
237   - init();
238   - return true;
239   - }
240   - return false;
241   - }
242   -
243 230 Transform *smartCopy(bool &newTransform)
244 231 {
245 232 if (!timeVarying()) {
... ... @@ -392,21 +379,6 @@ public:
392 379 return output;
393 380 }
394 381  
395   - bool setPropertyRecursive(const QString &name, QVariant value)
396   - {
397   - if (br::Object::setPropertyRecursive(name, value))
398   - return true;
399   -
400   - for (int i=0; i < this->transforms.size();i++) {
401   - if (transforms[i]->setPropertyRecursive(name, value)) {
402   - init();
403   - return true;
404   - }
405   - }
406   - return false;
407   - }
408   -
409   -
410 382 protected:
411 383 bool isTimeVarying;
412 384  
... ...
openbr/plugins/stream.cpp
... ... @@ -1325,9 +1325,10 @@ public:
1325 1325 return res;
1326 1326 }
1327 1327  
  1328 + // can't use general setPropertyRecursive due to special handling of basis
1328 1329 bool setPropertyRecursive(const QString &name, QVariant value)
1329 1330 {
1330   - if (br::Object::setPropertyRecursive(name, value))
  1331 + if (br::Object::setExistingProperty(name, value))
1331 1332 return true;
1332 1333  
1333 1334 for (int i=0; i < basis->transforms.size();i++) {
... ... @@ -1336,6 +1337,12 @@ public:
1336 1337 return true;
1337 1338 }
1338 1339 }
  1340 +
  1341 + if (basis->endPoint->setPropertyRecursive(name, value)) {
  1342 + basis->endPoint->init();
  1343 + return true;
  1344 + }
  1345 +
1339 1346 return false;
1340 1347 }
1341 1348  
... ...