Skip to content

Commit 855d370

Browse files
author
jef
committedNov 7, 2010
more legend refactoring to fix it
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14526 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

4 files changed

+84
-103
lines changed

4 files changed

+84
-103
lines changed
 

‎src/app/legend/qgslegend.cpp‎

Lines changed: 76 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
5252
, mMousePressedFlag( false )
5353
, mMapCanvas( canvas )
5454
, mMinimumIconSize( 20, 20 )
55+
, mChanging( false )
5556
{
5657
setObjectName( name );
5758

@@ -145,6 +146,7 @@ void QgsLegend::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetI
145146
{
146147
mMapCanvas->setCurrentLayer( layer );
147148
}
149+
148150
emit currentLayerChanged( layer );
149151
}
150152

@@ -406,7 +408,13 @@ void QgsLegend::updateGroupCheckStates( QTreeWidgetItem *item )
406408
updateGroupCheckStates( item->child( i ) );
407409
}
408410

409-
lg->updateCheckState();
411+
Qt::CheckState theState = lg->pendingCheckState();
412+
if ( theState != lg->checkState( 0 ) )
413+
{
414+
blockSignals( true );
415+
lg->setCheckState( 0, theState );
416+
blockSignals( false );
417+
}
410418
}
411419

412420
void QgsLegend::mouseReleaseEvent( QMouseEvent * e )
@@ -587,14 +595,7 @@ void QgsLegend::addLayer( QgsMapLayer * layer )
587595

588596
//set the correct check states
589597
blockSignals( true );
590-
if ( llayer->isVisible() )
591-
{
592-
llayer->setCheckState( 0, Qt::Checked );
593-
}
594-
else
595-
{
596-
llayer->setCheckState( 0, Qt::Unchecked );
597-
}
598+
llayer->setCheckState( 0, llayer->isVisible() ? Qt::Checked : Qt::Unchecked );
598599
blockSignals( false );
599600

600601
QgsLegendGroup *lg = dynamic_cast<QgsLegendGroup *>( currentItem() );
@@ -1507,119 +1508,106 @@ void QgsLegend::removePixmapHeightValue( int height )
15071508
}
15081509

15091510

1510-
void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
1511+
void QgsLegend::handleItemChange( QTreeWidgetItem* item, int column )
15111512
{
15121513
if ( !item )
15131514
{
15141515
return;
15151516
}
15161517

1517-
//if the text of a QgsLegendLayer has changed, change the display names of all its maplayers
1518-
// TODO: is this still necessary?
1519-
QgsLegendLayer* theLegendLayer = dynamic_cast<QgsLegendLayer *>( item ); //item is a legend layer
1520-
if ( theLegendLayer )
1521-
{
1522-
theLegendLayer->layer()->setLayerName( theLegendLayer->text( 0 ) );
1523-
}
1524-
1525-
// has the checkState changed?
1526-
if ( item->data( 0, Qt::UserRole ).toInt() == item->checkState( 0 ) )
1527-
return;
1528-
1529-
saveCheckStates( invisibleRootItem() );
1518+
QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( item );
1519+
QgsLegendGroup *lg = dynamic_cast<QgsLegendGroup *>( item );
15301520

1531-
bool renderFlagState = mMapCanvas->renderFlag();
1532-
if ( renderFlagState )
1533-
mMapCanvas->setRenderFlag( false );
1534-
1535-
if ( !item->isSelected() )
1521+
if ( !ll && !lg )
15361522
{
1537-
propagateItemChange( item, item->checkState( 0 ) );
1523+
return;
15381524
}
1525+
1526+
#ifdef QGISDEBUG
1527+
if ( item->checkState( 0 ) == Qt::Checked )
1528+
showItem( "handleItemChange[checked]", item );
1529+
else if ( item->checkState( 0 ) == Qt::Unchecked )
1530+
showItem( "handleItemChange[unchecked]", item );
1531+
else if ( item->checkState( 0 ) == Qt::PartiallyChecked )
1532+
showItem( "handleItemChange[partially]", item );
15391533
else
1534+
showItem( "handleItemChange[?]", item );
1535+
#endif
1536+
1537+
if ( ll )
15401538
{
1541-
foreach( QTreeWidgetItem * i, selectedItems() )
1542-
{
1543-
propagateItemChange( i, item->checkState( 0 ) );
1544-
}
1539+
//if the text of a QgsLegendLayer has changed, change the display names of all its maplayers
1540+
// TODO: is this still necessary?
1541+
ll->layer()->setLayerName( ll->text( 0 ) );
15451542
}
15461543

1547-
// update layer set
1548-
updateMapCanvasLayerSet();
1544+
bool renderFlagState;
1545+
bool changing = mChanging;
1546+
mChanging = true;
15491547

1550-
// If it was on, turn it back on, otherwise leave it
1551-
// off, as turning it on causes a refresh.
1552-
if ( renderFlagState )
1553-
mMapCanvas->setRenderFlag( true );
1554-
}
1555-
1556-
void QgsLegend::saveCheckStates( QTreeWidgetItem *item )
1557-
{
1558-
for ( int i = 0; i < item->childCount(); i++ )
1548+
if ( !changing )
15591549
{
1560-
QTreeWidgetItem *child = item->child( i );
1561-
child->setData( 0, Qt::UserRole, child->checkState( 0 ) );
1562-
saveCheckStates( child );
1563-
}
1564-
}
1550+
renderFlagState = mMapCanvas->renderFlag();
1551+
if ( renderFlagState )
1552+
mMapCanvas->setRenderFlag( false );
15651553

