| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include "mainwindow.h" |
| 30 |
#include "ui_mainwindow.h" |
| 31 |
|
| 32 |
#include <QTimer> |
| 33 |
#include <QBitmap> |
| 34 |
#include <QImage> |
| 35 |
#include <QPainter> |
| 36 |
#include <QKeyEvent> |
| 37 |
|
| 38 |
MainWindow::MainWindow(QWidget *parent) : |
| 39 |
QMainWindow(parent), ui(new Ui::MainWindow) |
| 40 |
{ |
| 41 |
ui->setupUi(this); |
| 42 |
QPixmap pix(":/data/monkey_on_64x64.png"); |
| 43 |
|
| 44 |
QImage mask(16, 16, QImage::Format_MonoLSB); |
| 45 |
QImage bw(16, 16, QImage::Format_MonoLSB); |
| 46 |
mask.fill(0); |
| 47 |
bw.fill(0); |
| 48 |
for (int x = 0; x < 16; x++) { |
| 49 |
bw.setPixel(x, x, 1); |
| 50 |
bw.setPixel(x, 15 - x, 1); |
| 51 |
mask.setPixel(x, x, 1); |
| 52 |
mask.setPixel(x, 15 - x, 1); |
| 53 |
if (x > 0 && x < 15) { |
| 54 |
mask.setPixel(x - 1, x, 1); |
| 55 |
mask.setPixel(x + 1, x, 1); |
| 56 |
mask.setPixel(x - 1, 15 - x, 1); |
| 57 |
mask.setPixel(x + 1, 15 - x, 1); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
ccurs = QCursor(pix); |
| 62 |
bcurs = QCursor(QBitmap::fromImage(bw), QBitmap::fromImage(mask)); |
| 63 |
ui->label->setCursor(ccurs); |
| 64 |
|
| 65 |
timer = new QTimer(this); |
| 66 |
connect(timer, SIGNAL(timeout()), this, SLOT(toggleOverrideCursor())); |
| 67 |
timer->start(2000); |
| 68 |
|
| 69 |
override = 0; |
| 70 |
} |
| 71 |
|
| 72 |
MainWindow::~MainWindow() |
| 73 |
{ |
| 74 |
delete timer; |
| 75 |
delete ui; |
| 76 |
} |
| 77 |
|
| 78 |
void MainWindow::toggleOverrideCursor() |
| 79 |
{ |
| 80 |
switch (override) { |
| 81 |
case 0: |
| 82 |
QGuiApplication::setOverrideCursor(Qt::BusyCursor); |
| 83 |
break; |
| 84 |
case 1: |
| 85 |
QGuiApplication::restoreOverrideCursor(); |
| 86 |
break; |
| 87 |
case 2: |
| 88 |
ui->label->grabMouse(Qt::ForbiddenCursor); |
| 89 |
break; |
| 90 |
case 3: |
| 91 |
case 5: |
| 92 |
ui->label->releaseMouse(); |
| 93 |
break; |
| 94 |
case 4: |
| 95 |
ui->label->grabMouse(); |
| 96 |
break; |
| 97 |
case 6: |
| 98 |
ui->label->setCursor(bcurs); |
| 99 |
break; |
| 100 |
case 7: |
| 101 |
ui->label->setCursor(ccurs); |
| 102 |
break; |
| 103 |
} |
| 104 |
override = (override + 1) % 8; |
| 105 |
} |
| 106 |
|
| 107 |
void MainWindow::keyPressEvent(QKeyEvent* event) |
| 108 |
{ |
| 109 |
QPoint off(0, 0); |
| 110 |
switch (event->key()) { |
| 111 |
case Qt::Key_Up: |
| 112 |
off.setY(-4); |
| 113 |
break; |
| 114 |
case Qt::Key_Down: |
| 115 |
off.setY(4); |
| 116 |
break; |
| 117 |
case Qt::Key_Left: |
| 118 |
off.setX(-4); |
| 119 |
break; |
| 120 |
case Qt::Key_Right: |
| 121 |
off.setX(4); |
| 122 |
break; |
| 123 |
default: |
| 124 |
return QMainWindow::keyPressEvent(event); |
| 125 |
} |
| 126 |
off += QCursor::pos(); |
| 127 |
QCursor::setPos(off); |
| 128 |
} |