-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgslegend.cpp
executable file
·1923 lines (1705 loc) · 55.7 KB
/
qgslegend.cpp
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgslegend.cpp - description
-------------------
begin : Sun Jul 28 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc dot com
Romans 3:23=>Romans 6:23=>Romans 10:9,10=>Romans 12
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/* $Id$ */
#include "qgsapplication.h"
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgslegend.h"
#include "qgslegendgroup.h"
#include "qgslegendlayer.h"
#include "qgslegendpropertygroup.h"
#include "qgslegendsymbologyitem.h"
#include "qgslegendlayerfile.h"
#include "qgslegendlayerfilegroup.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvasmap.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaprenderer.h"
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgsrasterlayerproperties.h"
#include "qgsvectorlayerproperties.h"
#include "qgsattributetabledialog.h"
#include <cfloat>
#include <iostream>
#include <QFont>
#include <QDomDocument>
#include <QHeaderView>
#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPixmap>
#include <QTreeWidgetItem>
static const char *const ident_ = "$Id$";
const int AUTOSCROLL_MARGIN = 16;
/**
@note
set mItemBeingMoved pointer to 0 to prevent SuSE 9.0 crash
*/
QgsLegend::QgsLegend( QWidget * parent, const char *name )
: QTreeWidget( parent ), mMousePressedFlag( false ), mItemBeingMoved( 0 ), mShowLegendLayerFiles( false ), mToggleEditingAction( 0 ), mMapCanvas( 0 ), mMinimumIconSize( 20, 20 )
{
connect( this, SIGNAL( itemChanged( QTreeWidgetItem*, int ) ),
this, SLOT( handleItemChange( QTreeWidgetItem*, int ) ) );
connect( this, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ),
this, SLOT( handleCurrentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ) );
// project handling
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ),
this, SLOT( readProject( const QDomDocument & ) ) );
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ),
this, SLOT( writeProject( QDomDocument & ) ) );
setSortingEnabled( false );
setDragEnabled( false );
setAutoScroll( true );
QFont f( "Arial", 10, QFont::Normal );
setFont( f );
QPalette palette;
palette.setColor( backgroundRole(), QColor( 192, 192, 192 ) );
setPalette( palette );
setColumnCount( 1 );
header()->setHidden( 1 );
setRootIsDecorated( true );
initPixmaps();
}
QgsLegend::~QgsLegend()
{}
void QgsLegend::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous )
{
QgsMapLayer *layer = currentLayer();
if ( mMapCanvas )
{
mMapCanvas->setCurrentLayer( layer );
}
emit currentLayerChanged( layer );
}
void QgsLegend::addGroup()
{
QgsLegendGroup* group = new QgsLegendGroup( this, tr( "group" ) );
mStateOfCheckBoxes.insert( std::make_pair( group, Qt::Checked ) ); //insert the check state into the map to query for changes later
setExpanded( indexFromItem( group ), true );
}
void QgsLegend::removeAll()
{
mStateOfCheckBoxes.clear();
clear();
mPixmapWidthValues.clear();
mPixmapHeightValues.clear();
updateMapCanvasLayerSet();
setIconSize( mMinimumIconSize );
}
void QgsLegend::selectAll( bool select )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QTreeWidgetItem* theItem = firstItem();
while ( theItem )
{
QgsLegendItem* litem = dynamic_cast<QgsLegendItem*>( theItem );
if ( litem && litem->type() == QgsLegendItem::LEGEND_LAYER_FILE )
{
theItem->setCheckState( 0, ( select ? Qt::Checked : Qt::Unchecked ) );
handleItemChange( theItem, 0 );
}
theItem = nextItem( theItem );
}
}
void QgsLegend::removeLayer( QString layer_key )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QTreeWidgetItem* theItem = firstItem();
QgsDebugMsg( "called." );
QgsLegendLayer* lastLL = NULL;
while ( theItem )
{
QgsLegendItem *li = dynamic_cast<QgsLegendItem*>( theItem );
if ( li )
{
// save legend layer (parent of a legend layer file we're going to delete)
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>( li );
if ( ll ) lastLL = ll;
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>( li );
if ( llf )
{
if ( llf->layer() && llf->layer()->getLayerID() == layer_key )
{
//remove the map entry for the checkbox
mStateOfCheckBoxes.erase( llf );
removeItem( llf );
delete llf;
// delete also parent legend layer if now it's empty
if ( lastLL->mapLayers().size() == 0 )
{
mStateOfCheckBoxes.erase( lastLL );
removeItem( lastLL );
delete lastLL;
}
break;
}
}
}
theItem = nextItem( theItem );
}
updateMapCanvasLayerSet();
adjustIconSize();
}
void QgsLegend::mousePressEvent( QMouseEvent * e )
{
if ( e->button() == Qt::LeftButton )
{
mLastPressPos = e->pos();
mMousePressedFlag = true;
}
else if ( e->button() == Qt::RightButton )
{
QTreeWidgetItem* item = itemAt( e->pos() );
setCurrentItem( item );
handleRightClickEvent( item, e->globalPos() );
}
QTreeWidget::mousePressEvent( e );
} // contentsMousePressEvent
void QgsLegend::mouseMoveEvent( QMouseEvent * e )
{
if ( mMousePressedFlag )
{
//set the flag back such that the else if(mItemBeingMoved)
//code part is passed during the next mouse moves
mMousePressedFlag = false;
// remember item we've pressed as the one being moved
// and where it was originally
QTreeWidgetItem* item = itemAt( mLastPressPos );
if ( item )
{
mItemBeingMoved = item;
mItemBeingMovedOrigPos = getItemPos( mItemBeingMoved );
//store information to insert the item back to the original position
storeInitialPosition( mItemBeingMoved );
setCursor( Qt::SizeVerCursor );
}
}
else if ( mItemBeingMoved )
{
QPoint p( e->pos() );
mLastPressPos = p;
// change the cursor appropriate to if drop is allowed
QTreeWidgetItem* item = itemAt( p );
QgsLegendItem* origin = dynamic_cast<QgsLegendItem*>( mItemBeingMoved );
QgsLegendItem* dest = dynamic_cast<QgsLegendItem*>( item );
if ( !item )
{
setCursor( QCursor( Qt::ForbiddenCursor ) );
}
if ( item && ( item != mItemBeingMoved ) )
{
QgsLegendItem::DRAG_ACTION action = dest->accept( origin );
if ( yCoordAboveCenter( dest, e->y() ) ) //over center of item
{
if ( action == QgsLegendItem::REORDER )
{
if ( origin->nextSibling() != dest )
{
moveItem( dest, origin );setCurrentItem( origin );
}
setCurrentItem( origin );
setCursor( QCursor( Qt::SizeVerCursor ) );
}
else if ( action == QgsLegendItem::INSERT )
{
setCursor( QCursor( Qt::PointingHandCursor ) );
if ( origin->parent() != dest )
{
insertItem( origin, dest );
}
setCurrentItem( origin );
setCursor( QCursor( Qt::PointingHandCursor ) );
}
else //no action
{
QgsDebugMsg( "mouseMoveEvent::NO_ACTION" );
if ( origin->type() == QgsLegendItem::LEGEND_LAYER_FILE && mItemBeingMovedOrigPos != getItemPos( mItemBeingMoved ) )
{
resetToInitialPosition( mItemBeingMoved );
}
setCursor( QCursor( Qt::ForbiddenCursor ) );
}
}
else // below center of item
{
if ( action == QgsLegendItem::REORDER )
{
QgsDebugMsg( "mouseMoveEvent::REORDER bottom half" );
if ( mItemBeingMoved != dest->nextSibling() )
{
//origin->moveItem(dest);
moveItem( origin, dest );
}
setCursor( QCursor( Qt::SizeVerCursor ) );
setCurrentItem( origin );
}
else if ( action == QgsLegendItem::INSERT )
{
QgsDebugMsg( "mouseMoveEvent::INSERT" );
setCursor( QCursor( Qt::PointingHandCursor ) );
if ( origin->parent() != dest )
{
insertItem( origin, dest );
setCurrentItem( origin );
}
}
else//no action
{
QgsDebugMsg( "mouseMoveEvent::NO_ACTION" );
if ( origin->type() == QgsLegendItem::LEGEND_LAYER_FILE && mItemBeingMovedOrigPos != getItemPos( mItemBeingMoved ) )
{
resetToInitialPosition( mItemBeingMoved );
}
setCursor( QCursor( Qt::ForbiddenCursor ) );
}
}
}
}
}
void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
{
QTreeWidget::mouseReleaseEvent( e );
setCursor( QCursor( Qt::ArrowCursor ) );
mMousePressedFlag = false;
if ( !mItemBeingMoved )
{
return;
}
QTreeWidgetItem *destItem = itemAt( e->pos() );
QgsLegendItem* origin = dynamic_cast<QgsLegendItem*>( mItemBeingMoved );
QgsLegendItem* dest = dynamic_cast<QgsLegendItem*>( destItem );
// no change?
if ( !dest || !origin )
{
checkLayerOrderUpdate();
return;
}
QgsLegendItem::LEGEND_ITEM_TYPE originType = origin->type();
QgsLegendItem::LEGEND_ITEM_TYPE destType = dest->type();
if ( originType == QgsLegendItem::LEGEND_LAYER_FILE && destType == QgsLegendItem::LEGEND_LAYER_FILE_GROUP )
{
QgsDebugMsg( "Legend layer file moved to layer file group" );
// Not used... delete?
//QgsMapLayer* origLayer = ((QgsLegendLayerFile*)(origin))->layer();
if ( dest->childCount() > 1 )
{
//find the first layer in the legend layer group != origLayer and copy its settings
QgsLegendItem* currentItem = dynamic_cast<QgsLegendItem*>( dest->child( 0 ) );
while ( currentItem )
{
if ( currentItem != origin )
{
QgsMapLayer* origLayer = (( QgsLegendLayerFile* )( origin ) )->layer();
QgsMapLayer* currentLayer = (( QgsLegendLayerFile* )( currentItem ) )->layer();
origLayer->copySymbologySettings( *currentLayer );
break;
}
currentItem = currentItem->nextSibling();
}
}
}
else if ( originType == QgsLegendItem::LEGEND_LAYER_FILE && destType == QgsLegendItem::LEGEND_LAYER_FILE )
{
QgsDebugMsg( "Legend layer file moved to legend layer file" );
// Not used. Delete?
// QgsMapLayer* origLayer = ((QgsLegendLayerFile*)(origin))->layer();
// QgsMapLayer* destLayer = ((QgsLegendLayerFile*)(dest))->layer();
if ( dest == origin )//origin item has been moved in mouseMoveEvent such that it is under the mouse cursor now
{
if ( origin->parent()->childCount() > 1 )
{
//find the first layer in the legend layer group != origLayer and copy its settings
QTreeWidgetItem* currentItem = dest->parent()->child( 0 );
while ( currentItem )
{
if ( currentItem != origin )
{
QgsMapLayer* origLayer = (( QgsLegendLayerFile* )( origin ) )->layer();
QgsMapLayer* currentLayer = (( QgsLegendLayerFile* )( currentItem ) )->layer();
origLayer->copySymbologySettings( *currentLayer );
break;
}
currentItem = dynamic_cast<QgsLegendItem*>( currentItem )->nextSibling();
}
}
}
else
{
QgsMapLayer* origLayer = (( QgsLegendLayerFile* )( origin ) )->layer();
QgsMapLayer* destLayer = (( QgsLegendLayerFile* )( dest ) )->layer();
origLayer->copySymbologySettings( *destLayer );
}
}
else
{
QgsDebugMsg( "Other type of drag'n'drop happened!" );
}
checkLayerOrderUpdate();
mItemBeingMoved = NULL;
}
void QgsLegend::mouseDoubleClickEvent( QMouseEvent* e )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
legendLayerShowProperties();
}
void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& position )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QMenu theMenu;
QgsLegendItem* li = dynamic_cast<QgsLegendItem*>( item );
if ( li )
{
if ( li->type() == QgsLegendItem::LEGEND_LAYER_FILE )
{
( static_cast<QgsLegendLayerFile*>( li ) )->addToPopupMenu( theMenu, mToggleEditingAction );
}
else if ( li->type() == QgsLegendItem::LEGEND_LAYER )
{
( static_cast<QgsLegendLayer*>( li ) )->addToPopupMenu( theMenu, mToggleEditingAction );
if ( li->parent() )
{
theMenu.addAction( tr( "&Make to toplevel item" ), this, SLOT( makeToTopLevelItem() ) );
}
}
else if ( li->type() == QgsLegendItem::LEGEND_GROUP )
{
theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemove.png" ),
tr( "&Remove" ), this, SLOT( legendGroupRemove() ) );
}
if ( li->type() == QgsLegendItem::LEGEND_LAYER || li->type() == QgsLegendItem::LEGEND_GROUP )
{
theMenu.addAction( tr( "Re&name" ), this, SLOT( openEditor() ) );
}
}
theMenu.addAction( QgisApp::getThemeIcon( "/folder_new.png" ), tr( "&Add group" ), this, SLOT( addGroup() ) );
theMenu.addAction( QgisApp::getThemeIcon( "/mActionExpandTree.png" ), tr( "&Expand all" ), this, SLOT( expandAll() ) );
theMenu.addAction( QgisApp::getThemeIcon( "/mActionCollapseTree.png" ), tr( "&Collapse all" ), this, SLOT( collapseAll() ) );
QAction* showFileGroupsAction = theMenu.addAction( tr( "Show file groups" ), this, SLOT( showLegendLayerFileGroups() ) );
showFileGroupsAction->setCheckable( true );
showFileGroupsAction->blockSignals( true );
showFileGroupsAction->setChecked( mShowLegendLayerFiles );
showFileGroupsAction->blockSignals( false );
theMenu.exec( position );
}
void QgsLegend::initPixmaps()
{
mPixmaps.mOriginalPixmap = QgisApp::getThemePixmap( "/mActionFileSmall.png" );
mPixmaps.mInOverviewPixmap = QgisApp::getThemePixmap( "/mActionInOverview.png" );
mPixmaps.mEditablePixmap = QgisApp::getThemePixmap( "/mIconEditable.png" );
mPixmaps.mProjectionErrorPixmap = QgisApp::getThemePixmap( "/mIconProjectionProblem.png" );
}
int QgsLegend::getItemPos( QTreeWidgetItem* item )
{
int counter = 1;
QTreeWidgetItem* theItem = firstItem();
while ( theItem )
{
if ( theItem == item )
{
return counter;
}
theItem = nextItem( theItem );
++counter;
}
return -1;
}
void QgsLegend::addLayer( QgsMapLayer * layer )
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QgsLegendLayer * llayer = new QgsLegendLayer( layer->name() );//generate entry for mStateOfCheckBoxes below
QgsLegendLayerFileGroup * llfgroup = new QgsLegendLayerFileGroup( llayer, QString( "Files" ) );
QgsLegendLayerFile * llfile = new QgsLegendLayerFile( llfgroup, QgsLegendLayerFile::nameFromLayer( layer ), layer );
llayer->updateIcon();
llayer->setToolTip( 0, layer->publicSource() );
//set the correct check states
blockSignals( true );
if ( llfile->isVisible() )
{
llfile->setCheckState( 0, Qt::Checked );
llayer->setCheckState( 0, Qt::Checked );
mStateOfCheckBoxes.insert( std::make_pair( llfile, Qt::Checked ) ); //insert the check state into the map to query for changes later
mStateOfCheckBoxes.insert( std::make_pair( llayer, Qt::Checked ) );
}
else
{
llfile->setCheckState( 0, Qt::Unchecked );
llayer->setCheckState( 0, Qt::Unchecked );
mStateOfCheckBoxes.insert( std::make_pair( llfile, Qt::Unchecked ) ); //insert the check state into the map to query for changes later
mStateOfCheckBoxes.insert( std::make_pair( llayer, Qt::Unchecked ) );
}
blockSignals( false );
insertTopLevelItem( 0, llayer );
setItemExpanded( llayer, true );
setItemExpanded( llfgroup, false );
//only if qsetting for 'legend layer file visible' is not set
if ( !mShowLegendLayerFiles )
{
llfgroup->setHidden( true );
}
llfile->updateLegendItem();
refreshLayerSymbology( layer->getLayerID() );
updateMapCanvasLayerSet();
// first layer?
if ( mMapCanvas->layerCount() == 1 )
mMapCanvas->zoomToFullExtent();
setCurrentItem( llayer );
//make the QTreeWidget item up-to-date
doItemsLayout();
// setup connections that will update the layer icons
connect( layer, SIGNAL( editingStarted() ), llayer, SLOT( updateIcon() ) );
connect( layer, SIGNAL( editingStopped() ), llayer, SLOT( updateIcon() ) );
}
QgsLegendLayerFile* QgsLegend::currentLayerFile()
{
QgsLegendItem* citem = dynamic_cast<QgsLegendItem*>( currentItem() );
if ( citem )
{
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>( citem );
if ( llf )
{
return llf; //the current item is itself a legend layer file
}
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>( citem );
if ( ll )
{
return ll->firstLayerFile(); //the current item is a legend layer, so return its first layer
}
QgsLegendLayer* lpl = dynamic_cast<QgsLegendLayer*>( citem->parent() );
if ( lpl )
{
return lpl->firstLayerFile(); //the parent of the current item is a legend layer, return its first layer
}
}
return 0;
}
QgsMapLayer* QgsLegend::currentLayer()
{
QgsLegendLayerFile* llf = currentLayerFile();
if ( llf )
{
return llf->layer();
}
else
{
return 0;
}
}
void QgsLegend::legendGroupRemove()
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup*>( currentItem() );
if ( lg )
{
//delete the legend layers first
QTreeWidgetItem * child = lg->child( 0 );
while ( child )
{
setCurrentItem( child );
legendLayerRemove();
child = lg->child( 0 );
}
delete lg;
adjustIconSize();
}
}
void QgsLegend::legendLayerRemove()
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
//if the current item is a legend layer: remove all layers of the current legendLayer
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>( currentItem() );
if ( ll )
{
std::list<QgsMapLayer*> maplayers = ll->mapLayers();
int layerCount = maplayers.size();
for ( std::list<QgsMapLayer*>::iterator it = maplayers.begin(); it != maplayers.end(); ++it )
{
//remove the layer
if ( *it )
{
//the map layer registry emits a signal and this will remove the legend layer file
//from the legend and from memory by calling QgsLegend::removeLayer(QString layer key)
QgsMapLayerRegistry::instance()->removeMapLayer(( *it )->getLayerID() );
}
}
if ( layerCount == 0 )
{
// delete the item only when it didn't have any child legend layer files
// (otherwise it is deleted in QgsLegend::removeLayer when deleting last legend layer file)
mStateOfCheckBoxes.erase( ll );
removeItem( ll );
delete ll;
}
adjustIconSize();
return;
}
//if the current item is a legend layer file
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>( currentItem() );
if ( llf )
{
if ( llf->layer() )
{
//the map layer registry emits a signal an this will remove the legend layer
//from the legend and from memory by calling QgsLegend::removeLayer(QString layer key)
QgsMapLayerRegistry::instance()->removeMapLayer( llf->layer()->getLayerID() );
}
}
return;
}
void QgsLegend::legendLayerShowProperties()
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QgsLegendItem* li = dynamic_cast<QgsLegendItem*>( currentItem() );
QgsLegendLayerFile* llf = 0;
if ( !li )
{
return;
}
if ( li->type() == QgsLegendItem::LEGEND_LAYER_FILE )
{
llf = dynamic_cast<QgsLegendLayerFile*>( li );
}
else if ( li->type() == QgsLegendItem::LEGEND_LAYER )
{
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>( li );
llf = ll->firstLayerFile();
}
if ( !llf )
{
return;
}
//QgsDebugMsg("Showing layer properties dialog");
QgsMapLayer* ml = llf->layer();
/*
TODO: Consider reusing the property dialogs again.
Sometimes around mid 2005, the property dialogs were saved for later reuse;
this resulted in a time savings when reopening the dialog. The code below
cannot be used as is, however, simply by saving the dialog pointer here.
Either the map layer needs to be passed as an argument to sync or else
a separate copy of the dialog pointer needs to be stored with each layer.
*/
if ( ml->type() == QgsMapLayer::RasterLayer )
{
QgsRasterLayerProperties *rlp = NULL; // See note above about reusing this
if ( rlp )
{
rlp->sync();
}
else
{
rlp = new QgsRasterLayerProperties( ml );
connect( rlp, SIGNAL( refreshLegend( QString, bool ) ), this, SLOT( refreshLayerSymbology( QString, bool ) ) );
}
rlp->exec();
delete rlp; // delete since dialog cannot be reused without updating code
}
else // VECTOR
{
QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( ml );
QgsVectorLayerProperties *vlp = NULL; // See note above about reusing this
if ( vlp )
{
vlp->reset();
}
else
{
vlp = new QgsVectorLayerProperties( vlayer );
connect( vlp, SIGNAL( refreshLegend( QString, bool ) ), this, SLOT( refreshLayerSymbology( QString, bool ) ) );
}
vlp->exec();
delete vlp; // delete since dialog cannot be reused without updating code
}
llf->updateLegendItem();
}
void QgsLegend::legendLayerShowInOverview()
{
if ( !mMapCanvas || mMapCanvas->isDrawing() )
{
return;
}
QgsLegendItem* li = dynamic_cast<QgsLegendItem*>( currentItem() );
if ( !li )
return;
if ( li->type() == QgsLegendItem::LEGEND_LAYER_FILE )
{
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>( li );
if ( !llf ) return;
llf->showInOverview();
}
else if ( li->type() == QgsLegendItem::LEGEND_LAYER )
{
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>( li );
if ( !ll ) return;
ll->showInOverview();
}
}
void QgsLegend::expandAll()
{
QTreeWidgetItem* theItem = firstItem();
while ( theItem )
{
setExpanded( indexFromItem( theItem ), true );
theItem = nextItem( theItem );
}
}
void QgsLegend::collapseAll()
{
QTreeWidgetItem* theItem = firstItem();
while ( theItem )
{
setExpanded( indexFromItem( theItem ), false );
theItem = nextItem( theItem );
}
}
bool QgsLegend::writeXML( QDomNode & legendnode, QDomDocument & document )
{
QDomNode tmplegendnode = legendnode; /*copy of the legendnode*/
QDomElement legendgroupnode;
QDomElement legendlayernode;
QDomElement layerfilegroupnode;
QDomElement legendsymbolnode;
QDomElement legendpropertynode;
QDomElement legendlayerfilenode;
QgsLegendLayerFile* llf;
Qt::CheckState cstate; //check state for legend layers and legend groups
QTreeWidgetItem* currentItem = firstItem();
while ( currentItem )
{
QgsLegendItem *item = dynamic_cast<QgsLegendItem*>( currentItem );
if ( item )
{
switch ( item->type() )
{
case QgsLegendItem::LEGEND_GROUP:
//make sure the legendnode is 'legend' again after a legend group
if ( !( item->parent() ) )
{
legendnode = tmplegendnode;
}
legendgroupnode = document.createElement( "legendgroup" );
if ( isItemExpanded( item ) )
{
legendgroupnode.setAttribute( "open", "true" );
}
else
{
legendgroupnode.setAttribute( "open", "false" );
}
legendgroupnode.setAttribute( "name", item->text( 0 ) );
cstate = item->checkState( 0 );
if ( cstate == Qt::Checked )
{
legendgroupnode.setAttribute( "checked", "Qt::Checked" );
}
else if ( cstate == Qt::Unchecked )
{
legendgroupnode.setAttribute( "checked", "Qt::Unchecked" );
}
else if ( cstate == Qt::PartiallyChecked )
{
legendgroupnode.setAttribute( "checked", "Qt::PartiallyChecked" );
}
legendnode.appendChild( legendgroupnode );
tmplegendnode = legendnode;
legendnode = legendgroupnode;
break;
case QgsLegendItem::LEGEND_LAYER:
//make sure the legendnode is 'legend' again after a legend group
if ( !( item->parent() ) )
{
legendnode = tmplegendnode;
}
legendlayernode = document.createElement( "legendlayer" );
if ( isItemExpanded( item ) )
{
legendlayernode.setAttribute( "open", "true" );
}
else
{
legendlayernode.setAttribute( "open", "false" );
}
cstate = item->checkState( 0 );
if ( cstate == Qt::Checked )
{
legendlayernode.setAttribute( "checked", "Qt::Checked" );
}
else if ( cstate == Qt::Unchecked )
{
legendlayernode.setAttribute( "checked", "Qt::Unchecked" );
}
else if ( cstate == Qt::PartiallyChecked )
{
legendlayernode.setAttribute( "checked", "Qt::PartiallyChecked" );
}
legendlayernode.setAttribute( "name", item->text( 0 ) );
legendnode.appendChild( legendlayernode );
break;
case QgsLegendItem::LEGEND_PROPERTY_GROUP:
legendpropertynode = document.createElement( "propertygroup" );
if ( isItemExpanded( item ) )
{
legendpropertynode.setAttribute( "open", "true" );
}
else
{
legendpropertynode.setAttribute( "open", "false" );
}
legendlayernode.appendChild( legendpropertynode );
break;
case QgsLegendItem::LEGEND_SYMBOL_GROUP:
legendsymbolnode = document.createElement( "symbolgroup" );
if ( isItemExpanded( item ) )
{
legendsymbolnode.setAttribute( "open", "true" );
}
else
{
legendsymbolnode.setAttribute( "open", "false" );
}
legendlayernode.appendChild( legendsymbolnode );
break;
case QgsLegendItem::LEGEND_LAYER_FILE_GROUP:
layerfilegroupnode = document.createElement( "filegroup" );
if ( isItemExpanded( item ) )
{
layerfilegroupnode.setAttribute( "open", "true" );
}
else
{
layerfilegroupnode.setAttribute( "open", "false" );
}
if ( isItemHidden( item ) )
{
layerfilegroupnode.setAttribute( "hidden", "true" );
}
else
{
layerfilegroupnode.setAttribute( "hidden", "false" );
}
legendlayernode.appendChild( layerfilegroupnode );
break;
case QgsLegendItem::LEGEND_LAYER_FILE:
legendlayerfilenode = document.createElement( "legendlayerfile" );
llf = dynamic_cast<QgsLegendLayerFile*>( item );
if ( llf )
{
QgsMapLayer* layer = llf->layer();
// layer id
legendlayerfilenode.setAttribute( "layerid", layer->getLayerID() );
layerfilegroupnode.appendChild( legendlayerfilenode );
// visible flag
legendlayerfilenode.setAttribute( "visible", llf->isVisible() );
// show in overview flag
legendlayerfilenode.setAttribute( "isInOverview", llf->isInOverview() );
}
break;
default: //do nothing for the leaf nodes
break;
}
}
currentItem = nextItem( currentItem );
}
return true;
}
bool QgsLegend::readXML( QDomNode& legendnode )
{
QDomElement childelem;
QDomNode child;
QgsLegendGroup* lastGroup = 0; //pointer to the last inserted group
QgsLegendLayer* lastLayer = 0; //pointer to the last inserted legendlayer
QgsLegendLayerFileGroup* lastLayerFileGroup = 0; //pointer to the last inserted layerfilegroup
child = legendnode.firstChild();
// For some unexplained reason, collapsing/expanding the legendLayer items
// immediately after they have been created doesn't work (they all end up
// expanded). The legendGroups and legendLayerFiles seems ok through. The
// workaround is to store the required states of the legendLayers and set
// them at the end of this function.
QList<QTreeWidgetItem*> collapsed, expanded;
if ( !child.isNull() )
{
clear(); //remove all items first
mStateOfCheckBoxes.clear();
do
{
QDomElement childelem = child.toElement();
QString name = childelem.attribute( "name" );
//test every possibility of element...
if ( childelem.tagName() == "legendgroup" )
{
QgsLegendGroup* theGroup = new QgsLegendGroup( this, name );
childelem.attribute( "open" ) == "true" ? expanded.push_back( theGroup ) : collapsed.push_back( theGroup );
//set the checkbox of the legend group to the right state
blockSignals( true );
QString checked = childelem.attribute( "checked" );
if ( checked == "Qt::Checked" )
{
theGroup->setCheckState( 0, Qt::Checked );