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"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { 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 Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main 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 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
|
---|
37 | the <a href="xml.html#dom">DOM classes</a>. The format of the outlines
|
---|
38 | is 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
|
---|
40 | traverse it.
|
---|
41 | <p> <hr>
|
---|
42 | <p> Sample XML file (todos.opml):
|
---|
43 | <p> <pre><?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
44 | <opml version="1.0">
|
---|
45 | <head>
|
---|
46 | <title>Todo List</title>
|
---|
47 | <dateCreated>Tue, 31 Oct 2000 17:00:17 CET</dateCreated>
|
---|
48 | <dateModified>Tue, 31 Oct 2000 17:00:17 CET</dateModified>
|
---|
49 | <ownerName>Arthur Dent</ownerName>
|
---|
50 | <ownerEmail>info@trolltech.com</ownerEmail>
|
---|
51 | </head>
|
---|
52 | <body>
|
---|
53 | <outline text="Background">
|
---|
54 | <outline text="This is an example todo list."/>
|
---|
55 | </outline>
|
---|
56 | <outline text="Books to read">
|
---|
57 | <outline text="Science Fiction">
|
---|
58 | <outline text="Philip K. Dick">
|
---|
59 | <outline text="Do Androids Dream of Electical Sheep?"/>
|
---|
60 | <outline text="The Three Stigmata of Palmer Eldritch"/>
|
---|
61 | </outline>
|
---|
62 | <outline text="Robert A. Heinlein">
|
---|
63 | <outline text="Stranger in a Strange Land"/>
|
---|
64 | </outline>
|
---|
65 | <outline text="Isaac Asimov">
|
---|
66 | <outline text="Foundation and Empire"/>
|
---|
67 | </outline>
|
---|
68 | </outline>
|
---|
69 | <outline text="Qt Books (in English)">
|
---|
70 | <outline text="Blanchette &amp; Summerfield: C++ GUI Programming with Qt 3"/>
|
---|
71 | <outline text="Dalheimer: Programming with Qt"/>
|
---|
72 | <outline text="Griffith: KDE 2/Qt Programming Bible"/>
|
---|
73 | <outline text="Hughes: Linux Rapid Application Development"/>
|
---|
74 | <outline text="Solin: <a href="qt.html">Qt</a> Programming in 24 hours"/>
|
---|
75 | <outline text="Ward: <a href="qt.html">Qt</a> 2 Programming for Linux and Windows 2000"/>
|
---|
76 | </outline>
|
---|
77 | </outline>
|
---|
78 | <outline text="Shopping list">
|
---|
79 | <outline text="General">
|
---|
80 | <outline text="Towel"/>
|
---|
81 | <outline text="Hair dryer"/>
|
---|
82 | <outline text="Underpants"/>
|
---|
83 | </outline>
|
---|
84 | <outline text="For Sunday">
|
---|
85 | <outline text="Beef"/>
|
---|
86 | <outline text="Rice"/>
|
---|
87 | <outline text="Carrots"/>
|
---|
88 | <outline text="Beans"/>
|
---|
89 | <outline text="Beer"/>
|
---|
90 | <outline text="Wine"/>
|
---|
91 | <outline text="Orange juice"/>
|
---|
92 | </outline>
|
---|
93 | </outline>
|
---|
94 | <outline text="Write a letter to Ford">
|
---|
95 | </outline>
|
---|
96 | </body>
|
---|
97 | </opml>
|
---|
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 <<a href="qlistview-h.html">qlistview.h</a>>
|
---|
116 | #include <<a href="qdom-h.html">qdom.h</a>>
|
---|
117 |
|
---|
118 | class OutlineTree : public <a href="qlistview.html">QListView</a>
|
---|
119 | {
|
---|
120 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
121 |
|
---|
122 | public:
|
---|
123 | OutlineTree( const <a href="qstring.html">QString</a> fileName, QWidget *parent = 0, const char *name = 0 );
|
---|
124 | ~OutlineTree();
|
---|
125 |
|
---|
126 | private:
|
---|
127 | <a href="qdomdocument.html">QDomDocument</a> domTree;
|
---|
128 | void getHeaderInformation( const <a href="qdomelement.html">QDomElement</a> &header );
|
---|
129 | void buildTree( <a href="qlistviewitem.html">QListViewItem</a> *parentItem, const <a href="qdomelement.html">QDomElement</a> &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 <<a href="qfile-h.html">qfile.h</a>>
|
---|
149 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
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( &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>() && 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>() && 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 |
|
---|
200 | OutlineTree::~OutlineTree()
|
---|
201 | {
|
---|
202 | }
|
---|
203 |
|
---|
204 | void <a name="f523"></a>OutlineTree::getHeaderInformation( const <a href="qdomelement.html">QDomElement</a> &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 |
|
---|
223 | void <a name="f524"></a>OutlineTree::buildTree( <a href="qlistviewitem.html">QListViewItem</a> *parentItem, const <a href="qdomelement.html">QDomElement</a> &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>() && 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-><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 <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
256 | #include "outlinetree.h"
|
---|
257 |
|
---|
258 | int 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>( &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 © 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>
|
---|