diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-12-01 11:36:45 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-12-01 15:25:55 -0300 |
commit | 6ea32275b157bcf3a97725172a06122b4ff98d0a (patch) | |
tree | 66f8117318e275b211c067f0e908e8c12ec7097e /tests/libsample | |
parent | 64cda5a441fd912469c4ad2c7afcc39f0da6819c (diff) |
Updated ObjectType and ObjectTypeLayout to resemble the Qt's QLayout class.
Also added more test cases that use ObjectTypeLayout.
Diffstat (limited to 'tests/libsample')
-rw-r--r-- | tests/libsample/objecttype.cpp | 50 | ||||
-rw-r--r-- | tests/libsample/objecttype.h | 12 | ||||
-rw-r--r-- | tests/libsample/objecttypelayout.cpp | 29 | ||||
-rw-r--r-- | tests/libsample/objecttypelayout.h | 8 |
4 files changed, 90 insertions, 9 deletions
diff --git a/tests/libsample/objecttype.cpp b/tests/libsample/objecttype.cpp index d691c6960..98d6a8685 100644 --- a/tests/libsample/objecttype.cpp +++ b/tests/libsample/objecttype.cpp @@ -35,10 +35,11 @@ #include "objecttype.h" #include "objecttypelayout.h" #include <algorithm> +#include <iostream> using namespace std; -ObjectType::ObjectType(ObjectType* parent) : m_parent(0) +ObjectType::ObjectType(ObjectType* parent) : m_parent(0), m_layout(0) { setParent(parent); } @@ -161,12 +162,47 @@ ObjectType::event(Event* event) return true; } -void ObjectType::setObjectLayout(ObjectTypeLayout* layout) +void ObjectType::setLayout(ObjectTypeLayout* l) { - layout->setParent(this); - std::list<ObjectType*> objects = layout->objects(); - std::list<ObjectType*>::const_iterator it = objects.begin(); - for (; it != objects.end(); ++it) - (*it)->setParent(this); + if (!l) { + cerr << "[WARNING] ObjectType::setLayout: Cannot set layout to 0." << endl; + return; + } + + if (layout()) { + if (layout() != l) { + cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring(); + cerr << "' on ObjectType '" << objectName().cstring() << "', which already has a layout." << endl; + } + return; + } + + ObjectType* oldParent = l->parent(); + if (oldParent && oldParent != this) { + if (oldParent->isLayoutType()) { + cerr << "[WARNING] ObjectType::setLayout: Attempting to set ObjectTypeLayout '" << l->objectName().cstring(); + cerr << "' on ObjectType '" << objectName().cstring() << "', when the ObjectTypeLayout already has a parent layout." << endl; + return; + } else { + // Steal the layout from an ObjectType parent. + oldParent->takeLayout(); + } + } + + m_layout = l; + if (oldParent != this) { + l->setParent(this); + l->reparentChildren(this); + } +} + +ObjectTypeLayout* ObjectType::takeLayout() +{ + ObjectTypeLayout* l = layout(); + if (!l) + return 0; + m_layout = 0; + l->setParent(0); + return l; } diff --git a/tests/libsample/objecttype.h b/tests/libsample/objecttype.h index bd58b7b64..d7a7ae1c6 100644 --- a/tests/libsample/objecttype.h +++ b/tests/libsample/objecttype.h @@ -85,17 +85,27 @@ public: // Returns true if the event is processed. virtual bool event(Event* event); + // This nonsense method emulate QWidget.setLayout method // All layout objects will became children of this object. - void setObjectLayout(ObjectTypeLayout* layout); + void setLayout(ObjectTypeLayout* layout); + ObjectTypeLayout* layout() const { return m_layout; } + + // This method should be reimplemented by ObjectTypeLayout. + virtual bool isLayoutType() { return false; } private: ObjectType(const ObjectType&); ObjectType& operator=(const ObjectType&); + ObjectTypeLayout* takeLayout(); + Str m_objectName; ObjectType* m_parent; ObjectTypeList m_children; + + ObjectTypeLayout* m_layout; }; + #endif // OBJECTTYPE_H diff --git a/tests/libsample/objecttypelayout.cpp b/tests/libsample/objecttypelayout.cpp index 06c4555b4..92180418f 100644 --- a/tests/libsample/objecttypelayout.cpp +++ b/tests/libsample/objecttypelayout.cpp @@ -33,9 +33,26 @@ */ #include "objecttypelayout.h" +#include <iostream> + +using namespace std; void ObjectTypeLayout::addObject(ObjectType* obj) { + if (obj->isLayoutType()) { + ObjectTypeLayout* l = reinterpret_cast<ObjectTypeLayout*>(obj); + if (l->parent()) { + cerr << "[WARNING] ObjectTypeLayout::addObject: layout '" << l->objectName().cstring(); + cerr << "' already has a parent." << endl; + return; + } + + l->setParent(this); + + if (parent() && !parent()->isLayoutType()) + l->reparentChildren(parent()); + } + m_objects.push_back(obj); } @@ -43,3 +60,15 @@ std::list< ObjectType* > ObjectTypeLayout::objects() const { return m_objects; } + +void ObjectTypeLayout::reparentChildren(ObjectType* parent) +{ + std::list<ObjectType*>::const_iterator it = m_objects.begin(); + for (; it != m_objects.end(); ++it) { + if ((*it)->isLayoutType()) + reinterpret_cast<ObjectTypeLayout*>(*it)->reparentChildren(parent); + else + (*it)->setParent(parent); + } +} + diff --git a/tests/libsample/objecttypelayout.h b/tests/libsample/objecttypelayout.h index 7d25e09be..ba39fd330 100644 --- a/tests/libsample/objecttypelayout.h +++ b/tests/libsample/objecttypelayout.h @@ -46,8 +46,14 @@ class LIBSAMPLE_API ObjectTypeLayout : public ObjectType public: void addObject(ObjectType* obj); std::list<ObjectType*> objects() const; + + virtual bool isLayoutType() { return true; } private: std::list<ObjectType*> m_objects; + + void reparentChildren(ObjectType* parent); + friend LIBSAMPLE_API void ObjectType::setLayout(ObjectTypeLayout* l); }; -#endif +#endif // OBJECTTYPELAYOUT_H + |