demos/qtdemo/examplecontent.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 demonstration applications 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 
       
    42 #include "examplecontent.h"
       
    43 #include "colors.h"
       
    44 #include "menumanager.h"
       
    45 #include "imageitem.h"
       
    46 #include "headingitem.h"
       
    47 
       
    48 ExampleContent::ExampleContent(const QString &name, QGraphicsScene *scene, QGraphicsItem *parent)
       
    49     : DemoItem(scene, parent)
       
    50 {
       
    51     this->name = name;
       
    52     this->heading = 0;
       
    53     this->description = 0;
       
    54     this->screenshot = 0;
       
    55 }
       
    56 
       
    57 void ExampleContent::prepare()
       
    58 {
       
    59     if (!this->prepared){
       
    60         this->prepared = true;
       
    61         this->createContent();
       
    62     }
       
    63 }
       
    64 
       
    65 void ExampleContent::animationStopped(int id)
       
    66 {
       
    67     if (id == DemoItemAnimation::ANIM_OUT){
       
    68         // Free up some memory:
       
    69         delete this->heading;
       
    70         delete this->description;
       
    71         delete this->screenshot;
       
    72         this->heading = 0;
       
    73         this->description = 0;
       
    74         this->screenshot = 0;
       
    75         this->prepared = false;
       
    76     }
       
    77 }
       
    78 
       
    79 QString ExampleContent::loadDescription()
       
    80 {
       
    81     QByteArray ba = MenuManager::instance()->getHtml(this->name);
       
    82 
       
    83     QDomDocument exampleDoc;
       
    84     exampleDoc.setContent(ba, false);
       
    85 
       
    86     QDomNodeList paragraphs = exampleDoc.elementsByTagName("p");
       
    87     if (paragraphs.length() < 1 && Colors::verbose)
       
    88         qDebug() << "- ExampleContent::loadDescription(): Could not load description:" << MenuManager::instance()->info[this->name]["docfile"];
       
    89     QString description = Colors::contentColor + QLatin1String("Could not load description. Ensure that the documentation for Qt is built.");
       
    90     for (int p = 0; p < int(paragraphs.length()); ++p) {
       
    91         description = this->extractTextFromParagraph(paragraphs.item(p));
       
    92         if (this->isSummary(description)) {
       
    93             break;
       
    94         }
       
    95     }
       
    96     return Colors::contentColor + description;
       
    97 }
       
    98 
       
    99 bool ExampleContent::isSummary(const QString &text)
       
   100 {
       
   101     return (!text.contains("[") &&
       
   102         text.indexOf(QRegExp(QString("(In )?((The|This) )?(%1 )?.*(tutorial|example|demo|application)").arg(this->name), Qt::CaseInsensitive)) != -1);
       
   103 }
       
   104 
       
   105 QString ExampleContent::extractTextFromParagraph(const QDomNode &parentNode)
       
   106 {
       
   107     QString description;
       
   108     QDomNode node = parentNode.firstChild();
       
   109 
       
   110     while (!node.isNull()) {
       
   111         QString beginTag;
       
   112         QString endTag;
       
   113         if (node.isText())
       
   114             description += Colors::contentColor + node.nodeValue();
       
   115         else if (node.hasChildNodes()) {
       
   116             if (node.nodeName() == "b") {
       
   117                 beginTag = "<b>";
       
   118                 endTag = "</b>";
       
   119             } else if (node.nodeName() == "a") {
       
   120                 beginTag = Colors::contentColor;
       
   121                 endTag = "</font>";
       
   122             } else if (node.nodeName() == "i") {
       
   123                 beginTag = "<i>";
       
   124                 endTag = "</i>";
       
   125             } else if (node.nodeName() == "tt") {
       
   126                 beginTag = "<tt>";
       
   127                 endTag = "</tt>";
       
   128             }
       
   129             description += beginTag + this->extractTextFromParagraph(node) + endTag;
       
   130         }
       
   131         node = node.nextSibling();
       
   132     }
       
   133 
       
   134     return description;
       
   135 }
       
   136 
       
   137 void ExampleContent::createContent()
       
   138 {
       
   139     // Create the items:
       
   140     this->heading = new HeadingItem(this->name, this->scene(), this);
       
   141     this->description = new DemoTextItem(this->loadDescription(), Colors::contentFont(),
       
   142         Colors::heading, 500, this->scene(), this);
       
   143     int imgHeight = 340 - int(this->description->boundingRect().height()) + 50;
       
   144     this->screenshot = new ImageItem(QImage::fromData(MenuManager::instance()->getImage(this->name)),
       
   145           550, imgHeight, this->scene(), this);
       
   146 
       
   147     // Place the items on screen:
       
   148     this->heading->setPos(0, 3);
       
   149     this->description->setPos(0, this->heading->pos().y() + this->heading->boundingRect().height() + 10);
       
   150     this->screenshot->setPos(0, this->description->pos().y() + this->description->boundingRect().height() + 10);
       
   151 }
       
   152 
       
   153 QRectF ExampleContent::boundingRect() const
       
   154 {
       
   155     return QRectF(0, 0, 500, 100);
       
   156 }
       
   157 
       
   158