File indexing completed on 2024-04-28 16:49:18

0001 /*
0002     SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include <QApplication>
0007 #include <QCursor>
0008 #include <QMouseEvent>
0009 #include <QPainter>
0010 #include <QWidget>
0011 
0012 class MouseCursorWidget : public QWidget
0013 {
0014     Q_OBJECT
0015 public:
0016     explicit MouseCursorWidget();
0017     ~MouseCursorWidget() override;
0018 
0019 protected:
0020     void paintEvent(QPaintEvent *event) override;
0021     void mouseMoveEvent(QMouseEvent *event) override;
0022     void keyPressEvent(QKeyEvent *event) override;
0023 
0024 private:
0025     QPoint m_cursorPos;
0026     QCursor m_cursors[5];
0027     int m_cursorIndex = 0;
0028 };
0029 
0030 namespace
0031 {
0032 QCursor createCenterHotspotCursor()
0033 {
0034     QPixmap cursor(64, 64);
0035     cursor.fill(Qt::transparent);
0036     QPainter p(&cursor);
0037     p.setPen(Qt::black);
0038     const QPoint center = cursor.rect().center();
0039     p.drawLine(0, center.y(), center.x() - 1, center.y());
0040     p.drawLine(center.x() + 1, center.y(), cursor.width(), center.y());
0041     p.drawLine(center.x(), 0, center.x(), center.y() - 1);
0042     p.drawLine(center.x(), center.y() + 1, center.x(), cursor.height());
0043     return QCursor(cursor, 31, 31);
0044 }
0045 
0046 QCursor createTopLeftHotspotCursor()
0047 {
0048     QPixmap cursor(64, 64);
0049     cursor.fill(Qt::transparent);
0050     QPainter p(&cursor);
0051     p.setPen(Qt::black);
0052     p.drawLine(0, 1, 0, cursor.height());
0053     p.drawLine(1, 0, cursor.width(), 0);
0054     return QCursor(cursor, 0, 0);
0055 }
0056 
0057 QCursor createTopRightHotspotCursor()
0058 {
0059     QPixmap cursor(64, 64);
0060     cursor.fill(Qt::transparent);
0061     QPainter p(&cursor);
0062     p.setPen(Qt::black);
0063     p.drawLine(cursor.width() - 1, 1, cursor.width() - 1, cursor.height());
0064     p.drawLine(0, 0, cursor.width() - 2, 0);
0065     return QCursor(cursor, 63, 0);
0066 }
0067 
0068 QCursor createButtomRightHotspotCursor()
0069 {
0070     QPixmap cursor(64, 64);
0071     cursor.fill(Qt::transparent);
0072     QPainter p(&cursor);
0073     p.setPen(Qt::black);
0074     p.drawLine(cursor.width() - 1, 0, cursor.width() - 1, cursor.height() - 2);
0075     p.drawLine(0, cursor.height() - 1, cursor.width() - 2, cursor.height() - 1);
0076     return QCursor(cursor, 63, 63);
0077 }
0078 
0079 QCursor createButtomLeftHotspotCursor()
0080 {
0081     QPixmap cursor(64, 64);
0082     cursor.fill(Qt::transparent);
0083     QPainter p(&cursor);
0084     p.setPen(Qt::black);
0085     p.drawLine(0, 0, 0, cursor.height() - 2);
0086     p.drawLine(1, cursor.height() - 1, cursor.width(), cursor.height() - 1);
0087     return QCursor(cursor, 0, 63);
0088 }
0089 
0090 }
0091 
0092 MouseCursorWidget::MouseCursorWidget()
0093     : QWidget()
0094 {
0095     setMouseTracking(true);
0096     // create cursors
0097     m_cursors[0] = createCenterHotspotCursor();
0098     m_cursors[1] = createTopLeftHotspotCursor();
0099     m_cursors[2] = createTopRightHotspotCursor();
0100     m_cursors[3] = createButtomRightHotspotCursor();
0101     m_cursors[4] = createButtomLeftHotspotCursor();
0102 
0103     setCursor(m_cursors[m_cursorIndex]);
0104 }
0105 
0106 MouseCursorWidget::~MouseCursorWidget() = default;
0107 
0108 void MouseCursorWidget::paintEvent(QPaintEvent *event)
0109 {
0110     QPainter p(this);
0111     p.fillRect(0, 0, width(), height(), Qt::white);
0112     if (geometry().contains(m_cursorPos)) {
0113         p.setPen(Qt::red);
0114         p.drawPoint(m_cursorPos);
0115     }
0116 }
0117 
0118 void MouseCursorWidget::mouseMoveEvent(QMouseEvent *event)
0119 {
0120     m_cursorPos = event->pos();
0121     update();
0122 }
0123 
0124 void MouseCursorWidget::keyPressEvent(QKeyEvent *event)
0125 {
0126     if (event->key() == Qt::Key_Space) {
0127         m_cursorIndex = (m_cursorIndex + 1) % 5;
0128         setCursor(m_cursors[m_cursorIndex]);
0129     }
0130 }
0131 
0132 int main(int argc, char *argv[])
0133 {
0134     QApplication app(argc, argv);
0135 
0136     MouseCursorWidget widget;
0137     widget.show();
0138 
0139     return app.exec();
0140 }
0141 
0142 #include "cursorhotspottest.moc"