summaryrefslogtreecommitdiffstats
path: root/doc/html/outliner-example.html
blob: ec2f895d955bdd57bbef71276ab855ff8320e1a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/xml/outliner/outliner.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Outliner to show use of DOM</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

 
<p> 
<p> This example presents a small outliner program to show the basic usage of
the <a href="xml.html#dom">DOM classes</a>. The format of the outlines
is the OPML format as described in http://www.opml.org/spec.
<p> This example shows how to load a DOM tree from an XML file and how to
traverse it.
<p> <hr>
<p> Sample XML file (todos.opml):
<p> <pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;opml version="1.0"&gt;
    &lt;head&gt;
        &lt;title&gt;Todo List&lt;/title&gt;
        &lt;dateCreated&gt;Tue, 31 Oct 2000 17:00:17 CET&lt;/dateCreated&gt;
        &lt;dateModified&gt;Tue, 31 Oct 2000 17:00:17 CET&lt;/dateModified&gt;
        &lt;ownerName&gt;Arthur Dent&lt;/ownerName&gt;
        &lt;ownerEmail&gt;info@trolltech.com&lt;/ownerEmail&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;outline text="Background"&gt;
            &lt;outline text="This is an example todo list."/&gt;
        &lt;/outline&gt;
        &lt;outline text="Books to read"&gt;
            &lt;outline text="Science Fiction"&gt;
                &lt;outline text="Philip K. Dick"&gt;
                    &lt;outline text="Do Androids Dream of Electical Sheep?"/&gt;
                    &lt;outline text="The Three Stigmata of Palmer Eldritch"/&gt;
                &lt;/outline&gt;
                &lt;outline text="Robert A. Heinlein"&gt;
                    &lt;outline text="Stranger in a Strange Land"/&gt;
                &lt;/outline&gt;
                &lt;outline text="Isaac Asimov"&gt;
                    &lt;outline text="Foundation and Empire"/&gt;
                &lt;/outline&gt;
            &lt;/outline&gt;
            &lt;outline text="TQt Books (in English)"&gt;
                &lt;outline text="Blanchette &amp;amp; Summerfield: C++ GUI Programming with TQt 3"/&gt;
                &lt;outline text="Dalheimer: Programming with TQt"/&gt;
                &lt;outline text="Griffith: KDE 2/TQt Programming Bible"/&gt;
                &lt;outline text="Hughes: Linux Rapid Application Development"/&gt;
                &lt;outline text="Solin: <a href="ntqt.html">TQt</a> Programming in 24 hours"/&gt;
                &lt;outline text="Ward: <a href="ntqt.html">TQt</a> 2 Programming for Linux and Windows 2000"/&gt;
            &lt;/outline&gt;
        &lt;/outline&gt;
        &lt;outline text="Shopping list"&gt;
            &lt;outline text="General"&gt;
                &lt;outline text="Towel"/&gt;
                &lt;outline text="Hair dryer"/&gt;
                &lt;outline text="Underpants"/&gt;
            &lt;/outline&gt;
            &lt;outline text="For Sunday"&gt;
                &lt;outline text="Beef"/&gt;
                &lt;outline text="Rice"/&gt;
                &lt;outline text="Carrots"/&gt;
                &lt;outline text="Beans"/&gt;
                &lt;outline text="Beer"/&gt;
                &lt;outline text="Wine"/&gt;
                &lt;outline text="Orange juice"/&gt;
            &lt;/outline&gt;
        &lt;/outline&gt;
        &lt;outline text="Write a letter to Ford"&gt;
        &lt;/outline&gt;
    &lt;/body&gt;
&lt;/opml&gt;
</pre>

