source: trunk/doc/html/outliner-example.html@ 208

Last change on this file since 208 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 12.8 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/xml/outliner/outliner.doc:5 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Outliner to show use of DOM</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Outliner to show use of DOM</h1>
33
34
35<p>
36<p> This example presents a small outliner program to show the basic usage of
37the <a href="xml.html#dom">DOM classes</a>. The format of the outlines
38is the OPML format as described in http://www.opml.org/spec.
39<p> This example shows how to load a DOM tree from an XML file and how to
40traverse it.
41<p> <hr>
42<p> Sample XML file (todos.opml):
43<p> <pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
44&lt;opml version="1.0"&gt;
45 &lt;head&gt;
46 &lt;title&gt;Todo List&lt;/title&gt;
47 &lt;dateCreated&gt;Tue, 31 Oct 2000 17:00:17 CET&lt;/dateCreated&gt;
48 &lt;dateModified&gt;Tue, 31 Oct 2000 17:00:17 CET&lt;/dateModified&gt;
49 &lt;ownerName&gt;Arthur Dent&lt;/ownerName&gt;
50 &lt;ownerEmail&gt;info@trolltech.com&lt;/ownerEmail&gt;
51 &lt;/head&gt;
52 &lt;body&gt;
53 &lt;outline text="Background"&gt;
54 &lt;outline text="This is an example todo list."/&gt;
55 &lt;/outline&gt;
56 &lt;outline text="Books to read"&gt;
57 &lt;outline text="Science Fiction"&gt;
58 &lt;outline text="Philip K. Dick"&gt;
59 &lt;outline text="Do Androids Dream of Electical Sheep?"/&gt;
60 &lt;outline text="The Three Stigmata of Palmer Eldritch"/&gt;
61 &lt;/outline&gt;
62 &lt;outline text="Robert A. Heinlein"&gt;
63 &lt;outline text="Stranger in a Strange Land"/&gt;
64 &lt;/outline&gt;
65 &lt;outline text="Isaac Asimov"&gt;
66 &lt;outline text="Foundation and Empire"/&gt;
67 &lt;/outline&gt;
68 &lt;/outline&gt;
69 &lt;outline text="Qt Books (in English)"&gt;
70 &lt;outline text="Blanchette &amp;amp; Summerfield: C++ GUI Programming with Qt 3"/&gt;
71 &lt;outline text="Dalheimer: Programming with Qt"/&gt;
72 &lt;outline text="Griffith: KDE 2/Qt Programming Bible"/&gt;
73 &lt;outline text="Hughes: Linux Rapid Application Development"/&gt;
74 &lt;outline text="Solin: <a href="qt.html">Qt</a> Programming in 24 hours"/&gt;
75 &lt;outline text="Ward: <a href="qt.html">Qt</a> 2 Programming for Linux and Windows 2000"/&gt;
76 &lt;/outline&gt;
77 &lt;/outline&gt;
78 &lt;outline text="Shopping list"&gt;
79 &lt;outline text="General"&gt;
80 &lt;outline text="Towel"/&gt;
81 &lt;outline text="Hair dryer"/&gt;
82 &lt;outline text="Underpants"/&gt;
83 &lt;/outline&gt;
84 &lt;outline text="For Sunday"&gt;
85 &lt;outline text="Beef"/&gt;
86 &lt;outline text="Rice"/&gt;
87 &lt;outline text="Carrots"/&gt;
88 &lt;outline text="Beans"/&gt;
89 &lt;outline text="Beer"/&gt;
90 &lt;outline text="Wine"/&gt;
91 &lt;outline text="Orange juice"/&gt;
92 &lt;/outline&gt;
93 &lt;/outline&gt;
94 &lt;outline text="Write a letter to Ford"&gt;
95 &lt;/outline&gt;
96 &lt;/body&gt;
97&lt;/opml&gt;
98</pre>
99
100<p> <hr>
101<p> Header file (outlinetree.h):
102<p> <pre>/****************************************************************************
103** $Id: outliner-example.html 2051 2007-02-21 10:04:20Z chehrlic $
104**
105** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
106**
107** This file is part of an example program for Qt. This example
108** program may be used, distributed and modified without limitation.
109**
110*****************************************************************************/
111
112#ifndef OUTLINETREE_H
113#define OUTLINETREE_H
114
115#include &lt;<a href="qlistview-h.html">qlistview.h</a>&gt;
116#include &lt;<a href="qdom-h.html">qdom.h</a>&gt;
117
118class OutlineTree : public <a href="qlistview.html">QListView</a>
119{
120 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
121
122public:
123 OutlineTree( const <a href="qstring.html">QString</a> fileName, QWidget *parent = 0, const char *name = 0 );
124 ~OutlineTree();
125
126private:
127 <a href="qdomdocument.html">QDomDocument</a> domTree;
128 void getHeaderInformation( const <a href="qdomelement.html">QDomElement</a> &amp;header );
129 void buildTree( <a href="qlistviewitem.html">QListViewItem</a> *parentItem, const <a href="qdomelement.html">QDomElement</a> &amp;parentElement );
130};
131
132#endif // OUTLINETREE_H
133</pre>
134
135<p> <hr>
136<p> Implementation (outlinetree.cpp):
137<p> <pre>/****************************************************************************
138** $Id: outliner-example.html 2051 2007-02-21 10:04:20Z chehrlic $
139**
140** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
141**
142** This file is part of an example program for Qt. This example
143** program may be used, distributed and modified without limitation.
144**
145*****************************************************************************/
146
147#include "outlinetree.h"
148#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
149#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
150
151<a name="f522"></a>OutlineTree::OutlineTree( const <a href="qstring.html">QString</a> fileName, QWidget *parent, const char *name )
152 : <a href="qlistview.html">QListView</a>( parent, name )
153{
154 // div. configuration of the list view
155 <a href="qlistview.html#addColumn">addColumn</a>( "Outlines" );
156 <a href="qlistview.html#setSorting">setSorting</a>( -1 );
157 <a href="qlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );
158
159 // read the XML file and create DOM tree
160 <a href="qfile.html">QFile</a> opmlFile( fileName );
161<a name="x1915"></a> if ( !opmlFile.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {
162<a name="x1917"></a> QMessageBox::<a href="qmessagebox.html#critical">critical</a>( 0,
163 <a href="qobject.html#tr">tr</a>( "Critical Error" ),
164 <a href="qobject.html#tr">tr</a>( "Cannot open file %1" ).arg( fileName ) );
165 return;
166 }
167 if ( !domTree.setContent( &amp;opmlFile ) ) {
168 QMessageBox::<a href="qmessagebox.html#critical">critical</a>( 0,
169 <a href="qobject.html#tr">tr</a>( "Critical Error" ),
170 <a href="qobject.html#tr">tr</a>( "Parsing error for file %1" ).arg( fileName ) );
171<a name="x1914"></a> opmlFile.<a href="qfile.html#close">close</a>();
172 return;
173 }
174 opmlFile.<a href="qfile.html#close">close</a>();
175
176 // get the header information from the DOM
177 <a href="qdomelement.html">QDomElement</a> root = domTree.documentElement();
178 <a href="qdomnode.html">QDomNode</a> node;
179<a name="x1907"></a> node = root.<a href="qdomnode.html#firstChild">firstChild</a>();
180<a name="x1909"></a> while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {
181<a name="x1911"></a><a name="x1908"></a> if ( node.<a href="qdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="qdomnode.html#nodeName">nodeName</a>() == "head" ) {
182<a name="x1913"></a> <a href="qdomelement.html">QDomElement</a> header = node.<a href="qdomnode.html#toElement">toElement</a>();
183 getHeaderInformation( header );
184 break;
185 }
186<a name="x1910"></a> node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();
187 }
188 // create the tree view out of the DOM
189 node = root.<a href="qdomnode.html#firstChild">firstChild</a>();
190 while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {
191 if ( node.<a href="qdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="qdomnode.html#nodeName">nodeName</a>() == "body" ) {
192 <a href="qdomelement.html">QDomElement</a> body = node.<a href="qdomnode.html#toElement">toElement</a>();
193 buildTree( 0, body );
194 break;
195 }
196 node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();
197 }
198}
199
200OutlineTree::~OutlineTree()
201{
202}
203
204void <a name="f523"></a>OutlineTree::getHeaderInformation( const <a href="qdomelement.html">QDomElement</a> &amp;header )
205{
206 // visit all children of the header element and look if you can make
207 // something with it
208 <a href="qdomnode.html">QDomNode</a> node = header.<a href="qdomnode.html#firstChild">firstChild</a>();
209 while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {
210 if ( node.<a href="qdomnode.html#isElement">isElement</a>() ) {
211 // case for the different header entries
212 if ( node.<a href="qdomnode.html#nodeName">nodeName</a>() == "title" ) {
213 <a href="qdomtext.html">QDomText</a> textChild = node.<a href="qdomnode.html#firstChild">firstChild</a>().toText();
214 if ( !textChild.<a href="qdomnode.html#isNull">isNull</a>() ) {
215<a name="x1912"></a> <a href="qlistview.html#setColumnText">setColumnText</a>( 0, textChild.<a href="qdomnode.html#nodeValue">nodeValue</a>() );
216 }
217 }
218 }
219 node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();
220 }
221}
222
223void <a name="f524"></a>OutlineTree::buildTree( <a href="qlistviewitem.html">QListViewItem</a> *parentItem, const <a href="qdomelement.html">QDomElement</a> &amp;parentElement )
224{
225 <a href="qlistviewitem.html">QListViewItem</a> *thisItem = 0;
226 <a href="qdomnode.html">QDomNode</a> node = parentElement.<a href="qdomnode.html#firstChild">firstChild</a>();
227 while ( !node.<a href="qdomnode.html#isNull">isNull</a>() ) {
228 if ( node.<a href="qdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="qdomnode.html#nodeName">nodeName</a>() == "outline" ) {
229 // add a new list view item for the outline
230 if ( parentItem == 0 )
231 thisItem = new <a href="qlistviewitem.html">QListViewItem</a>( this, thisItem );
232 else
233 thisItem = new <a href="qlistviewitem.html">QListViewItem</a>( parentItem, thisItem );
234<a name="x1916"></a> thisItem-&gt;<a href="qlistviewitem.html#setText">setText</a>( 0, node.<a href="qdomnode.html#toElement">toElement</a>().attribute( "text" ) );
235 // recursive build of the tree
236 buildTree( thisItem, node.<a href="qdomnode.html#toElement">toElement</a>() );
237 }
238 node = node.<a href="qdomnode.html#nextSibling">nextSibling</a>();
239 }
240}
241</pre>
242
243<p> <hr>
244<p> Main (main.cpp):
245<p> <pre>/****************************************************************************
246** $Id: outliner-example.html 2051 2007-02-21 10:04:20Z chehrlic $
247**
248** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
249**
250** This file is part of an example program for Qt. This example
251** program may be used, distributed and modified without limitation.
252**
253*****************************************************************************/
254
255#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
256#include "outlinetree.h"
257
258int main( int argc, char **argv )
259{
260 <a href="qapplication.html">QApplication</a> a( argc, argv );
261
262 OutlineTree outline( "todos.opml" );
263 a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;outline );
264<a name="x1920"></a> outline.<a href="qwidget.html#show">show</a>();
265
266 return a.<a href="qapplication.html#exec">exec</a>();
267}
268</pre>
269
270<p>See also <a href="xml-examples.html">Qt XML Examples</a>.
271
272<!-- eof -->
273<p><address><hr><div align=center>
274<table width=100% cellspacing=0 border=0><tr>
275<td>Copyright &copy; 2007
276<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
277<td align=right><div align=right>Qt 3.3.8</div>
278</table></div></address></body>
279</html>
Note: See TracBrowser for help on using the repository browser.