From: CHEMIN Sébastien 236498 Date: Mon, 14 Oct 2024 12:25:46 +0000 (+0200) Subject: Objects are stored in a QHash X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fsc%2Fgui_913_for_guithare;p=modules%2Fgui.git Objects are stored in a QHash --- diff --git a/src/QtViewer/QtViewer_Object.cxx b/src/QtViewer/QtViewer_Object.cxx index 18fb3a347..273cd7267 100644 --- a/src/QtViewer/QtViewer_Object.cxx +++ b/src/QtViewer/QtViewer_Object.cxx @@ -18,6 +18,7 @@ // #include "QtViewer_Object.h" +#include "QtViewer_Scene.h" //======================================================================= // Name : QtViewer_Object @@ -35,3 +36,14 @@ QtViewer_Object::QtViewer_Object( QGraphicsItem* theParent ) QtViewer_Object::~QtViewer_Object() { } + +void QtViewer_Object::setParentItem(QGraphicsItem *newParent) +{ + QGraphicsItem::setParentItem(newParent); + + if(QtViewer_Object* obj = dynamic_cast(this)) + { + if (QtViewer_Scene* theScene = dynamic_cast(scene())) + theScene->addObject(obj); + } +} diff --git a/src/QtViewer/QtViewer_Object.h b/src/QtViewer/QtViewer_Object.h index 7e69f1a1d..a28cc838b 100644 --- a/src/QtViewer/QtViewer_Object.h +++ b/src/QtViewer/QtViewer_Object.h @@ -34,6 +34,8 @@ public: QtViewer_Object( QGraphicsItem* theParent = nullptr ); ~QtViewer_Object(); + virtual void setParentItem(QGraphicsItem *newParent); + const QString& getName() const { return myName; } virtual void setName( const QString& theName ) { myName = theName; } diff --git a/src/QtViewer/QtViewer_Scene.cxx b/src/QtViewer/QtViewer_Scene.cxx index 29675ffe9..b7d9c523a 100644 --- a/src/QtViewer/QtViewer_Scene.cxx +++ b/src/QtViewer/QtViewer_Scene.cxx @@ -18,6 +18,7 @@ // #include "QtViewer_Scene.h" +#include "QtViewer_Object.h" //======================================================================= // Name : QtViewer_Scene @@ -36,3 +37,35 @@ QtViewer_Scene::~QtViewer_Scene() { } +void QtViewer_Scene::addItem(QGraphicsItem *item) +{ + QGraphicsScene::addItem(item); + + if(QtViewer_Object* obj = dynamic_cast(item)) + addObject(obj); +} + +void QtViewer_Scene::removeItem(QGraphicsItem *item) +{ + QGraphicsScene::removeItem(item); + + if(QtViewer_Object* obj = dynamic_cast(item)) + myObjects.remove(obj->getName()); +} + +QtViewer_Object* QtViewer_Scene::getObject(const QString& name) +{ + return myObjects[name]; +} + +void QtViewer_Scene::addObject(QtViewer_Object* obj) +{ + myObjects[obj->getName()] = obj; +} + +void QtViewer_Scene::clear() +{ + myObjects.clear(); + QGraphicsScene::clear(); +} + diff --git a/src/QtViewer/QtViewer_Scene.h b/src/QtViewer/QtViewer_Scene.h index 47c1702da..1050004c2 100644 --- a/src/QtViewer/QtViewer_Scene.h +++ b/src/QtViewer/QtViewer_Scene.h @@ -22,7 +22,9 @@ #include "QtViewer.h" #include +#include +class QtViewer_Object; class QGraphicsRectItem; /* @@ -36,5 +38,16 @@ class QTVIEWER_API QtViewer_Scene : public QGraphicsScene public: QtViewer_Scene( QObject* theParent = 0 ); ~QtViewer_Scene(); + + virtual void addItem(QGraphicsItem *item); + virtual void removeItem(QGraphicsItem *item); + QtViewer_Object* getObject(const QString& name); + void addObject(QtViewer_Object* obj); + +public slots: + virtual void clear(); + +protected: + QHash myObjects; }; #endif diff --git a/src/QtViewer/QtViewer_ViewPort.cxx b/src/QtViewer/QtViewer_ViewPort.cxx index 44402b418..af79f2f2b 100644 --- a/src/QtViewer/QtViewer_ViewPort.cxx +++ b/src/QtViewer/QtViewer_ViewPort.cxx @@ -92,11 +92,6 @@ void QtViewer_ViewPort::drawForeground(QPainter *painter, const QRectF &rect) obj->draw(painter); } -void QtViewer_ViewPort::addItem( QGraphicsItem* theItem ) -{ - scene()->addItem(theItem); -} - void QtViewer_ViewPort::fitAll() { QRectF aRect; diff --git a/src/QtViewer/QtViewer_ViewPort.h b/src/QtViewer/QtViewer_ViewPort.h index 2749f9dd0..ae361b6da 100644 --- a/src/QtViewer/QtViewer_ViewPort.h +++ b/src/QtViewer/QtViewer_ViewPort.h @@ -38,7 +38,6 @@ public: QtViewer_ViewPort(QWidget* theParent); ~QtViewer_ViewPort(); - void addItem(QGraphicsItem* theItem); void fitAll(); void fitSelect();