-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternViewSelectionHandler.cpp
107 lines (97 loc) · 3.13 KB
/
PatternViewSelectionHandler.cpp
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
#include "PatternViewSelectionHandler.h"
#include "PatternView.h"
PatternViewSelectionHandler::PatternViewSelectionHandler(PatternView& a_view)
: m_view(a_view)
{
}
void PatternViewSelectionHandler::reset()
{
const PatternSelection previous = m_selection;
m_selection.invalidate();
redrawSelections(previous, m_selection);
emit m_view.selectionChanged(m_selection);
}
void PatternViewSelectionHandler::mousePressEvent(const QMouseEvent* a_event)
{
if (isSelectionDefiningEvent(a_event))
{
const PatternSelection previous = m_selection;
Frame x = QtTools::graphicsScenePosition(a_event, &m_view).x();
if (x > m_view.sceneRect().width())
x = m_view.sceneRect().width();
Frame frame = x/Properties::Core::FrameWidth;
m_selection = PatternSelection(frame, frame);
redrawSelections(previous, m_selection);
emit m_view.selectionChanged(m_selection);
}
else if (isSelectionClearingEvent(a_event))
{
const PatternSelection previous = m_selection;
m_selection.invalidate();
redrawSelections(previous, m_selection);
emit m_view.selectionChanged(m_selection);
}
}
void PatternViewSelectionHandler::mouseMoveEvent(const QMouseEvent* a_event)
{
if (isSelectionDefiningEvent(a_event))
{
const PatternSelection previous = m_selection;
Frame x = QtTools::graphicsScenePosition(a_event, &m_view).x();
if (x > m_view.sceneRect().width())
x = m_view.sceneRect().width();
Frame last = x/Properties::Core::FrameWidth;
if ( last != m_selection.last() && last >= 0)
{
m_selection = m_selection.extended(last);
redrawSelections(previous, m_selection);
emit m_view.selectionChanged(m_selection);
}
}
}
void PatternViewSelectionHandler::mouseReleaseEvent(const QMouseEvent* a_event)
{
if (isSelectionDefiningEvent(a_event))
{
const PatternSelection previous = m_selection;
Frame x = QtTools::graphicsScenePosition(a_event, &m_view).x();
if (x > m_view.sceneRect().width())
x = m_view.sceneRect().width();
Frame last = x/Properties::Core::FrameWidth;
m_selection = PatternSelection(m_selection.first(), last);
if (m_selection.size() == 1)
m_selection.invalidate();
redrawSelections(previous, m_selection);
emit m_view.selectionChanged(m_selection);
}
}
bool PatternViewSelectionHandler::isSelectionDefiningEvent(const QMouseEvent* a_event)
{
return
a_event->buttons().testFlag(Qt::MiddleButton) &&
a_event->modifiers() == Qt::NoModifier;
}
bool PatternViewSelectionHandler::isSelectionClearingEvent(const QMouseEvent* a_event)
{
return
a_event->buttons().testFlag(Qt::MiddleButton) &&
a_event->modifiers() == Qt::AltModifier;
}
void PatternViewSelectionHandler::redrawSelections(const PatternSelection& a_first, const PatternSelection& a_second)
{
redrawSelection(a_first);
redrawSelection(a_second);
}
void PatternViewSelectionHandler::redrawSelection(const PatternSelection& a_selection)
{
if (!a_selection.isValid())
return;
QRectF rect(
/* x */ Properties::Core::FrameWidth*a_selection.first(),
/* y */ 0,
/* w */ Properties::Core::FrameWidth*a_selection.size(),
/* h */ m_view.sceneRect().bottom()
);
if (m_view.scene() != nullptr)
m_view.scene()->invalidate(rect, QGraphicsScene::ForegroundLayer);
}