1566-
void QgsLegend::propagateItemChange( QTreeWidgetItem *item, Qt::CheckState state )
1567-
{
1568-
QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item ); //item is a legend group
1569-
if ( lg )
1570-
{
1571-
QList<QTreeWidgetItem *> items;
1572-
items << item;
1573-
while ( !items.isEmpty() )
1554+
if ( item->isSelected() )
15741555
{
1575-
QTreeWidgetItem *litem = items.takeFirst();
1576-
1577-
QgsLegendLayer *ll = dynamic_cast<QgsLegendLayer *>( litem );
1578-
if ( ll )
1556+
foreach( QTreeWidgetItem * i, selectedItems() )
15791557
{
1580-
blockSignals( true );
1581-
ll->setCheckState( 0, state );
1582-
blockSignals( false );
1583-
1584-
if ( ll->layer() )
1558+
if ( i != item )
15851559
{
1586-
ll->setVisible( lg->checkState( 0 ) == Qt::Checked );
1560+
i->setCheckState( 0, item->checkState( 0 ) );
15871561
}
15881562
}
1589-
1590-
QgsLegendGroup *lg = dynamic_cast<QgsLegendGroup *>( litem );
1591-
if ( lg )
1592-
{
1593-
blockSignals( true );
1594-
lg->setCheckState( 0, state );
1595-
blockSignals( false );
1596-
1597-
for ( int i = 0; i < lg->childCount(); i++ )
1598-
items << lg->child( i );
1599-
}
16001563
}
16011564
}
16021565

1603-
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer *>( item ); //item is a legend layer
16041566
if ( ll )
16051567
{
1606-
blockSignals( true );
1607-
ll->setCheckState( 0, state );
1568+
ll->setVisible( ll->checkState( 0 ) == Qt::Checked );
1569+
}
16081570

1609-
if ( ll->layer() )
1571+
if ( lg && lg->checkState( 0 ) != Qt::PartiallyChecked )
1572+
{
1573+
Qt::CheckState theState = lg->checkState( 0 );
1574+
for ( int i = 0; i < item->childCount(); i++ )
16101575
{
1611-
ll->setVisible( state == Qt::Checked );
1576+
QTreeWidgetItem *child = item->child( i );
1577+
if ( child->checkState( 0 ) != item->checkState( 0 ) )
1578+
child->setCheckState( 0, theState );
16121579
}
1580+
}
16131581

1614-
QgsLegendGroup *lg = dynamic_cast<QgsLegendGroup*>( ll->parent() );
1615-
while ( lg )
1582+
// propagate updates to upper groups
1583+
for (
1584+
QgsLegendGroup *plg = dynamic_cast<QgsLegendGroup *>( item->parent() );
1585+
plg;
1586+
plg = dynamic_cast<QgsLegendGroup *>( plg->parent() )
1587+
)
1588+
{
1589+
Qt::CheckState theState = plg->pendingCheckState();
1590+
1591+
if ( theState != plg->checkState( 0 ) )
16161592
{
1617-
lg->updateCheckState();
1618-
lg = dynamic_cast<QgsLegendGroup*>( lg->parent() );
1593+
blockSignals( true );
1594+
plg->setCheckState( 0, theState );
1595+
blockSignals( false );
16191596
}
1597+
}
16201598

