32
32
#include " qgsmaprenderer.h"
33
33
#include " qgsproject.h"
34
34
#include " qgsrasterlayer.h"
35
- #include " qgsrasterlayerproperties.h"
36
- #include " qgsvectorlayerproperties.h"
37
-
38
- #include " qgsattributetabledialog.h"
39
-
40
- #include < cfloat>
35
+ #include " qgsvectorlayer.h"
41
36
42
37
#include < QFont>
43
38
#include < QDomDocument>
@@ -57,14 +52,15 @@ const int AUTOSCROLL_MARGIN = 16;
57
52
58
53
set mItemBeingMoved pointer to 0 to prevent SuSE 9.0 crash
59
54
*/
60
- QgsLegend::QgsLegend ( QWidget * parent, const char *name )
55
+ QgsLegend::QgsLegend ( QgsMapCanvas *canvas, QWidget * parent, const char *name )
61
56
: QTreeWidget( parent ),
62
57
mMousePressedFlag( false ),
63
58
mItemBeingMoved( 0 ),
64
- mToggleEditingAction( 0 ),
65
- mMapCanvas( 0 ),
59
+ mMapCanvas( canvas ),
66
60
mMinimumIconSize( 20 , 20 )
67
61
{
62
+ setObjectName ( name );
63
+
68
64
connect ( this , SIGNAL ( itemChanged ( QTreeWidgetItem*, int ) ),
69
65
this , SLOT ( handleItemChange ( QTreeWidgetItem*, int ) ) );
70
66
@@ -77,6 +73,17 @@ QgsLegend::QgsLegend( QWidget * parent, const char *name )
77
73
connect ( QgsProject::instance (), SIGNAL ( writeProject ( QDomDocument & ) ),
78
74
this , SLOT ( writeProject ( QDomDocument & ) ) );
79
75
76
+ // connect map layer registry signal to legend
77
+ connect ( QgsMapLayerRegistry::instance (), SIGNAL ( layerWillBeRemoved ( QString ) ),
78
+ this , SLOT ( removeLayer ( QString ) ) );
79
+ connect ( QgsMapLayerRegistry::instance (), SIGNAL ( removedAll () ),
80
+ this , SLOT ( removeAll () ) );
81
+ connect ( QgsMapLayerRegistry::instance (), SIGNAL ( layerWasAdded ( QgsMapLayer* ) ),
82
+ this , SLOT ( addLayer ( QgsMapLayer * ) ) );
83
+
84
+ connect ( mMapCanvas , SIGNAL ( layersChanged () ),
85
+ this , SLOT ( refreshCheckStates () ) );
86
+
80
87
// Initialise the line indicator widget.
81
88
mInsertionLine = new QWidget ( viewport () );
82
89
hideLine ();
@@ -174,13 +181,8 @@ void QgsLegend::removeGroup( int groupIndex )
174
181
}
175
182
}
176
183
177
- void QgsLegend::removeLayer ( QString layer_key )
184
+ void QgsLegend::removeLayer ( QString layerId )
178
185
{
179
- if ( !mMapCanvas || mMapCanvas ->isDrawing () )
180
- {
181
- return ;
182
- }
183
-
184
186
QgsDebugMsg ( " called." );
185
187
186
188
for ( QTreeWidgetItem* theItem = firstItem (); theItem; theItem = nextItem ( theItem ) )
@@ -191,11 +193,10 @@ void QgsLegend::removeLayer( QString layer_key )
191
193
// save legend layer (parent of a legend layer file we're going to delete)
192
194
QgsLegendLayer* ll = qobject_cast<QgsLegendLayer *>( li );
193
195
194
- if ( ll && ll->layer () && ll->layer ()->getLayerID () == layer_key )
196
+ if ( ll && ll->layer () && ll->layer ()->getLayerID () == layerId )
195
197
{
196
198
removeItem ( ll );
197
199
delete ll;
198
-
199
200
break ;
200
201
}
201
202
@@ -422,11 +423,7 @@ void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
422
423
423
424
void QgsLegend::mouseDoubleClickEvent ( QMouseEvent* e )
424
425
{
425
- if ( !mMapCanvas || mMapCanvas ->isDrawing () )
426
- {
427
- return ;
428
- }
429
- legendLayerShowProperties ();
426
+ QgisApp::instance ()->layerProperties ();
430
427
}
431
428
432
429
void QgsLegend::handleRightClickEvent ( QTreeWidgetItem* item, const QPoint& position )
@@ -444,7 +441,7 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
444
441
445
442
if ( li->type () == QgsLegendItem::LEGEND_LAYER )
446
443
{
447
- ( static_cast <QgsLegendLayer*>( li ) ) ->addToPopupMenu ( theMenu, mToggleEditingAction );
444
+ qobject_cast <QgsLegendLayer*>( li )->addToPopupMenu ( theMenu );
448
445
449
446
if ( li->parent () )
450
447
{
@@ -565,17 +562,6 @@ void QgsLegend::setLayerVisible( QgsMapLayer * layer, bool visible )
565
562
}
566
563
}
567
564
568
- void QgsLegend::setMapCanvas ( QgsMapCanvas * canvas )
569
- {
570
- if ( mMapCanvas )
571
- {
572
- disconnect ( mMapCanvas , SIGNAL ( layersChanged () ) );
573
- }
574
-
575
- mMapCanvas = canvas;
576
- connect ( mMapCanvas , SIGNAL ( layersChanged () ), this , SLOT ( refreshCheckStates () ) );
577
- }
578
-
579
565
QgsLegendLayer* QgsLegend::currentLegendLayer ()
580
566
{
581
567
QgsLegendItem* citem = dynamic_cast <QgsLegendItem *>( currentItem () );
@@ -646,84 +632,16 @@ void QgsLegend::removeGroup( QgsLegendGroup * lg )
646
632
QTreeWidgetItem * child = lg->child ( 0 );
647
633
while ( child )
648
634
{
649
- setCurrentItem ( child );
650
- removeCurrentLayer ();
635
+ QgsLegendLayer* ll = dynamic_cast <QgsLegendLayer *>( child );
636
+ if ( ll )
637
+ QgsMapLayerRegistry::instance ()->removeMapLayer ( ll->layer ()->getLayerID () );
651
638
child = lg->child ( 0 );
652
639
}
653
640
delete lg;
654
- adjustIconSize ();
655
- }
656
-
657
- void QgsLegend::removeCurrentLayer ()
658
- {
659
- if ( !mMapCanvas || mMapCanvas ->isDrawing () )
660
- {
661
- return ;
662
- }
663
-
664
- // if the current item is a legend layer: remove all layers of the current legendLayer
665
- QgsLegendLayer* ll = dynamic_cast <QgsLegendLayer *>( currentItem () );
666
- if ( !ll )
667
- return ;
668
-
669
- removeLayer ( ll->layer (), true );
670
641
671
642
adjustIconSize ();
672
643
}
673
644
674
- bool QgsLegend::removeLayer ( QgsMapLayer* ml, bool askCancelOnEditable )
675
- {
676
- if ( !ml )
677
- {
678
- return false ;
679
- }
680
-
681
- QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( ml );
682
- if ( vl )
683
- {
684
- // is layer editable and changed?
685
- if ( vl->isEditable () && vl->isModified () )
686
- {
687
- QMessageBox::StandardButton commit;
688
- if ( askCancelOnEditable )
689
- {
690
- commit = QMessageBox::information ( this ,
691
- tr ( " Stop editing" ),
692
- tr ( " Do you want to save the changes to layer %1?" ).arg ( vl->name () ),
693
- QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel );
694
- if ( commit == QMessageBox::Cancel )
695
- {
696
- return false ;
697
- }
698
- }
699
- else
700
- {
701
- commit = QMessageBox::information ( this ,
702
- tr ( " Stop editing" ),
703
- tr ( " Do you want to save the changes to layer %1?" ).arg ( vl->name () ),
704
- QMessageBox::Save | QMessageBox::Discard );
705
- }
706
-
707
- if ( commit == QMessageBox::Save )
708
- {
709
- if ( !vl->commitChanges () )
710
- {
711
- return false ;
712
- }
713
- }
714
- else if ( commit == QMessageBox::Discard )
715
- {
716
- if ( !vl->rollBack () )
717
- {
718
- return false ;
719
- }
720
- }
721
- }
722
- }
723
- QgsMapLayerRegistry::instance ()->removeMapLayer ( ml->getLayerID () );
724
- return true ;
725
- }
726
-
727
645
void QgsLegend::moveLayer ( QgsMapLayer * ml, int groupIndex )
728
646
{
729
647
if ( !ml )
@@ -740,35 +658,6 @@ void QgsLegend::moveLayer( QgsMapLayer * ml, int groupIndex )
740
658
insertItem ( layer, group );
741
659
}
742
660
743
- void QgsLegend::legendLayerShowProperties ()
744
- {
745
- if ( !mMapCanvas || mMapCanvas ->isDrawing () )
746
- {
747
- return ;
748
- }
749
-
750
- QgsLegendItem* li = dynamic_cast <QgsLegendItem *>( currentItem () );
751
-
752
- if ( !li )
753
- {
754
- return ;
755
- }
756
-
757
- if ( li->type () != QgsLegendItem::LEGEND_LAYER )
758
- return ;
759
-
760
- QgsLegendLayer* ll = qobject_cast<QgsLegendLayer *>( li );
761
- if ( !ll )
762
- return ;
763
-
764
- // QgsDebugMsg("Showing layer properties dialog");
765
-
766
- QgisApp::instance ()->showLayerProperties ( ll->layer () );
767
-
768
- ll->updateIcon ();
769
-
770
- }
771
-
772
661
void QgsLegend::legendLayerShowInOverview ()
773
662
{
774
663
if ( !mMapCanvas || mMapCanvas ->isDrawing () )
@@ -1732,35 +1621,6 @@ void QgsLegend::legendLayerZoomNative()
1732
1621
}
1733
1622
}
1734
1623
1735
- void QgsLegend::legendLayerAttributeTable ()
1736
- {
1737
- if ( !mMapCanvas || mMapCanvas ->isDrawing () )
1738
- {
1739
- return ;
1740
- }
1741
-
1742
- QgsVectorLayer *vlayer = 0 ;
1743
-
1744
- // try whether it's a legend layer
1745
- QgsLegendLayer* ll = dynamic_cast <QgsLegendLayer *>( currentItem () );
1746
- if ( !ll )
1747
- {
1748
- // nothing selected
1749
- QMessageBox::information ( this ,
1750
- tr ( " No Layer Selected" ),
1751
- tr ( " To open an attribute table, you must select a vector layer in the legend" ) );
1752
- return ;
1753
- }
1754
-
1755
- vlayer = qobject_cast<QgsVectorLayer *>( ll->layer () );
1756
- if ( vlayer )
1757
- {
1758
- QgsAttributeTableDialog *mDialog = new QgsAttributeTableDialog ( vlayer );
1759
- mDialog ->show ();
1760
- // the dialog will be deleted by itself on close
1761
- }
1762
- }
1763
-
1764
1624
void QgsLegend::readProject ( const QDomDocument & doc )
1765
1625
{
1766
1626
QDomNodeList nodes = doc.elementsByTagName ( " legend" );
0 commit comments