54 #include <QApplication>
58 #include <QGraphicsItem>
59 #include <QGraphicsScene>
62 #include <QMessageLogger>
63 #include <QMouseEvent>
64 #include <QGestureEvent>
70 #include <QWheelEvent>
73 #include <QGuiApplication>
75 #include <QAbstractScrollArea>
78 #include "../vmisc/logging.h"
79 #include "../vmisc/def.h"
80 #include "../vmisc/vmath.h"
81 #include "../vmisc/vsettings.h"
82 #include "../vmisc/vcommonsettings.h"
83 #include "../vmisc/vabstractapplication.h"
84 #include "../ifc/ifcdef.h"
89 Q_LOGGING_CATEGORY(vGraphicsViewZoom,
"vgraphicsviewzoom")
97 , m_modifiers(Qt::ControlModifier)
98 , m_zoomSpeedFactor(1.0015)
99 , targetScenePos(QPointF())
100 , targetViewPos(QPointF())
103 , verticalScrollAnim(new QTimeLine(300, this))
104 , m_numScheduledVerticalScrollings(0)
105 , horizontalScrollAnim(new QTimeLine(300, this))
106 , m_numScheduledHorizontalScrollings(0)
109 , horizontalOffset(0.0)
110 , verticalOffset(0.0)
112 , currentScaleFactor(0.0)
114 m_view->viewport()->installEventFilter(
this);
117 m_view->viewport()->grabGesture(Qt::PinchGesture);
118 m_view->viewport()->grabGesture(Qt::PanGesture);
119 m_view->setMouseTracking(
true);
121 initScrollAnimations();
134 const qreal m11 =
m_view->transform().m11();
138 m_view->scale(factor, factor);
148 m_view->viewport()->height() / 2.0);
150 m_view->centerOn(
m_view->mapToScene(viewCenter.toPoint()));
195 if (qAbs(scroll) < 1)
202 scroll = scroll * -1;
204 m_view->verticalScrollBar()->setValue(qRound(
m_view->verticalScrollBar()->value() + scroll));
217 if (qAbs(scroll) < 1)
224 scroll = scroll * -1;
226 m_view->horizontalScrollBar()->setValue(qRound(
m_view->horizontalScrollBar()->value() + scroll));
241 const QPoint pos =
m_view->mapFromGlobal(QCursor::pos());
243 if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
253 m_modifiers = (
qApp->Settings()->getZoomModKey()) ? Qt::ControlModifier : Qt::NoModifier;
256 if (event->type() == QEvent::MouseMove)
263 QMouseEvent* mouse_event =
static_cast<QMouseEvent*
>(event);
265 if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
272 else if (event->type() == QEvent::Wheel)
274 QWheelEvent* wheel_event =
static_cast<QWheelEvent*
>(event);
276 if (QApplication::keyboardModifiers() ==
m_modifiers)
278 if (wheel_event->angleDelta().y() != 0)
280 const double angle = wheel_event->angleDelta().y();
282 qCDebug(vGraphicsViewZoom,
"zoom Factor =%f \n", factor);
289 if (QApplication::keyboardModifiers() == Qt::ShiftModifier)
299 else if (event->type() == QEvent::Gesture)
301 return gestureEvent(
static_cast<QGestureEvent*
>(event));
304 return QObject::eventFilter(
object, event);
310 if (QGesture *
pinch = event->gesture(Qt::PinchGesture))
312 qCDebug(vGraphicsViewZoom,
"pinch gestureEvent");
316 if (QGesture *
pan = event->gesture(Qt::PanGesture))
318 qCDebug(vGraphicsViewZoom,
"pan gestureEvent");
329 switch (gesture->state())
331 case Qt::GestureStarted:
332 case Qt::GestureUpdated:
333 m_view->viewport()->setCursor(Qt::SizeAllCursor);
336 m_view->viewport()->setCursor(Qt::ArrowCursor);
339 QPointF delta = gesture->delta();
340 qCDebug(vGraphicsViewZoom) <<
"panTriggered():" << gesture;
348 QPinchGesture::ChangeFlags flags = gesture->changeFlags();
349 if (flags & QPinchGesture::ScaleFactorChanged)
364 const QPointF a = view->mapToScene(0, 0 );
365 const QPointF b = view->mapToScene(view->viewport()->width(), view->viewport()->height());
366 QRectF viewRect = QRectF( a, b );
369 QLineF topLeftRay(viewRect.center(), viewRect.topLeft());
370 topLeftRay.setLength(topLeftRay.length()*2);
372 QLineF bottomRightRay(viewRect.center(), viewRect.bottomRight());
373 bottomRightRay.setLength(bottomRightRay.length()*2);
375 viewRect = QRectF(topLeftRay.p2(), bottomRightRay.p2());
378 const QRectF sceneRect = sc->sceneRect();
381 const QRectF newRect = sceneRect.united(viewRect);
383 sc->setSceneRect(newRect);
391 const QPoint numPixels = wheel_event->pixelDelta();
392 const QPoint numDegrees = wheel_event->angleDelta() / 8;
395 if (not numPixels.isNull())
397 numSteps = numPixels.y();
399 else if (not numDegrees.isNull())
401 numSteps = numDegrees.y() / 15;
428 const QPoint numPixels = wheel_event->pixelDelta();
429 const QPoint numDegrees = wheel_event->angleDelta() / 8;
432 if (not numPixels.isNull())
434 numSteps = numPixels.y();
436 else if (not numDegrees.isNull())
438 numSteps = numDegrees.y() / 15;
460 Q_LOGGING_CATEGORY(vMainGraphicsView,
"vmaingraphicsview")
468 : QGraphicsView(parent)
469 , curMagnifier(new QCursor(QPixmap(":/cursor/magnifier_cursor.png"), 2, 2))
471 , showToolProperties(true)
473 , isallowRubberBand(true)
474 , isZoomToAreaActive(false)
475 , isRubberBandActive(false)
476 , isRubberBandColorSet(false)
477 , isZoomPanActive(false)
478 , isPanDragActive(false)
479 , rubberBand(
nullptr)
480 , rubberBandRect(
nullptr)
481 , startPoint(QPoint())
483 , m_ptStartPos(QPoint())
484 , cursorPos(QPoint())
488 this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
489 this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
490 this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
491 this->setInteractive(
true);
500 QTransform transform = this->transform();
501 transform.setMatrix(factor, transform.m12(), transform.m13(),
502 transform.m21(), factor, transform.m23(),
503 transform.m31(), transform.m32(), transform.m33());
504 this->setTransform(transform);
513 if (this->transform().m11() <=
MaxScale())
525 if (this->transform().m11() >=
MinScale())
527 scale(1.0/1.1, 1.0/1.1);
535 QTransform transform = this->transform();
536 transform.setMatrix(1.0, transform.m12(), transform.m13(),
537 transform.m21(), 1.0, transform.m23(),
538 transform.m31(), transform.m32(), transform.m33());
539 this->setTransform(transform);
562 this->fitInView(rect, Qt::KeepAspectRatio);
563 QTransform transform = this->transform();
565 qreal factor = transform.m11();
569 transform.setMatrix(factor, transform.m12(), transform.m13(),
570 transform.m21(), factor, transform.m23(),
571 transform.m31(), transform.m32(), transform.m33());
572 this->setTransform(transform);
590 qCDebug(vMainGraphicsView,
"View->Zoom to Selected Area Mode active\n");
596 qCDebug(vMainGraphicsView,
"View->Zoom to Selected Area Mode not active\n");
597 viewport()->setCursor(Qt::ArrowCursor);
607 qCDebug(vMainGraphicsView,
"View->Pan Zoom Mode active\n");
609 viewport()->setCursor(Qt::OpenHandCursor);
613 qCDebug(vMainGraphicsView,
"View->Pan Zoom Mode not active\n");
614 viewport()->setCursor(Qt::ArrowCursor);
621 palette.setColor(QPalette::Active, QPalette::Highlight, color);
622 band->setPalette(palette);
633 switch (event->button())
641 rubberBand =
new QRubberBand(QRubberBand::Rectangle,
this);
647 qCDebug(vMainGraphicsView,
"Select Area...\nstart point X=%d Y=%d\n",
startPoint.x(),
startPoint.y());
653 qCDebug(vMainGraphicsView,
"Start Pan\n");
656 if (list.size() == 0)
659 QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag);
661 viewport()->setCursor(Qt::ClosedHandCursor);
669 QGraphicsView::setDragMode(QGraphicsView::RubberBandDrag);
674 if (list.size() == 0)
679 for (
int i = 0; i < list.size(); ++i)
681 if (this->scene()->items().contains(list.at(i)))
683 if (list.at(i)->type() > QGraphicsItem::UserType && list.at(i)->type() <=
VSimpleCurve::Type)
698 case Qt::MiddleButton:
701 if (list.size() == 0)
704 QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag);
706 viewport()->setCursor(Qt::ClosedHandCursor);
713 QGraphicsView::mousePressEvent(event);
719 if ( (dragMode() == QGraphicsView::ScrollHandDrag) ||
isPanDragActive )
721 QScrollBar *hBar = horizontalScrollBar();
722 QScrollBar *vBar = verticalScrollBar();
724 hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
725 vBar->setValue(vBar->value() - delta.y());
744 qCDebug(vMainGraphicsView,
"End point X=%d Y=%d",
endPoint.x(),
endPoint.y());
748 QGraphicsView::mouseMoveEvent(event);
759 QGraphicsView::mouseReleaseEvent ( event );
760 QGraphicsView::setDragMode( QGraphicsView::NoDrag );
764 viewport()->setCursor(Qt::OpenHandCursor);
765 qCDebug(vMainGraphicsView,
"Stop Pan\n");
771 qCDebug( vMainGraphicsView,
"End point X=%d Y=%d",
endPoint.x(),
endPoint.y() );
783 else if (event->button() == Qt::LeftButton)
792 if (event->button() == Qt::LeftButton &&
qApp->Settings()->isZoomDoubleClick())
796 window->zoomToSelected();
799 QGraphicsView::mouseDoubleClickEvent(event);
805 const QRect screenRect(QGuiApplication::primaryScreen()->availableGeometry());
806 const qreal screenSize = qMin(screenRect.width(), screenRect.height());
814 const QRect screenRect(QGuiApplication::primaryScreen()->availableGeometry());
815 const qreal screenSize = qMin(screenRect.width(), screenRect.height());
854 sc->setSceneRect(itemsRect.united(viewRect));
856 else if (not sc->sceneRect().contains(item->sceneBoundingRect()))
858 sc->setSceneRect(sc->sceneRect().united(item->sceneBoundingRect()));
867 return QRectF(view->mapToScene(0, 0), view->mapToScene(view->width(), view->height()));
875 QString horizontal_styleSheet = QString(
"QScrollBar {height: ");
876 horizontal_styleSheet += QString().number(
qApp->Settings()->getScrollBarWidth());
877 horizontal_styleSheet += QString(
"px;}");
879 QString vertical_styleSheet = QString(
"QScrollBar {width: ");
880 vertical_styleSheet += QString().number(
qApp->Settings()->getScrollBarWidth());
881 vertical_styleSheet += QString(
"px;}");
883 this->horizontalScrollBar()->setStyleSheet(horizontal_styleSheet);
884 this->verticalScrollBar()->setStyleSheet(vertical_styleSheet);
888 this->horizontalScrollBar()->setStyleSheet(
"QScrollBar {height: 0px;}");
889 this->verticalScrollBar()->setStyleSheet(
"QScrollBar {width: 0px;}");
892 this->horizontalScrollBar()->setSingleStep(1);
893 this->horizontalScrollBar()->update();
895 this->verticalScrollBar()->setSingleStep(1);
896 this->verticalScrollBar()->update();
QTimeLine * verticalScrollAnim
void panTriggered(QPanGesture *gesture)
void horizontalScrollingTime(qreal x)
Qt::KeyboardModifiers m_modifiers
bool startHorizontalScrollings(QWheelEvent *wheel_event)
qint32 m_numScheduledVerticalScrollings
_numScheduledVerticalScrollings keep number scheduled vertical scrollings.
virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
bool gestureEvent(QGestureEvent *event)
void setModifiers(Qt::KeyboardModifiers modifiers)
void pinchTriggered(QPinchGesture *gesture)
void setZoomSpeedFactor(qreal value)
void verticalScrollingTime(qreal x)
void fictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view)
bool startVerticalScrollings(QWheelEvent *wheel_event)
void gentleZoom(qreal factor)
QTimeLine * horizontalScrollAnim
void initScrollAnimations()
qint32 m_numScheduledHorizontalScrollings
_numScheduledHorizontalScrollings keep number scheduled horizontal scrollings.
The VMainGraphicsScene class main scene.
QRectF visibleItemsBoundingRect() const
void setOriginsVisible(bool visible)
void setCurrentTransform(const QTransform &transform)
setCurrentTransform set view transformation.
The VMainGraphicsView class main scene view.
void itemClicked(QGraphicsItem *item)
void signalZoomScaleChanged(qreal scale)
virtual void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE
mouseReleaseEvent handle mouse release events.
QSharedPointer< QCursor > curMagnifier
virtual void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE
static QRectF SceneVisibleArea(QGraphicsView *view)
virtual void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE
void zoomPanEnabled(bool value)
void zoomToRect(const QRectF &rect)
void mouseRelease()
mouseRelease help catch mouse release event.
void updateView(const QTransform &transform)
void setShowToolOptions(bool value)
void zoomByScale(qreal scale)
virtual void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE
mousePressEvent handle mouse press events.
static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item=nullptr)
NewSceneRect calculate scene rect what contains all items and doesn't less that size of scene view.
void zoomToAreaEnabled(bool value)
bool isRubberBandColorSet
void resetScrollAnimations()
void setRubberBandColor(QRubberBand *band, const QColor &color)
void allowRubberBand(bool value)