1621-
blockSignals( false );
1599+
if ( !changing )
1600+
{
1601+
// update layer set
1602+
updateMapCanvasLayerSet();
1603+
1604+
// If it was on, turn it back on, otherwise leave it
1605+
// off, as turning it on causes a refresh.
1606+
if ( renderFlagState )
1607+
mMapCanvas->setRenderFlag( true );
16221608
}
1609+
1610+
mChanging = changing;
16231611
}
16241612

16251613
void QgsLegend::openEditor()

‎src/app/legend/qgslegend.h‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ class QgsLegend : public QTreeWidget
189189
/**Returns a layers check state*/
190190
Qt::CheckState layerCheckState( QgsMapLayer * layer );
191191

192-
void updateCheckStates( QTreeWidgetItem* item, Qt::CheckState state ) { item->setData( 0, Qt::UserRole, state ); }
193-
194-
void updateGroupCheckStates( QTreeWidgetItem *item );
195-
196192
public slots:
197193

198194
/*!Adds a new layer group with the maplayer to the canvas*/
@@ -373,7 +369,6 @@ class QgsLegend : public QTreeWidget
373369
/**Sets all listview items to closed*/
374370
void collapseAll();
375371
void handleItemChange( QTreeWidgetItem* item, int row );
376-
void propagateItemChange( QTreeWidgetItem *item, Qt::CheckState state );
377372
/** delegates current layer to map canvas */
378373
void handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous );
379374
/**Calls openPersistentEditor for the current item*/
@@ -419,8 +414,7 @@ class QgsLegend : public QTreeWidget
419414
/**QgsLegend does not set the icon with/height to values lower than the minimum icon size*/
420415
QSize mMinimumIconSize;
421416

422-
/** save item check states */
423-
void saveCheckStates( QTreeWidgetItem *item );
417+
bool mChanging;
424418

425419
/** structure which holds pixmap which are used in legend */
426420
class QgsLegendPixmaps
@@ -447,6 +441,8 @@ class QgsLegend : public QTreeWidget
447441
void showItem( QString msg, QTreeWidgetItem *item );
448442
#endif
449443

444+
void updateGroupCheckStates( QTreeWidgetItem *item );
445+
450446
signals:
451447
void itemMoved( QModelIndex oldIndex, QModelIndex newIndex );
452448

‎src/app/legend/qgslegendgroup.cpp‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ QList<QgsLegendLayer*> QgsLegendGroup::legendLayers( bool recurse )
9393
return result;
9494
}
9595

96-
void QgsLegendGroup::updateCheckState()
96+
Qt::CheckState QgsLegendGroup::pendingCheckState()
9797
{
9898
QList<QgsLegendItem *> elements;
9999

@@ -111,7 +111,7 @@ void QgsLegendGroup::updateCheckState()
111111
}
112112

113113
if ( elements.isEmpty() )
114-
return;
114+
return Qt::PartiallyChecked;
115115

116116
Qt::CheckState theState = elements[0]->checkState( 0 );
117117
foreach( QgsLegendItem * li, elements )
@@ -123,8 +123,5 @@ void QgsLegendGroup::updateCheckState()
123123
}
124124
}
125125

126-
if ( theState != checkState( 0 ) )
127-
{
128-
setCheckState( 0, theState );
129-
}
126+
return theState;
130127
}

‎src/app/legend/qgslegendgroup.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class QgsLegendGroup : public QgsLegendItem
4040
bool insert( QgsLegendItem* theItem );
4141
/**Returns all legend layers under this group (including those of subgroups by default)*/
4242
QList<QgsLegendLayer*> legendLayers( bool recurse = true );
43-
/**Goes through all the legendlayers and sets check state to checked/partially checked/unchecked*/
44-
void updateCheckState();
43+
44+
Qt::CheckState pendingCheckState();
4545
};
4646

4747
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.