tests/arthur/shower/shower.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "shower.h"
       
    42 
       
    43 #include "qengines.h"
       
    44 
       
    45 #include <QApplication>
       
    46 #include <QSvgRenderer>
       
    47 #include <QPainter>
       
    48 #include <QPaintEvent>
       
    49 #include <QFile>
       
    50 #include <QTextStream>
       
    51 #include <QTemporaryFile>
       
    52 #include <QDir>
       
    53 #include <QtDebug>
       
    54 
       
    55 static QString loadFile(const QString &name)
       
    56 {
       
    57     QFile file(name);
       
    58     if (!file.open(QFile::ReadOnly)) {
       
    59         qDebug("Can't open file '%s'", qPrintable(name));
       
    60         return QString();
       
    61     }
       
    62     QTextStream str(&file);
       
    63     return str.readAll();
       
    64 }
       
    65 
       
    66 Shower::Shower(const QString &file,
       
    67                const QString &engineName)
       
    68     : QWidget(0)
       
    69 {
       
    70     foreach(QEngine *qengine, QtEngines::self()->engines()) {
       
    71         if (qengine->name() == engineName) {
       
    72             engine = qengine;
       
    73             break;
       
    74         }
       
    75     }
       
    76 
       
    77     QFileInfo fi(file);
       
    78     baseDataDir = fi.absolutePath();
       
    79     if (file.endsWith("svg")) {
       
    80         renderer = new QSvgRenderer(this);
       
    81         renderer->load(file);
       
    82     } else {
       
    83         qps = QFileInfo(file);
       
    84         QString script = loadFile(qps.absoluteFilePath());
       
    85         qpsScript = script.split("\n", QString::SkipEmptyParts);
       
    86         renderer = 0;
       
    87         if (qpsScript.isEmpty()) {
       
    88             printf("failed to read file: '%s'\n", qPrintable(qps.fileName()));
       
    89             return;
       
    90         }
       
    91     }
       
    92 }
       
    93 
       
    94 
       
    95 QSize Shower::sizeHint() const
       
    96 {
       
    97     return QSize(600, 600);
       
    98 }
       
    99 
       
   100 
       
   101 void Shower::paintEvent(QPaintEvent *)
       
   102 {
       
   103     if (buffer.size() != size()) {
       
   104         buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
       
   105         QPainter p(&buffer);
       
   106         p.setViewport(0, 0, width(), height());
       
   107         p.eraseRect(0, 0, width(), height());
       
   108         engine->prepare(size());
       
   109         if (renderer) {
       
   110             engine->render(renderer, QString("sample"));
       
   111         } else {
       
   112             engine->render(qpsScript, qps.absoluteFilePath());
       
   113         }
       
   114         if (!engine->drawOnPainter(&p)) {
       
   115             QString tempFileName = QString("%1sample.png").arg(QDir::tempPath());
       
   116             engine->save(tempFileName);
       
   117             QImage img(tempFileName);
       
   118             engine->cleanup();
       
   119             QFile::remove(tempFileName);
       
   120             p.drawImage(0, 0, img);
       
   121         }
       
   122     }
       
   123     QPainter pt(this);
       
   124     pt.drawImage(0, 0, buffer);
       
   125 }