Debian Codegraph @

tests/manual/cocoa/qt_on_cocoa/rasterwindow.cpp

raw
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 "rasterwindow.h"
30
31 //#include <private/qguiapplication_p.h>
32
33 #include <QBackingStore>
34 #include <QPainter>
35 #include <QtWidgets>
36
37 static int colorIndexId = 0;
38
39 QColor colorTable[] =
40 {
41 QColor("#f09f8f"),
42 QColor("#a2bff2"),
43 QColor("#c0ef8f")
44 };
45
46 RasterWindow::RasterWindow(QRasterWindow *parent)
47 : QRasterWindow(parent)
48 , m_backgroundColorIndex(colorIndexId++)
49 {
50 initialize();
51 }
52
53 void RasterWindow::initialize()
54 {
55 create();
56 m_backingStore = new QBackingStore(this);
57
58 m_image = QImage(geometry().size(), QImage::Format_RGB32);
59 m_image.fill(colorTable[m_backgroundColorIndex % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba());
60
61 m_lastPos = QPoint(-1, -1);
62 }
63
64 void RasterWindow::mousePressEvent(QMouseEvent *event)
65 {
66 m_lastPos = event->pos();
67 unsetCursor();
68 }
69
70 void RasterWindow::mouseMoveEvent(QMouseEvent *event)
71 {
72 if (m_lastPos != QPoint(-1, -1)) {
73 QPainter p(&m_image);
74 p.setRenderHint(QPainter::Antialiasing);
75 p.drawLine(m_lastPos, event->pos());
76 m_lastPos = event->pos();
77 }
78
79 scheduleRender();
80 }
81
82 void RasterWindow::mouseReleaseEvent(QMouseEvent *event)
83 {
84 if (m_lastPos != QPoint(-1, -1)) {
85 QPainter p(&m_image);
86 p.setRenderHint(QPainter::Antialiasing);
87 p.drawLine(m_lastPos, event->pos());
88 m_lastPos = QPoint(-1, -1);
89 }
90
91 scheduleRender();
92 }
93
94 void RasterWindow::exposeEvent(QExposeEvent *)
95 {
96 render();
97 }
98
99 void RasterWindow::resizeEvent(QResizeEvent *)
100 {
101 QImage old = m_image;
102
103 //qDebug() << "RasterWindow::resizeEvent" << width << height;
104
105 int width = qMax(geometry().width(), old.width());
106 int height = qMax(geometry().height(), old.height());
107
108 if (width > old.width() || height > old.height()) {
109 m_image = QImage(width, height, QImage::Format_RGB32);
110 m_image.fill(colorTable[(m_backgroundColorIndex) % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba());
111
112 QPainter p(&m_image);
113 p.drawImage(0, 0, old);
114 }
115
116 render();
117 }
118
119 void RasterWindow::keyPressEvent(QKeyEvent *event)
120 {
121 switch (event->key()) {
122 case Qt::Key_Backspace:
123 m_text.chop(1);
124 break;
125 case Qt::Key_Enter:
126 case Qt::Key_Return:
127 m_text.append('\n');
128 break;
129 default:
130 m_text.append(event->text());
131 break;
132 }
133 scheduleRender();
134 }
135
136 void RasterWindow::scheduleRender()
137 {
138 requestUpdate();
139 }
140
141 bool RasterWindow::event(QEvent *e)
142 {
143 if (e->type() == QEvent::UpdateRequest)
144 render();
145
146 return QWindow::event(e);
147 }
148
149 void RasterWindow::render()
150 {
151 if (!isExposed()) {
152 qDebug() << "Skipping render, not exposed";
153 return;
154 }
155
156 QRect rect(QPoint(), geometry().size());
157
158 m_backingStore->resize(rect.size());
159
160 m_backingStore->beginPaint(rect);
161
162 QPaintDevice *device = m_backingStore->paintDevice();
163
164 QPainter p(device);
165 p.drawImage(0, 0, m_image);
166
167 QFont font;
168 font.setPixelSize(32);
169
170 p.setFont(font);
171 p.drawText(rect, 0, m_text);
172
173 m_backingStore->endPaint();
174 m_backingStore->flush(rect);
175 }
176
177