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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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
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
99bool 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
105QString 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
137void 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
153QRectF ExampleContent::boundingRect() const
154{
155 return QRectF(0, 0, 500, 100);
156}
157
158
Note: See TracBrowser for help on using the repository browser.