<p> <hr>
<p> Header file (outlinetree.h):
<p> <pre>/****************************************************************************
** $Id: qt/outlinetree.h   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef OUTLINETREE_H
#define OUTLINETREE_H

#include &lt;<a href="qlistview-h.html">ntqlistview.h</a>&gt;
#include &lt;<a href="tqdom-h.html">tqdom.h</a>&gt;

class OutlineTree : public <a href="ntqlistview.html">TQListView</a>
{
    <a href="metaobjects.html#TQ_OBJECT">TQ_OBJECT</a>

public:
    OutlineTree( const <a href="ntqstring.html">TQString</a> fileName, TQWidget *parent = 0, const char *name = 0 );
    ~OutlineTree();

private:
    <a href="tqdomdocument.html">TQDomDocument</a> domTree;
    void getHeaderInformation( const <a href="tqdomelement.html">TQDomElement</a> &amp;header );
    void buildTree( <a href="qlistviewitem.html">TQListViewItem</a> *parentItem, const <a href="tqdomelement.html">TQDomElement</a> &amp;parentElement );
};

#endif // OUTLINETREE_H
</pre>

<p> <hr>
<p> Implementation (outlinetree.cpp):
<p> <pre>/****************************************************************************
** $Id: qt/outlinetree.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "outlinetree.h"
#include &lt;<a href="qfile-h.html">ntqfile.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">ntqmessagebox.h</a>&gt;

<a name="f522"></a>OutlineTree::OutlineTree( const <a href="ntqstring.html">TQString</a> fileName, TQWidget *parent, const char *name )
    : <a href="ntqlistview.html">TQListView</a>( parent, name )
{
    // div. configuration of the list view
    <a href="ntqlistview.html#addColumn">addColumn</a>( "Outlines" );
    <a href="ntqlistview.html#setSorting">setSorting</a>( -1 );
    <a href="ntqlistview.html#setRootIsDecorated">setRootIsDecorated</a>( TRUE );

    // read the XML file and create DOM tree
    <a href="ntqfile.html">TQFile</a> opmlFile( fileName );
<a name="x1915"></a>    if ( !opmlFile.<a href="ntqfile.html#open">open</a>( <a href="ntqfile.html#open">IO_ReadOnly</a> ) ) {
<a name="x1917"></a>        TQMessageBox::<a href="ntqmessagebox.html#critical">critical</a>( 0,
                <a href="tqobject.html#tr">tr</a>( "Critical Error" ),
                <a href="tqobject.html#tr">tr</a>( "Cannot open file %1" ).arg( fileName ) );
        return;
    }
    if ( !domTree.setContent( &amp;opmlFile ) ) {
        TQMessageBox::<a href="ntqmessagebox.html#critical">critical</a>( 0,
                <a href="tqobject.html#tr">tr</a>( "Critical Error" ),
                <a href="tqobject.html#tr">tr</a>( "Parsing error for file %1" ).arg( fileName ) );
<a name="x1914"></a>        opmlFile.<a href="ntqfile.html#close">close</a>();
        return;
    }
    opmlFile.<a href="ntqfile.html#close">close</a>();

    // get the header information from the DOM
    <a href="tqdomelement.html">TQDomElement</a> root = domTree.documentElement();
    <a href="tqdomnode.html">TQDomNode</a> node;
<a name="x1907"></a>    node = root.<a href="tqdomnode.html#firstChild">firstChild</a>();
<a name="x1909"></a>    while ( !node.<a href="tqdomnode.html#isNull">isNull</a>() ) {
<a name="x1911"></a><a name="x1908"></a>        if ( node.<a href="tqdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="tqdomnode.html#nodeName">nodeName</a>() == "head" ) {
<a name="x1913"></a>            <a href="tqdomelement.html">TQDomElement</a> header = node.<a href="tqdomnode.html#toElement">toElement</a>();
            getHeaderInformation( header );
            break;
        }
<a name="x1910"></a>        node = node.<a href="tqdomnode.html#nextSibling">nextSibling</a>();
    }
    // create the tree view out of the DOM
    node = root.<a href="tqdomnode.html#firstChild">firstChild</a>();
    while ( !node.<a href="tqdomnode.html#isNull">isNull</a>() ) {
        if ( node.<a href="tqdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="tqdomnode.html#nodeName">nodeName</a>() == "body" ) {
            <a href="tqdomelement.html">TQDomElement</a> body = node.<a href="tqdomnode.html#toElement">toElement</a>();
            buildTree( 0, body );
            break;
        }
        node = node.<a href="tqdomnode.html#nextSibling">nextSibling</a>();
    }
}

OutlineTree::~OutlineTree()
{
}

void <a name="f523"></a>OutlineTree::getHeaderInformation( const <a href="tqdomelement.html">TQDomElement</a> &amp;header )
{
    // visit all children of the header element and look if you can make
    // something with it
    <a href="tqdomnode.html">TQDomNode</a> node = header.<a href="tqdomnode.html#firstChild">firstChild</a>();
    while ( !node.<a href="tqdomnode.html#isNull">isNull</a>() ) {
        if ( node.<a href="tqdomnode.html#isElement">isElement</a>() ) {
            // case for the different header entries
            if ( node.<a href="tqdomnode.html#nodeName">nodeName</a>() == "title" ) {
                <a href="tqdomtext.html">TQDomText</a> textChild = node.<a href="tqdomnode.html#firstChild">firstChild</a>().toText();
                if ( !textChild.<a href="tqdomnode.html#isNull">isNull</a>() ) {
<a name="x1912"></a>                    <a href="ntqlistview.html#setColumnText">setColumnText</a>( 0, textChild.<a href="tqdomnode.html#nodeValue">nodeValue</a>() );
                }
            }
        }
        node = node.<a href="tqdomnode.html#nextSibling">nextSibling</a>();
    }
}

void <a name="f524"></a>OutlineTree::buildTree( <a href="qlistviewitem.html">TQListViewItem</a> *parentItem, const <a href="tqdomelement.html">TQDomElement</a> &amp;parentElement )
{
    <a href="qlistviewitem.html">TQListViewItem</a> *thisItem = 0;
    <a href="tqdomnode.html">TQDomNode</a> node = parentElement.<a href="tqdomnode.html#firstChild">firstChild</a>();
    while ( !node.<a href="tqdomnode.html#isNull">isNull</a>() ) {
        if ( node.<a href="tqdomnode.html#isElement">isElement</a>() &amp;&amp; node.<a href="tqdomnode.html#nodeName">nodeName</a>() == "outline" ) {
            // add a new list view item for the outline
            if ( parentItem == 0 )
                thisItem = new <a href="qlistviewitem.html">TQListViewItem</a>( this, thisItem );
            else
                thisItem = new <a href="qlistviewitem.html">TQListViewItem</a>( parentItem, thisItem );
<a name="x1916"></a>            thisItem-&gt;<a href="qlistviewitem.html#setText">setText</a>( 0, node.<a href="tqdomnode.html#toElement">toElement</a>().attribute( "text" ) );
            // recursive build of the tree
            buildTree( thisItem, node.<a href="tqdomnode.html#toElement">toElement</a>() );
        }
        node = node.<a href="tqdomnode.html#nextSibling">nextSibling</a>();
    }
}
</pre>

<p> <hr>
<p> Main (main.cpp):
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp   3.3.8   edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include "outlinetree.h"

int main( int argc, char **argv )
{
    <a href="ntqapplication.html">TQApplication</a> a( argc, argv );

    OutlineTree outline( "todos.opml" );
    a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &amp;outline );
<a name="x1920"></a>    outline.<a href="ntqwidget.html#show">show</a>();

    return a.<a href="ntqapplication.html#exec">exec</a>();
}
</pre>

<p>See also <a href="xml-examples.html">TQt XML Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>