source: trunk/demos/qtdemo/examplecontent.cpp@ 858

Last change on this file since 858 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
48ExampleContent::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
57void ExampleContent::prepare()
58{
59 if (!this->prepared){
60 this->prepared = true;
61 this->createContent();
62 }
63}
64
65void 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
79QString ExampleContent::loadDescription()
80{
81 QByteArray ba = MenuManager::instance()->getHtml(this->name);
82 QString errorMsg;
83 int errorLine, errorColumn;
84
85 QDomDocument exampleDoc;
86 if (!exampleDoc.setContent(ba, false, &errorMsg, &errorLine, &errorColumn)) {
87 qDebug() << errorMsg << errorLine << errorColumn;
88 }
89
90 QDomNodeList paragraphs = exampleDoc.elementsByTagName("p");
91 if (paragraphs.length() < 1 && Colors::verbose)
92 qDebug() << "- ExampleContent::loadDescription(): Could not load description:"
93 << MenuManager::instance()->info[this->name]["docfile"];
94 QString description = Colors::contentColor + QLatin1String("");
95 //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?
96 for (int p = 0; p < int(paragraphs.length()); ++p) {
97 description = this->extractTextFromParagraph(paragraphs.item(p));
98 if (this->isSummary(description)) {
99 break;
100 }
101 }
102 return Colors::contentColor + description;
103}
104
105bool ExampleContent::isSummary(const QString &text)
106{
107 return (!text.contains("[") &&
108 text.indexOf(QRegExp(QString("(In )?((The|This) )?(%1 )?.*(tutorial|example|demo|application)").arg(this->name),
109 Qt::CaseInsensitive)) != -1);
110}
111
112QString ExampleContent::extractTextFromParagraph(const QDomNode &parentNode)
113{
114 QString description;
115 QDomNode node = parentNode.firstChild();
116
117 while (!node.isNull()) {
118 QString beginTag;
119 QString endTag;
120 if (node.isText())
121 description += Colors::contentColor + node.nodeValue();
122 else if (node.hasChildNodes()) {
123 if (node.nodeName() == "b") {
124 beginTag = "<b>";
125 endTag = "</b>";
126 } else if (node.nodeName() == "a") {
127 beginTag = Colors::contentColor;
128 endTag = "</font>";
129 } else if (node.nodeName() == "i") {
130 beginTag = "<i>";
131 endTag = "</i>";
132 } else if (node.nodeName() == "tt") {
133 beginTag = "<tt>";
134 endTag = "</tt>";
135 }
136 description += beginTag + this->extractTextFromParagraph(node) + endTag;
137 }
138 node = node.nextSibling();
139 }
140
141 return description;
142}
143
144void ExampleContent::createContent()
145{
146 // Create the items:
147 this->heading = new HeadingItem(this->name, this->scene(), this);
148 this->description = new DemoTextItem(this->loadDescription(), Colors::contentFont(),
149 Colors::heading, 500, this->scene(), this);
150 int imgHeight = 340 - int(this->description->boundingRect().height()) + 50;
151 this->screenshot = new ImageItem(QImage::fromData(MenuManager::instance()->getImage(this->name)),
152 550, imgHeight, this->scene(), this);
153
154 // Place the items on screen:
155 this->heading->setPos(0, 3);
156 this->description->setPos(0, this->heading->pos().y() + this->heading->boundingRect().height() + 10);
157 this->screenshot->setPos(0, this->description->pos().y() + this->description->boundingRect().height() + 10);
158}
159
160QRectF ExampleContent::boundingRect() const
161{
162 return QRectF(0, 0, 500, 100);
163}
164
165
Note: See TracBrowser for help on using the repository browser.