Fixed scroll hand drag mode.

--HG--
branch : release
This commit is contained in:
Roman Telezhynskyi 2017-04-14 08:22:28 +03:00
parent 78bdd5a8af
commit 6d271423e0
2 changed files with 41 additions and 15 deletions

View File

@ -343,9 +343,12 @@ bool GraphicsViewZoom::StartHorizontalScrollings(QWheelEvent *wheel_event)
* @param parent parent object. * @param parent parent object.
*/ */
VMainGraphicsView::VMainGraphicsView(QWidget *parent) VMainGraphicsView::VMainGraphicsView(QWidget *parent)
:QGraphicsView(parent), zoom(nullptr), showToolOptions(true), isAllowRubberBand(true) : QGraphicsView(parent),
zoom(new GraphicsViewZoom(this)),
showToolOptions(true),
isAllowRubberBand(true),
m_ptStartPos()
{ {
zoom = new GraphicsViewZoom(this);
this->setResizeAnchor(QGraphicsView::AnchorUnderMouse); this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse); this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
@ -423,9 +426,9 @@ void VMainGraphicsView::ZoomFitBest()
* @brief mousePressEvent handle mouse press events. * @brief mousePressEvent handle mouse press events.
* @param mousePress mouse press event. * @param mousePress mouse press event.
*/ */
void VMainGraphicsView::mousePressEvent(QMouseEvent *mousePress) void VMainGraphicsView::mousePressEvent(QMouseEvent *event)
{ {
switch (mousePress->button()) switch (event->button())
{ {
case Qt::LeftButton: case Qt::LeftButton:
{ {
@ -435,7 +438,7 @@ void VMainGraphicsView::mousePressEvent(QMouseEvent *mousePress)
} }
if (showToolOptions) if (showToolOptions)
{ {
QList<QGraphicsItem *> list = items(mousePress->pos()); QList<QGraphicsItem *> list = items(event->pos());
if (list.size() == 0) if (list.size() == 0)
{ {
emit itemClicked(nullptr); emit itemClicked(nullptr);
@ -462,17 +465,38 @@ void VMainGraphicsView::mousePressEvent(QMouseEvent *mousePress)
} }
case Qt::MiddleButton: case Qt::MiddleButton:
{ {
const QList<QGraphicsItem *> list = items(event->pos());
if (list.size() == 0)
{// Only when the user clicks on the scene background
m_ptStartPos = event->pos();
QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag); QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag);
// create a new mouse event that simulates a click of the left button instead of the middle button event->accept();
QMouseEvent mouseEvent (mousePress->type(), mousePress->pos(), Qt::LeftButton, Qt::LeftButton, viewport()->setCursor(Qt::ClosedHandCursor);
mousePress->modifiers()); }
QGraphicsView::mousePressEvent(&mouseEvent); break;
return;
} }
default: default:
break; break;
} }
QGraphicsView::mousePressEvent(mousePress); QGraphicsView::mousePressEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void VMainGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (dragMode() == QGraphicsView::ScrollHandDrag)
{
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
const QPoint delta = event->pos() - m_ptStartPos;
hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() - delta.y());
m_ptStartPos = event->pos();
}
else
{
QGraphicsView::mouseMoveEvent(event);
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -143,13 +143,15 @@ public slots:
void ZoomOriginal(); void ZoomOriginal();
void ZoomFitBest(); void ZoomFitBest();
protected: protected:
virtual void mousePressEvent(QMouseEvent *mousePress) Q_DECL_OVERRIDE; virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(VMainGraphicsView) Q_DISABLE_COPY(VMainGraphicsView)
GraphicsViewZoom* zoom; GraphicsViewZoom* zoom;
bool showToolOptions; bool showToolOptions;
bool isAllowRubberBand; bool isAllowRubberBand;
QPoint m_ptStartPos;
}; };
#endif // VMAINGRAPHICSVIEW_H #endif // VMAINGRAPHICSVIEW_H