File: SizeHandle.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 (103 lines) | stat: -rw-r--r-- 2,726 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
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Overlay/SizeHandle.cpp
//! @brief     Implements class SizeHandle.
//!
//! @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/Overlay/SizeHandle.h"
#include "GUI/View/Overlay/OverlayStyle.h"
#include <QCursor>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <array>

namespace {

const std::array<Qt::CursorShape, 8> cursors{
    Qt::SizeFDiagCursor, Qt::SizeVerCursor, Qt::SizeBDiagCursor, Qt::SizeHorCursor,
    Qt::SizeFDiagCursor, Qt::SizeVerCursor, Qt::SizeBDiagCursor, Qt::SizeHorCursor};

} // namespace


SizeHandle::SizeHandle(int i, QGraphicsObject* parent)
    : m_i(i)
{
    setParentItem(parent);
    hide();
    setCursor(::cursors[i]);
}

QRectF SizeHandle::boundingRect() const
{
    return {-4, -4, 8, 8};
}

void SizeHandle::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
{
    painter->setRenderHints(QPainter::Antialiasing);
    painter->setBrush(GUI::Overlay::getSelectionMarkerBrush());
    painter->setPen(GUI::Overlay::getSelectionMarkerPen());
    if (m_i % 2 == 1)
        painter->drawRect(boundingRect());
    else
        painter->drawEllipse(boundingRect());
}

void SizeHandle::mousePressEvent(QGraphicsSceneMouseEvent*)
{
    emit requestResizing(true);
}

void SizeHandle::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
    emit requestEnactResize(m_i, event->scenePos());
}

void SizeHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent*)
{
    emit requestResizing(false);
}

void SizeHandle::updateHandlePosition(const QRectF& rect)
{
    switch (m_i) {
    case 0:
        setPos(rect.topLeft());
        break;
    case 1:
        setPos(QPointF(rect.x() + rect.width() / 2, rect.y()));
        break;
    case 2:
        setPos(rect.topRight());
        break;
    case 3:
        setPos(QPointF(rect.x() + rect.width(), rect.y() + rect.height() / 2));
        break;
    case 4:
        setPos(rect.bottomRight());
        break;
    case 5:
        setPos(QPointF(rect.x() + rect.width() / 2, rect.y() + rect.height()));
        break;
    case 6:
        setPos(rect.bottomLeft());
        break;
    case 7:
        setPos(QPointF(rect.x(), rect.y() + rect.height() / 2));
        break;
    }
}

void SizeHandle::update_view()
{
    update();
}