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 examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "window.h" |
52 | |
53 | #include <private/qguiapplication_p.h> |
54 | |
55 | #include <QBackingStore> |
56 | #include <QPainter> |
57 | |
58 | static int colorIndexId = 0; |
59 | |
60 | QColor colorTable[] = |
61 | { |
62 | QColor("#f09f8f" ), |
63 | QColor("#a2bff2" ), |
64 | QColor("#c0ef8f" ) |
65 | }; |
66 | |
67 | Window::Window(QScreen *screen) |
68 | : QWindow(screen) |
69 | , m_backgroundColorIndex(colorIndexId++) |
70 | { |
71 | initialize(); |
72 | } |
73 | |
74 | Window::Window(QWindow *parent) |
75 | : QWindow(parent) |
76 | , m_backgroundColorIndex(colorIndexId++) |
77 | { |
78 | initialize(); |
79 | } |
80 | |
81 | void Window::initialize() |
82 | { |
83 | if (parent()) |
84 | setGeometry(QRect(160, 120, 320, 240)); |
85 | else { |
86 | setFlags(flags() | Qt::WindowTitleHint | Qt::WindowSystemMenuHint |
87 | | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint); |
88 | const QSize baseSize = QSize(640, 480); |
89 | setGeometry(QRect(geometry().topLeft(), baseSize)); |
90 | |
91 | setSizeIncrement(QSize(10, 10)); |
92 | setBaseSize(baseSize); |
93 | setMinimumSize(QSize(240, 160)); |
94 | setMaximumSize(QSize(800, 600)); |
95 | } |
96 | |
97 | create(); |
98 | m_backingStore = new QBackingStore(this); |
99 | |
100 | m_image = QImage(geometry().size(), QImage::Format_RGB32); |
101 | m_image.fill(pixel: colorTable[m_backgroundColorIndex % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba()); |
102 | |
103 | m_lastPos = QPoint(-1, -1); |
104 | m_renderTimer = 0; |
105 | } |
106 | |
107 | void Window::mousePressEvent(QMouseEvent *event) |
108 | { |
109 | m_lastPos = event->pos(); |
110 | } |
111 | |
112 | void Window::mouseMoveEvent(QMouseEvent *event) |
113 | { |
114 | if (m_lastPos != QPoint(-1, -1)) { |
115 | QPainter p(&m_image); |
116 | p.setRenderHint(hint: QPainter::Antialiasing); |
117 | p.drawLine(p1: m_lastPos, p2: event->pos()); |
118 | m_lastPos = event->pos(); |
119 | |
120 | scheduleRender(); |
121 | } |
122 | } |
123 | |
124 | void Window::mouseReleaseEvent(QMouseEvent *event) |
125 | { |
126 | if (m_lastPos != QPoint(-1, -1)) { |
127 | QPainter p(&m_image); |
128 | p.setRenderHint(hint: QPainter::Antialiasing); |
129 | p.drawLine(p1: m_lastPos, p2: event->pos()); |
130 | m_lastPos = QPoint(-1, -1); |
131 | |
132 | scheduleRender(); |
133 | } |
134 | } |
135 | |
136 | void Window::exposeEvent(QExposeEvent *) |
137 | { |
138 | scheduleRender(); |
139 | } |
140 | |
141 | void Window::resizeEvent(QResizeEvent *) |
142 | { |
143 | QImage old = m_image; |
144 | |
145 | int width = qMax(a: geometry().width(), b: old.width()); |
146 | int height = qMax(a: geometry().height(), b: old.height()); |
147 | |
148 | if (width > old.width() || height > old.height()) { |
149 | m_image = QImage(width, height, QImage::Format_RGB32); |
150 | m_image.fill(pixel: colorTable[(m_backgroundColorIndex) % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba()); |
151 | |
152 | QPainter p(&m_image); |
153 | p.drawImage(x: 0, y: 0, image: old); |
154 | } |
155 | scheduleRender(); |
156 | } |
157 | |
158 | void Window::keyPressEvent(QKeyEvent *event) |
159 | { |
160 | switch (event->key()) { |
161 | case Qt::Key_Backspace: |
162 | m_text.chop(n: 1); |
163 | break; |
164 | case Qt::Key_Enter: |
165 | case Qt::Key_Return: |
166 | m_text.append(c: '\n'); |
167 | break; |
168 | default: |
169 | m_text.append(s: event->text()); |
170 | break; |
171 | } |
172 | scheduleRender(); |
173 | } |
174 | |
175 | void Window::scheduleRender() |
176 | { |
177 | if (!m_renderTimer) |
178 | m_renderTimer = startTimer(interval: 1); |
179 | } |
180 | |
181 | void Window::timerEvent(QTimerEvent *) |
182 | { |
183 | if (isExposed()) |
184 | render(); |
185 | killTimer(id: m_renderTimer); |
186 | m_renderTimer = 0; |
187 | } |
188 | |
189 | void Window::render() |
190 | { |
191 | QRect rect(QPoint(), geometry().size()); |
192 | m_backingStore->resize(size: rect.size()); |
193 | |
194 | m_backingStore->beginPaint(rect); |
195 | |
196 | QPaintDevice *device = m_backingStore->paintDevice(); |
197 | |
198 | QPainter p(device); |
199 | p.drawImage(x: 0, y: 0, image: m_image); |
200 | |
201 | QFont font; |
202 | font.setPixelSize(32); |
203 | |
204 | p.setFont(font); |
205 | p.drawText(r: rect, flags: 0, text: m_text); |
206 | |
207 | m_backingStore->endPaint(); |
208 | m_backingStore->flush(region: rect); |
209 | } |
210 | |
211 | |
212 | |