2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vmaingraphicsview.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-08-13 18:48:36 +02:00
|
|
|
#include "vmaingraphicsview.h"
|
|
|
|
|
2013-11-21 13:05:26 +01:00
|
|
|
#include <QTimeLine>
|
|
|
|
#include <QWheelEvent>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QScrollBar>
|
2014-09-03 18:09:59 +02:00
|
|
|
#include "../visualization/vsimplecurve.h"
|
2013-11-21 13:05:26 +01:00
|
|
|
|
2014-09-03 18:09:59 +02:00
|
|
|
#include <QGraphicsItem>
|
2014-08-19 14:46:52 +02:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <qmath.h>
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-19 14:46:52 +02:00
|
|
|
GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view)
|
2014-09-11 16:15:49 +02:00
|
|
|
: QObject(view), _view(view), _modifiers(Qt::ControlModifier), _zoom_factor_base(1.0015),
|
2014-10-04 22:11:30 +02:00
|
|
|
target_scene_pos(QPointF()), target_viewport_pos(QPointF()), anim(nullptr), _numScheduledScalings(0)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-08-19 14:46:52 +02:00
|
|
|
_view->viewport()->installEventFilter(this);
|
|
|
|
_view->setMouseTracking(true);
|
2014-10-04 21:12:46 +02:00
|
|
|
anim = new QTimeLine(300, this);
|
|
|
|
anim->setUpdateInterval(20);
|
|
|
|
connect(anim, &QTimeLine::valueChanged, this, &GraphicsViewZoom::scrollingTime, Qt::UniqueConnection);
|
|
|
|
connect(anim, &QTimeLine::finished, this, &GraphicsViewZoom::animFinished, Qt::UniqueConnection);
|
2013-08-13 18:48:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-19 14:46:52 +02:00
|
|
|
void GraphicsViewZoom::gentle_zoom(double factor)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-08-19 14:46:52 +02:00
|
|
|
_view->scale(factor, factor);
|
|
|
|
_view->centerOn(target_scene_pos);
|
|
|
|
QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
|
|
|
|
_view->viewport()->height() / 2.0);
|
|
|
|
QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
|
|
|
|
_view->centerOn(_view->mapToScene(viewport_center.toPoint()));
|
|
|
|
emit zoomed();
|
2013-08-13 18:48:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-08-19 14:46:52 +02:00
|
|
|
void GraphicsViewZoom::set_modifiers(Qt::KeyboardModifiers modifiers)
|
2014-01-14 23:08:34 +01:00
|
|
|
{
|
2014-08-19 14:46:52 +02:00
|
|
|
_modifiers = modifiers;
|
|
|
|
}
|
2014-01-14 23:08:34 +01:00
|
|
|
|
2014-08-19 14:46:52 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-08-19 14:46:52 +02:00
|
|
|
void GraphicsViewZoom::set_zoom_factor_base(double value)
|
|
|
|
{
|
|
|
|
_zoom_factor_base = value;
|
2014-01-14 23:08:34 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-19 14:46:52 +02:00
|
|
|
void GraphicsViewZoom::scrollingTime(qreal x)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2013-10-14 10:40:01 +02:00
|
|
|
Q_UNUSED(x);
|
2014-01-05 21:33:02 +01:00
|
|
|
qreal factor = 1.0;
|
2014-01-14 23:08:34 +01:00
|
|
|
if (_numScheduledScalings < 0)
|
|
|
|
{
|
2014-10-25 16:23:55 +02:00
|
|
|
factor = factor*13.8;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-25 16:23:55 +02:00
|
|
|
factor = factor*-13.8;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
2014-08-19 14:46:52 +02:00
|
|
|
_view->verticalScrollBar()->setValue(qRound(_view->verticalScrollBar()->value() + factor));
|
2013-08-13 18:48:36 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-19 14:46:52 +02:00
|
|
|
void GraphicsViewZoom::animFinished()
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
if (_numScheduledScalings > 0)
|
|
|
|
{
|
2013-10-14 10:40:01 +02:00
|
|
|
_numScheduledScalings--;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-14 10:40:01 +02:00
|
|
|
_numScheduledScalings++;
|
|
|
|
}
|
2014-10-04 21:12:46 +02:00
|
|
|
anim->stop();
|
2014-10-25 17:30:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In moust cases cursor position on view doesn't change, but for scene after scrolling position will be different.
|
|
|
|
* We are goint to check changes and save new value.
|
|
|
|
* If don't do that we will zoom using old value cursor position on scene. It is not what we expect.
|
|
|
|
* Almoust the same we do in method GraphicsViewZoom::eventFilter.
|
|
|
|
*/
|
|
|
|
QPoint pos = _view->mapFromGlobal(QCursor::pos());
|
|
|
|
QPointF delta = target_scene_pos - _view->mapToScene(pos);
|
|
|
|
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
|
|
|
|
{
|
|
|
|
target_viewport_pos = pos;
|
|
|
|
target_scene_pos = _view->mapToScene(pos);
|
|
|
|
}
|
2013-08-13 18:48:36 +02:00
|
|
|
}
|
2013-10-14 11:31:56 +02:00
|
|
|
|
2014-08-19 14:46:52 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseMove)
|
|
|
|
{
|
2014-10-25 17:30:00 +02:00
|
|
|
/*
|
|
|
|
* Here we are saving cursor position on view and scene.
|
|
|
|
* This data need for gentle_zoom().
|
|
|
|
* Almoust the same we do in method GraphicsViewZoom::animFinished.
|
|
|
|
*/
|
2014-08-19 14:46:52 +02:00
|
|
|
QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
|
|
|
|
QPointF delta = target_viewport_pos - mouse_event->pos();
|
|
|
|
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
|
|
|
|
{
|
|
|
|
target_viewport_pos = mouse_event->pos();
|
|
|
|
target_scene_pos = _view->mapToScene(mouse_event->pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::Wheel)
|
|
|
|
{
|
|
|
|
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
|
|
|
|
if (QApplication::keyboardModifiers() == _modifiers)
|
|
|
|
{
|
|
|
|
if (wheel_event->orientation() == Qt::Vertical)
|
|
|
|
{
|
|
|
|
double angle = wheel_event->angleDelta().y();
|
|
|
|
double factor = qPow(_zoom_factor_base, angle);
|
|
|
|
gentle_zoom(factor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int numSteps = wheel_event->delta() / 8 / 15; // see QWheelEvent documentation
|
|
|
|
|
|
|
|
_numScheduledScalings += numSteps;
|
|
|
|
if (_numScheduledScalings * numSteps < 0)
|
|
|
|
{ // if user moved the wheel in another direction, we reset
|
|
|
|
_numScheduledScalings = numSteps; // previously scheduled scalings
|
|
|
|
}
|
|
|
|
|
2014-10-17 16:51:34 +02:00
|
|
|
if (anim->state() != QTimeLine::Running)
|
|
|
|
{
|
|
|
|
anim->start();
|
|
|
|
}
|
2014-08-19 14:46:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_UNUSED(object)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VMainGraphicsView constructor.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
|
|
|
VMainGraphicsView::VMainGraphicsView(QWidget *parent)
|
2014-08-30 21:58:31 +02:00
|
|
|
:QGraphicsView(parent), zoom(nullptr), showToolOptions(true)
|
2014-08-19 14:46:52 +02:00
|
|
|
{
|
|
|
|
zoom = new GraphicsViewZoom(this);
|
|
|
|
this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
|
|
|
|
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
|
|
|
this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
|
|
|
|
this->setInteractive(true);
|
|
|
|
}
|
|
|
|
|
2014-06-02 20:42:46 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ZoomIn()
|
|
|
|
{
|
|
|
|
scale(1.1, 1.1);
|
2015-06-15 09:40:23 +02:00
|
|
|
VMainGraphicsView::NewSceneRect(this->scene(), this);
|
2014-06-02 20:42:46 +02:00
|
|
|
emit NewFactor(1.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ZoomOut()
|
|
|
|
{
|
|
|
|
scale(1.0/1.1, 1.0/1.1);
|
2015-06-15 09:40:23 +02:00
|
|
|
VMainGraphicsView::NewSceneRect(this->scene(), this);
|
2014-06-02 20:42:46 +02:00
|
|
|
emit NewFactor(1.0/1.1);
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-07-11 15:10:54 +02:00
|
|
|
void VMainGraphicsView::ZoomOriginal()
|
|
|
|
{
|
|
|
|
QTransform trans = this->transform();
|
|
|
|
trans.setMatrix(1.0, trans.m12(), trans.m13(), trans.m21(), 1.0, trans.m23(), trans.m31(), trans.m32(),
|
|
|
|
trans.m33());
|
|
|
|
this->setTransform(trans);
|
|
|
|
emit NewFactor(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ZoomFitBest()
|
|
|
|
{
|
|
|
|
QRectF rect = this->scene()->itemsBoundingRect();
|
|
|
|
|
|
|
|
if (rect.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-12 18:25:25 +02:00
|
|
|
this->fitInView(rect, Qt::KeepAspectRatio);
|
2014-07-13 14:49:00 +02:00
|
|
|
emit NewFactor(this->transform().m11());
|
2014-07-11 15:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief mousePressEvent handle mouse press events.
|
|
|
|
* @param mousePress mouse press event.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VMainGraphicsView::mousePressEvent(QMouseEvent *mousePress)
|
|
|
|
{
|
|
|
|
if (mousePress->button() & Qt::LeftButton)
|
|
|
|
{
|
|
|
|
switch (QGuiApplication::keyboardModifiers())
|
|
|
|
{
|
|
|
|
case Qt::ControlModifier:
|
|
|
|
QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag);
|
|
|
|
break;
|
2014-08-26 19:23:08 +02:00
|
|
|
case Qt::NoModifier:
|
2014-08-30 21:58:31 +02:00
|
|
|
if (showToolOptions)
|
|
|
|
{
|
2014-09-03 18:09:59 +02:00
|
|
|
QList<QGraphicsItem *> list = items(mousePress->pos());
|
|
|
|
if (list.size() == 0)
|
|
|
|
{
|
|
|
|
emit itemClicked(nullptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < list.size(); ++i)
|
|
|
|
{
|
2015-06-15 09:40:23 +02:00
|
|
|
if (this->scene()->items().contains(list.at(i)))
|
2014-09-03 18:09:59 +02:00
|
|
|
{
|
2015-05-30 12:10:15 +02:00
|
|
|
if (list.at(i)->type() <= VSimpleCurve::Type &&
|
|
|
|
list.at(i)->type() > QGraphicsItem::UserType)
|
|
|
|
{
|
|
|
|
emit itemClicked(list.at(i));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit itemClicked(nullptr);
|
|
|
|
}
|
2014-09-03 18:09:59 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-30 21:58:31 +02:00
|
|
|
}
|
2014-08-26 19:23:08 +02:00
|
|
|
break;
|
2013-11-04 21:35:15 +01:00
|
|
|
default:
|
|
|
|
break;
|
2013-10-14 11:31:56 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-23 10:56:32 +02:00
|
|
|
QGraphicsView::mousePressEvent(mousePress);
|
2013-10-14 11:31:56 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief mouseReleaseEvent handle mouse release events.
|
|
|
|
* @param event mouse release event.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VMainGraphicsView::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
2013-10-14 11:31:56 +02:00
|
|
|
QGraphicsView::mouseReleaseEvent ( event );
|
|
|
|
QGraphicsView::setDragMode( QGraphicsView::RubberBandDrag );
|
2014-07-23 10:56:32 +02:00
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
emit MouseRelease();
|
|
|
|
}
|
2013-10-14 11:31:56 +02:00
|
|
|
}
|
2014-08-30 21:58:31 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::setShowToolOptions(bool value)
|
|
|
|
{
|
|
|
|
showToolOptions = value;
|
|
|
|
}
|
2015-06-15 09:40:23 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief NewSceneRect calculate scene rect what contains all items and doesn't less that size of scene view.
|
|
|
|
* @param sc scene.
|
|
|
|
* @param view view.
|
|
|
|
*/
|
|
|
|
void VMainGraphicsView::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view)
|
|
|
|
{
|
|
|
|
SCASSERT(sc != nullptr);
|
|
|
|
SCASSERT(view != nullptr);
|
|
|
|
|
|
|
|
const QRectF rect = sc->itemsBoundingRect();
|
|
|
|
const QRect rec0 = QRect(0, 0, view->rect().width()-2, view->rect().height()-2);
|
|
|
|
const QTransform t = view->transform();
|
|
|
|
|
|
|
|
QRectF rec1;
|
|
|
|
if (t.m11() < 1)
|
|
|
|
{
|
|
|
|
const qreal width = rec0.width()/t.m11();
|
|
|
|
const qreal height = rec0.height()/t.m22();
|
|
|
|
rec1 = QRect(0, 0, static_cast<qint32>(width), static_cast<qint32>(height));
|
|
|
|
|
|
|
|
rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y());
|
|
|
|
const QPolygonF polygone = view->mapToScene(rec1.toRect());
|
|
|
|
rec1 = polygone.boundingRect();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rec1 = rec0;
|
|
|
|
}
|
|
|
|
rec1 = rec1.united(rect.toRect());
|
|
|
|
sc->setSceneRect(rec1);
|
|
|
|
}
|