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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/***************************************************************************
kxe_treeviewitem.cpp - description
-------------------
begin : Wed Nov 21 2001
copyright : (C) 2001, 2002, 2003 by The KXMLEditor Team
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "kxe_treeviewitem.h"
#include "kxmleditorfactory.h"
#include "kxeconfiguration.h"
#include "kxetreeviewsettings.h"
#include "qdom_add.h"
#include <tqregexp.h>
#include <tdelistview.h>
#include <kdebug.h>
#include <kiconloader.h>
KXE_TreeViewItem::KXE_TreeViewItem( const TQDomNode & xmlNode, TQListViewItem * pParent )
: TQListViewItem(pParent),
m_xmlNode(xmlNode),
m_pPrevSibling(0),
m_bBookmarked(false),
m_bChildsCreated(false),
m_bGrandChildsCreated(false)
{
init();
initChilds();
}
KXE_TreeViewItem::KXE_TreeViewItem( const TQDomNode & xmlNode, TDEListView * pParent, TQListViewItem* pAfter)
: TQListViewItem(pParent,pAfter),
m_xmlNode(xmlNode),
m_pPrevSibling(0),
m_bBookmarked(false),
m_bChildsCreated(false),
m_bGrandChildsCreated(false)
{
init();
initChilds();
}
KXE_TreeViewItem::KXE_TreeViewItem( const TQDomNode & xmlNode, TQListViewItem * pParent, TQListViewItem * pAfter )
: TQListViewItem( pParent, pAfter ),
m_xmlNode(xmlNode),
m_pPrevSibling(0),
m_bBookmarked(false),
m_bChildsCreated(false),
m_bGrandChildsCreated(false)
{
init();
initChilds();
}
KXE_TreeViewItem::~KXE_TreeViewItem()
{
// inform the next sibling (if there's any) about destroying this item (as its previous sibling)
KXE_TreeViewItem * pNextItem = static_cast <KXE_TreeViewItem*> (nextSibling());
if ( pNextItem )
{ pNextItem->setPrevSibling(m_pPrevSibling);
}
}
void KXE_TreeViewItem::init()
{
// A tree view item can only be in-place renameable, if it represents an XML element
// (the final check occurs in KXE_TreeViewItem::startRename).
if ( m_xmlNode.isElement() )
setRenameEnabled( 0, true );
// inform the next sibling (if there's any) about this item (as its previous sibling)
KXE_TreeViewItem * pNextItem = static_cast <KXE_TreeViewItem*> (nextSibling());
if ( pNextItem )
pNextItem->setPrevSibling(this);
setPixmap(0, domTool_getIconForNodeType(m_xmlNode.nodeType(), false));
if ( domTool_getLevel(m_xmlNode) < (unsigned int)KXMLEditorFactory::configuration()->treeview()->dfltExpLevel() )
setOpen(true);
setTexts();
}
void KXE_TreeViewItem::setTexts()
{
switch ( m_xmlNode.nodeType() )
{
case TQDomNode::ElementNode:
setText( 0, m_xmlNode.toElement().nodeName() );
if ( KXMLEditorFactory::configuration()->treeview()->elemDisplMode() == KXETreeViewSettings::NoAttributes )
setText( 1, TQString() );
else
{
// parse all attributes to fill the second column
TQString str2ndCol;
for ( uint i=0; i < m_xmlNode.toElement().attributes().length(); i++ )
{
if ( i > 0 )
str2ndCol += ", ";
str2ndCol += m_xmlNode.toElement().attributes().item(i).toAttr().name();
if ( KXMLEditorFactory::configuration()->treeview()->elemDisplMode() == KXETreeViewSettings::NamesAndValues )
str2ndCol += '=' + m_xmlNode.toElement().attributes().item(i).toAttr().value();
}
setText( 1, str2ndCol );
}
break;
case TQDomNode::TextNode:
case TQDomNode::CDATASectionNode:
case TQDomNode::CommentNode:
{
// set name
TQString strText = m_xmlNode.toCharacterData().data();
strText = strText.replace( TQRegExp("\n"), " " ); // replace every newline by a space
strText = strText.replace( TQRegExp("\t"), "" ); // removes every tab
strText = strText.replace( TQRegExp("\r"), "" ); // removes every return
strText = strText.simplifyWhiteSpace();
if( strText.length() > 30 ) // reduce name length, if necessary
strText = strText.left(30) + "...";
setText( 0, strText );
break;
}
case TQDomNode::ProcessingInstructionNode:
setText( 0, m_xmlNode.toProcessingInstruction().target() );
break;
default:
kdDebug() << "KXE_TreeViewItem::init: unknown node type (" << m_xmlNode.nodeType() << ")" << endl;
}
}
void KXE_TreeViewItem::initChilds()
{
if ( ! KXMLEditorFactory::configuration()->treeview()->createItemsOnDemand() ||
( ! m_bChildsCreated &&
( ! parent() || ( parent() && parent()->isOpen() ) )
)
)
ensureChildItemsCreated();
}
bool KXE_TreeViewItem::toggleBookmark()
{
m_bBookmarked = ! m_bBookmarked;
setPixmap(0, domTool_getIconForNodeType(m_xmlNode.nodeType(), m_bBookmarked));
return m_bBookmarked;
}
KXE_TreeViewItem * KXE_TreeViewItem::lastChild() const
{
// take the first child
TQListViewItem * pTmpItem = firstChild();
// if there are no childs return 0
if ( ! pTmpItem )
return 0;
TQListViewItem * pTmpItem2;
while ( (pTmpItem2 = pTmpItem->nextSibling()) != 0 ) // traversing all childs
{
pTmpItem = pTmpItem2;
}
return static_cast <KXE_TreeViewItem*> (pTmpItem);
}
KXE_TreeViewItem * KXE_TreeViewItem::prevItem()
{
if ( m_pPrevSibling ) // if there is a prev. sibling
{ // return its last grand child (if there is any)
KXE_TreeViewItem * pPrevItem = m_pPrevSibling;
KXE_TreeViewItem * pTmpItem;
while ( (pTmpItem=pPrevItem->lastChild()) )
pPrevItem = pTmpItem;
return pPrevItem;
}
else // if there is no prev. sibling,
return static_cast <KXE_TreeViewItem*> (parent()); // return this' parent (if there is any)
}
KXE_TreeViewItem * KXE_TreeViewItem::nextItem()
{
// checking for a child
TQListViewItem * pTmp = firstChild();
if (pTmp)
return static_cast <KXE_TreeViewItem*> (pTmp);
// there is no child -> checking for the next sibling
pTmp = nextSibling();
if (pTmp)
return static_cast <KXE_TreeViewItem*> (pTmp);
// there is no next sibling -> checking for parents' next sibling(s)
TQListViewItem * pParent = parent();
while (pParent)
{
pTmp = pParent->nextSibling();
if (pTmp)
return static_cast <KXE_TreeViewItem*> (pTmp);
pParent = pParent->parent();
}
return 0;
}
void KXE_TreeViewItem::expandSubTree( int iLevel )
{
setOpen(true); // expand this item
if ( iLevel == 0 ) // return, if we are deep enough
return;
// expand childs (recursive)
int iNewLevel = ( iLevel == -1 ) ? -1 : iLevel-1;
KXE_TreeViewItem * pChild = static_cast <KXE_TreeViewItem*> ( firstChild() );
while ( pChild )
{
pChild->expandSubTree(iNewLevel);
pChild = static_cast <KXE_TreeViewItem*> ( pChild->nextSibling() );
}
}
void KXE_TreeViewItem::collapseSubTree( int iLevel )
{
if ( iLevel < 0 )
{
kdDebug() << "KXE_TreeViewItem::collapseSubTree: wrong level given (iLevel=" << iLevel << ")" << endl;
return;
}
int iNewLevel;
if (iLevel==0) // collapse this item,
{ // because we are deep enough
setOpen(false);
iNewLevel = 0;
}
else
iNewLevel = iLevel - 1;
// collapsing in childs (recursive)
KXE_TreeViewItem * pChild = static_cast <KXE_TreeViewItem*> ( firstChild() );
while ( pChild )
{
pChild->collapseSubTree( iNewLevel );
pChild = static_cast <KXE_TreeViewItem*> ( pChild->nextSibling() );
}
}
/** Test, if item in parameter is my direct or indirect child item */
bool KXE_TreeViewItem::isMyChildren(const KXE_TreeViewItem *pTestItem)
{
KXE_TreeViewItem* pChildXmlTreeItem = (KXE_TreeViewItem*) firstChild();
while(pChildXmlTreeItem)
{ if(pChildXmlTreeItem == pTestItem)
return true;
// test child item childrens
if(pChildXmlTreeItem->isMyChildren(pTestItem))
return true;
pChildXmlTreeItem = (KXE_TreeViewItem*) pChildXmlTreeItem->nextSibling();
}
return false;
}
void KXE_TreeViewItem::ensureChildItemsCreated()
{
if ( ! m_bChildsCreated )
{
TQDomNode tmpNode = m_xmlNode.lastChild();
while ( ! tmpNode.isNull() )
{
new KXE_TreeViewItem( tmpNode, this );
tmpNode = tmpNode.previousSibling();
}
m_bChildsCreated = true;
}
}
void KXE_TreeViewItem::ensureGrandChildItemsCreated()
{
if ( ! m_bGrandChildsCreated )
{
// ensure, that all child items of this item are created
if ( ! m_bChildsCreated )
ensureChildItemsCreated();
// Iterate over all children now and ensure their child items
// (this' grandchildrens) are created.
KXE_TreeViewItem * pChild = static_cast <KXE_TreeViewItem*> ( firstChild() );
while ( pChild )
{
pChild->ensureChildItemsCreated();
pChild = static_cast <KXE_TreeViewItem*> ( pChild->nextSibling() );
}
m_bGrandChildsCreated = true;
}
}
void KXE_TreeViewItem::startRename( int iCol )
{
// If the given column is set to be in-place renameable in this item's view,
// we can start renaming in-place.
// Remember: This function is only reached for items representing XML elements.
if ( (reinterpret_cast<TDEListView*> ( listView() ))->isRenameable( iCol ) )
TQListViewItem::startRename( iCol );
}
|