00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "arthurwidgets.h"
00045 #include <QApplication>
00046 #include <QPainter>
00047 #include <QPainterPath>
00048 #include <QPixmapCache>
00049 #include <QtEvents>
00050 #include <QTextDocument>
00051 #include <QAbstractTextDocumentLayout>
00052 #include <QFile>
00053 #include <QTextBrowser>
00054 #include <QBoxLayout>
00055
00056 extern QPixmap cached(const QString &img);
00057
00058 ArthurFrame::ArthurFrame(QWidget *parent)
00059 : QWidget(parent)
00060 , m_prefer_image(false)
00061 {
00062 #ifdef QT_OPENGL_SUPPORT
00063 glw = 0;
00064 m_use_opengl = false;
00065 QGLFormat f = QGLFormat::defaultFormat();
00066 f.setSampleBuffers(true);
00067 f.setStencil(true);
00068 f.setAlpha(true);
00069 f.setAlphaBufferSize(8);
00070 QGLFormat::setDefaultFormat(f);
00071 #endif
00072 m_document = 0;
00073 m_show_doc = false;
00074
00075 m_tile = QPixmap(100, 100);
00076 m_tile.fill(Qt::white);
00077 QPainter pt(&m_tile);
00078 QColor color(240, 240, 240);
00079 pt.fillRect(0, 0, 50, 50, color);
00080 pt.fillRect(50, 50, 50, 50, color);
00081 pt.end();
00082
00083
00084
00085
00086
00087 #ifdef Q_WS_X11
00088 QPixmap xRenderPixmap(1, 1);
00089 m_prefer_image = !xRenderPixmap.x11PictureHandle();
00090 #endif
00091 }
00092
00093
00094 #ifdef QT_OPENGL_SUPPORT
00095 void ArthurFrame::enableOpenGL(bool use_opengl)
00096 {
00097 m_use_opengl = use_opengl;
00098
00099 if (!glw) {
00100 glw = new ArthurGLWidget(this);
00101 glw->setAutoFillBackground(false);
00102 glw->disableAutoBufferSwap();
00103 QApplication::postEvent(this, new QResizeEvent(size(), size()));
00104 }
00105
00106 if (use_opengl) {
00107 glw->show();
00108 } else {
00109 glw->hide();
00110 }
00111 }
00112 #endif
00113
00114 void ArthurFrame::paintEvent(QPaintEvent *e)
00115 {
00116 static QImage *static_image = 0;
00117 QPainter painter;
00118 if (preferImage()
00119 #ifdef QT_OPENGL_SUPPORT
00120 && !m_use_opengl
00121 #endif
00122 ) {
00123 if (!static_image) {
00124 static_image = new QImage(size(), QImage::Format_RGB32);
00125 } else if (static_image->size() != size()) {
00126 delete static_image;
00127 static_image = new QImage(size(), QImage::Format_RGB32);
00128 }
00129 painter.begin(static_image);
00130
00131 int o = 10;
00132
00133 QBrush bg = palette().brush(QPalette::Background);
00134 painter.fillRect(0, 0, o, o, bg);
00135 painter.fillRect(width() - o, 0, o, o, bg);
00136 painter.fillRect(0, height() - o, o, o, bg);
00137 painter.fillRect(width() - o, height() - o, o, o, bg);
00138 } else {
00139 #ifdef QT_OPENGL_SUPPORT
00140 if (m_use_opengl) {
00141 painter.begin(glw);
00142 painter.fillRect(QRectF(0, 0, glw->width(), glw->height()), palette().color(backgroundRole()));
00143 } else {
00144 painter.begin(this);
00145 }
00146 #else
00147 painter.begin(this);
00148 #endif
00149 }
00150
00151 painter.setClipRect(e->rect());
00152
00153 painter.setRenderHint(QPainter::Antialiasing);
00154
00155 QPainterPath clipPath;
00156
00157 QRect r = rect();
00158 double left = r.x() + 1;
00159 double top = r.y() + 1;
00160 double right = r.right();
00161 double bottom = r.bottom();
00162 double radius2 = 8 * 2;
00163
00164 clipPath.moveTo(right - radius2, top);
00165 clipPath.arcTo(right - radius2, top, radius2, radius2, 90, -90);
00166 clipPath.arcTo(right - radius2, bottom - radius2, radius2, radius2, 0, -90);
00167 clipPath.arcTo(left, bottom - radius2, radius2, radius2, 270, -90);
00168 clipPath.arcTo(left, top, radius2, radius2, 180, -90);
00169 clipPath.closeSubpath();
00170
00171 painter.save();
00172 painter.setClipPath(clipPath, Qt::IntersectClip);
00173
00174 painter.drawTiledPixmap(rect(), m_tile);
00175
00176
00177
00178 paint(&painter);
00179
00180 painter.restore();
00181
00182 painter.save();
00183 if (m_show_doc)
00184 paintDescription(&painter);
00185 painter.restore();
00186
00187 int level = 180;
00188 painter.setPen(QPen(QColor(level, level, level), 2));
00189 painter.setBrush(Qt::NoBrush);
00190 painter.drawPath(clipPath);
00191
00192 if (preferImage()
00193 #ifdef QT_OPENGL_SUPPORT
00194 && !m_use_opengl
00195 #endif
00196 ) {
00197 painter.end();
00198 painter.begin(this);
00199 painter.drawImage(e->rect(), *static_image, e->rect());
00200 }
00201
00202 #ifdef QT_OPENGL_SUPPORT
00203 if (m_use_opengl && (inherits("PathDeformRenderer") || inherits("PathStrokeRenderer") || inherits("CompositionRenderer") || m_show_doc))
00204 glw->swapBuffers();
00205 #endif
00206 }
00207
00208 void ArthurFrame::resizeEvent(QResizeEvent *e)
00209 {
00210 #ifdef QT_OPENGL_SUPPORT
00211 if (glw)
00212 glw->setGeometry(0, 0, e->size().width()-1, e->size().height()-1);
00213 #endif
00214 QWidget::resizeEvent(e);
00215 }
00216
00217 void ArthurFrame::setDescriptionEnabled(bool enabled)
00218 {
00219 if (m_show_doc != enabled) {
00220 m_show_doc = enabled;
00221 emit descriptionEnabledChanged(m_show_doc);
00222 update();
00223 }
00224 }
00225
00226 void ArthurFrame::loadDescription(const QString &fileName)
00227 {
00228 QFile textFile(fileName);
00229 QString text;
00230 if (!textFile.open(QFile::ReadOnly))
00231 text = QString("Unable to load resource file: '%1'").arg(fileName);
00232 else
00233 text = textFile.readAll();
00234 setDescription(text);
00235 }
00236
00237
00238 void ArthurFrame::setDescription(const QString &text)
00239 {
00240 m_document = new QTextDocument(this);
00241 m_document->setHtml(text);
00242 }
00243
00244 void ArthurFrame::paintDescription(QPainter *painter)
00245 {
00246 if (!m_document)
00247 return;
00248
00249 int pageWidth = qMax(width() - 100, 100);
00250 int pageHeight = qMax(height() - 100, 100);
00251 if (pageWidth != m_document->pageSize().width()) {
00252 m_document->setPageSize(QSize(pageWidth, pageHeight));
00253 }
00254
00255 QRect textRect(width() / 2 - pageWidth / 2,
00256 height() / 2 - pageHeight / 2,
00257 pageWidth,
00258 pageHeight);
00259 int pad = 10;
00260 QRect clearRect = textRect.adjusted(-pad, -pad, pad, pad);
00261 painter->setPen(Qt::NoPen);
00262 painter->setBrush(QColor(0, 0, 0, 63));
00263 int shade = 10;
00264 painter->drawRect(clearRect.x() + clearRect.width() + 1,
00265 clearRect.y() + shade,
00266 shade,
00267 clearRect.height() + 1);
00268 painter->drawRect(clearRect.x() + shade,
00269 clearRect.y() + clearRect.height() + 1,
00270 clearRect.width() - shade + 1,
00271 shade);
00272
00273 painter->setRenderHint(QPainter::Antialiasing, false);
00274 painter->setBrush(QColor(255, 255, 255, 220));
00275 painter->setPen(Qt::black);
00276 painter->drawRect(clearRect);
00277
00278 painter->setClipRegion(textRect, Qt::IntersectClip);
00279 painter->translate(textRect.topLeft());
00280
00281 QAbstractTextDocumentLayout::PaintContext ctx;
00282
00283 QLinearGradient g(0, 0, 0, textRect.height());
00284 g.setColorAt(0, Qt::black);
00285 g.setColorAt(0.9, Qt::black);
00286 g.setColorAt(1, Qt::transparent);
00287
00288 QPalette pal = palette();
00289 pal.setBrush(QPalette::Text, g);
00290
00291 ctx.palette = pal;
00292 ctx.clip = QRect(0, 0, textRect.width(), textRect.height());
00293 m_document->documentLayout()->draw(painter, ctx);
00294 }
00295
00296 void ArthurFrame::loadSourceFile(const QString &sourceFile)
00297 {
00298 m_sourceFileName = sourceFile;
00299 }
00300
00301 void ArthurFrame::showSource()
00302 {
00303
00304 if (qFindChild<QTextBrowser *>(this))
00305 return;
00306
00307 QString contents;
00308 if (m_sourceFileName.isEmpty()) {
00309 contents = QString("No source for widget: '%1'").arg(objectName());
00310 } else {
00311 QFile f(m_sourceFileName);
00312 if (!f.open(QFile::ReadOnly))
00313 contents = QString("Could not open file: '%1'").arg(m_sourceFileName);
00314 else
00315 contents = f.readAll();
00316 }
00317
00318 contents.replace('&', "&");
00319 contents.replace('<', "<");
00320 contents.replace('>', ">");
00321
00322 QStringList keywords;
00323 keywords << "for " << "if " << "switch " << " int " << "#include " << "const"
00324 << "void " << "uint " << "case " << "double " << "#define " << "static"
00325 << "new" << "this";
00326
00327 foreach (QString keyword, keywords)
00328 contents.replace(keyword, QLatin1String("<font color=olive>") + keyword + QLatin1String("</font>"));
00329 contents.replace("(int ", "(<font color=olive><b>int </b></font>");
00330
00331 QStringList ppKeywords;
00332 ppKeywords << "#ifdef" << "#ifndef" << "#if" << "#endif" << "#else";
00333
00334 foreach (QString keyword, ppKeywords)
00335 contents.replace(keyword, QLatin1String("<font color=navy>") + keyword + QLatin1String("</font>"));
00336
00337 contents.replace(QRegExp("(\\d\\d?)"), QLatin1String("<font color=navy>\\1</font>"));
00338
00339 QRegExp commentRe("(//.+)\\n");
00340 commentRe.setMinimal(true);
00341 contents.replace(commentRe, QLatin1String("<font color=red>\\1</font>\n"));
00342
00343 QRegExp stringLiteralRe("(\".+\")");
00344 stringLiteralRe.setMinimal(true);
00345 contents.replace(stringLiteralRe, QLatin1String("<font color=green>\\1</font>"));
00346
00347 QString html = contents;
00348 html.prepend("<html><pre>");
00349 html.append("</pre></html>");
00350
00351 QTextBrowser *sourceViewer = new QTextBrowser(0);
00352 sourceViewer->setWindowTitle("Source: " + m_sourceFileName.mid(5));
00353 sourceViewer->setParent(this, Qt::Dialog);
00354 sourceViewer->setAttribute(Qt::WA_DeleteOnClose);
00355 sourceViewer->setLineWrapMode(QTextEdit::NoWrap);
00356 sourceViewer->setHtml(html);
00357 sourceViewer->resize(600, 600);
00358 sourceViewer->show();
00359 }