1 | /****************************************************************************
|
---|
2 | ** $Id: outlinetree.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include "outlinetree.h"
|
---|
12 | #include <qfile.h>
|
---|
13 | #include <qmessagebox.h>
|
---|
14 |
|
---|
15 | OutlineTree::OutlineTree( const QString fileName, QWidget *parent, const char *name )
|
---|
16 | : QListView( parent, name )
|
---|
17 | {
|
---|
18 | // div. configuration of the list view
|
---|
19 | addColumn( "Outlines" );
|
---|
20 | setSorting( -1 );
|
---|
21 | setRootIsDecorated( TRUE );
|
---|
22 |
|
---|
23 | // read the XML file and create DOM tree
|
---|
24 | QFile opmlFile( fileName );
|
---|
25 | if ( !opmlFile.open( IO_ReadOnly ) ) {
|
---|
26 | QMessageBox::critical( 0,
|
---|
27 | tr( "Critical Error" ),
|
---|
28 | tr( "Cannot open file %1" ).arg( fileName ) );
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | if ( !domTree.setContent( &opmlFile ) ) {
|
---|
32 | QMessageBox::critical( 0,
|
---|
33 | tr( "Critical Error" ),
|
---|
34 | tr( "Parsing error for file %1" ).arg( fileName ) );
|
---|
35 | opmlFile.close();
|
---|
36 | return;
|
---|
37 | }
|
---|
38 | opmlFile.close();
|
---|
39 |
|
---|
40 | // get the header information from the DOM
|
---|
41 | QDomElement root = domTree.documentElement();
|
---|
42 | QDomNode node;
|
---|
43 | node = root.firstChild();
|
---|
44 | while ( !node.isNull() ) {
|
---|
45 | if ( node.isElement() && node.nodeName() == "head" ) {
|
---|
46 | QDomElement header = node.toElement();
|
---|
47 | getHeaderInformation( header );
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | node = node.nextSibling();
|
---|
51 | }
|
---|
52 | // create the tree view out of the DOM
|
---|
53 | node = root.firstChild();
|
---|
54 | while ( !node.isNull() ) {
|
---|
55 | if ( node.isElement() && node.nodeName() == "body" ) {
|
---|
56 | QDomElement body = node.toElement();
|
---|
57 | buildTree( 0, body );
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | node = node.nextSibling();
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | OutlineTree::~OutlineTree()
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | void OutlineTree::getHeaderInformation( const QDomElement &header )
|
---|
69 | {
|
---|
70 | // visit all children of the header element and look if you can make
|
---|
71 | // something with it
|
---|
72 | QDomNode node = header.firstChild();
|
---|
73 | while ( !node.isNull() ) {
|
---|
74 | if ( node.isElement() ) {
|
---|
75 | // case for the different header entries
|
---|
76 | if ( node.nodeName() == "title" ) {
|
---|
77 | QDomText textChild = node.firstChild().toText();
|
---|
78 | if ( !textChild.isNull() ) {
|
---|
79 | setColumnText( 0, textChild.nodeValue() );
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | node = node.nextSibling();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | void OutlineTree::buildTree( QListViewItem *parentItem, const QDomElement &parentElement )
|
---|
88 | {
|
---|
89 | QListViewItem *thisItem = 0;
|
---|
90 | QDomNode node = parentElement.firstChild();
|
---|
91 | while ( !node.isNull() ) {
|
---|
92 | if ( node.isElement() && node.nodeName() == "outline" ) {
|
---|
93 | // add a new list view item for the outline
|
---|
94 | if ( parentItem == 0 )
|
---|
95 | thisItem = new QListViewItem( this, thisItem );
|
---|
96 | else
|
---|
97 | thisItem = new QListViewItem( parentItem, thisItem );
|
---|
98 | thisItem->setText( 0, node.toElement().attribute( "text" ) );
|
---|
99 | // recursive build of the tree
|
---|
100 | buildTree( thisItem, node.toElement() );
|
---|
101 | }
|
---|
102 | node = node.nextSibling();
|
---|
103 | }
|
---|
104 | }
|
---|