Debian Codegraph @

src/cursor.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 "cursor.h"
22
23 Cursor::Cursor(Qt::Orientation orientation, Qt::CursorShape mouseCursorShape, QObject * parent) : QObject::QObject(parent), orientation(orientation), cursorShape(mouseCursorShape)
24 {
25
26 }
27
28 int Cursor::fromPoint(QPoint point)
29 {
30 return (orientation == Qt::Vertical) ? point.x() : point.y();
31 }
32
33 bool Cursor::pointOverCursor(QPoint point)
34 {
35 const int margin = 5;
36 range_t<int> range = {cursorPosition - margin, cursorPosition + margin};
37 return range.contains(fromPoint(point));
38 }
39
40 bool Cursor::mouseEvent(QEvent::Type type, QMouseEvent *event)
41 {
42 // If the mouse pointer moves over a cursor, display a resize pointer
43 if (pointOverCursor(event->pos())) {
44 if (!cursorOverriden) {
45 cursorOverriden = true;
46 QApplication::setOverrideCursor(QCursor(cursorShape));
47 }
48 // Restore pointer if it moves off the cursor
49 } else if (cursorOverriden) {
50 cursorOverriden = false;
51 QApplication::restoreOverrideCursor();
52 }
53
54 // Start dragging on left mouse button press, if over a cursor
55 if (type == QEvent::MouseButtonPress) {
56 if (event->button() == Qt::LeftButton) {
57 if (pointOverCursor(event->pos())) {
58 dragging = true;
59 return true;
60 }
61 }
62
63 // Update current cursor position if we're dragging
64 } else if (type == QEvent::MouseMove) {
65 if (dragging) {
66 cursorPosition = fromPoint(event->pos());
67 emit posChanged();
68 }
69
70 // Stop dragging on left mouse button release
71 } else if (type == QEvent::MouseButtonRelease) {
72 if (event->button() == Qt::LeftButton && dragging) {
73 dragging = false;
74 return true;
75 }
76 }
77 return false;
78 }
79
80 void Cursor::leaveEvent()
81 {
82 if (cursorOverriden) {
83 cursorOverriden = false;
84 QApplication::restoreOverrideCursor();
85 }
86 }
87
88 int Cursor::pos()
89 {
90 return cursorPosition;
91 }
92
93 void Cursor::setPos(int newPos)
94 {
95 cursorPosition = newPos;
96 }