Browse Source

Function for geting scene of an object.

Makes use of the fact that scene and only scene is parent of itself.
pull/279/head
Vladimír Vondruš 16 years ago
parent
commit
23b8fb197f
  1. 13
      src/Object.cpp
  2. 6
      src/Object.h
  3. 14
      src/Test/ObjectTest.cpp
  4. 1
      src/Test/ObjectTest.h

13
src/Object.cpp

@ -14,6 +14,7 @@
*/ */
#include "Object.h" #include "Object.h"
#include "Scene.h"
using namespace std; using namespace std;
@ -51,4 +52,16 @@ Object::~Object() {
delete *it; 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<Scene*>(p);
p = p->parent();
}
return 0;
}
} }

6
src/Object.h

@ -57,6 +57,12 @@ class Object {
*/ */
virtual ~Object(); virtual ~Object();
/**
* @brief Scene
* @return If the object is not assigned to any scene, returns 0.
*/
Scene* scene() const;
/** @brief Parent object */ /** @brief Parent object */
inline Object* parent() const { return _parent; } inline Object* parent() const { return _parent; }

14
src/Test/ObjectTest.cpp

@ -14,6 +14,7 @@
*/ */
#include "ObjectTest.h" #include "ObjectTest.h"
#include "Scene.h"
#include <QtTest/QTest> #include <QtTest/QTest>
@ -48,4 +49,17 @@ void ObjectTest::parenting() {
QVERIFY(childOne->children().size() == 0); 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);
}
}} }}

1
src/Test/ObjectTest.h

@ -26,6 +26,7 @@ class ObjectTest: public QObject {
private slots: private slots:
void parenting(); void parenting();
void scene();
}; };
}} }}

Loading…
Cancel
Save