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