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& ) ) );
-
- setSceneRect( -2000, -2000, 4000, 4000 );
-
- QGraphicsLineItem* aHorLineItem = new QGraphicsLineItem( -2000, 0, 2000, 0 );
- aHorLineItem->setPen( QPen( Qt::red ) );
- addItem( aHorLineItem );
-
- QGraphicsLineItem* aVerLineItem = new QGraphicsLineItem( 0, -2000, 0, 2000 );
- aVerLineItem->setPen( QPen( Qt::red ) );
- addItem( aVerLineItem );
-#endif
}
//=======================================================================
{
}
-//================================================================
-// Function : processRectChanged
-// Purpose :
-//================================================================
-void GraphicsView_Scene::processRectChanged()
-{
- emit gsBoundingRectChanged();
-}
-
-//================================================================
-// Function : onSceneRectChanged
-// Purpose :
-//================================================================
-void GraphicsView_Scene::onSceneRectChanged( const QRectF& theRect )
-{
- (void)theRect; // unused in debug mode
-#ifdef VIEWER_DEBUG
- mySceneRectItem->setRect( theRect );
-#endif
-}
-
-//================================================================
-// Function : keyPressEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::keyPressEvent( QKeyEvent* e )
-{
- emit gsKeyEvent( e );
- QGraphicsScene::keyPressEvent( e );
-}
-
-//================================================================
-// Function : keyReleaseEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::keyReleaseEvent( QKeyEvent* e )
-{
- emit gsKeyEvent( e );
- QGraphicsScene::keyReleaseEvent( e );
-}
-
-//================================================================
-// Function : mousePressEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::mousePressEvent( QGraphicsSceneMouseEvent* e )
-{
-/*
- std::cout << "============================================================" << std::endl;
- std::cout << "QGraphicsScene::mousePressEvent --> nb selected : " << selectedItems().count() << std::endl;
-
- if (!selectedItems().isEmpty())
- {
- GraphicsView_Object* obj = (GraphicsView_Object*) selectedItems().front();
- std::cout << " Item : " << obj->getName().toStdString() << std::endl;
- }
-
-// emit gsMouseEvent( e );
-*/
-/*
- QList<QGraphicsItem *> items1 = items(e->scenePos());
- QList<QGraphicsItem*>::const_iterator it;
-
- for (it = items1.constBegin(); it != items1.constEnd(); ++it)
- {
- GraphicsView_Object* anItem = (GraphicsView_Object*)(*it);
- std::cout << "Item at pos : " << anItem->getName().toStdString() << std::endl;
- }
-*/
- QGraphicsScene::mousePressEvent( e );
-}
-
-//================================================================
-// Function : mouseMoveEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::mouseMoveEvent( QGraphicsSceneMouseEvent* e )
-{
- emit gsMouseEvent( e );
- QGraphicsScene::mouseMoveEvent( e );
-}
-
-//================================================================
-// Function : mouseReleaseEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::mouseReleaseEvent( QGraphicsSceneMouseEvent* e )
-{
-// emit gsMouseEvent( e );
-
- QGraphicsScene::mouseReleaseEvent( e );
- std::cout << "QGraphicsScene::mouseReleaseEvent --> nb selected : " << selectedItems().count() << std::endl;
-/*
- if (!selectedItems().isEmpty())
- {
- GraphicsView_Object* obj = (GraphicsView_Object*) selectedItems().front();
- std::cout << " Item : " << obj->getName().toStdString() << std::endl;
- }
-
-
- QList<QGraphicsItem *> items1 = items(e->scenePos());
- QList<QGraphicsItem*>::const_iterator it;
-
- for (it = selectedItems().constBegin(); it != selectedItems().constEnd(); ++it)
- {
- GraphicsView_Object* anItem = (GraphicsView_Object*)(*it);
- std::cout << "Item selected : " << anItem->getName().toStdString() << std::endl;
- }
-
-*/
-}
-
-//================================================================
-// Function : mouseDoubleClickEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::mouseDoubleClickEvent( QGraphicsSceneMouseEvent* e )
-{
- // emit gsMouseEvent( e );
- QGraphicsScene::mouseDoubleClickEvent( e );
-}
-
-//================================================================
-// Function : wheelEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::wheelEvent( QGraphicsSceneWheelEvent* e )
-{
- emit gsWheelEvent( e );
-
- // accept the event to prevent calling QAbstractScrollArea::wheelEvent()
- // from QGraphicsView::wheelEvent(), which will change values of scroll-bars
- e->accept();
-
- //QGraphicsScene::wheelEvent( e ); // don't uncomment
-}
-
-//================================================================
-// Function : contextMenuEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::contextMenuEvent( QGraphicsSceneContextMenuEvent* e )
-{
-// emit gsContextMenuEvent( e );
- QGraphicsScene::contextMenuEvent( e );
-}
-
-//================================================================
-// Function : dragEnterEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::dragEnterEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable
-{
- //QGraphicsScene::dragEnterEvent( e ); // don't uncomment
-}
-
-//================================================================
-// Function : dragLeaveEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::dragLeaveEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable
-{
- //QGraphicsScene::dragLeaveEvent( e ); // don't uncomment
-}
-
-//================================================================
-// Function : dragMoveEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::dragMoveEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable
-{
- //QGraphicsScene::dragMoveEvent( e ); // don't uncomment
-}
-
-//================================================================
-// Function : dropEvent
-// Purpose :
-//================================================================
-void GraphicsView_Scene::dropEvent( QGraphicsSceneDragDropEvent* /*e*/ ) //!< TODO: unused variable
-{
- //QGraphicsScene::dropEvent( e ); // don't uncomment
-}
#include "GraphicsView_ViewPort.h"
#include "GraphicsView_Scene.h"
+#include <QGraphicsItem>
+#include <QRubberBand>
+#include <QMouseEvent>
#include <iostream>
//=======================================================================
: QGraphicsView( theParent )
{
setScene(new GraphicsView_Scene(this));
+
+ setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
+ myFitAllGap = 40;
+
+ myRectBand = nullptr;
}
//=======================================================================
scene()->addItem(theItem);
}
+void GraphicsView_ViewPort::fitAll()
+{
+ QRectF aRect = scene()->sceneRect();
+ fitInView( aRect.adjusted(-myFitAllGap, -myFitAllGap, myFitAllGap, myFitAllGap), Qt::KeepAspectRatio);
+}
+
+void GraphicsView_ViewPort::fitSelect()
+{
+ if (scene()->selectedItems().isEmpty())
+ return;
+
+ QRectF selectionRect;
+ foreach (QGraphicsItem *item, scene()->selectedItems())
+ {
+ selectionRect = selectionRect.united(item->sceneBoundingRect());
+ }
+
+ if( !selectionRect.isNull() )
+ {
+// double aGap = qMax( selectionRect.width(), selectionRect.height() ) / 5;
+// selectionRect.adjust( -aGap, -aGap, aGap, aGap );
+ fitInView( selectionRect, Qt::KeepAspectRatio );
+ }
+}
+
+void GraphicsView_ViewPort::fitRect(const QRectF& theRect)
+{
+ fitInView(theRect, Qt::KeepAspectRatio);
+}
+
+void GraphicsView_ViewPort::startSelectByRect( int x, int y )
+{
+ myRectBandStart = QPoint(x,y);
+
+ if (!myRectBand)
+ {
+ myRectBand = new QRubberBand(QRubberBand::Rectangle, this);
+ myRectBand->setGeometry(QRect(myRectBandStart, QSize()));
+ myRectBand->show();
+ }
+/*
+ if( !myAreSelectionPointsInitialized )
+ {
+ myFirstSelectionPoint = QPoint( x, y );
+ myLastSelectionPoint = QPoint( x, y );
+ myAreSelectionPointsInitialized = true;
+ }
+
+ if( !myRectBand )
+ {
+ myRectBand = new QRubberBand( QRubberBand::Rectangle, this );
+ QPalette palette;
+ palette.setColor( myRectBand->foregroundRole(), Qt::white );
+ myRectBand->setPalette( palette );
+ }
+ myRectBand->hide();
+*/
+}
+
+void GraphicsView_ViewPort::drawSelectByRect( int x, int y )
+{
+ QPoint endPoint = QPoint(x,y);
+ if (myRectBand)
+ myRectBand->setGeometry(QRect(myRectBandStart, endPoint).normalized());
+}
+
+void GraphicsView_ViewPort::finishSelectByRect()
+{
+ myRectBand->hide();
+ QRectF selectionRect = mapToScene(myRectBand->geometry()).boundingRect();
+ QList<QGraphicsItem *> selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape);
+ for (QGraphicsItem *item : selectedItems)
+ item->setSelected(true);
+
+ delete myRectBand;
+ myRectBand = nullptr;
+
+}
+
+void GraphicsView_ViewPort::mousePressEvent(QMouseEvent *event)
+{
+ if (items(event->pos()).count()==0)
+ {
+ startSelectByRect(event->pos().x(), event->pos().y());
+ }
+
+ QGraphicsView::mousePressEvent(event);
+}
+
+
+void GraphicsView_ViewPort::mouseMoveEvent(QMouseEvent *event)
+{
+ drawSelectByRect(event->pos().x(), event->pos().y());
+ QGraphicsView::mouseMoveEvent(event);
+}
+
+void GraphicsView_ViewPort::mouseReleaseEvent(QMouseEvent *event)
+{
+ if (myRectBand)
+ finishSelectByRect();
+ QGraphicsView::mouseReleaseEvent(event);
+}
+
+