source: branches/4.5.1/demos/shared/arthurwidgets.cpp@ 921

Last change on this file since 921 was 514, checked in by Dmitry A. Kuminov, 15 years ago

demos/shared: Don't depend on the private header on platforms that do not need it [vendor bug].

File size: 11.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "arthurwidgets.h"
43#include <QApplication>
44#include <QPainter>
45#include <QPainterPath>
46#include <QPixmapCache>
47#include <QtEvents>
48#include <QTextDocument>
49#include <QAbstractTextDocumentLayout>
50#include <QFile>
51#include <QTextBrowser>
52#include <QBoxLayout>
53
54#ifdef Q_WS_X11
55#include <private/qpixmapdata_p.h>
56#endif
57
58extern QPixmap cached(const QString &img);
59
60ArthurFrame::ArthurFrame(QWidget *parent)
61 : QWidget(parent)
62 , m_prefer_image(false)
63{
64#ifdef QT_OPENGL_SUPPORT
65 glw = 0;
66 m_use_opengl = false;
67 QGLFormat f = QGLFormat::defaultFormat();
68 f.setSampleBuffers(true);
69 f.setStencil(true);
70 f.setAlpha(true);
71 f.setAlphaBufferSize(8);
72 QGLFormat::setDefaultFormat(f);
73#endif
74 m_document = 0;
75 m_show_doc = false;
76
77 m_tile = QPixmap(128, 128);
78 m_tile.fill(Qt::white);
79 QPainter pt(&m_tile);
80 QColor color(230, 230, 230);
81 pt.fillRect(0, 0, 64, 64, color);
82 pt.fillRect(64, 64, 64, 64, color);
83 pt.end();
84
85// QPalette pal = palette();
86// pal.setBrush(backgroundRole(), m_tile);
87// setPalette(pal);
88
89#ifdef Q_WS_X11
90 QPixmap xRenderPixmap(1, 1);
91 m_prefer_image = xRenderPixmap.pixmapData()->classId() == QPixmapData::X11Class && !xRenderPixmap.x11PictureHandle();
92#endif
93}
94
95
96#ifdef QT_OPENGL_SUPPORT
97void ArthurFrame::enableOpenGL(bool use_opengl)
98{
99 m_use_opengl = use_opengl;
100
101 if (!glw) {
102 glw = new GLWidget(this);
103 glw->setAutoFillBackground(false);
104 glw->disableAutoBufferSwap();
105 QApplication::postEvent(this, new QResizeEvent(size(), size()));
106 }
107
108 if (use_opengl) {
109 glw->show();
110 } else {
111 glw->hide();
112 }
113
114 update();
115}
116#endif
117
118void ArthurFrame::paintEvent(QPaintEvent *e)
119{
120#ifdef Q_WS_QWS
121 static QPixmap *static_image = 0;
122#else
123 static QImage *static_image = 0;
124#endif
125 QPainter painter;
126 if (preferImage()
127#ifdef QT_OPENGL_SUPPORT
128 && !m_use_opengl
129#endif
130 ) {
131 if (!static_image || static_image->size() != size()) {
132 delete static_image;
133#ifdef Q_WS_QWS
134 static_image = new QPixmap(size());
135#else
136 static_image = new QImage(size(), QImage::Format_RGB32);
137#endif
138 }
139 painter.begin(static_image);
140
141 int o = 10;
142
143 QBrush bg = palette().brush(QPalette::Background);
144 painter.fillRect(0, 0, o, o, bg);
145 painter.fillRect(width() - o, 0, o, o, bg);
146 painter.fillRect(0, height() - o, o, o, bg);
147 painter.fillRect(width() - o, height() - o, o, o, bg);
148 } else {
149#ifdef QT_OPENGL_SUPPORT
150 if (m_use_opengl) {
151 painter.begin(glw);
152 painter.fillRect(QRectF(0, 0, glw->width(), glw->height()), palette().color(backgroundRole()));
153 } else {
154 painter.begin(this);
155 }
156#else
157 painter.begin(this);
158#endif
159 }
160
161 painter.setClipRect(e->rect());
162
163 painter.setRenderHint(QPainter::Antialiasing);
164
165 QPainterPath clipPath;
166
167 QRect r = rect();
168 qreal left = r.x() + 1;
169 qreal top = r.y() + 1;
170 qreal right = r.right();
171 qreal bottom = r.bottom();
172 qreal radius2 = 8 * 2;
173
174 clipPath.moveTo(right - radius2, top);
175 clipPath.arcTo(right - radius2, top, radius2, radius2, 90, -90);
176 clipPath.arcTo(right - radius2, bottom - radius2, radius2, radius2, 0, -90);
177 clipPath.arcTo(left, bottom - radius2, radius2, radius2, 270, -90);
178 clipPath.arcTo(left, top, radius2, radius2, 180, -90);
179 clipPath.closeSubpath();
180
181 painter.save();
182 painter.setClipPath(clipPath, Qt::IntersectClip);
183
184 painter.drawTiledPixmap(rect(), m_tile);
185
186 // client painting
187
188 paint(&painter);
189
190 painter.restore();
191
192 painter.save();
193 if (m_show_doc)
194 paintDescription(&painter);
195 painter.restore();
196
197 int level = 180;
198 painter.setPen(QPen(QColor(level, level, level), 2));
199 painter.setBrush(Qt::NoBrush);
200 painter.drawPath(clipPath);
201
202 if (preferImage()