File: MaskGraphicsView.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (122 lines) | stat: -rw-r--r-- 3,566 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Scene/MaskGraphicsView.cpp
//! @brief     Implements class MaskGraphicsView.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Scene/MaskGraphicsView.h"
#include "GUI/View/Scene/MaskGraphicsScene.h"
#include <QScrollBar>
#include <QTransform>

namespace {

const double min_zoom_value = 1.0;
const double max_zoom_value = 5.0;
const double zoom_step = 0.05;

} // namespace


MaskGraphicsView::MaskGraphicsView(MaskGraphicsScene* scene, QWidget* parent)
    : QGraphicsView(scene, parent)
    , m_scene(scene)
    , m_current_zoom_value(1.0)
{
    setObjectName("MaskGraphicsView");
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    setMouseTracking(true);
}

//! Reset given view to original zoom state. Also asks graphics scene to do the same with color map.
void MaskGraphicsView::onResetViewRequest()
{
    setZoomValue(1.0);
}

void MaskGraphicsView::wheelEvent(QWheelEvent* event)
{
    // hold control button
    if (isControlButtonIsPressed(event)) {
        centerOn(mapToScene(event->position().toPoint()));
        if (event->angleDelta().y() > 0) {
            // Zoom in
            increaseZoomValue();
        } else {
            // Zooming out
            if (horizontalScrollBar()->isVisible() || verticalScrollBar()->isVisible())
                decreaseZoomValue();
        }
    } else {
        QGraphicsView::wheelEvent(event);
    }
}

//! On resize event changes scene size and MaskGraphicsProxy so they would get the size of viewport
void MaskGraphicsView::resizeEvent(QResizeEvent* event)
{
    QWidget::resizeEvent(event);
    m_scene->updateSize(event->size());
}

void MaskGraphicsView::keyPressEvent(QKeyEvent* event)
{
    switch (event->key()) {
    case Qt::Key_Left:
        break;
    case Qt::Key_Space:
        if (!event->isAutoRepeat())
            emit changeActivityRequest(Canvas2DMode::PAN_ZOOM);
        break;
    case Qt::Key_Escape:
        m_scene->cancelCurrentDrawing();
        break;
    case Qt::Key_Delete:
    case Qt::Key_Backspace:
        emit deleteCurrentRequest();
        break;
    default:
        QWidget::keyPressEvent(event);
    }
}

bool MaskGraphicsView::isControlButtonIsPressed(QWheelEvent* event)
{
    return event->modifiers().testFlag(Qt::ControlModifier);
}

void MaskGraphicsView::setZoomValue(double zoom_value)
{
    if (zoom_value == m_current_zoom_value)
        return;
    const QTransform oldMatrix = transform();
    resetTransform();
    translate(oldMatrix.dx(), oldMatrix.dy());
    scale(zoom_value, zoom_value);
    m_current_zoom_value = zoom_value;
}

void MaskGraphicsView::decreaseZoomValue()
{
    double zoom_value = m_current_zoom_value - zoom_step;
    if (zoom_value < min_zoom_value)
        zoom_value = min_zoom_value;
    setZoomValue(zoom_value);
}

void MaskGraphicsView::increaseZoomValue()
{
    double zoom_value = m_current_zoom_value + zoom_step;
    if (zoom_value > max_zoom_value)
        zoom_value = max_zoom_value;
    setZoomValue(zoom_value);
}