From 23b8fb197f4f81530bde6e8d9cac7eaed20a5b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 31 Dec 2010 16:03:58 +0100 Subject: [PATCH] Function for geting scene of an object. Makes use of the fact that scene and only scene is parent of itself. --- src/Object.cpp | 13 +++++++++++++ src/Object.h | 6 ++++++ src/Test/ObjectTest.cpp | 14 ++++++++++++++ src/Test/ObjectTest.h | 1 + 4 files changed, 34 insertions(+) diff --git a/src/Object.cpp b/src/Object.cpp index eb948d854..8ba541163 100644 --- a/src/Object.cpp +++ b/src/Object.cpp @@ -14,6 +14,7 @@ */ #include "Object.h" +#include "Scene.h" using namespace std; @@ -51,4 +52,16 @@ Object::~Object() { delete *it; } +Scene* Object::scene() const { + /* Goes up the family tree until it finds object which is parent of itself + (that's the scene) */ + Object* p = parent(); + while(p != 0) { + if(p->parent() == p) return static_cast(p); + p = p->parent(); + } + + return 0; +} + } diff --git a/src/Object.h b/src/Object.h index 489a5b749..3f35eec74 100644 --- a/src/Object.h +++ b/src/Object.h @@ -57,6 +57,12 @@ class Object { */ virtual ~Object(); + /** + * @brief Scene + * @return If the object is not assigned to any scene, returns 0. + */ + Scene* scene() const; + /** @brief Parent object */ inline Object* parent() const { return _parent; } diff --git a/src/Test/ObjectTest.cpp b/src/Test/ObjectTest.cpp index 12f02dca3..22a15959b 100644 --- a/src/Test/ObjectTest.cpp +++ b/src/Test/ObjectTest.cpp @@ -14,6 +14,7 @@ */ #include "ObjectTest.h" +#include "Scene.h" #include @@ -48,4 +49,17 @@ void ObjectTest::parenting() { QVERIFY(childOne->children().size() == 0); } +void ObjectTest::scene() { + Scene scene; + + Object* childOne = new Object(&scene); + Object* childTwo = new Object(childOne); + + Object* orphan = new Object; + Object* childOfOrphan = new Object(orphan); + + QVERIFY(childTwo->scene() == &scene); + QVERIFY(childOfOrphan->scene() == 0); +} + }} diff --git a/src/Test/ObjectTest.h b/src/Test/ObjectTest.h index 8e311d828..fccd4a903 100644 --- a/src/Test/ObjectTest.h +++ b/src/Test/ObjectTest.h @@ -26,6 +26,7 @@ class ObjectTest: public QObject { private slots: void parenting(); + void scene(); }; }}