summaryrefslogtreecommitdiffstats
path: root/examples/xml/outliner/outlinetree.cpp
blob: 965d0c0e8b5cdf7c9afaff65896878acb8f2dbf9 (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
/****************************************************************************
**
** Copyright (C) 1992-2008 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 <tqfile.h>
#include <ntqmessagebox.h>

OutlineTree::OutlineTree( const TQString fileName, TQWidget *parent, const char *name )
    : TQListView( parent, name )
{
    // div. configuration of the list view
    addColumn( "Outlines" );
    setSorting( -1 );
    setRootIsDecorated( TRUE );

    // read the XML file and create DOM tree
    TQFile opmlFile( fileName );
    if ( !opmlFile.open( IO_ReadOnly ) ) {
	TQMessageBox::critical( 0,
		tr( "Critical Error" ),
		tr( "Cannot open file %1" ).arg( fileName ) );
	return;
    }
    if ( !domTree.setContent( &opmlFile ) ) {
	TQMessageBox::critical( 0,
		tr( "Critical Error" ),
		tr( "Parsing error for file %1" ).arg( fileName ) );
	opmlFile.close();
	return;
    }
    opmlFile.close();

    // get the header information from the DOM
    TQDomElement root = domTree.documentElement();
    TQDomNode node;
    node = root.firstChild();
    while ( !node.isNull() ) {
	if ( node.isElement() && node.nodeName() == "head" ) {
	    TQDomElement header = node.toElement();
	    getHeaderInformation( header );
	    break;
	}
	node = node.nextSibling();
    }
    // create the tree view out of the DOM
    node = root.firstChild();
    while ( !node.isNull() ) {
	if ( node.isElement() && node.nodeName() == "body" ) {
	    TQDomElement body = node.toElement();
	    buildTree( 0, body );
	    break;
	}
	node = node.nextSibling();
    }
}

OutlineTree::~OutlineTree()
{
}

void OutlineTree::getHeaderInformation( const TQDomElement &header )
{
    // visit all children of the header element and look if you can make
    // something with it
    TQDomNode node = header.firstChild();
    while ( !node.isNull() ) {
	if ( node.isElement() ) {
	    // case for the different header entries
	    if ( node.nodeName() == "title" ) {
		TQDomText textChild = node.firstChild().toText();
		if ( !textChild.isNull() ) {
		    setColumnText( 0, textChild.nodeValue() );
		}
	    }
	}
	node = node.nextSibling();
    }
}

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