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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
|
/***************************************************************************
kxe_treeview.cpp - description
-------------------
begin : Thu Sep 20 2001
copyright : (C) 2001, 2002, 2003 by The KXMLEditor Team
email : OleBowle@gmx.de
***************************************************************************/
/***************************************************************************
* *
* 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_treeview.h"
#include "kxe_treeviewitem.h"
#include "kxesearchdialog.h"
#include "kxmleditorpart.h"
#include "kxmleditorfactory.h"
#include "kxeconfiguration.h"
#include "kxetreeviewsettings.h"
#include <klocale.h>
#include <kdebug.h>
#include <kxmlgui.h>
#include <kxmlguiclient.h>
#include <kpopupmenu.h>
#include <kglobalsettings.h>
#include <kmessagebox.h>
// include files for Qt
#include "qdom_add.h"
#include <qheader.h>
#include <qdragobject.h>
#include <qtimer.h>
#include <qdom.h>
#include <qcursor.h>
#include <qevent.h>
static const int autoOpenTimeout = 750;
KXE_TreeView::KXE_TreeView( KXMLGUIClient * pGUIClient, QWidget * pParent, const char * pszName )
: KListView(pParent,pszName),
m_pGUIClient(pGUIClient),
m_nBookmarkedItems(0)
{
setSorting(-1); // no sorting
addColumn(i18n("Qualified name"));
setSelectionMode(QListView::Single);
connect( this, SIGNAL(selectionChanged()), this, SLOT(slotSelectionChanged()) );
connect( this, SIGNAL(expanded(QListViewItem*)), this, SLOT(slotItemExpanded(QListViewItem*)) );
setReadWrite(false);
m_bDrag = false;
m_pCurrentBeforeDropItem = 0;
m_pDropItem = 0;
m_autoOpenTimer = new QTimer(this);
connect(m_autoOpenTimer, SIGNAL(timeout()), this, SLOT(slotAutoOpenFolder()));
// Apply current configuration
slotTreeViewSettingsChanged();
// and make sure to be informed about its changes.
connect( KXMLEditorFactory::configuration()->treeview(), SIGNAL(sigChanged()), this, SLOT(slotTreeViewSettingsChanged()) );
}
void KXE_TreeView::setReadWrite( bool fReadWrite )
{
setItemsRenameable( fReadWrite );
setRenameable( 0, fReadWrite );
if ( fReadWrite ) // If the widget enters read/write mode, then enable/disable
{ // dropping (according to the configuration data).
setAcceptDrops( KXMLEditorFactory::configuration()->treeview()->enableDropping() );
viewport()->setAcceptDrops( KXMLEditorFactory::configuration()->treeview()->enableDropping() );
}
else // If the widget enter read only mode,
{ // then disable dropping.
setAcceptDrops( false );
viewport()->setAcceptDrops( false );
}
}
//////////////////////////////////////////////////////////////
// configuration slots
//////////////////////////////////////////////////////////////
void KXE_TreeView::slotTreeViewSettingsChanged()
{
setRootIsDecorated( KXMLEditorFactory::configuration()->treeview()->decorateRoot() );
if ( KXMLEditorFactory::configuration()->treeview()->elemDisplMode() == KXETreeViewSettings::NoAttributes )
{
if ( columns() > 1 )
removeColumn(1);
}
else
{
if ( columns() < 2 )
addColumn( i18n("Attributes") );
}
KXE_TreeViewItem * pItem = static_cast<KXE_TreeViewItem*> (firstChild());
while (pItem)
{
pItem->setTexts();
pItem = pItem->nextItem();
}
if ( itemsRenameable() ) // If the widget is in read/write mode, then enable/disable
{ // dropping (according to the configuration data).
setAcceptDrops( KXMLEditorFactory::configuration()->treeview()->enableDropping() );
viewport()->setAcceptDrops( KXMLEditorFactory::configuration()->treeview()->enableDropping() );
}
}
//////////////////////////////////////////////////////////////
// action slots
//////////////////////////////////////////////////////////////
void KXE_TreeView::editDeselect()
{
clearSelection();
}
void KXE_TreeView::viewNodeUp()
{
// get selected item from tree view
QListViewItem * pSelItem = selectedItem();
if ( ! pSelItem )
{
kdDebug() << "KXE_TreeView::slotViewNodeUp no item selected" << endl;
return;
}
// get parent item
QListViewItem * pParentItem = pSelItem->parent();
// select parent item in tree view
if (pParentItem)
{
setCurrentItem(pParentItem);
ensureItemVisible(pParentItem);
}
}
void KXE_TreeView::viewExpNode( int nLevel )
{
// get selected item from tree view (if any)
QListViewItem * pSelItem = selectedItem();
if ( ! pSelItem )
{
kdDebug() << "KXE_TreeView::slotViewExpNode no item selected" << endl;
return;
}
// expand node
KXE_TreeViewItem * pSelTreeItem = static_cast <KXE_TreeViewItem*> (pSelItem);
pSelTreeItem->expandSubTree(nLevel);
}
void KXE_TreeView::viewColNode( int nLevel )
{
// get selected item from tree view (if any)
QListViewItem * pSelItem = selectedItem();
if ( ! pSelItem )
{
kdDebug() << "KXE_TreeView::slotViewColNode no item selected" << endl;
return;
}
// expand node
KXE_TreeViewItem * pSelTreeItem = static_cast <KXE_TreeViewItem*> (pSelItem);
pSelTreeItem->collapseSubTree(nLevel);
}
void KXE_TreeView::bookmarksToggle()
{
// get selected item from tree view
KXE_TreeViewItem * pSelItem = static_cast <KXE_TreeViewItem*> (selectedItem());
if ( ! pSelItem )
{
kdDebug() << "KXE_TreeView::bookmarksToggle: no item selected" << endl;
return;
}
// toggle bookmark on selected item
if(pSelItem->toggleBookmark())
m_nBookmarkedItems++;
else
m_nBookmarkedItems--;
}
void KXE_TreeView::bookmarksPrev()
{
if ( childCount() < 1 )
{
kdDebug() << "KXE_TreeView::bookmarksPrev: internal error - this tree view is empty" << endl;
return;
}
// get selected item from tree view
KXE_TreeViewItem * pSelItem = static_cast <KXE_TreeViewItem*> (selectedItem());
if ( ! pSelItem ) // If there is no item selected we take
{ // the last root items last grand child.
QListViewItem * pTmpItem = firstChild(); // Take first child and
while ( pTmpItem->nextSibling() ) // find last child by
pTmpItem = pTmpItem->nextSibling(); // traversing all childs
pSelItem = static_cast <KXE_TreeViewItem*> (pTmpItem); // this is the last root item
while ( pSelItem->lastChild() ) // find its last
pSelItem = pSelItem->lastChild(); // grand child
if ( pSelItem->isBookmarked() ) // We have to check its
{ // bookmarked-status
selectItem(pSelItem); // and select it, in case
return; // it is bookmarked.
}
}
// Search items above the selected one
while ( (pSelItem = pSelItem->prevItem()) != 0 )
{
if ( pSelItem->isBookmarked() )
{
selectItem(pSelItem);
return;
}
}
}
void KXE_TreeView::bookmarksNext()
{
if ( childCount() < 1 )
{
kdDebug() << "KXE_TreeView::bookmarksNext: internal error - this tree view is empty" << endl;
return;
}
// get selected item from tree view
KXE_TreeViewItem * pSelItem = static_cast <KXE_TreeViewItem*> (selectedItem());
if ( ! pSelItem )
{ // If there is no item selected
pSelItem = static_cast <KXE_TreeViewItem*> (firstChild()); // we take the first root item,
if ( pSelItem->isBookmarked() ) // but we have to check its
{ // bookmarked-status
selectItem(pSelItem); // and select it, in case
return; // it is bookmarked.
}
}
// Search items below the selected one
while ( (pSelItem = pSelItem->nextItem()) != 0 )
{
if ( pSelItem->isBookmarked() )
{
selectItem(pSelItem);
return;
}
}
}
void KXE_TreeView::selectItem( KXE_TreeViewItem * const pItem )
{
if ( ! pItem )
{
kdDebug() << "KXE_TreeView::selectItem: the given pointer is a null pointer" << endl;
return;
}
setSelected( pItem, true );
setCurrentItem( pItem );
ensureItemVisible( pItem );
}
bool KXE_TreeView::selectNode( const QDomNode & node )
{
if ( node.isNull() )
{
kdError() << "KXE_TreeView::selectNode: the given node is an empty one" << endl;
return false;
}
KXE_TreeViewItem * pItem = findCorrespondingItem(node);
if ( ! pItem ) // can't find the corresponding item
{
kdError() << "KXE_TreeView::selectNode can't find an item to the given node." << endl;
return false;
}
selectItem(pItem);
return true;
}
QDomNode * KXE_TreeView::getSelectedNode() const
{
// get selected item from tree view
QListViewItem * pSelItem = selectedItem();
if ( ! pSelItem )
return 0;
KXE_TreeViewItem * pSelTreeItem = static_cast <KXE_TreeViewItem *> (pSelItem);
return pSelTreeItem->xmlNode();
}
QDomNode * KXE_TreeView::getSpecProcInstrNode(const QString& target) const
{
KXE_TreeViewItem * pTreeItem = static_cast<KXE_TreeViewItem*> (firstChild());
while ( pTreeItem )
{
if (pTreeItem->xmlNode()->isProcessingInstruction())
{
QDomProcessingInstruction domProcInstr = pTreeItem->xmlNode()->toProcessingInstruction();
if(domProcInstr.target() == target)
return pTreeItem->xmlNode();
}
pTreeItem = pTreeItem->nextItem();
}
return 0;
}
// Return info, is root element is already created
bool KXE_TreeView::hasRootNode()
{
KXE_TreeViewItem * pTreeItem = static_cast<KXE_TreeViewItem*> (firstChild());
while ( pTreeItem )
{
if (pTreeItem->xmlNode()->isElement())
{
return true;
}
pTreeItem = pTreeItem->nextItem();
}
return false;
}
QString KXE_TreeView::getSelectedPath() const
{
// get selected item from tree view
QListViewItem * pSelItem = selectedItem();
if ( ! pSelItem )
return QString();
KXE_TreeViewItem * pSelTreeItem = static_cast <KXE_TreeViewItem *> (pSelItem);
return domTool_getPath( * pSelTreeItem->xmlNode() );
}
void KXE_TreeView::contentsMousePressEvent( QMouseEvent * pEvent )
{
KListView::contentsMousePressEvent(pEvent);
if ( pEvent->button() == RightButton )
{
QString szMenuName;
QListViewItem * pItem = itemAt( contentsToViewport(pEvent->pos()) );
if (pItem)
{
KXE_TreeViewItem * pTreeItem = static_cast <KXE_TreeViewItem*> (pItem);
switch( pTreeItem->xmlNode()->nodeType() )
{
case QDomNode::ElementNode:
szMenuName = "popupXmlElement";
break;
case QDomNode::TextNode:
case QDomNode::CDATASectionNode:
case QDomNode::CommentNode:
szMenuName = "popupXmlContent";
break;
case QDomNode::ProcessingInstructionNode:
szMenuName = "popupXmlProcInstr";
break;
default:
kdDebug() << "KXE_TreeView::contentsMousePressEvent unknown item type" << endl;
return;
}
}
else
szMenuName = "popupXmlTree";
emit sigContextMenuRequested( szMenuName, QCursor::pos() );
return;
}
//--- Drag & Drop ------------------------------------------------------
QPoint p(contentsToViewport(pEvent->pos()));
QListViewItem *i = itemAt(p);
if(pEvent->button() == LeftButton && i)
{ // if the user clicked into the root decoration of the item, don't try to start a drag!
if(p.x() > header()->cellPos(header()->mapToActual(0)) +
treeStepSize() * ( i->depth() + (rootIsDecorated() ? 1 : 0)) + itemMargin() ||
p.x() < header()->cellPos(header()->mapToActual(0)))
{
m_dragPos = pEvent->pos();
m_bDrag = true;
}
}
}
void KXE_TreeView::slotSelectionChanged()
{
KXE_TreeViewItem * pItem = static_cast <KXE_TreeViewItem*> (selectedItem());
if ( ! pItem )
emit sigSelectionCleared(hasRootNode());
else
{
QDomNode selectedNode = * ( pItem->xmlNode() ); // uses QDomNode copy constructor
// choose appropriate object kind
switch ( selectedNode.nodeType() )
{
case QDomNode::ElementNode:
emit sigSelectionChanged( selectedNode.toElement());
break;
case QDomNode::TextNode:
case QDomNode::CDATASectionNode:
case QDomNode::CommentNode:
emit sigSelectionChanged( selectedNode.toCharacterData());
break;
case QDomNode::ProcessingInstructionNode:
emit sigSelectionChanged( selectedNode.toProcessingInstruction());
break;
default:
kdDebug() << "KXE_TreeView::slotSelectionChanged unknown object type selected" << endl;
return;
}
}
}
void KXE_TreeView::slotItemExpanded( QListViewItem * pItem )
{
KXE_TreeViewItem * pTreeViewItem = static_cast<KXE_TreeViewItem*> (pItem);
pTreeViewItem->ensureGrandChildItemsCreated();
}
//////////////////////////////////////////////////////////////
// update slots
//////////////////////////////////////////////////////////////
void KXE_TreeView::updateNodeCreated( const QDomNode & node )
{
if ( node.isNull() )
{
kdError() << "KXE_TreeView::slotUpdateNodeCreated the given node is an empty one." << endl;
return;
}
KXE_TreeViewItem * pNewItem;
if ( node.parentNode().isDocument() ) // the new nodes parent is the document itself,
{
// so we have to create a root item.
// Now it depends: either it's a processing instruction, or element
if (node.isProcessingInstruction())
// Tree looks much nicer if root processing instructions are ont the top...
{
QDomNode *pNode = getSpecProcInstrNode("xml");
if (pNode)
pNewItem = new KXE_TreeViewItem( node, this,findCorrespondingItem(*pNode));
else
pNewItem = new KXE_TreeViewItem( node, this);
}
else
// ...and root element is placed at the bottom.
pNewItem = new KXE_TreeViewItem( node, this,lastChild());
// pNewItem = new KXE_TreeViewItem( node, this);
if ( ! rootIsDecorated() )
pNewItem->setOpen(true);
}
else
{
if ( node.parentNode().isNull() )
{
kdError() << "KXE_TreeView::slotUpdateNodeCreated the given node has no parent node (but should)." << endl;
return;
}
// To create the new item, we need (1st) the item corresponding to the parent node of the given one.
QDomNode parentNode = node.parentNode();
// Because the currently selected item is very likely (in many cases) the correct one, try it first.
KXE_TreeViewItem * pParentItem = static_cast<KXE_TreeViewItem*> (selectedItem());
if ( (!pParentItem) || ( *(pParentItem->xmlNode()) != parentNode ) )
{ // no strike :-(
pParentItem = findCorrespondingItem(parentNode); // do it the "long" way
}
if ( ! pParentItem ) // can't find the corresponding item
{
kdError() << "KXE_TreeView::slotUpdateNodeCreated can't find an item to the given nodes parent node." << endl;
return;
}
// Now we need (2nd) the item corresponding to the previous sibling of the given one,
// because, the new item has to be inserted behind the given one.
QDomNode prevNode = node.previousSibling();
if ( prevNode.isNull() )
{ // it seems to be the first child node, so create a first child item
pNewItem = new KXE_TreeViewItem( node, pParentItem );
}
else
{
KXE_TreeViewItem * pPrevItem = findCorrespondingItem(prevNode);
if ( ! pParentItem ) // can't find the corresponding item :-(
{
kdError() << "KXE_TreeView::slotUpdateNodeCreated can't find an item to the given nodes previous sibling." << endl;
return;
}
// everything's alright, let's create the new item
pNewItem = new KXE_TreeViewItem( node, pParentItem, pPrevItem );
}
}
setSelected( pNewItem, true );
ensureItemVisible( pNewItem );
}
void KXE_TreeView::updateNodeChanged( const QDomNode & node )
{
if ( node.isNull() )
{
kdError() << "KXE_TreeView::slotUpdateNodeChanged the given node is an empty one." << endl;
return;
}
// To change the item, we have to find it.
// Because the currently selected item is very likely (in many cases) the correct one, try it first.
KXE_TreeViewItem * pItem = static_cast<KXE_TreeViewItem*> (selectedItem());
if ( (!pItem) || ( *(pItem->xmlNode()) != node ) ) // no strike :-(
pItem = findCorrespondingItem(node); // do it the "long" way
if ( ! pItem ) // can't find the corresponding item
{
kdError() << "KXE_TreeView::slotUpdateNodeChanged can't find an item to the given node." << endl;
return;
}
pItem->setTexts(); // update the item
setSelected( pItem, true );
ensureItemVisible( pItem );
}
void KXE_TreeView::updateNodeDeleted( const QDomNode & node )
{
if ( node.isNull() )
{
kdError() << "KXE_TreeView::slotUpdateNodeDeleted the given node is an empty one." << endl;
return;
}
// To remove the item, we have to find it.
// Because the currently selected item is very likely (in many cases) the correct one, try it first.
KXE_TreeViewItem * pItem = static_cast<KXE_TreeViewItem*> (selectedItem());
if ( (!pItem) || ( *(pItem->xmlNode()) != node ) ) // no strike :-(
pItem = findCorrespondingItem(node); // do it the "long" way
if ( ! pItem ) // can't find the corresponding item
{
kdError() << "KXE_TreeView::slotUpdateNodeDeleted can't find an item to the given node." << endl;
return;
}
clearSelection();
delete pItem;
emit sigSelectionCleared(hasRootNode());
}
void KXE_TreeView::updateNodeMoved( const QDomNode & node )
{
if ( node.isNull() )
{
kdError() << "KXE_TreeView::slotUpdateNodeMoved the given node is an empty one." << endl;
return;
}
// To move the item, we have to find it.
// Because the currently selected item is very likely (in many cases) the correct one, try it first.
KXE_TreeViewItem * pItem = static_cast<KXE_TreeViewItem*> (selectedItem());
if ( (!pItem) || ( *(pItem->xmlNode()) != node ) ) // no strike :-(
pItem = findCorrespondingItem(node); // do it the "long" way
if ( ! pItem ) // can't find the corresponding item
{
kdError() << "KXE_TreeView::slotUpdateNodeMoved can't find an item to the given node." << endl;
return;
}
// Now we can move the item (of the moved node).
// We have to differenciate between the following 2 cases.
if ( node.previousSibling().isNull() )
{
// The node does not has a previous sibling. This means, it has been moved
// to be its parent first child. In this case, we have to find the tree
// view item of the node's next sibling to swap them.
// It's very likely the previous sibling of the item corresponding to the
// moved node.
KXE_TreeViewItem * pOldPrevItem = pItem->prevSibling();
// Was it really?
if ( ! pOldPrevItem || ( *(pOldPrevItem->xmlNode()) != node.nextSibling() ) )
// It wasn't (how can it be?) - we have to find it using the "long" way.
pOldPrevItem = findCorrespondingItem( node.nextSibling() );
if ( ! pOldPrevItem ) // something went wrong
{
kdError() << "KXE_TreeView::slotUpdateNodeMoved can't find the item to the given node's next sibling." << endl;
return;
}
// Now we can swap them (make the old previous item the new next item of
// the moved node's item).
pOldPrevItem->moveItem( pItem );
}
else
{
// The node has a previous sibling. In this case we have to find the
// corresponding tree view item to swap it with the item corresponding to
// the moved node.
KXE_TreeViewItem * pNewPrevItem = findCorrespondingItem( node.previousSibling() );
if ( ! pNewPrevItem )
{
kdError() << "KXE_TreeView::slotUpdateNodeMoved can't find the new prev.item to the given nodes prev.node." << endl;
return;
}
// swap them (move the moved node's item after the previous sibling's item)
pItem->moveItem( pNewPrevItem );
}
setSelected( pItem, true );
ensureItemVisible( pItem );
}
void KXE_TreeView::updateClear()
{
clear();
}
void KXE_TreeView::rename( QListViewItem * pItem, int nColumn )
{
if ( nColumn != 0 ) // inplace editing only
return; // for the first column
KXE_TreeViewItem * pXMLItem = static_cast <KXE_TreeViewItem*> (pItem);
if ( pXMLItem->xmlNode()->isElement() ) // inplace-renaming only for items representing XML elements
KListView::rename( pItem, nColumn ); // inplace-renaming via base class functionality
else if(pXMLItem->xmlNode()->isCharacterData()) // launch dialog for editing text nodes
(dynamic_cast <KXMLEditorPart *> (m_pGUIClient))->slotXmlCharDataEdit();
else if(pXMLItem->xmlNode()->isProcessingInstruction()) // launch dialog for editing proc.instr.
(dynamic_cast <KXMLEditorPart *> (m_pGUIClient))->slotXmlProcInstrEdit();
}
//////////////////////////////////////////////////////////////
// misc functions
//////////////////////////////////////////////////////////////
KXE_TreeViewItem * KXE_TreeView::findCorrespondingItem( const QDomNode & node )
{
KXE_TreeViewItem * pItem = static_cast<KXE_TreeViewItem*> (firstChild());
while ( pItem )
{
if ( *(pItem->xmlNode()) == node )
return pItem;
pItem = pItem->nextItem();
}
return 0;
}
//////////////////////////////////////////////////////////////
// Drag & Drop
//////////////////////////////////////////////////////////////
/** Overrides KListView::contentsMouseMoveEvent */
void KXE_TreeView::contentsMouseMoveEvent(QMouseEvent *e)
{
KListView::contentsMouseMoveEvent(e);
// exit, if dragging is disabled
if ( ! KXMLEditorFactory::configuration()->treeview()->enableDragging() )
return;
if(!m_bDrag || (e->pos() - m_dragPos).manhattanLength() <= KGlobalSettings::dndEventDelay())
return;
m_bDrag = false;
QListViewItem *item = itemAt(contentsToViewport(m_dragPos));
if(!item || !item->isSelectable())
return;
// copy item into clipboard
KXE_TreeViewItem *pXmlTreeItem = static_cast <KXE_TreeViewItem *> (item);
QTextDrag *pDrag = (dynamic_cast <KXMLEditorPart *> (m_pGUIClient))->copyNode(pXmlTreeItem->xmlNode());
// Start a drag
const QPixmap *pix = item->pixmap(0);
if(pix && pDrag->pixmap().isNull())
{ QPoint hotspot(pix->width() / 2, pix->height() / 2);
pDrag->setPixmap(*pix, hotspot);
}
pDrag->drag();
}
/** Overrides KListView::contentsMouseReleaseEvent */
void KXE_TreeView::contentsMouseReleaseEvent(QMouseEvent *e)
{
KListView::contentsMouseReleaseEvent(e);
m_bDrag = false;
}
/** Overrides QScrollView::contentsDragEnterEvent */
void KXE_TreeView::contentsDragEnterEvent(QDragEnterEvent *e)
{
m_pDropItem = 0;
m_pCurrentBeforeDropItem = selectedItem();
// Save the available formats
m_lstDropFormats.clear();
for(int i = 0; e->format(i); i++)
{ if(*(e->format(i)))
{ m_lstDropFormats.append(e->format(i));
}
}
}
/** Overrides QScrollView::contentsDragMoveEvent */
void KXE_TreeView::contentsDragMoveEvent(QDragMoveEvent *e)
{
QListViewItem *item = itemAt(contentsToViewport(e->pos()));
// Accept drops on the background, if Texts
if(!item && (m_lstDropFormats.contains("text/")))
{ m_pDropItem = 0;
e->acceptAction();
if(selectedItem())
setSelected(selectedItem(), false); // no item selected
return;
}
if(!item || !item->isSelectable())
{ m_pDropItem = 0;
m_autoOpenTimer->stop();
e->ignore();
return;
}
e->acceptAction();
setSelected(item, true);
if(item != m_pDropItem )
{ m_autoOpenTimer->stop();
m_pDropItem = item;
m_autoOpenTimer->start(autoOpenTimeout);
}
}
/** Overrides QScrollView::contentsDragLeaveEvent */
void KXE_TreeView::contentsDragLeaveEvent(QDragLeaveEvent *e)
{
e=e;
// Restore the current item to what it was before the dragging (#17070)
if(m_pCurrentBeforeDropItem)
setSelected(m_pCurrentBeforeDropItem, true);
else
setSelected(m_pDropItem, false); // no item selected
m_pCurrentBeforeDropItem = 0;
m_pDropItem = 0;
m_lstDropFormats.clear();
}
/** Overrides QScrollView::contentsDropEvent */
void KXE_TreeView::contentsDropEvent(QDropEvent *pDropEvent)
{
m_autoOpenTimer->stop();
drop(selectedItem(), pDropEvent);
}
/** Called, when m_autoOpenTimer timeout occured */
void KXE_TreeView::slotAutoOpenFolder()
{
m_autoOpenTimer->stop();
if(!m_pDropItem || m_pDropItem->isOpen())
return;
m_pDropItem->setOpen( true );
m_pDropItem->repaint();
}
/** Drop or paste text into item */
bool KXE_TreeView::drop(QListViewItem *pItem, QDropEvent *pDropEvent)
{
KXE_TreeViewItem* pTreeItem = 0;
if(pItem)
pTreeItem = static_cast <KXE_TreeViewItem *> (pItem);
QDomNode *pTargetNode = pTreeItem->xmlNode();
// First, make check, if moved item is not moved to their children
if((pDropEvent->source() == this) && (pDropEvent->action() == QDropEvent::Move))
{ // make check, if moved item is not moved to itself
if(m_pCurrentBeforeDropItem && pTreeItem && (m_pCurrentBeforeDropItem == pTreeItem))
{ return false;
}
if(m_pCurrentBeforeDropItem && pTreeItem &&
static_cast <KXE_TreeViewItem*> (m_pCurrentBeforeDropItem)->isMyChildren(pTreeItem))
{ KMessageBox::sorry(0, i18n("An XML element can't be moved to its own subtree."));
return false;
}
if (pTreeItem->xmlNode()->isProcessingInstruction())
{
KMessageBox::sorry(0, i18n("An XML node can't be moved in a processing instruction."));
return false;
}
QDomNode * pNode = static_cast <KXE_TreeViewItem*> (m_pCurrentBeforeDropItem)->xmlNode();
if (pNode->isProcessingInstruction())
{
QDomProcessingInstruction domProcInstr = pNode->toProcessingInstruction();
if(domProcInstr.target() == "xml")
{ KMessageBox::sorry(0, i18n("This processing instruction cannot be moved !"));
return false;
}
}
}
//-- If Move from same instance of this widget
if((pDropEvent->source() == this) && (pDropEvent->action() == QDropEvent::Move) && (m_pCurrentBeforeDropItem) && pTargetNode->isElement())
{
// remove source item
QDomNode * pSourceNode = static_cast <KXE_TreeViewItem*> (m_pCurrentBeforeDropItem)->xmlNode();
QDomElement domTargetElement = pTargetNode->toElement();
if((dynamic_cast <KXMLEditorPart *> (m_pGUIClient))->dropMoveNode(domTargetElement, *pSourceNode))
{
pDropEvent->acceptAction();
return true;
}
}
else
{
//-- If Copy, do standart Paste function
if((dynamic_cast <KXMLEditorPart *> (m_pGUIClient))->pasteNode(pTargetNode, pDropEvent))
{
pDropEvent->acceptAction();
return true;
}
}
return false;
}
//
// returns last child on the tree (top-level)
//
KXE_TreeViewItem* KXE_TreeView::lastChild()
{
QListViewItem* pItem = firstChild();
if (pItem && pItem->nextSibling())
do
pItem = pItem->nextSibling();
while (pItem->nextSibling());
// here we have it...
return (KXE_TreeViewItem*) pItem;
}
void KXE_TreeView::keyPressEvent(QKeyEvent *e)
{
KListView::keyPressEvent(e);
emit sigKeyPressed(e);
}
#include "kxe_treeview.moc"
|