Debian Codegraph @

examples/widgets/painting/shared/arthurwidgets.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 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
68 extern QPixmap cached(const QString &img);
69
70 ArthurFrame::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(Qt::white);
84 QPainter pt(&m_tile);
85 QColor color(230, 230, 230);
86 pt.fillRect(0, 0, 64, 64, color);
87 pt.fillRect(64, 64, 64, 64, 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)
97 void 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(this, 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
119 void 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(width(), height());
131 m_glWidget = QWidget::createWindowContainer(m_glWindow, 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
139 void 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(QPalette::Window);
159 painter.fillRect(0, 0, o, o, bg);
160 painter.fillRect(width() - o, 0, o, o, bg);
161 painter.fillRect(0, height() - o, o, o, bg);
162 painter.fillRect(width() - o, height() - o, o, o, 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()), palette().color(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(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(right - radius2, top);
192 clipPath.arcTo(right - radius2, top, radius2, radius2, 90, -90);
193 clipPath.arcTo(right - radius2, bottom - radius2, radius2, radius2, 0, -90);
194 clipPath.arcTo(left, bottom - radius2, radius2, radius2, 270, -90);
195 clipPath.arcTo(left, top, radius2, radius2, 180, -90);
196 clipPath.closeSubpath();
197
198 painter.save();
199 painter.setClipPath(clipPath, Qt::IntersectClip);
200
201 painter.drawTiledPixmap(rect(), 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(&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(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(e->rect(), *static_image, e->rect());
227 }
228 #if QT_CONFIG(opengl)
229 if (m_use_opengl)
230 m_glWindow->update();
231 #endif
232 }
233
234 void ArthurFrame::resizeEvent(QResizeEvent *e)
235 {
236 #if QT_CONFIG(opengl)
237 if (m_glWidget)
238 m_glWidget->setGeometry(0, 0, e->size().width(), e->size().height());
239 #endif
240 QWidget::resizeEvent(e);
241 }
242
243 void 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
252 void ArthurFrame::loadDescription(const QString &fileName)
253 {
254 QFile textFile(fileName);
255 QString text;
256 if (!textFile.open(QFile::ReadOnly))
257 text = QString("Unable to load resource file: '%1'").arg(fileName);
258 else
259 text = textFile.readAll();
260 setDescription(text);
261 }
262
263
264 void ArthurFrame::setDescription(const QString &text)
265 {
266 m_document = new QTextDocument(this);
267 m_document->setHtml(text);
268 }
269
270 void ArthurFrame::paintDescription(QPainter *painter)
271 {
272 if (!m_document)
273 return;
274
275 int pageWidth = qMax(width() - 100, 100);
276 int pageHeight = qMax(height() - 100, 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(-pad, -pad, pad, pad);
287 painter->setPen(Qt::NoPen);
288 painter->setBrush(QColor(0, 0, 0, 63));
289 int shade = 10;
290 painter->drawRect(clearRect.x() + clearRect.width() + 1,
291 clearRect.y() + shade,
292 shade,
293 clearRect.height() + 1);
294 painter->drawRect(clearRect.x() + shade,
295 clearRect.y() + clearRect.height() + 1,
296 clearRect.width() - shade + 1,
297 shade);
298
299 painter->setRenderHint(QPainter::Antialiasing, false);
300 painter->setBrush(QColor(255, 255, 255, 220));
301 painter->setPen(Qt::black);
302 painter->drawRect(clearRect);
303
304 painter->setClipRegion(textRect, Qt::IntersectClip);
305 painter->translate(textRect.topLeft());
306
307 QAbstractTextDocumentLayout::PaintContext ctx;
308
309 QLinearGradient g(0, 0, 0, textRect.height());
310 g.setColorAt(0, Qt::black);
311 g.setColorAt(0.9, Qt::black);
312 g.setColorAt(1, Qt::transparent);
313
314 QPalette pal = palette();
315 pal.setBrush(QPalette::Text, g);
316
317 ctx.palette = pal;
318 ctx.clip = QRect(0, 0, textRect.width(), textRect.height());
319 m_document->documentLayout()->draw(painter, ctx);
320 }
321
322 void ArthurFrame::loadSourceFile(const QString &sourceFile)
323 {
324 m_sourceFileName = sourceFile;
325 }
326
327 void 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("No source for widget: '%1'").arg(objectName());
336 } else {
337 QFile f(m_sourceFileName);
338 if (!f.open(QFile::ReadOnly))
339 contents = tr("Could not open file: '%1'").arg(m_sourceFileName);
340 else
341 contents = f.readAll();
342 }
343
344 contents.replace(QLatin1Char('&'), QStringLiteral("&amp;"));
345 contents.replace(QLatin1Char('<'), QStringLiteral("&lt;"));
346 contents.replace(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(keyword, 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(keyword, QLatin1String("<font color=navy>") + keyword + QLatin1String("</font>"));
370
371 contents.replace(QRegularExpression("(\\d\\d?)"), QLatin1String("<font color=navy>\\1</font>"));
372
373 QRegularExpression commentRe("(//.+?)\\n");
374 contents.replace(commentRe, QLatin1String("<font color=red>\\1</font>\n"));
375
376 QRegularExpression stringLiteralRe("(\".+?\")");
377 contents.replace(stringLiteralRe, 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("Source: %1").arg(m_sourceFileName.midRef(5)));
383 sourceViewer->setParent(this, Qt::Dialog);
384 sourceViewer->setAttribute(Qt::WA_DeleteOnClose);
385 sourceViewer->setLineWrapMode(QTextEdit::NoWrap);
386 sourceViewer->setHtml(html);
387 sourceViewer->resize(600, 600);
388 sourceViewer->show();
389 }