Debian Codegraph @

src/cursors.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2016, Mike Walters <[email protected]>
3 *
4 * This file is part of inspectrum.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <QApplication>
21 #include <QDebug>
22 #include "cursors.h"
23
24 Cursors::Cursors(QObject * parent) : QObject::QObject(parent)
25 {
26 minCursor = new Cursor(Qt::Vertical, Qt::SizeHorCursor, this);
27 maxCursor = new Cursor(Qt::Vertical, Qt::SizeHorCursor, this);
28 connect(minCursor, &Cursor::posChanged, this, &Cursors::cursorMoved);
29 connect(maxCursor, &Cursor::posChanged, this, &Cursors::cursorMoved);
30 }
31
32 void Cursors::cursorMoved()
33 {
34 // Swap cursors if one has been dragged past the other
35 if (minCursor->pos() > maxCursor->pos()) {
36 std::swap(minCursor, maxCursor);
37 }
38 emit cursorsMoved();
39 }
40
41 bool Cursors::pointWithinDragRegion(QPoint point) {
42 int margin = 10;
43 range_t<int> range = {minCursor->pos()+margin, maxCursor->pos()-margin};
44 return range.contains(point.x());
45 }
46
47 bool Cursors::mouseEvent(QEvent::Type type, QMouseEvent *event)
48 {
49 if (minCursor->mouseEvent(type, event))
50 return true;
51 if (maxCursor->mouseEvent(type, event))
52 return true;
53
54 // If the mouse pointer is between the cursors, display a resize pointer
55 if (pointWithinDragRegion(event->pos()) ) {
56 if (!cursorOverride) {
57 cursorOverride = true;
58 QApplication::setOverrideCursor(QCursor(Qt::SizeAllCursor));
59 }
60 // Restore pointer otherwise
61 } else {
62 if (cursorOverride) {
63 cursorOverride = false;
64 QApplication::restoreOverrideCursor();
65 }
66 }
67 // Start dragging on left mouse button press, if between the cursors
68 if (type == QEvent::MouseButtonPress) {
69 if (event->button() == Qt::LeftButton) {
70 if (pointWithinDragRegion(event->pos())) {
71 dragging = true;
72 dragPos = event->pos();
73 return true;
74 }
75 }
76 // Update both cursor positons if we're dragging
77 } else if (type == QEvent::MouseMove) {
78 if (dragging) {
79 int dx = event->pos().x() - dragPos.x();
80 minCursor->setPos(minCursor->pos() + dx);
81 maxCursor->setPos(maxCursor->pos() + dx);
82 dragPos = event->pos();
83 emit cursorsMoved();
84 }
85 // Stop dragging on left mouse button release
86 } else if (type == QEvent::MouseButtonRelease) {
87 if (event->button() == Qt::LeftButton && dragging) {
88 dragging = false;
89 return true;
90 }
91 }
92 return false;
93 }
94
95 void Cursors::leaveEvent()
96 {
97 minCursor->leaveEvent();
98 maxCursor->leaveEvent();
99
100 if (cursorOverride) {
101 cursorOverride = false;
102 QApplication::restoreOverrideCursor();
103 }
104 }
105
106 void Cursors::paintFront(QPainter &painter, QRect &rect, range_t<size_t> sampleRange)
107 {
108 painter.save();
109
110 QRect cursorRect(minCursor->pos(), rect.top(), maxCursor->pos() - minCursor->pos(), rect.height());
111
112 // Draw translucent white fill for highlight
113 painter.fillRect(
114 cursorRect,
115 QBrush(QColor(255, 255, 255, 50))
116 );
117
118 // Draw vertical edges for individual segments
119 painter.setPen(QPen(Qt::gray, 1, Qt::DashLine));
120 for (long i = 1; i < segmentCount; i++) {
121 int pos = minCursor->pos() + (i * cursorRect.width() / segmentCount);
122 painter.drawLine(pos, rect.top(), pos, rect.bottom());
123 }
124
125 // Draw vertical edges
126 painter.setPen(QPen(Qt::white, 1, Qt::SolidLine));
127 painter.drawLine(minCursor->pos(), rect.top(), minCursor->pos(), rect.bottom());
128 painter.drawLine(maxCursor->pos(), rect.top(), maxCursor->pos(), rect.bottom());
129
130 painter.restore();
131 }
132
133 int Cursors::segments()
134 {
135 return segmentCount;
136 }
137
138 range_t<int> Cursors::selection()
139 {
140 return {minCursor->pos(), maxCursor->pos()};
141 }
142
143 void Cursors::setSegments(int segments)
144 {
145 segmentCount = std::max(segments, 1);
146 }
147
148 void Cursors::setSelection(range_t<int> selection)
149 {
150 minCursor->setPos(selection.minimum);
151 maxCursor->setPos(selection.maximum);
152 }