KWin
Loading...
Searching...
No Matches
cursorhotspottest.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6#include <QApplication>
7#include <QCursor>
8#include <QMouseEvent>
9#include <QPainter>
10#include <QWidget>
11
12class MouseCursorWidget : public QWidget
13{
14 Q_OBJECT
15public:
16 explicit MouseCursorWidget();
18
19protected:
20 void paintEvent(QPaintEvent *event) override;
21 void mouseMoveEvent(QMouseEvent *event) override;
22 void keyPressEvent(QKeyEvent *event) override;
23
24private:
25 QPoint m_cursorPos;
26 QCursor m_cursors[5];
27 int m_cursorIndex = 0;
28};
29
30namespace
31{
32QCursor createCenterHotspotCursor()
33{
34 QPixmap cursor(64, 64);
35 cursor.fill(Qt::transparent);
36 QPainter p(&cursor);
37 p.setPen(Qt::black);
38 const QPoint center = cursor.rect().center();
39 p.drawLine(0, center.y(), center.x() - 1, center.y());
40 p.drawLine(center.x() + 1, center.y(), cursor.width(), center.y());
41 p.drawLine(center.x(), 0, center.x(), center.y() - 1);
42 p.drawLine(center.x(), center.y() + 1, center.x(), cursor.height());
43 return QCursor(cursor, 31, 31);
44}
45
46QCursor createTopLeftHotspotCursor()
47{
48 QPixmap cursor(64, 64);
49 cursor.fill(Qt::transparent);
50 QPainter p(&cursor);
51 p.setPen(Qt::black);
52 p.drawLine(0, 1, 0, cursor.height());
53 p.drawLine(1, 0, cursor.width(), 0);
54 return QCursor(cursor, 0, 0);
55}
56
57QCursor createTopRightHotspotCursor()
58{
59 QPixmap cursor(64, 64);
60 cursor.fill(Qt::transparent);
61 QPainter p(&cursor);
62 p.setPen(Qt::black);
63 p.drawLine(cursor.width() - 1, 1, cursor.width() - 1, cursor.height());
64 p.drawLine(0, 0, cursor.width() - 2, 0);
65 return QCursor(cursor, 63, 0);
66}
67
68QCursor createButtomRightHotspotCursor()
69{
70 QPixmap cursor(64, 64);
71 cursor.fill(Qt::transparent);
72 QPainter p(&cursor);
73 p.setPen(Qt::black);
74 p.drawLine(cursor.width() - 1, 0, cursor.width() - 1, cursor.height() - 2);
75 p.drawLine(0, cursor.height() - 1, cursor.width() - 2, cursor.height() - 1);
76 return QCursor(cursor, 63, 63);
77}
78
79QCursor createButtomLeftHotspotCursor()
80{
81 QPixmap cursor(64, 64);
82 cursor.fill(Qt::transparent);
83 QPainter p(&cursor);
84 p.setPen(Qt::black);
85 p.drawLine(0, 0, 0, cursor.height() - 2);
86 p.drawLine(1, cursor.height() - 1, cursor.width(), cursor.height() - 1);
87 return QCursor(cursor, 0, 63);
88}
89
90}
91
93 : QWidget()
94{
95 setMouseTracking(true);
96 // create cursors
97 m_cursors[0] = createCenterHotspotCursor();
98 m_cursors[1] = createTopLeftHotspotCursor();
99 m_cursors[2] = createTopRightHotspotCursor();
100 m_cursors[3] = createButtomRightHotspotCursor();
101 m_cursors[4] = createButtomLeftHotspotCursor();
102
103 setCursor(m_cursors[m_cursorIndex]);
104}
105
107
108void MouseCursorWidget::paintEvent(QPaintEvent *event)
109{
110 QPainter p(this);
111 p.fillRect(0, 0, width(), height(), Qt::white);
112 if (geometry().contains(m_cursorPos)) {
113 p.setPen(Qt::red);
114 p.drawPoint(m_cursorPos);
115 }
116}
117
118void MouseCursorWidget::mouseMoveEvent(QMouseEvent *event)
119{
120 m_cursorPos = event->pos();
121 update();
122}
123
125{
126 if (event->key() == Qt::Key_Space) {
127 m_cursorIndex = (m_cursorIndex + 1) % 5;
128 setCursor(m_cursors[m_cursorIndex]);
129 }
130}
131
132int main(int argc, char *argv[])
133{
134 QApplication app(argc, argv);
135
136 MouseCursorWidget widget;
137 widget.show();
138
139 return app.exec();
140}
141
142#include "cursorhotspottest.moc"
void paintEvent(QPaintEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void mouseMoveEvent(QMouseEvent *event) override
~MouseCursorWidget() override