Debian Codegraph @

examples/widgets/painting/shared/arthurwidgets.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "arthurwidgets.h"
5 #include <QApplication>
6 #include <QPainter>
7 #include <QPainterPath>
8 #include <QPixmapCache>
9 #include <QtEvents>
10 #include <QTextDocument>
11 #include <QAbstractTextDocumentLayout>
12 #include <QFile>
13 #include <QTextBrowser>
14 #include <QBoxLayout>
15 #include <QRegularExpression>
16 #include <QOffscreenSurface>
17
18 extern QPixmap cached(const QString &img);
19
20 ArthurFrame::ArthurFrame(QWidget *parent)
21 : QWidget(parent),
22 m_tile(QPixmap(128, 128))
23 {
24 m_tile.fill(Qt::white);
25 QPainter pt(&m_tile);
26 QColor color(230, 230, 230);
27 pt.fillRect(0, 0, 64, 64, color);
28 pt.fillRect(64, 64, 64, 64, color);
29 pt.end();
30 }
31
32 void ArthurFrame::paintEvent(QPaintEvent *e)
33 {
34 static QImage *static_image = nullptr;
35
36 QPainter painter;
37
38 if (preferImage()) {
39 if (!static_image || static_image->size() != size()) {
40 delete static_image;
41 static_image = new QImage(size(), QImage::Format_RGB32);
42 }
43 painter.begin(static_image);
44
45 int o = 10;
46
47 QBrush bg = palette().brush(QPalette::Window);
48 painter.fillRect(0, 0, o, o, bg);
49 painter.fillRect(width() - o, 0, o, o, bg);
50 painter.fillRect(0, height() - o, o, o, bg);
51 painter.fillRect(width() - o, height() - o, o, o, bg);
52 } else {
53 painter.begin(this);
54 }
55
56 painter.setClipRect(e->rect());
57
58 painter.setRenderHint(QPainter::Antialiasing);
59
60 QPainterPath clipPath;
61
62 QRect r = rect();
63 qreal left = r.x() + 1;
64 qreal top = r.y() + 1;
65 qreal right = r.right();
66 qreal bottom = r.bottom();
67 qreal radius2 = 8 * 2;
68
69 clipPath.moveTo(right - radius2, top);
70 clipPath.arcTo(right - radius2, top, radius2, radius2, 90, -90);
71 clipPath.arcTo(right - radius2, bottom - radius2, radius2, radius2, 0, -90);
72 clipPath.arcTo(left, bottom - radius2, radius2, radius2, 270, -90);
73 clipPath.arcTo(left, top, radius2, radius2, 180, -90);
74 clipPath.closeSubpath();
75
76 painter.save();
77 painter.setClipPath(clipPath, Qt::IntersectClip);
78
79 painter.drawTiledPixmap(rect(), m_tile);
80
81 // client painting
82
83 paint(&painter);
84
85 painter.restore();
86
87 painter.save();
88 if (m_showDoc)
89 paintDescription(&painter);
90 painter.restore();
91
92 int level = 180;
93 painter.setPen(QPen(QColor(level, level, level), 2));
94 painter.setBrush(Qt::NoBrush);
95 painter.drawPath(clipPath);
96
97 if (preferImage()) {
98 painter.end();
99 painter.begin(this);
100 painter.drawImage(e->rect(), *static_image, e->rect());
101 }
102 }
103
104 void ArthurFrame::resizeEvent(QResizeEvent *e)
105 {
106 QWidget::resizeEvent(e);
107 }
108
109 void ArthurFrame::setDescriptionEnabled(bool enabled)
110 {
111 if (m_showDoc != enabled) {
112 m_showDoc = enabled;
113 emit descriptionEnabledChanged(m_showDoc);
114 update();
115 }
116 }
117
118 void ArthurFrame::loadDescription(const QString &fileName)
119 {
120 QFile textFile(fileName);
121 QString text;
122 if (!textFile.open(QFile::ReadOnly))
123 text = QString("Unable to load resource file: '%1'").arg(fileName);
124 else
125 text = textFile.readAll();
126 setDescription(text);
127 }
128
129
130 void ArthurFrame::setDescription(const QString &text)
131 {
132 m_document = new QTextDocument(this);
133 m_document->setHtml(text);
134 }
135
136 void ArthurFrame::paintDescription(QPainter *painter)
137 {
138 if (!m_document)
139 return;
140
141 int pageWidth = qMax(width() - 100, 100);
142 int pageHeight = qMax(height() - 100, 100);
143 if (pageWidth != m_document->pageSize().width())
144 m_document->setPageSize(QSize(pageWidth, pageHeight));
145
146 QRect textRect(width() / 2 - pageWidth / 2,
147 height() / 2 - pageHeight / 2,
148 pageWidth,
149 pageHeight);
150 int pad = 10;
151 QRect clearRect = textRect.adjusted(-pad, -pad, pad, pad);
152 painter->setPen(Qt::NoPen);
153 painter->setBrush(QColor(0, 0, 0, 63));
154 int shade = 10;
155 painter->drawRect(clearRect.x() + clearRect.width() + 1,
156 clearRect.y() + shade,
157 shade,
158 clearRect.height() + 1);
159 painter->drawRect(clearRect.x() + shade,
160 clearRect.y() + clearRect.height() + 1,
161 clearRect.width() - shade + 1,
162 shade);
163
164 painter->setRenderHint(QPainter::Antialiasing, false);
165 painter->setBrush(QColor(255, 255, 255, 220));
166 painter->setPen(Qt::black);
167 painter->drawRect(clearRect);
168
169 painter->setClipRegion(textRect, Qt::IntersectClip);
170 painter->translate(textRect.topLeft());
171
172 QAbstractTextDocumentLayout::PaintContext ctx;
173
174 QLinearGradient g(0, 0, 0, textRect.height());
175 g.setColorAt(0, Qt::black);
176 g.setColorAt(0.9, Qt::black);
177 g.setColorAt(1, Qt::transparent);
178
179 QPalette pal = palette();
180 pal.setBrush(QPalette::Text, g);
181
182 ctx.palette = pal;
183 ctx.clip = QRect(0, 0, textRect.width(), textRect.height());
184 m_document->documentLayout()->draw(painter, ctx);
185 }
186
187 void ArthurFrame::loadSourceFile(const QString &sourceFile)
188 {
189 m_sourceFileName = sourceFile;
190 }
191
192 void ArthurFrame::showSource()
193 {
194 // Check for existing source
195 if (findChild<QTextBrowser *>())
196 return;
197
198 QString contents;
199 if (m_sourceFileName.isEmpty()) {
200 contents = tr("No source for widget: '%1'").arg(objectName());
201 } else {
202 QFile f(m_sourceFileName);
203 if (!f.open(QFile::ReadOnly))
204 contents = tr("Could not open file: '%1'").arg(m_sourceFileName);
205 else
206 contents = QString::fromUtf8(f.readAll());
207 }
208
209 contents.replace(QLatin1Char('&'), QStringLiteral("&amp;"));
210 contents.replace(QLatin1Char('<'), QStringLiteral("&lt;"));
211 contents.replace(QLatin1Char('>'), QStringLiteral("&gt;"));
212
213 static const QString keywords[] = {
214 QStringLiteral("for "), QStringLiteral("if "),
215 QStringLiteral("switch "), QStringLiteral(" int "),
216 QStringLiteral("#include "), QStringLiteral("const"),
217 QStringLiteral("void "), QStringLiteral("uint "),
218 QStringLiteral("case "), QStringLiteral("double "),
219 QStringLiteral("#define "), QStringLiteral("static"),
220 QStringLiteral("new"), QStringLiteral("this")
221 };
222
223 for (const QString &keyword : keywords)
224 contents.replace(keyword, QLatin1String("<font color=olive>") + keyword + QLatin1String("</font>"));
225 contents.replace(QStringLiteral("(int "), QStringLiteral("(<font color=olive><b>int </b></font>"));
226
227 static const QString ppKeywords[] = {
228 QStringLiteral("#ifdef"), QStringLiteral("#ifndef"),
229 QStringLiteral("#if"), QStringLiteral("#endif"),
230 QStringLiteral("#else")
231 };
232
233 for (const QString &keyword : ppKeywords)
234 contents.replace(keyword, QLatin1String("<font color=navy>") + keyword + QLatin1String("</font>"));
235
236 contents.replace(QRegularExpression("(\\d\\d?)"), QLatin1String("<font color=navy>\\1</font>"));
237
238 QRegularExpression commentRe("(//.+?)\\n");
239 contents.replace(commentRe, QLatin1String("<font color=red>\\1</font>\n"));
240
241 QRegularExpression stringLiteralRe("(\".+?\")");
242 contents.replace(stringLiteralRe, QLatin1String("<font color=green>\\1</font>"));
243
244 const QString html = QStringLiteral("<html><pre>") + contents + QStringLiteral("</pre></html>");
245
246 QTextBrowser *sourceViewer = new QTextBrowser;
247 sourceViewer->setWindowTitle(tr("Source: %1").arg(QStringView{ m_sourceFileName }.mid(5)));
248 sourceViewer->setParent(this, Qt::Dialog);
249 sourceViewer->setAttribute(Qt::WA_DeleteOnClose);
250 sourceViewer->setLineWrapMode(QTextEdit::NoWrap);
251 sourceViewer->setHtml(html);
252 sourceViewer->resize(600, 600);
253 sourceViewer->show();
254 }