1 /*
2 * Copyright (C) 2016, Mike Walters <mike@flomp.net>
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
Cursor(Qt::Orientation orientation,Qt::CursorShape mouseCursorShape,QObject * parent)23 Cursor::Cursor(Qt::Orientation orientation, Qt::CursorShape mouseCursorShape, QObject * parent) : QObject::QObject(parent), orientation(orientation), cursorShape(mouseCursorShape)
24 {
25
26 }
27
fromPoint(QPoint point)28 int Cursor::fromPoint(QPoint point)
29 {
30 return (orientation == Qt::Vertical) ? point.x() : point.y();
31 }
32
pointOverCursor(QPoint point)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
mouseEvent(QEvent::Type type,QMouseEvent event)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()) && type != QEvent::Leave) {
44 if (!cursorOverrided) {
45 cursorOverrided = true;
46 QApplication::setOverrideCursor(QCursor(cursorShape));
47 }
48 // Restore pointer if it moves off the cursor, or leaves the widget
49 } else if (cursorOverrided) {
50 cursorOverrided = 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
pos()80 int Cursor::pos()
81 {
82 return cursorPosition;
83 }
84
setPos(int newPos)85 void Cursor::setPos(int newPos)
86 {
87 cursorPosition = newPos;
88 }
89