| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "window.h" |
| 5 |
|
| 6 |
#include <private/qguiapplication_p.h> |
| 7 |
|
| 8 |
#include <QBackingStore> |
| 9 |
#include <QPainter> |
| 10 |
#include <QtWidgets> |
| 11 |
|
| 12 |
static int colorIndexId = 0; |
| 13 |
|
| 14 |
QColor colorTable[] = |
| 15 |
{ |
| 16 |
QColor("#f09f8f"), |
| 17 |
QColor("#a2bff2"), |
| 18 |
QColor("#c0ef8f") |
| 19 |
}; |
| 20 |
|
| 21 |
Window::Window(QScreen *screen) |
| 22 |
: QWindow(screen) |
| 23 |
, m_backgroundColorIndex(colorIndexId++) |
| 24 |
{ |
| 25 |
initialize(); |
| 26 |
} |
| 27 |
|
| 28 |
Window::Window(QWindow *parent) |
| 29 |
: QWindow(parent) |
| 30 |
, m_backgroundColorIndex(colorIndexId++) |
| 31 |
{ |
| 32 |
initialize(); |
| 33 |
} |
| 34 |
|
| 35 |
void Window::initialize() |
| 36 |
{ |
| 37 |
if (parent()) |
| 38 |
setGeometry(QRect(160, 120, 320, 240)); |
| 39 |
else { |
| 40 |
setGeometry(QRect(10, 10, 640, 480)); |
| 41 |
|
| 42 |
setSizeIncrement(QSize(10, 10)); |
| 43 |
setBaseSize(QSize(640, 480)); |
| 44 |
setMinimumSize(QSize(240, 160)); |
| 45 |
} |
| 46 |
|
| 47 |
create(); |
| 48 |
m_backingStore = new QBackingStore(this); |
| 49 |
|
| 50 |
m_image = QImage(geometry().size(), QImage::Format_RGB32); |
| 51 |
m_image.fill(colorTable[m_backgroundColorIndex % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba()); |
| 52 |
|
| 53 |
m_lastPos = QPoint(-1, -1); |
| 54 |
m_renderTimer = 0; |
| 55 |
} |
| 56 |
|
| 57 |
void Window::mousePressEvent(QMouseEvent *event) |
| 58 |
{ |
| 59 |
m_lastPos = event->pos(); |
| 60 |
} |
| 61 |
|
| 62 |
void Window::mouseMoveEvent(QMouseEvent *event) |
| 63 |
{ |
| 64 |
if (m_lastPos != QPoint(-1, -1)) { |
| 65 |
QPainter p(&m_image); |
| 66 |
|
| 67 |
QPen pen; |
| 68 |
pen.setCosmetic(true); |
| 69 |
pen.setWidth(1); |
| 70 |
p.setPen(pen); |
| 71 |
|
| 72 |
p.setRenderHint(QPainter::Antialiasing); |
| 73 |
p.drawLine(m_lastPos, event->pos()); |
| 74 |
m_lastPos = event->pos(); |
| 75 |
} |
| 76 |
|
| 77 |
scheduleRender(); |
| 78 |
} |
| 79 |
|
| 80 |
void Window::wheelEvent(QWheelEvent *event) |
| 81 |
{ |
| 82 |
qDebug() << "wheelEvent delta" << event->delta() << "orientation" << event->orientation(); |
| 83 |
qDebug() << "wheelEvent pixelDelta" << event->pixelDelta(); |
| 84 |
qDebug() << "wheelEvent angleDelta" << event->angleDelta(); |
| 85 |
|
| 86 |
const bool useQt4API = false; |
| 87 |
|
| 88 |
if (useQt4API) { |
| 89 |
if (event->orientation() == Qt::Horizontal) |
| 90 |
scrollOffset.setX(scrollOffset.x() + event->delta()); |
| 91 |
else |
| 92 |
scrollOffset.setY(scrollOffset.y() + event->delta()); |
| 93 |
scheduleRender(); |
| 94 |
} else { |
| 95 |
if (!event->pixelDelta().isNull()) { |
| 96 |
scrollOffset += event->pixelDelta(); |
| 97 |
scheduleRender(); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void Window::mouseReleaseEvent(QMouseEvent *event) |
| 103 |
{ |
| 104 |
if (m_lastPos != QPoint(-1, -1)) { |
| 105 |
QPainter p(&m_image); |
| 106 |
p.setRenderHint(QPainter::Antialiasing); |
| 107 |
p.drawLine(m_lastPos, event->pos()); |
| 108 |
m_lastPos = QPoint(-1, -1); |
| 109 |
} |
| 110 |
|
| 111 |
scheduleRender(); |
| 112 |
} |
| 113 |
|
| 114 |
void Window::exposeEvent(QExposeEvent *) |
| 115 |
{ |
| 116 |
scheduleRender(); |
| 117 |
} |
| 118 |
|
| 119 |
void Window::resizeEvent(QResizeEvent *) |
| 120 |
{ |
| 121 |
QImage old = m_image; |
| 122 |
|
| 123 |
qDebug() << "Window::resizeEvent" << geometry(); |
| 124 |
|
| 125 |
int width = qMax(geometry().width() * devicePixelRatio(), qreal(old.width())); |
| 126 |
int height = qMax(geometry().height() * devicePixelRatio(), qreal(old.height())); |
| 127 |
|
| 128 |
qDebug() << "Window::resizeEvent" << width << height; |
| 129 |
|
| 130 |
if (width > old.width() || height > old.height()) { |
| 131 |
m_image = QImage(width, height, QImage::Format_RGB32); |
| 132 |
m_image.setDevicePixelRatio(devicePixelRatio()); |
| 133 |
m_image.fill(colorTable[(m_backgroundColorIndex) % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba()); |
| 134 |
|
| 135 |
QPainter p(&m_image); |
| 136 |
p.drawImage(0, 0, old); |
| 137 |
} |
| 138 |
|
| 139 |
render(); |
| 140 |
} |
| 141 |
|
| 142 |
void Window::keyPressEvent(QKeyEvent *event) |
| 143 |
{ |
| 144 |
switch (event->key()) { |
| 145 |
case Qt::Key_Backspace: |
| 146 |
m_text.chop(1); |
| 147 |
break; |
| 148 |
case Qt::Key_Enter: |
| 149 |
case Qt::Key_Return: |
| 150 |
m_text.append('\n'); |
| 151 |
break; |
| 152 |
default: |
| 153 |
m_text.append(event->text()); |
| 154 |
break; |
| 155 |
} |
| 156 |
scheduleRender(); |
| 157 |
} |
| 158 |
|
| 159 |
void Window::scheduleRender() |
| 160 |
{ |
| 161 |
if (!m_renderTimer) |
| 162 |
m_renderTimer = startTimer(1); |
| 163 |
} |
| 164 |
|
| 165 |
void Window::timerEvent(QTimerEvent *) |
| 166 |
{ |
| 167 |
render(); |
| 168 |
killTimer(m_renderTimer); |
| 169 |
m_renderTimer = 0; |
| 170 |
} |
| 171 |
|
| 172 |
void Window::render() |
| 173 |
{ |
| 174 |
QRect rect(QPoint(), geometry().size()); |
| 175 |
|
| 176 |
m_backingStore->resize(rect.size()); |
| 177 |
|
| 178 |
m_backingStore->beginPaint(rect); |
| 179 |
|
| 180 |
QPaintDevice *device = m_backingStore->paintDevice(); |
| 181 |
|
| 182 |
QPainter p(device); |
| 183 |
p.drawImage(0, 0, m_image); |
| 184 |
|
| 185 |
QFont font; |
| 186 |
font.setPixelSize(32); |
| 187 |
|
| 188 |
p.setFont(font); |
| 189 |
p.drawText(rect, 0, m_text); |
| 190 |
|
| 191 |
// draw grid: |
| 192 |
int gridSpace = 80; |
| 193 |
|
| 194 |
QPen pen; |
| 195 |
pen.setCosmetic(true); |
| 196 |
pen.setWidth(1); |
| 197 |
p.setPen(pen); |
| 198 |
|
| 199 |
|
| 200 |
for (int y = 0; y < geometry().height() + gridSpace; y+= gridSpace) { |
| 201 |
int offset = scrollOffset.y() % gridSpace; |
| 202 |
//int color = ((y + offset) %255);// + scrollOffset.y()) % 255); |
| 203 |
p.drawLine(0, y + offset, geometry().width(), y + offset); |
| 204 |
//p.setBrush(QColor(color,0, 0)); |
| 205 |
//p.fillRect(0, y + offset, geometry().width(), gridSpace,QColor(color,0, 0)); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
for (int x = 0; x < geometry().width() + gridSpace; x+= gridSpace) { |
| 210 |
p.drawLine(x + scrollOffset.x() % gridSpace, 0, x + scrollOffset.x() % gridSpace, geometry().height()); |
| 211 |
} |
| 212 |
|
| 213 |
m_backingStore->endPaint(); |
| 214 |
m_backingStore->flush(rect); |
| 215 |
} |
| 216 |
|
| 217 |
|