1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "examplecontent.h"
44 #include "menumanager.h"
45 #include "imageitem.h"
46 #include "headingitem.h"
48 ExampleContent::ExampleContent(const QString &name, QGraphicsScene *scene, QGraphicsItem *parent)
49 : DemoItem(scene, parent)
53 this->description = 0;
57 void ExampleContent::prepare()
60 this->prepared = true;
61 this->createContent();
65 void ExampleContent::animationStopped(int id)
67 if (id == DemoItemAnimation::ANIM_OUT){
68 // Free up some memory:
70 delete this->description;
71 delete this->screenshot;
73 this->description = 0;
75 this->prepared = false;
79 QString ExampleContent::loadDescription()
81 QByteArray ba = MenuManager::instance()->getHtml(this->name);
83 int errorLine, errorColumn;
85 QDomDocument exampleDoc;
87 qDebug() << "No documentation found for" << name << "Is the documentation built?";
88 } else if (!exampleDoc.setContent(ba, false, &errorMsg, &errorLine, &errorColumn)) {
89 qDebug() << "Error loading documentation for " << name << ": " << errorMsg << errorLine << errorColumn;
92 QDomNodeList paragraphs = exampleDoc.elementsByTagName("p");
93 if (paragraphs.length() < 1 && Colors::verbose)
94 qDebug() << "- ExampleContent::loadDescription(): Could not load description:"
95 << MenuManager::instance()->info[this->name]["docfile"];
96 QString description = Colors::contentColor + QLatin1String("");
97 //QLatin1String("Could not load description. Ensure that the documentation for Qt is built."); // QTBUG-12522: If there is no description why show an error to the user when qDebug above communications the issue (if it is indeed an issue at all) when demos are built?
98 for (int p = 0; p < int(paragraphs.length()); ++p) {
99 description = this->extractTextFromParagraph(paragraphs.item(p));
100 if (this->isSummary(description)) {
104 return Colors::contentColor + description;
107 bool ExampleContent::isSummary(const QString &text)
109 return (!text.contains("[") &&
110 text.indexOf(QRegExp(QString("(In )?((The|This) )?(%1 )?.*(tutorial|example|demo|application)").arg(this->name),
111 Qt::CaseInsensitive)) != -1);
114 QString ExampleContent::extractTextFromParagraph(const QDomNode &parentNode)
117 QDomNode node = parentNode.firstChild();
119 while (!node.isNull()) {
123 description += Colors::contentColor + node.nodeValue();
124 else if (node.hasChildNodes()) {
125 if (node.nodeName() == "b") {
128 } else if (node.nodeName() == "a") {
129 beginTag = Colors::contentColor;
131 } else if (node.nodeName() == "i") {
134 } else if (node.nodeName() == "tt") {
138 description += beginTag + this->extractTextFromParagraph(node) + endTag;
140 node = node.nextSibling();
146 void ExampleContent::createContent()
149 this->heading = new HeadingItem(this->name, this->scene(), this);
150 this->description = new DemoTextItem(this->loadDescription(), Colors::contentFont(),
151 Colors::heading, 500, this->scene(), this);
152 int imgHeight = 340 - int(this->description->boundingRect().height()) + 50;
153 this->screenshot = new ImageItem(QImage::fromData(MenuManager::instance()->getImage(this->name)),
154 550, imgHeight, this->scene(), this);
156 // Place the items on screen:
157 this->heading->setPos(0, 3);
158 this->description->setPos(0, this->heading->pos().y() + this->heading->boundingRect().height() + 10);
159 this->screenshot->setPos(0, this->description->pos().y() + this->description->boundingRect().height() + 10);
162 QRectF ExampleContent::boundingRect() const
164 return QRectF(0, 0, 500, 100);