PageRenderTime 309ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/widgets/painting/shared/arthurwidgets.cpp

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