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 demonstration applications 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 "arthurwidgets.h"
52#include <QApplication>
53#include <QPainter>
54#include <QPainterPath>
55#include <QPixmapCache>
56#include <QtEvents>
57#include <QTextDocument>
58#include <QAbstractTextDocumentLayout>
59#include <QFile>
60#include <QTextBrowser>
61#include <QBoxLayout>
62#include <QRegularExpression>
63#include <QOffscreenSurface>
64#include <QOpenGLContext>
65#include <QOpenGLPaintDevice>
66#include <QOpenGLWindow>
67
68extern QPixmap cached(const QString &img);
69
70ArthurFrame::ArthurFrame(QWidget *parent)
71 : QWidget(parent)
72 , m_prefer_image(false)
73{
74#if QT_CONFIG(opengl)
75 m_glWindow = nullptr;
76 m_glWidget = nullptr;
77 m_use_opengl = false;
78#endif
79 m_document = nullptr;
80 m_show_doc = false;
81
82 m_tile = QPixmap(128, 128);
83 m_tile.fill(fillColor: Qt::white);
84 QPainter pt(&m_tile);
85 QColor color(230, 230, 230);
86 pt.fillRect(x: 0, y: 0, w: 64, h: 64, b: color);
87 pt.fillRect(x: 64, y: 64, w: 64, h: 64, b: color);
88 pt.end();
89
90// QPalette pal = palette();
91// pal.setBrush(backgroundRole(), m_tile);
92// setPalette(pal);
93}
94
95
96#if QT_CONFIG(opengl)
97void ArthurFrame::enableOpenGL(bool use_opengl)
98{
99 if (m_use_opengl == use_opengl)
100 return;
101
102 m_use_opengl = use_opengl;
103
104 if (!m_glWindow && use_opengl) {
105 createGlWindow();
106 QApplication::postEvent(receiver: this, event: new QResizeEvent(size(), size()));
107 }
108
109 if (use_opengl) {
110 m_glWidget->show();
111 } else {
112 if (m_glWidget)
113 m_glWidget->hide();
114 }
115
116 update();
117}
118
119void ArthurFrame::createGlWindow()
120{
121 Q_ASSERT(m_use_opengl);
122
123 m_glWindow = new QOpenGLWindow();
124 QSurfaceFormat f = QSurfaceFormat::defaultFormat();
125 f.setSamples(4);
126 f.setAlphaBufferSize(8);
127 f.setStencilBufferSize(8);
128 m_glWindow->setFormat(f);
129 m_glWindow->setFlags(Qt::WindowTransparentForInput);
130 m_glWindow->resize(w: width(), h: height());
131 m_glWidget = QWidget::createWindowContainer(window: m_glWindow, parent: this);
132 // create() must be called after createWindowContainer() otherwise
133 // an incorrect offsetting of the position will occur.
134 m_glWindow->create();
135}
136#endif
137
138
139void ArthurFrame::paintEvent(QPaintEvent *e)
140{
141 static QImage *static_image = nullptr;
142
143 QPainter painter;
144
145 if (preferImage()
146#if QT_CONFIG(opengl)
147 && !m_use_opengl
148#endif
149 ) {
150 if (!static_image || static_image->size() != size()) {
151 delete static_image;
152 static_image = new QImage(size(), QImage::Format_RGB32);
153 }
154 painter.begin(static_image);
155
156 int o = 10;
157
158 QBrush bg = palette().brush(cr: QPalette::Window);
159 painter.fillRect(x: 0, y: 0, w: o, h: o, b: bg);
160 painter.fillRect(x: width() - o, y: 0, w: o, h: o, b: bg);
161 painter.fillRect(x: 0, y: height() - o, w: o, h: o, b: bg);
162 painter.fillRect(x: width() - o, y: height() - o, w: o, h: o, b: bg);
163 } else {
164#if QT_CONFIG(opengl)
165 if (m_use_opengl && m_glWindow->isValid()) {
166 m_glWindow->makeCurrent();
167
168 painter.begin(m_glWindow);
169 painter.fillRect(QRectF(0, 0, m_glWindow->width(), m_glWindow->height()), color: palette().color(cr: backgroundRole()));
170 } else {
171 painter.begin(this);
172 }
173#else
174 painter.begin(this);
175#endif
176 }
177
178 painter.setClipRect(e->rect());
179
180 painter.setRenderHint(hint: QPainter::Antialiasing);
181
182 QPainterPath clipPath;
183
184 QRect r = rect();
185 qreal left = r.x() + 1;
186 qreal top = r.y() + 1;
187 qreal right = r.right();
188 qreal bottom = r.bottom();
189 qreal radius2 = 8 * 2;
190
191 clipPath.moveTo(x: right - radius2, y: top);
192 clipPath.arcTo(x: right - radius2, y: top, w: radius2, h: radius2, startAngle: 90, arcLength: -90);
193 clipPath.arcTo(x: right - radius2, y: bottom - radius2, w: radius2, h: radius2, startAngle: 0, arcLength: -90);
194 clipPath.arcTo(x: left, y: bottom - radius2, w: radius2, h: radius2, startAngle: 270, arcLength: -90);
195 clipPath.arcTo(x: left, y: top, w: radius2, h: radius2, startAngle: 180, arcLength: -90);
196 clipPath.closeSubpath();
197
198 painter.save();
199 painter.setClipPath(path: clipPath, op: Qt::IntersectClip);
200
201 painter.drawTiledPixmap(rect: rect(), pm: m_tile);
202
203 // client painting
204
205 paint(&painter);
206
207 painter.restore();
208
209 painter.save();
210 if (m_show_doc)
211 paintDescription(p: &painter);
212 painter.restore();
213
214 int level = 180;
215 painter.setPen(QPen(QColor(level, level, level), 2));
216 painter.setBrush(Qt::NoBrush);
217 painter.drawPath(path: clipPath);
218
219 if (preferImage()
220#if QT_CONFIG(opengl)
221 && !m_use_opengl
222#endif
223 ) {
224 painter.end();
225 painter.begin(this);
226 painter.drawImage(targetRect: e->rect(), image: *static_image, sourceRect: e->rect());
227 }
228#if QT_CONFIG(opengl)
229 if (m_use_opengl)
230 m_glWindow->update();
231#endif
232}
233
234void ArthurFrame::resizeEvent(QResizeEvent *e)
235{
236#if QT_CONFIG(opengl)
237 if (m_glWidget)
238 m_glWidget->setGeometry(ax: 0, ay: 0, aw: e->size().width(), ah: e->size().height());
239#endif
240 QWidget::resizeEvent(event: e);
241}
242
243void ArthurFrame::setDescriptionEnabled(bool enabled)
244{
245 if (m_show_doc != enabled) {
246 m_show_doc = enabled;
247 emit descriptionEnabledChanged(m_show_doc);
248 update();
249 }
250}
251
252void ArthurFrame::loadDescription(const QString &fileName)
253{
254 QFile textFile(fileName);
255 QString text;
256 if (!textFile.open(flags: QFile::ReadOnly))
257 text = QString("Unable to load resource file: '%1'").arg(a: fileName);
258 else
259 text = textFile.readAll();
260 setDescription(text);
261}
262
263
264void ArthurFrame::setDescription(const QString &text)
265{
266 m_document = new QTextDocument(this);
267 m_document->setHtml(text);
268}
269
270void ArthurFrame::paintDescription(QPainter *painter)
271{
272 if (!m_document)
273 return;
274
275 int pageWidth = qMax(a: width() - 100, b: 100);
276 int pageHeight = qMax(a: height() - 100, b: 100);
277 if (pageWidth != m_document->pageSize().width()) {
278 m_document->setPageSize(QSize(pageWidth, pageHeight));
279 }
280
281 QRect textRect(width() / 2 - pageWidth / 2,
282 height() / 2 - pageHeight / 2,
283 pageWidth,
284 pageHeight);
285 int pad = 10;
286 QRect clearRect = textRect.adjusted(xp1: -pad, yp1: -pad, xp2: pad, yp2: pad);
287 painter->setPen(Qt::NoPen);
288 painter->setBrush(QColor(0, 0, 0, 63));
289 int shade = 10;
290 painter->drawRect(x: clearRect.x() + clearRect.width() + 1,
291 y: clearRect.y() + shade,
292 w: shade,
293 h: clearRect.height() + 1);
294 painter->drawRect(x: clearRect.x() + shade,
295 y: clearRect.y() + clearRect.height() + 1,
296 w: clearRect.width() - shade + 1,
297 h: shade);
298
299 painter->setRenderHint(hint: QPainter::Antialiasing, on: false);
300 painter->setBrush(QColor(255, 255, 255, 220));
301 painter->setPen(Qt::black);
302 painter->drawRect(r: clearRect);
303
304 painter->setClipRegion(textRect, op: Qt::IntersectClip);
305 painter->translate(offset: textRect.topLeft());
306
307 QAbstractTextDocumentLayout::PaintContext ctx;
308
309 QLinearGradient g(0, 0, 0, textRect.height());
310 g.setColorAt(pos: 0, color: Qt::black);
311 g.setColorAt(pos: 0.9, color: Qt::black);
312 g.setColorAt(pos: 1, color: Qt::transparent);
313
314 QPalette pal = palette();
315 pal.setBrush(acr: QPalette::Text, abrush: g);
316
317 ctx.palette = pal;
318 ctx.clip = QRect(0, 0, textRect.width(), textRect.height());
319 m_document->documentLayout()->draw(painter, context: ctx);
320}
321
322void ArthurFrame::loadSourceFile(const QString &sourceFile)
323{
324 m_sourceFileName = sourceFile;
325}
326
327void ArthurFrame::showSource()
328{
329 // Check for existing source
330 if (findChild<QTextBrowser *>())
331 return;
332
333 QString contents;
334 if (m_sourceFileName.isEmpty()) {
335 contents = tr(s: "No source for widget: '%1'").arg(a: objectName());
336 } else {
337 QFile f(m_sourceFileName);
338 if (!f.open(flags: QFile::ReadOnly))
339 contents = tr(s: "Could not open file: '%1'").arg(a: m_sourceFileName);
340 else
341 contents = f.readAll();
342 }
343
344 contents.replace(c: QLatin1Char('&'), QStringLiteral("&amp;"));
345 contents.replace(c: QLatin1Char('<'), QStringLiteral("&lt;"));
346 contents.replace(c: QLatin1Char('>'), QStringLiteral("&gt;"));
347
348 static const QString keywords[] = {
349 QStringLiteral("for "), QStringLiteral("if "),
350 QStringLiteral("switch "), QStringLiteral(" int "),
351 QStringLiteral("#include "), QStringLiteral("const"),
352 QStringLiteral("void "), QStringLiteral("uint "),
353 QStringLiteral("case "), QStringLiteral("double "),
354 QStringLiteral("#define "), QStringLiteral("static"),
355 QStringLiteral("new"), QStringLiteral("this")
356 };
357
358 for (const QString &keyword : keywords)
359 contents.replace(before: keyword, after: QLatin1String("<font color=olive>") + keyword + QLatin1String("</font>"));
360 contents.replace(QStringLiteral("(int "), QStringLiteral("(<font color=olive><b>int </b></font>"));
361
362 static const QString ppKeywords[] = {
363 QStringLiteral("#ifdef"), QStringLiteral("#ifndef"),
364 QStringLiteral("#if"), QStringLiteral("#endif"),
365 QStringLiteral("#else")
366 };
367
368 for (const QString &keyword : ppKeywords)
369 contents.replace(before: keyword, after: QLatin1String("<font color=navy>") + keyword + QLatin1String("</font>"));
370
371 contents.replace(re: QRegularExpression("(\\d\\d?)"), after: QLatin1String("<font color=navy>\\1</font>"));
372
373 QRegularExpression commentRe("(//.+?)\\n");
374 contents.replace(re: commentRe, after: QLatin1String("<font color=red>\\1</font>\n"));
375
376 QRegularExpression stringLiteralRe("(\".+?\")");
377 contents.replace(re: stringLiteralRe, after: QLatin1String("<font color=green>\\1</font>"));
378
379 const QString html = QStringLiteral("<html><pre>") + contents + QStringLiteral("</pre></html>");
380
381 QTextBrowser *sourceViewer = new QTextBrowser;
382 sourceViewer->setWindowTitle(tr(s: "Source: %1").arg(a: m_sourceFileName.midRef(position: 5)));
383 sourceViewer->setParent(parent: this, f: Qt::Dialog);
384 sourceViewer->setAttribute(Qt::WA_DeleteOnClose);
385 sourceViewer->setLineWrapMode(QTextEdit::NoWrap);
386 sourceViewer->setHtml(html);
387 sourceViewer->resize(w: 600, h: 600);
388 sourceViewer->show();
389}
390

source code of qtbase/examples/widgets/painting/shared/arthurwidgets.cpp