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
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2013-11-15 13:41:26 +01:00
|
|
|
** 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 <QApplication>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QCursor>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QFlags>
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QLineF>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPoint>
|
2013-11-21 13:05:26 +01:00
|
|
|
#include <QScrollBar>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QTimeLine>
|
|
|
|
#include <QTransform>
|
|
|
|
#include <QWheelEvent>
|
|
|
|
#include <QWidget>
|
2017-10-02 15:25:18 +02:00
|
|
|
#include <QThread>
|
2018-04-16 10:14:17 +02:00
|
|
|
#include <QGestureEvent>
|
2018-05-27 10:34:35 +02:00
|
|
|
#include <QScreen>
|
2019-04-05 14:42:22 +02:00
|
|
|
#include <QOpenGLWidget>
|
2018-03-10 15:39:37 +01:00
|
|
|
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-08-16 18:57:32 +02:00
|
|
|
#include "../vmisc/vmath.h"
|
2016-01-13 14:47:38 +01:00
|
|
|
#include "vmaingraphicsscene.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "vsimplecurve.h"
|
2018-06-19 12:08:54 +02:00
|
|
|
#include "vcontrolpointspline.h"
|
2018-03-10 15:39:37 +01:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
|
|
|
#include "../vmisc/vsettings.h"
|
2018-04-17 08:56:41 +02:00
|
|
|
#include "vabstractmainwindow.h"
|
2013-11-21 13:05:26 +01:00
|
|
|
|
2017-06-16 14:03:24 +02:00
|
|
|
const qreal maxSceneSize = ((20.0 * 1000.0) / 25.4) * PrintDPI; // 20 meters in pixels
|
2016-08-12 16:37:47 +02:00
|
|
|
|
2018-04-16 12:15:16 +02:00
|
|
|
namespace
|
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
qreal ScrollingSteps(QWheelEvent* wheel_event)
|
2018-04-16 12:15:16 +02:00
|
|
|
{
|
|
|
|
SCASSERT(wheel_event != nullptr)
|
|
|
|
|
|
|
|
const QPoint numPixels = wheel_event->pixelDelta();
|
|
|
|
const QPoint numDegrees = wheel_event->angleDelta() / 8;
|
2018-04-26 14:08:04 +02:00
|
|
|
qreal numSteps = 0;
|
2018-05-11 11:42:20 +02:00
|
|
|
VSettings *settings = qobject_cast<VSettings *>(qApp->Settings());
|
2018-04-16 12:15:16 +02:00
|
|
|
|
|
|
|
if (not numPixels.isNull())
|
|
|
|
{
|
2018-05-11 11:42:20 +02:00
|
|
|
const qreal mouseScale = settings->GetSensorMouseScale();
|
2018-04-26 14:08:04 +02:00
|
|
|
numSteps = (wheel_event->orientation() == Qt::Vertical ? numPixels.y() : numPixels.x()) / mouseScale;
|
2018-04-16 12:15:16 +02:00
|
|
|
}
|
|
|
|
else if (not numDegrees.isNull())
|
|
|
|
{
|
2018-05-11 11:42:20 +02:00
|
|
|
const qreal mouseScale = settings->GetWheelMouseScale();
|
2018-04-26 14:08:04 +02:00
|
|
|
numSteps = (wheel_event->orientation() == Qt::Vertical ? numDegrees.y() : numDegrees.x()) / 15. * mouseScale;
|
2018-04-16 12:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return numSteps;
|
|
|
|
}
|
2018-04-26 14:08:04 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal PrepareScrolling(qreal scheduledScrollings, QWheelEvent *wheel_event)
|
|
|
|
{
|
|
|
|
const qreal numSteps = ScrollingSteps(wheel_event);
|
|
|
|
|
|
|
|
if (qFuzzyIsNull(numSteps))
|
|
|
|
{
|
|
|
|
return scheduledScrollings;//Just ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::signbit(scheduledScrollings) != std::signbit(numSteps))
|
|
|
|
{ // if user moved the wheel in another direction, we reset previously scheduled scalings
|
|
|
|
scheduledScrollings = numSteps;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scheduledScrollings += numSteps;
|
|
|
|
}
|
|
|
|
|
2018-05-11 11:42:20 +02:00
|
|
|
scheduledScrollings *= qobject_cast<VSettings *>(qApp->Settings())->GetScrollingAcceleration();
|
2018-04-26 14:08:04 +02:00
|
|
|
|
|
|
|
return scheduledScrollings;
|
|
|
|
}
|
2018-06-19 12:08:54 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief PrioritizeItems helps prioritize some items over others.
|
|
|
|
*
|
|
|
|
* In some cases we want items like curve handle points to be selected over other items on scene.
|
|
|
|
* @param list list of scene items under a mouse pointer
|
|
|
|
* @return prioritized list where prioritized items goes first
|
|
|
|
*/
|
|
|
|
QList<QGraphicsItem *> PrioritizeItems(const QList<QGraphicsItem *> &list)
|
|
|
|
{
|
|
|
|
QList<QGraphicsItem *> prioritized;
|
|
|
|
QList<QGraphicsItem *> nonPrioritized;
|
|
|
|
for (auto item : list)
|
|
|
|
{
|
|
|
|
item && item->type() == VControlPointSpline::Type ? prioritized.append(item) : nonPrioritized.append(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return prioritized + nonPrioritized;
|
|
|
|
}
|
2018-04-16 12:15:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-19 14:46:52 +02:00
|
|
|
GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view)
|
2016-03-30 10:25:13 +02:00
|
|
|
: QObject(view),
|
|
|
|
_view(view),
|
|
|
|
_modifiers(Qt::ControlModifier),
|
|
|
|
_zoom_factor_base(1.0015),
|
2018-05-11 11:42:20 +02:00
|
|
|
target_scene_pos(),
|
|
|
|
target_viewport_pos(),
|
|
|
|
verticalScrollAnim(),
|
2016-03-30 10:25:13 +02:00
|
|
|
_numScheduledVerticalScrollings(0),
|
2018-05-11 11:42:20 +02:00
|
|
|
horizontalScrollAnim(),
|
2016-03-30 10:25:13 +02:00
|
|
|
_numScheduledHorizontalScrollings(0)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-08-19 14:46:52 +02:00
|
|
|
_view->viewport()->installEventFilter(this);
|
2018-04-16 10:14:17 +02:00
|
|
|
_view->viewport()->grabGesture(Qt::PinchGesture);
|
2014-08-19 14:46:52 +02:00
|
|
|
_view->setMouseTracking(true);
|
2016-03-30 10:25:13 +02:00
|
|
|
|
2018-05-11 11:42:20 +02:00
|
|
|
InitScrollingAnimation();
|
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
|
|
|
{
|
2016-08-12 16:37:47 +02:00
|
|
|
// We need to check current scale factor because in Windows we have an error when we zoom in or zoom out to much.
|
|
|
|
// See issue #532: Unexpected error occurs when zoom out image.
|
|
|
|
// factor > 1 for zoomIn and factor < 1 for zoomOut.
|
2017-06-16 14:03:24 +02:00
|
|
|
const qreal m11 = _view->transform().m11();
|
|
|
|
|
|
|
|
if ((factor > 1 && m11 <= VMainGraphicsView::MaxScale()) || (factor < 1 && m11 >= VMainGraphicsView::MinScale()))
|
2016-08-12 16:37:47 +02:00
|
|
|
{
|
|
|
|
_view->scale(factor, factor);
|
|
|
|
if (factor < 1)
|
|
|
|
{
|
|
|
|
// Because QGraphicsView centers the picture when it's smaller than the view. And QGraphicsView's scrolls
|
|
|
|
// boundaries don't allow to put any picture point at any viewport position we will provide fictive scene
|
|
|
|
// size. Temporary and bigger than view, scene size will help position an image under cursor.
|
|
|
|
FictiveSceneRect(_view->scene(), _view);
|
|
|
|
}
|
|
|
|
_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()));
|
|
|
|
// In the end we just set correct scene size
|
|
|
|
VMainGraphicsView::NewSceneRect(_view->scene(), _view);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-11 11:42:20 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void GraphicsViewZoom::InitScrollingAnimation()
|
|
|
|
{
|
|
|
|
VSettings *settings = qobject_cast<VSettings *>(qApp->Settings());
|
|
|
|
|
|
|
|
if (not verticalScrollAnim.isNull())
|
|
|
|
{
|
|
|
|
delete verticalScrollAnim;
|
|
|
|
}
|
|
|
|
|
|
|
|
verticalScrollAnim = new QTimeLine(settings->GetScrollingDuration(), this);
|
|
|
|
verticalScrollAnim->setUpdateInterval(settings->GetScrollingUpdateInterval());
|
|
|
|
|
2018-05-11 12:14:57 +02:00
|
|
|
connect(verticalScrollAnim.data(), &QTimeLine::valueChanged, this, &GraphicsViewZoom::VerticalScrollingTime);
|
|
|
|
connect(verticalScrollAnim.data(), &QTimeLine::finished, this, &GraphicsViewZoom::animFinished);
|
2018-05-11 11:42:20 +02:00
|
|
|
|
|
|
|
if (not horizontalScrollAnim.isNull())
|
|
|
|
{
|
|
|
|
delete horizontalScrollAnim;
|
|
|
|
}
|
|
|
|
|
|
|
|
horizontalScrollAnim = new QTimeLine(settings->GetScrollingDuration(), this);
|
|
|
|
horizontalScrollAnim->setUpdateInterval(settings->GetScrollingUpdateInterval());
|
|
|
|
|
2018-05-11 12:14:57 +02:00
|
|
|
connect(horizontalScrollAnim.data(), &QTimeLine::valueChanged, this, &GraphicsViewZoom::HorizontalScrollingTime);
|
|
|
|
connect(horizontalScrollAnim.data(), &QTimeLine::finished, this, &GraphicsViewZoom::animFinished);
|
2018-05-11 11:42:20 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-03-30 10:25:13 +02:00
|
|
|
void GraphicsViewZoom::VerticalScrollingTime(qreal x)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
const qreal scroll = _numScheduledVerticalScrollings * x;
|
|
|
|
_numScheduledVerticalScrollings -= scroll;
|
|
|
|
_view->verticalScrollBar()->setValue(qRound(_view->verticalScrollBar()->value() - scroll));
|
2013-08-13 18:48:36 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 10:25:13 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void GraphicsViewZoom::HorizontalScrollingTime(qreal x)
|
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
const qreal scroll = _numScheduledHorizontalScrollings * x;
|
|
|
|
_numScheduledHorizontalScrollings -= scroll;
|
|
|
|
_view->horizontalScrollBar()->setValue(qRound(_view->horizontalScrollBar()->value() - scroll));
|
2016-03-30 10:25:13 +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
|
|
|
{
|
2016-03-30 10:25:13 +02:00
|
|
|
_numScheduledVerticalScrollings = 0;
|
2018-04-26 14:08:04 +02:00
|
|
|
_numScheduledHorizontalScrollings = 0;
|
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.
|
|
|
|
*/
|
2015-12-02 19:39:25 +01:00
|
|
|
const QPoint pos = _view->mapFromGlobal(QCursor::pos());
|
|
|
|
const QPointF delta = target_scene_pos - _view->mapToScene(pos);
|
2014-10-25 17:30:00 +02:00
|
|
|
if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
|
|
|
|
{
|
2015-12-02 19:39:25 +01:00
|
|
|
target_viewport_pos = pos;
|
|
|
|
target_scene_pos = _view->mapToScene(pos);
|
2014-10-25 17:30:00 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-12-17 20:13:07 +01:00
|
|
|
if (event->type() == QEvent::MouseMove)
|
2014-08-19 14:46:52 +02:00
|
|
|
{
|
2015-12-17 20:13:07 +01: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.
|
|
|
|
*/
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
return false;
|
2014-08-19 14:46:52 +02:00
|
|
|
}
|
2015-12-17 20:13:07 +01:00
|
|
|
else if (event->type() == QEvent::Wheel)
|
2014-08-19 14:46:52 +02:00
|
|
|
{
|
2018-04-16 10:05:57 +02:00
|
|
|
if (QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event))
|
2015-12-17 20:13:07 +01:00
|
|
|
{
|
|
|
|
if (wheel_event->orientation() == Qt::Vertical)
|
|
|
|
{
|
2018-04-16 10:05:57 +02:00
|
|
|
if (QGuiApplication::keyboardModifiers() == _modifiers)
|
|
|
|
{
|
|
|
|
gentle_zoom(qPow(_zoom_factor_base, wheel_event->angleDelta().y()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
|
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
StartHorizontalScrollings(wheel_event);
|
|
|
|
return true;
|
2018-04-16 10:05:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
StartVerticalScrollings(wheel_event);
|
|
|
|
return true;
|
2018-04-16 10:05:57 +02:00
|
|
|
}
|
2015-12-17 20:13:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-16 10:05:57 +02:00
|
|
|
if (QGuiApplication::keyboardModifiers() == _modifiers)
|
|
|
|
{
|
|
|
|
return true; //ignore
|
|
|
|
}
|
|
|
|
|
2018-04-26 14:08:04 +02:00
|
|
|
StartHorizontalScrollings(wheel_event);
|
|
|
|
return true;
|
2015-12-17 20:13:07 +01:00
|
|
|
}
|
2014-10-17 16:51:34 +02:00
|
|
|
}
|
2014-08-19 14:46:52 +02:00
|
|
|
}
|
2018-04-16 10:14:17 +02:00
|
|
|
else if (event->type() == QEvent::Gesture)
|
|
|
|
{
|
|
|
|
return GestureEvent(static_cast<QGestureEvent*>(event));
|
|
|
|
}
|
2015-12-17 20:13:07 +01:00
|
|
|
|
|
|
|
return QObject::eventFilter(object, event);
|
2014-08-19 14:46:52 +02:00
|
|
|
}
|
|
|
|
|
2015-12-02 19:39:25 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void GraphicsViewZoom::FictiveSceneRect(QGraphicsScene *sc, QGraphicsView *view)
|
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(sc != nullptr)
|
|
|
|
SCASSERT(view != nullptr)
|
2015-12-02 19:39:25 +01:00
|
|
|
|
|
|
|
//Calculate view rect
|
|
|
|
//to receive the currently visible area, map the widgets bounds to the scene
|
|
|
|
const QPointF a = view->mapToScene(0, 0 );
|
|
|
|
const QPointF b = view->mapToScene(view->viewport()->width(), view->viewport()->height());
|
|
|
|
QRectF viewRect = QRectF( a, b );
|
|
|
|
|
2015-12-04 09:04:43 +01:00
|
|
|
//Scale view
|
|
|
|
QLineF topLeftRay(viewRect.center(), viewRect.topLeft());
|
|
|
|
topLeftRay.setLength(topLeftRay.length()*2);
|
2015-12-02 19:39:25 +01:00
|
|
|
|
2015-12-04 09:04:43 +01:00
|
|
|
QLineF bottomRightRay(viewRect.center(), viewRect.bottomRight());
|
|
|
|
bottomRightRay.setLength(bottomRightRay.length()*2);
|
2015-12-02 19:39:25 +01:00
|
|
|
|
2015-12-04 09:04:43 +01:00
|
|
|
viewRect = QRectF(topLeftRay.p2(), bottomRightRay.p2());
|
2015-12-02 19:39:25 +01:00
|
|
|
|
2015-12-04 09:04:43 +01:00
|
|
|
//Calculate scene rect
|
|
|
|
const QRectF sceneRect = sc->sceneRect();
|
2015-12-02 19:39:25 +01:00
|
|
|
|
2015-12-04 09:04:43 +01:00
|
|
|
//Unite two rects
|
|
|
|
const QRectF newRect = sceneRect.united(viewRect);
|
2015-12-02 19:39:25 +01:00
|
|
|
|
2015-12-04 09:04:43 +01:00
|
|
|
sc->setSceneRect(newRect);
|
2015-12-02 19:39:25 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 10:25:13 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-04-26 14:08:04 +02:00
|
|
|
void GraphicsViewZoom::StartVerticalScrollings(QWheelEvent *wheel_event)
|
2016-03-30 10:25:13 +02:00
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
if (not wheel_event->pixelDelta().isNull())
|
|
|
|
{ // Native scrolling animation
|
|
|
|
_view->verticalScrollBar()->setValue(qCeil(_view->verticalScrollBar()->value() - ScrollingSteps(wheel_event)));
|
|
|
|
animFinished();
|
2016-03-30 10:25:13 +02:00
|
|
|
}
|
2018-04-26 14:08:04 +02:00
|
|
|
else
|
2016-03-30 10:25:13 +02:00
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
_numScheduledVerticalScrollings = PrepareScrolling(_numScheduledVerticalScrollings, wheel_event);
|
|
|
|
|
|
|
|
if (verticalScrollAnim->state() != QTimeLine::Running)
|
|
|
|
{
|
|
|
|
verticalScrollAnim->start();
|
|
|
|
}
|
2016-03-30 10:25:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-04-26 14:08:04 +02:00
|
|
|
void GraphicsViewZoom::StartHorizontalScrollings(QWheelEvent *wheel_event)
|
2016-03-30 10:25:13 +02:00
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
if (not wheel_event->pixelDelta().isNull())
|
|
|
|
{ // Native scrolling animation
|
|
|
|
_view->horizontalScrollBar()->setValue(qCeil(_view->horizontalScrollBar()->value() -
|
|
|
|
ScrollingSteps(wheel_event)));
|
|
|
|
animFinished();
|
2016-03-30 10:25:13 +02:00
|
|
|
}
|
2018-04-26 14:08:04 +02:00
|
|
|
else
|
2016-03-30 10:25:13 +02:00
|
|
|
{
|
2018-04-26 14:08:04 +02:00
|
|
|
_numScheduledHorizontalScrollings = PrepareScrolling(_numScheduledHorizontalScrollings, wheel_event);
|
|
|
|
|
|
|
|
if (horizontalScrollAnim->state() != QTimeLine::Running)
|
|
|
|
{
|
|
|
|
horizontalScrollAnim->start();
|
|
|
|
}
|
2016-03-30 10:25:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 10:14:17 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool GraphicsViewZoom::GestureEvent(QGestureEvent *event)
|
|
|
|
{
|
|
|
|
if (QGesture *pinch = event->gesture(Qt::PinchGesture))
|
|
|
|
{
|
|
|
|
PinchTriggered(static_cast<QPinchGesture *>(pinch));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void GraphicsViewZoom::PinchTriggered(QPinchGesture *gesture)
|
|
|
|
{
|
|
|
|
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
|
|
|
|
if (changeFlags & QPinchGesture::ScaleFactorChanged)
|
|
|
|
{
|
|
|
|
qreal currentStepScaleFactor = gesture->lastScaleFactor();
|
|
|
|
gentle_zoom(currentStepScaleFactor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:25:18 +02:00
|
|
|
const unsigned long VMainGraphicsView::scrollDelay = 80;
|
|
|
|
|
2014-08-19 14:46:52 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VMainGraphicsView constructor.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
|
|
|
VMainGraphicsView::VMainGraphicsView(QWidget *parent)
|
2017-04-14 07:22:28 +02:00
|
|
|
: QGraphicsView(parent),
|
2018-03-10 15:39:37 +01:00
|
|
|
zoom(nullptr),
|
2017-04-14 07:22:28 +02:00
|
|
|
showToolOptions(true),
|
|
|
|
isAllowRubberBand(true),
|
2017-10-30 10:08:25 +01:00
|
|
|
m_ptStartPos(),
|
2018-01-01 15:16:50 +01:00
|
|
|
m_oldCursor(),
|
|
|
|
m_currentCursor(Qt::ArrowCursor)
|
2014-08-19 14:46:52 +02:00
|
|
|
{
|
2018-03-10 15:39:37 +01:00
|
|
|
VSettings *settings = qobject_cast<VSettings *>(qApp->Settings());
|
|
|
|
if (settings && settings->IsOpenGLRender())
|
|
|
|
{
|
|
|
|
QOpenGLWidget *viewport = new QOpenGLWidget();
|
|
|
|
QSurfaceFormat fmt;
|
|
|
|
fmt.setSamples(settings->GetGraphicalOutput() ? 10 : 0);
|
2018-05-21 11:14:30 +02:00
|
|
|
fmt.setStencilBufferSize(8);
|
2018-03-10 15:39:37 +01:00
|
|
|
viewport->setFormat(fmt);
|
|
|
|
|
|
|
|
setViewport(viewport);
|
|
|
|
}
|
|
|
|
|
|
|
|
zoom = new GraphicsViewZoom(this);
|
|
|
|
|
2014-08-19 14:46:52 +02:00
|
|
|
this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
|
|
|
|
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
|
|
|
this->setInteractive(true);
|
2018-03-10 15:39:37 +01:00
|
|
|
SetAntialiasing(true);
|
2018-01-09 19:13:30 +01:00
|
|
|
|
|
|
|
connect(zoom, &GraphicsViewZoom::zoomed, this, [this](){emit ScaleChanged(transform().m11());});
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::Zoom(qreal scale)
|
|
|
|
{
|
|
|
|
qreal factor = qBound(MinScale(), scale, MaxScale());
|
|
|
|
QTransform transform = this->transform();
|
|
|
|
transform.setMatrix(factor, transform.m12(), transform.m13(), transform.m21(), factor, transform.m23(),
|
|
|
|
transform.m31(), transform.m32(), transform.m33());
|
|
|
|
this->setTransform(transform);
|
|
|
|
VMainGraphicsView::NewSceneRect(this->scene(), this);
|
|
|
|
emit ScaleChanged(this->transform().m11());
|
2014-08-19 14:46:52 +02:00
|
|
|
}
|
|
|
|
|
2014-06-02 20:42:46 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ZoomIn()
|
|
|
|
{
|
2016-08-12 16:37:47 +02:00
|
|
|
// We need to check current scale factor because in Windows we have an error when we zoom in or zoom out to much.
|
|
|
|
// See issue #532: Unexpected error occurs when zoom out image.
|
2017-06-16 14:03:24 +02:00
|
|
|
if (this->transform().m11() <= MaxScale())
|
2016-08-12 16:37:47 +02:00
|
|
|
{
|
|
|
|
scale(1.1, 1.1);
|
|
|
|
VMainGraphicsView::NewSceneRect(this->scene(), this);
|
2018-01-09 19:13:30 +01:00
|
|
|
emit ScaleChanged(transform().m11());
|
2016-08-12 16:37:47 +02:00
|
|
|
}
|
2014-06-02 20:42:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ZoomOut()
|
|
|
|
{
|
2016-08-12 16:37:47 +02:00
|
|
|
// We need to check current scale factor because in Windows we have an error when we zoom in or zoom out to much.
|
|
|
|
// See issue #532: Unexpected error occurs when zoom out image.
|
2017-06-16 14:03:24 +02:00
|
|
|
if (this->transform().m11() >= MinScale())
|
2016-08-12 16:37:47 +02:00
|
|
|
{
|
|
|
|
scale(1.0/1.1, 1.0/1.1);
|
|
|
|
VMainGraphicsView::NewSceneRect(this->scene(), this);
|
2018-01-09 19:13:30 +01:00
|
|
|
emit ScaleChanged(transform().m11());
|
2016-08-12 16:37:47 +02:00
|
|
|
}
|
2014-06-02 20:42:46 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2015-11-25 14:45:38 +01:00
|
|
|
VMainGraphicsView::NewSceneRect(this->scene(), this);
|
2018-01-09 19:13:30 +01:00
|
|
|
emit ScaleChanged(transform().m11());
|
2014-07-11 15:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ZoomFitBest()
|
|
|
|
{
|
2016-01-13 14:47:38 +01:00
|
|
|
VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(scene());
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(currentScene)
|
2016-01-15 13:55:56 +01:00
|
|
|
currentScene->SetOriginsVisible(false);
|
2016-01-13 14:47:38 +01:00
|
|
|
const QRectF rect = currentScene->VisibleItemsBoundingRect();
|
2016-01-15 13:55:56 +01:00
|
|
|
currentScene->SetOriginsVisible(true);
|
2014-07-11 15:10:54 +02:00
|
|
|
if (rect.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:35:09 +01:00
|
|
|
VMainGraphicsView::NewSceneRect(scene(), this);
|
|
|
|
|
2014-07-12 18:25:25 +02:00
|
|
|
this->fitInView(rect, Qt::KeepAspectRatio);
|
2016-12-06 15:18:40 +01:00
|
|
|
QTransform transform = this->transform();
|
|
|
|
|
2018-01-09 19:13:30 +01:00
|
|
|
const qreal factor = qBound(MinScale(), transform.m11(), MaxScale());
|
2016-12-06 15:18:40 +01:00
|
|
|
transform.setMatrix(factor, transform.m12(), transform.m13(), transform.m21(), factor, transform.m23(),
|
|
|
|
transform.m31(), transform.m32(), transform.m33());
|
|
|
|
this->setTransform(transform);
|
2018-01-09 19:13:30 +01:00
|
|
|
emit ScaleChanged(this->transform().m11());
|
2014-07-11 15:10:54 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 11:42:20 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::ResetScrollingAnimation()
|
|
|
|
{
|
|
|
|
zoom->InitScrollingAnimation();
|
|
|
|
}
|
|
|
|
|
2014-07-11 15:10:54 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief mousePressEvent handle mouse press events.
|
2017-04-14 16:45:59 +02:00
|
|
|
* @param event mouse press event.
|
2014-06-13 19:02:41 +02:00
|
|
|
*/
|
2017-04-14 07:22:28 +02:00
|
|
|
void VMainGraphicsView::mousePressEvent(QMouseEvent *event)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2017-04-14 07:22:28 +02:00
|
|
|
switch (event->button())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2016-03-30 10:51:09 +02:00
|
|
|
case Qt::LeftButton:
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2016-03-31 16:01:41 +02:00
|
|
|
if (isAllowRubberBand)
|
|
|
|
{
|
|
|
|
QGraphicsView::setDragMode(QGraphicsView::RubberBandDrag);
|
|
|
|
}
|
2018-05-24 08:56:19 +02:00
|
|
|
|
2016-03-30 10:51:09 +02:00
|
|
|
if (showToolOptions)
|
|
|
|
{
|
2018-05-24 08:56:19 +02:00
|
|
|
bool success = false;
|
2018-06-19 12:08:54 +02:00
|
|
|
const QList<QGraphicsItem *> list = PrioritizeItems(items(event->pos()));
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto item : list)
|
2016-03-30 10:51:09 +02:00
|
|
|
{
|
2018-05-24 08:56:19 +02:00
|
|
|
if (item && item->type() > QGraphicsItem::UserType && item->type() <= VSimpleCurve::Type)
|
2014-09-03 18:09:59 +02:00
|
|
|
{
|
2018-05-24 08:56:19 +02:00
|
|
|
emit itemClicked(item);
|
|
|
|
success = true;
|
2018-06-19 12:08:54 +02:00
|
|
|
break;
|
2014-09-03 18:09:59 +02:00
|
|
|
}
|
2014-08-30 21:58:31 +02:00
|
|
|
}
|
2018-05-24 08:56:19 +02:00
|
|
|
|
|
|
|
if (not success)
|
|
|
|
{
|
|
|
|
emit itemClicked(nullptr);
|
|
|
|
}
|
2016-03-30 10:51:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qt::MiddleButton:
|
|
|
|
{
|
2019-07-21 11:27:58 +02:00
|
|
|
auto scene = qobject_cast<VMainGraphicsScene*>(this->scene());
|
2017-04-14 07:22:28 +02:00
|
|
|
const QList<QGraphicsItem *> list = items(event->pos());
|
2019-07-21 11:27:58 +02:00
|
|
|
if (list.isEmpty() || (scene && scene->IsNonInteractive()))
|
|
|
|
{// Only when the user clicks on the scene background or non interactive scene
|
2017-04-14 07:22:28 +02:00
|
|
|
m_ptStartPos = event->pos();
|
2017-10-30 10:08:25 +01:00
|
|
|
m_oldCursor = viewport()->cursor();
|
2017-04-14 07:22:28 +02:00
|
|
|
QGraphicsView::setDragMode(QGraphicsView::ScrollHandDrag);
|
|
|
|
event->accept();
|
|
|
|
viewport()->setCursor(Qt::ClosedHandCursor);
|
|
|
|
}
|
|
|
|
break;
|
2013-10-14 11:31:56 +02:00
|
|
|
}
|
2016-03-30 10:51:09 +02:00
|
|
|
default:
|
|
|
|
break;
|
2013-10-14 11:31:56 +02:00
|
|
|
}
|
2017-04-14 07:22:28 +02:00
|
|
|
QGraphicsView::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
2018-01-01 15:16:50 +01:00
|
|
|
// Hack to fix problem with mouse cursor. Looks like after we switch cursor back it is rewrited back by a dialog.
|
|
|
|
// Because no real way to catch this call we will check state for each move and compare to excpected state.
|
2018-01-12 13:31:45 +01:00
|
|
|
if (dragMode() != QGraphicsView::ScrollHandDrag)
|
2018-01-01 15:16:50 +01:00
|
|
|
{
|
2018-01-12 13:31:45 +01:00
|
|
|
QCursor cur = viewport()->cursor();
|
|
|
|
// No way to restore bitmap from shape and we really don't need this for now.
|
|
|
|
if (m_currentCursor != Qt::BitmapCursor
|
|
|
|
&& cur.shape() == Qt::BitmapCursor
|
|
|
|
&& cur.pixmap().cacheKey() != QPixmapFromCache(cursorArrowOpenHand).cacheKey()
|
|
|
|
&& cur.pixmap().cacheKey() != QPixmapFromCache(cursorArrowCloseHand).cacheKey())
|
|
|
|
{
|
|
|
|
viewport()->setCursor(m_currentCursor);
|
|
|
|
}
|
2018-01-01 15:16:50 +01:00
|
|
|
}
|
|
|
|
|
2017-04-14 07:22:28 +02:00
|
|
|
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);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2016-03-31 16:01:41 +02:00
|
|
|
QGraphicsView::mouseReleaseEvent ( event ); // First because need to hide a rubber band
|
2016-03-30 10:51:09 +02:00
|
|
|
QGraphicsView::setDragMode( QGraphicsView::NoDrag );
|
2017-10-30 10:08:25 +01:00
|
|
|
if (event->button() == Qt::MiddleButton)
|
|
|
|
{
|
|
|
|
viewport()->setCursor(m_oldCursor);
|
|
|
|
}
|
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
|
|
|
|
2018-04-17 08:56:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
|
{
|
2018-05-10 13:12:41 +02:00
|
|
|
if (event->button() == Qt::LeftButton && qApp->Settings()->IsDoubleClickZoomFitBestCurrentPP())
|
2018-04-17 08:56:41 +02:00
|
|
|
{
|
|
|
|
if (VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow()))
|
|
|
|
{
|
|
|
|
window->ZoomFitBestCurrent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsView::mouseDoubleClickEvent(event);
|
|
|
|
}
|
|
|
|
|
2016-12-06 15:18:40 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VMainGraphicsView::MinScale()
|
|
|
|
{
|
2018-05-27 10:34:35 +02:00
|
|
|
const QRect screenRect = QGuiApplication::primaryScreen()->availableGeometry();
|
2017-06-16 14:03:24 +02:00
|
|
|
const qreal screenSize = qMin(screenRect.width(), screenRect.height());
|
2016-12-06 15:18:40 +01:00
|
|
|
|
|
|
|
return screenSize / maxSceneSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VMainGraphicsView::MaxScale()
|
|
|
|
{
|
2018-05-27 10:34:35 +02:00
|
|
|
const QRect screenRect = QGuiApplication::primaryScreen()->availableGeometry();
|
2017-06-16 14:03:24 +02:00
|
|
|
const qreal screenSize = qMin(screenRect.width(), screenRect.height());
|
2016-12-06 15:18:40 +01:00
|
|
|
|
|
|
|
return maxSceneSize / screenSize;
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:25:18 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::EnsureVisibleWithDelay(const QRectF &rect, unsigned long msecs, int xmargin, int ymargin)
|
|
|
|
{
|
|
|
|
const int hbar = horizontalScrollBar()->value();
|
|
|
|
const int vbar = verticalScrollBar()->value();
|
|
|
|
|
|
|
|
ensureVisible(rect, xmargin, ymargin);
|
|
|
|
|
|
|
|
if (hbar != horizontalScrollBar()->value() || vbar != verticalScrollBar()->value())
|
|
|
|
{
|
|
|
|
QThread::msleep(msecs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::EnsureVisibleWithDelay(const QGraphicsItem *item, unsigned long msecs, int xmargin, int ymargin)
|
|
|
|
{
|
|
|
|
const int hbar = horizontalScrollBar()->value();
|
|
|
|
const int vbar = verticalScrollBar()->value();
|
|
|
|
|
|
|
|
ensureVisible(item, xmargin, ymargin);
|
|
|
|
|
|
|
|
if (hbar != horizontalScrollBar()->value() || vbar != verticalScrollBar()->value())
|
|
|
|
{
|
|
|
|
QThread::msleep(msecs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 21:58:31 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-01-01 15:16:50 +01:00
|
|
|
void VMainGraphicsView::setCurrentCursorShape()
|
|
|
|
{
|
|
|
|
m_currentCursor = viewport()->cursor().shape();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-03-10 15:39:37 +01:00
|
|
|
void VMainGraphicsView::SetAntialiasing(bool value)
|
|
|
|
{
|
|
|
|
setRenderHint(QPainter::Antialiasing, value);
|
|
|
|
setRenderHint(QPainter::SmoothPixmapTransform, value);
|
2018-03-12 10:56:47 +01:00
|
|
|
}
|
2018-03-10 15:39:37 +01:00
|
|
|
|
2018-03-12 10:56:47 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VMainGraphicsView::IsOpenGLRender() const
|
|
|
|
{
|
|
|
|
QOpenGLWidget *viewport = qobject_cast<QOpenGLWidget *>(this->viewport());
|
|
|
|
if (viewport)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-10 15:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-08-30 21:58:31 +02:00
|
|
|
void VMainGraphicsView::setShowToolOptions(bool value)
|
|
|
|
{
|
|
|
|
showToolOptions = value;
|
|
|
|
}
|
2015-06-15 09:40:23 +02:00
|
|
|
|
2016-03-31 16:01:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VMainGraphicsView::AllowRubberBand(bool value)
|
|
|
|
{
|
|
|
|
isAllowRubberBand = 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.
|
|
|
|
*/
|
2017-10-27 08:13:51 +02:00
|
|
|
void VMainGraphicsView::NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item)
|
2015-06-15 09:40:23 +02:00
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(sc != nullptr)
|
|
|
|
SCASSERT(view != nullptr)
|
2015-06-15 09:40:23 +02:00
|
|
|
|
2017-10-27 08:13:51 +02:00
|
|
|
if (item == nullptr)
|
|
|
|
{
|
|
|
|
//Calculate view rect
|
|
|
|
const QRectF viewRect = SceneVisibleArea(view);
|
2015-06-15 09:40:23 +02:00
|
|
|
|
2017-10-27 08:13:51 +02:00
|
|
|
//Calculate scene rect
|
|
|
|
VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(sc);
|
|
|
|
SCASSERT(currentScene)
|
|
|
|
const QRectF itemsRect = currentScene->VisibleItemsBoundingRect();
|
2015-06-15 09:40:23 +02:00
|
|
|
|
2017-10-27 08:13:51 +02:00
|
|
|
//Unite two rects
|
|
|
|
sc->setSceneRect(itemsRect.united(viewRect));
|
|
|
|
}
|
2017-10-28 16:31:18 +02:00
|
|
|
else
|
2017-10-27 08:13:51 +02:00
|
|
|
{
|
2017-10-28 16:31:18 +02:00
|
|
|
QRectF rect = item->sceneBoundingRect();
|
2018-04-03 10:15:58 +02:00
|
|
|
const QList<QGraphicsItem *> children = item->childItems();
|
|
|
|
for (auto child : children)
|
2017-10-28 16:31:18 +02:00
|
|
|
{
|
|
|
|
if(child->isVisible())
|
|
|
|
{
|
|
|
|
rect = rect.united(child->sceneBoundingRect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not sc->sceneRect().contains(rect))
|
|
|
|
{
|
|
|
|
sc->setSceneRect(sc->sceneRect().united(rect));
|
|
|
|
}
|
2017-10-27 08:13:51 +02:00
|
|
|
}
|
2015-06-15 09:40:23 +02:00
|
|
|
}
|
2016-01-15 13:57:47 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QRectF VMainGraphicsView::SceneVisibleArea(QGraphicsView *view)
|
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(view != nullptr)
|
2016-01-15 13:57:47 +01:00
|
|
|
//to receive the currently visible area, map the widgets bounds to the scene
|
|
|
|
return QRectF(view->mapToScene(0, 0), view->mapToScene(view->width(), view->height()));
|
|
|
|
}
|