#include <QGraphicsItem>
#include <QRubberBand>
#include <QMouseEvent>
-#include <iostream>
+#include <QCursor>
+
+#include "SUIT_ResourceMgr.h"
+#include "SUIT_Session.h"
//=======================================================================
// Name : GraphicsView_ViewPort
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
+
+ setMouseTracking( true );
+ setFocusPolicy( Qt::StrongFocus );
myFitAllGap = 40;
myRectBandStart = QPoint();
myRectBandEnd = QPoint();
fittingArea = false;
+
+ zooming = false;
+ previousPos = QPoint();
+
+ SUIT_ResourceMgr* rmgr = SUIT_Session::session()->resourceMgr();
+ zoomCursor = new QCursor( rmgr->loadPixmap( "GraphicsView", tr( "ICON_GV_CURSOR_ZOOM" ) ) );
}
-#include<iostream>
+
//=======================================================================
// Name : GraphicsView_ViewPort
// Purpose : Destructor
//=======================================================================
GraphicsView_ViewPort::~GraphicsView_ViewPort()
{
+ delete zoomCursor;
+ zoomCursor = nullptr;
}
void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem )
fitInView(theRect, Qt::KeepAspectRatio);
}
-void GraphicsView_ViewPort::startSelectByRect( int x, int y )
+void GraphicsView_ViewPort::startDrawingRect( int x, int y )
{
myRectBandStart = QPoint(x,y);
}
}
-void GraphicsView_ViewPort::drawSelectByRect( int x, int y )
+void GraphicsView_ViewPort::drawingRect( int x, int y )
{
myRectBandEnd = QPoint(x,y);
-
- if (myRectBand)
- {
- myRectBand->setGeometry(QRect(myRectBandStart, myRectBandEnd).normalized());
- }
+ myRectBand->setGeometry(QRect(myRectBandStart, myRectBandEnd).normalized());
}
-void GraphicsView_ViewPort::finishSelectByRect()
+void GraphicsView_ViewPort::finishDrawingRect()
{
myRectBand->hide();
fitInView(selectionRect, Qt::KeepAspectRatio);
else
{
- QList<QGraphicsItem *> selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape);
- for (QGraphicsItem *item : selectedItems)
- item->setSelected(true);
- }
-
- fittingArea = false;
+ QList<QGraphicsItem *> selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape);
+ for (QGraphicsItem *item : selectedItems)
+ item->setSelected(true);
+ }
+
myRectBandStart = QPoint();
myRectBandEnd = QPoint();
delete myRectBand;
}
void GraphicsView_ViewPort::mousePressEvent(QMouseEvent *event)
-{
- if (items(event->pos()).count()==0)
+{
+ if (!zooming && items(event->pos()).count()==0)
{
- startSelectByRect(event->pos().x(), event->pos().y());
+ startDrawingRect(event->pos().x(), event->pos().y());
}
QGraphicsView::mousePressEvent(event);
void GraphicsView_ViewPort::mouseMoveEvent(QMouseEvent *event)
-{
- drawSelectByRect(event->pos().x(), event->pos().y());
+{
+ if (zooming && (event->buttons() & Qt::LeftButton))
+ {
+ QPoint currentPos = event->pos();
+ if (!previousPos.isNull())
+ {
+ int deltaX = currentPos.x() - previousPos.x();
+ double scaleFactor = deltaX>0 ? 1.1 : 0.9;
+ scale(scaleFactor, scaleFactor);
+ }
+ previousPos = currentPos;
+ }
+ else if (myRectBand)
+ drawingRect(event->pos().x(), event->pos().y());
+
QGraphicsView::mouseMoveEvent(event);
emit vpMouseEvent(event);
}
void GraphicsView_ViewPort::mouseReleaseEvent(QMouseEvent *event)
{
if (myRectBand)
- finishSelectByRect();
+ finishDrawingRect();
+ clearActions();
QGraphicsView::mouseReleaseEvent(event);
emit vpMouseEvent(event);
}
{
QGraphicsView::resizeEvent(event);
}
+
+void GraphicsView_ViewPort::wheelEvent(QWheelEvent *event)
+{
+ int delta = event->angleDelta().y();
+ double scaleFactor = delta>0 ? 1.1 : 0.9;
+ scale(scaleFactor, scaleFactor);
+}
+
+void GraphicsView_ViewPort::keyPressEvent(QKeyEvent *event)
+{
+ if (event->key() == Qt::Key_Escape)
+ clearActions();
+
+ QGraphicsView::keyPressEvent(event);
+}
+
+void GraphicsView_ViewPort::clearActions()
+{
+ setCursor(Qt::ArrowCursor);
+ fittingArea = false;
+ zooming = false;
+}
+
+void GraphicsView_ViewPort::activateZoomAction()
+{
+ zooming = true;
+ setCursor(*zoomCursor);
+}
+
+void GraphicsView_ViewPort::activateFitAreaAction()
+{
+ fittingArea = true;
+ setCursor(Qt::PointingHandCursor);
+}
void fitRect(const QRectF& theRect);
// rectangle selection
- void startSelectByRect( int x, int y );
- void drawSelectByRect( int x, int y );
- void finishSelectByRect();
+ void startDrawingRect( int x, int y );
+ void drawingRect( int x, int y );
+ void finishDrawingRect();
QRect selectionRect();
- void setFitArea(bool isFitting) { fittingArea = isFitting; }
+
+ void activateZoomAction();
+ void activateFitAreaAction();
signals:
void vpMouseEvent(QMouseEvent*);
virtual void mouseMoveEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void resizeEvent(QResizeEvent *event);
+ virtual void wheelEvent(QWheelEvent *event);
+ virtual void keyPressEvent(QKeyEvent *event);
+
+ void clearActions();
private:
QPoint myRectBandEnd;
bool fittingArea;
+ bool zooming;
+ QPoint previousPos;
+ QCursor* zoomCursor;
};
#endif