From 92b3a1356e4474e7b9a15ffa47ef12d890e35a0a Mon Sep 17 00:00:00 2001 From: ouv Date: Thu, 3 Feb 2011 11:27:31 +0000 Subject: [PATCH] Improved scene bounding rectangle management --- src/GraphicsView/GraphicsView_Object.cxx | 6 ++- src/GraphicsView/GraphicsView_Scene.cxx | 32 ++++++++++++++ src/GraphicsView/GraphicsView_Scene.h | 13 ++++++ src/GraphicsView/GraphicsView_ViewPort.cxx | 50 ++++++++++++++++------ src/GraphicsView/GraphicsView_ViewPort.h | 5 +++ src/GraphicsView/GraphicsView_Viewer.cxx | 3 ++ 6 files changed, 96 insertions(+), 13 deletions(-) diff --git a/src/GraphicsView/GraphicsView_Object.cxx b/src/GraphicsView/GraphicsView_Object.cxx index fbe203129..75523c448 100644 --- a/src/GraphicsView/GraphicsView_Object.cxx +++ b/src/GraphicsView/GraphicsView_Object.cxx @@ -22,6 +22,8 @@ #include "GraphicsView_Object.h" +#include "GraphicsView_Scene.h" + //======================================================================= // Name : GraphicsView_Object // Purpose : Constructor @@ -130,5 +132,7 @@ void GraphicsView_Object::move( double theDX, double theDY, bool theIsAtOnce ) //================================================================ bool GraphicsView_Object::finishMove() { - return false; + if( GraphicsView_Scene* aScene = dynamic_cast( scene() ) ) + aScene->processRectChanged(); + return true; } diff --git a/src/GraphicsView/GraphicsView_Scene.cxx b/src/GraphicsView/GraphicsView_Scene.cxx index 78b206e05..79fb11c29 100644 --- a/src/GraphicsView/GraphicsView_Scene.cxx +++ b/src/GraphicsView/GraphicsView_Scene.cxx @@ -22,8 +22,11 @@ #include "GraphicsView_Scene.h" +#include #include +//#define VIEWER_DEBUG + //======================================================================= // Name : GraphicsView_Scene // Purpose : Constructor @@ -31,6 +34,15 @@ GraphicsView_Scene::GraphicsView_Scene( QObject* theParent ) : QGraphicsScene( theParent ) { +#ifdef VIEWER_DEBUG + mySceneRectItem = new QGraphicsRectItem(); + mySceneRectItem->setPen( QPen( Qt::red, 0.0 ) ); + + addItem( mySceneRectItem ); + + connect( this, SIGNAL( sceneRectChanged( const QRectF& ) ), + this, SLOT( onSceneRectChanged( const QRectF& ) ) ); +#endif } //======================================================================= @@ -41,6 +53,26 @@ GraphicsView_Scene::~GraphicsView_Scene() { } +//================================================================ +// Function : processRectChanged +// Purpose : +//================================================================ +void GraphicsView_Scene::processRectChanged() +{ + emit gsBoundingRectChanged(); +} + +//================================================================ +// Function : onSceneRectChanged +// Purpose : +//================================================================ +void GraphicsView_Scene::onSceneRectChanged( const QRectF& theRect ) +{ +#ifdef VIEWER_DEBUG + mySceneRectItem->setRect( theRect ); +#endif +} + //================================================================ // Function : mousePressEvent // Purpose : diff --git a/src/GraphicsView/GraphicsView_Scene.h b/src/GraphicsView/GraphicsView_Scene.h index e67cd8bfc..94ac92f9f 100644 --- a/src/GraphicsView/GraphicsView_Scene.h +++ b/src/GraphicsView/GraphicsView_Scene.h @@ -27,6 +27,8 @@ #include +class QGraphicsRectItem; + /* Class : GraphicsView_Scene Description : Scene of the graphics view @@ -39,6 +41,12 @@ public: GraphicsView_Scene( QObject* theParent = 0 ); ~GraphicsView_Scene(); +public: + void processRectChanged(); + +protected slots: + void onSceneRectChanged( const QRectF& theRect ); // for debug + protected: virtual void mousePressEvent( QGraphicsSceneMouseEvent* ); virtual void mouseMoveEvent( QGraphicsSceneMouseEvent* ); @@ -56,6 +64,11 @@ signals: void gsMouseEvent( QGraphicsSceneMouseEvent* ); void gsWheelEvent( QGraphicsSceneWheelEvent* ); void gsContextMenuEvent( QGraphicsSceneContextMenuEvent* ); + + void gsBoundingRectChanged(); + +private: + QGraphicsRectItem* mySceneRectItem; // for debug }; #endif diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 71f3ac13c..7a556ca4c 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -40,6 +40,8 @@ #include +#define VIEW_GAP 20 + int GraphicsView_ViewPort::nCounter = 0; QCursor* GraphicsView_ViewPort::defCursor = 0; QCursor* GraphicsView_ViewPort::handCursor = 0; @@ -166,6 +168,9 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) connect( myScene, SIGNAL( gsContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ), this, SLOT( onContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ) ); + connect( myScene, SIGNAL( gsBoundingRectChanged() ), + this, SLOT( onBoundingRectChanged() ) ); + initialize(); } @@ -214,6 +219,7 @@ void GraphicsView_ViewPort::cleanup() void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem ) { myScene->addItem( theItem ); + onBoundingRectChanged(); } //================================================================ @@ -229,6 +235,7 @@ void GraphicsView_ViewPort::removeItem( QGraphicsItem* theItem ) mySelectedObjects.removeAll( anObject ); } myScene->removeItem( theItem ); + onBoundingRectChanged(); } //================================================================ @@ -246,19 +253,11 @@ GraphicsView_ObjectList GraphicsView_ViewPort::getObjects() const } //================================================================ -// Function : dumpView +// Function : objectsBoundingRect // Purpose : //================================================================ -QImage GraphicsView_ViewPort::dumpView( bool theWholeScene ) +QRectF GraphicsView_ViewPort::objectsBoundingRect() const { - if( !theWholeScene ) // just grab the view contents - { - QPixmap aPixmap = QPixmap::grabWindow( viewport()->winId() ); - return aPixmap.toImage(); - } - - // get a bounding rect of all presented objects - // (itemsBoundingRect() method is unsuitable) QRectF aRect; QListIterator anIter( items() ); while( anIter.hasNext() ) @@ -275,7 +274,24 @@ QImage GraphicsView_ViewPort::dumpView( bool theWholeScene ) } } } + return aRect; +} + +//================================================================ +// Function : dumpView +// Purpose : +//================================================================ +QImage GraphicsView_ViewPort::dumpView( bool theWholeScene ) +{ + if( !theWholeScene ) // just grab the view contents + { + QPixmap aPixmap = QPixmap::grabWindow( viewport()->winId() ); + return aPixmap.toImage(); + } + // get a bounding rect of all presented objects + // (itemsBoundingRect() method is unsuitable) + QRectF aRect = objectsBoundingRect(); if( aRect.isNull() ) return QImage(); @@ -434,7 +450,7 @@ void GraphicsView_ViewPort::updateForeground() myForegroundItem->setVisible( true ); - myScene->setSceneRect( aRect.adjusted( -20, -20, 20, 20 ) ); + myScene->setSceneRect( aRect.adjusted( -VIEW_GAP, -VIEW_GAP, VIEW_GAP, VIEW_GAP ) ); } else { @@ -566,7 +582,7 @@ void GraphicsView_ViewPort::fitAll( bool theKeepScale ) if( theKeepScale ) myCurrentTransform = transform(); - fitInView( sceneRect(), Qt::KeepAspectRatio ); + fitInView( myScene->sceneRect(), Qt::KeepAspectRatio ); myIsTransforming = false; } @@ -1083,6 +1099,16 @@ void GraphicsView_ViewPort::finishPulling() setCursor( *getDefaultCursor() ); } +//================================================================ +// Function : onBoundingRectChanged +// Purpose : +//================================================================ +void GraphicsView_ViewPort::onBoundingRectChanged() +{ + QRectF aRect = objectsBoundingRect(); + myScene->setSceneRect( aRect.adjusted( -VIEW_GAP, -VIEW_GAP, VIEW_GAP, VIEW_GAP ) ); +} + //================================================================ // Function : onMouseEvent // Purpose : diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index 8cba516b0..cd40efe77 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -63,6 +63,8 @@ public: GraphicsView_ObjectList getObjects() const; + QRectF objectsBoundingRect() const; + QImage dumpView( bool theWholeScene = false ); public: @@ -150,6 +152,9 @@ public: static QCursor* getPanglCursor() { return panglCursor; } static QCursor* getZoomCursor() { return zoomCursor; } +public slots: + void onBoundingRectChanged(); + protected slots: void onMouseEvent( QGraphicsSceneMouseEvent* ); void onWheelEvent( QGraphicsSceneWheelEvent* ); diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index a7da60b45..83d867ced 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -504,5 +504,8 @@ void GraphicsView_Viewer::startOperations( QGraphicsSceneWheelEvent* e ) anIsScaleChanged = anObject->updateScale( anIsScaleUp, anIsCtrl ) || anIsScaleChanged; if( anIsScaleChanged ) + { emit wheelScaleChanged(); + aViewPort->onBoundingRectChanged(); + } } -- 2.39.2