Skip to content

Commit

Permalink
little legend cleanup: remove member mStateOfCheckBoxes
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12076 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 10, 2009
1 parent 80ecf23 commit 64eeec8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
40 changes: 15 additions & 25 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -119,13 +119,12 @@ void QgsLegend::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetI
void QgsLegend::addGroup() void QgsLegend::addGroup()
{ {
QgsLegendGroup* group = new QgsLegendGroup( this, tr( "group" ) ); 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 group->setData( 0, Qt::UserRole, Qt::Checked );
setExpanded( indexFromItem( group ), true ); setExpanded( indexFromItem( group ), true );
} }


void QgsLegend::removeAll() void QgsLegend::removeAll()
{ {
mStateOfCheckBoxes.clear();
clear(); clear();
mPixmapWidthValues.clear(); mPixmapWidthValues.clear();
mPixmapHeightValues.clear(); mPixmapHeightValues.clear();
Expand Down Expand Up @@ -182,8 +181,6 @@ void QgsLegend::removeLayer( QString layer_key )


if ( ll && ll->layer() && ll->layer()->getLayerID() == layer_key ) if ( ll && ll->layer() && ll->layer()->getLayerID() == layer_key )
{ {
//remove the map entry for the checkbox
mStateOfCheckBoxes.erase( ll );
removeItem( ll ); removeItem( ll );
delete ll; delete ll;


Expand Down Expand Up @@ -501,12 +498,12 @@ void QgsLegend::addLayer( QgsMapLayer * layer )
if ( llayer->isVisible() ) if ( llayer->isVisible() )
{ {
llayer->setCheckState( 0, Qt::Checked ); llayer->setCheckState( 0, Qt::Checked );
mStateOfCheckBoxes.insert( std::make_pair( llayer, Qt::Checked ) ); llayer->setData( 0, Qt::UserRole, Qt::Checked );
} }
else else
{ {
llayer->setCheckState( 0, Qt::Unchecked ); llayer->setCheckState( 0, Qt::Unchecked );
mStateOfCheckBoxes.insert( std::make_pair( llayer, Qt::Unchecked ) ); llayer->setData( 0, Qt::UserRole, Qt::Unchecked );
} }
blockSignals( false ); blockSignals( false );


Expand Down Expand Up @@ -909,7 +906,6 @@ bool QgsLegend::readXML( QDomNode& legendnode )
if ( !child.isNull() ) if ( !child.isNull() )
{ {
clear(); //remove all items first clear(); //remove all items first
mStateOfCheckBoxes.clear();


do do
{ {
Expand All @@ -927,24 +923,24 @@ bool QgsLegend::readXML( QDomNode& legendnode )
if ( checked == "Qt::Checked" ) if ( checked == "Qt::Checked" )
{ {
theGroup->setCheckState( 0, Qt::Checked ); theGroup->setCheckState( 0, Qt::Checked );
mStateOfCheckBoxes.insert( std::make_pair( theGroup, Qt::Checked ) ); theGroup->setData( 0, Qt::UserRole, Qt::Checked );
} }
else if ( checked == "Qt::Unchecked" ) else if ( checked == "Qt::Unchecked" )
{ {
theGroup->setCheckState( 0, Qt::Unchecked ); theGroup->setCheckState( 0, Qt::Unchecked );
mStateOfCheckBoxes.insert( std::make_pair( theGroup, Qt::Unchecked ) ); theGroup->setData( 0, Qt::UserRole, Qt::Checked );
} }
else if ( checked == "Qt::PartiallyChecked" ) else if ( checked == "Qt::PartiallyChecked" )
{ {
theGroup->setCheckState( 0, Qt::PartiallyChecked ); theGroup->setCheckState( 0, Qt::PartiallyChecked );
mStateOfCheckBoxes.insert( std::make_pair( theGroup, Qt::PartiallyChecked ) ); theGroup->setData( 0, Qt::UserRole, Qt::PartiallyChecked );
} }
blockSignals( false ); blockSignals( false );
lastGroup = theGroup; lastGroup = theGroup;
} }
else if ( childelem.tagName() == "legendlayer" ) else if ( childelem.tagName() == "legendlayer" )
{ {
bool isOpen; // to recieve info whether the item is open or closed bool isOpen; // to receive info whether the item is open or closed
lastLayer = readLayerFromXML(childelem, isOpen); lastLayer = readLayerFromXML(childelem, isOpen);


if (lastLayer) if (lastLayer)
Expand Down Expand Up @@ -1039,13 +1035,13 @@ QgsLegendLayer* QgsLegend::readLayerFromXML(QDomElement& childelem, bool& isOpen
{ {
ll->setVisible(true); ll->setVisible(true);
ll->setCheckState( 0, Qt::Checked ); ll->setCheckState( 0, Qt::Checked );
mStateOfCheckBoxes.insert( std::make_pair( ll, Qt::Checked ) ); ll->setData( 0, Qt::UserRole, Qt::Checked );
} }
else if ( checked == "Qt::Unchecked" ) else if ( checked == "Qt::Unchecked" )
{ {
ll->setVisible(false); ll->setVisible(false);
ll->setCheckState( 0, Qt::Unchecked ); ll->setCheckState( 0, Qt::Unchecked );
mStateOfCheckBoxes.insert( std::make_pair( ll, Qt::Unchecked ) ); ll->setData( 0, Qt::UserRole, Qt::Unchecked );
} }
blockSignals( false ); blockSignals( false );


Expand Down Expand Up @@ -1336,8 +1332,6 @@ void QgsLegend::removeItem( QTreeWidgetItem* item )


void QgsLegend::updateMapCanvasLayerSet() void QgsLegend::updateMapCanvasLayerSet()
{ {
//std::deque<QString> layers = layerIDs();

QList<QgsMapCanvasLayer> layers; QList<QgsMapCanvasLayer> layers;


// create list of the layers // create list of the layers
Expand Down Expand Up @@ -1484,12 +1478,8 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
theLegendLayer->layer()->setLayerName( theLegendLayer->text( 0 ) ); theLegendLayer->layer()->setLayerName( theLegendLayer->text( 0 ) );
} }


std::map<QTreeWidgetItem*, Qt::CheckState>::iterator it = mStateOfCheckBoxes.find( item );
if ( it == mStateOfCheckBoxes.end() )
return;

// has the checkState changed? // has the checkState changed?
if ( it->second == item->checkState( 0 ) ) if ( item->data(0, Qt::UserRole).toInt() == item->checkState( 0 ) )
return; return;


QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item ); //item is a legend group QgsLegendGroup* lg = dynamic_cast<QgsLegendGroup *>( item ); //item is a legend group
Expand Down Expand Up @@ -1518,7 +1508,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
blockSignals( true ); blockSignals( true );
( *iter )->setCheckState( 0, item->checkState( 0 ) ); ( *iter )->setCheckState( 0, item->checkState( 0 ) );
blockSignals( false ); blockSignals( false );
mStateOfCheckBoxes[( *iter )] = item->checkState( 0 ); item->setData( 0, Qt::UserRole, item->checkState( 0 ) );
if (( *iter )->layer() ) if (( *iter )->layer() )
{ {
( *iter )->setVisible( item->checkState( 0 ) == Qt::Checked ); ( *iter )->setVisible( item->checkState( 0 ) == Qt::Checked );
Expand All @@ -1529,7 +1519,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
// off, as turning it on causes a refresh. // off, as turning it on causes a refresh.
if ( renderFlagState ) if ( renderFlagState )
mMapCanvas->setRenderFlag( true ); mMapCanvas->setRenderFlag( true );
mStateOfCheckBoxes[item] = item->checkState( 0 ); item->setData( 0, Qt::UserRole, item->checkState( 0 ) );
} }


QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer *>( item ); //item is a legend layer QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer *>( item ); //item is a legend layer
Expand All @@ -1538,7 +1528,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
blockSignals( true ); blockSignals( true );
ll->setCheckState( 0, item->checkState( 0 ) ); ll->setCheckState( 0, item->checkState( 0 ) );
blockSignals( false ); blockSignals( false );
mStateOfCheckBoxes[ll] = item->checkState( 0 ); ll->setData( 0, Qt::UserRole, ll->checkState( 0 ) );
if (ll->layer() ) if (ll->layer() )
{ {
ll->setVisible( item->checkState( 0 ) == Qt::Checked ); ll->setVisible( item->checkState( 0 ) == Qt::Checked );
Expand All @@ -1547,7 +1537,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
if ( ll->parent() ) if ( ll->parent() )
{ {
static_cast<QgsLegendGroup*>( ll->parent() )->updateCheckState(); static_cast<QgsLegendGroup*>( ll->parent() )->updateCheckState();
mStateOfCheckBoxes[ll->parent()] = ll->parent()->checkState( 0 ); ll->parent()->setData( 0, Qt::UserRole, ll->parent()->checkState( 0 ) );
} }
// If it was on, turn it back on, otherwise leave it // If it was on, turn it back on, otherwise leave it
// off, as turning it on causes a refresh. // off, as turning it on causes a refresh.
Expand All @@ -1557,7 +1547,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
} }
mMapCanvas->freeze( false ); mMapCanvas->freeze( false );
//update check state of the legend group //update check state of the legend group
mStateOfCheckBoxes[item] = item->checkState( 0 ); item->setData( 0, Qt::UserRole, item->checkState( 0 ) );
} }


// update layer set // update layer set
Expand Down
6 changes: 1 addition & 5 deletions src/app/legend/qgslegend.h
Expand Up @@ -169,7 +169,7 @@ class QgsLegend : public QTreeWidget
QgsLegendPixmaps& pixmaps() { return mPixmaps; } QgsLegendPixmaps& pixmaps() { return mPixmaps; }




void updateCheckStates( QTreeWidgetItem* item, Qt::CheckState state ) {mStateOfCheckBoxes[item] = state;} void updateCheckStates( QTreeWidgetItem* item, Qt::CheckState state ) { item->setData( 0, Qt::UserRole, state ); }


public slots: public slots:


Expand Down Expand Up @@ -404,10 +404,6 @@ class QgsLegend : public QTreeWidget
/**Pointer to the main canvas. Used for requiring repaints in case of legend changes*/ /**Pointer to the main canvas. Used for requiring repaints in case of legend changes*/
QgsMapCanvas* mMapCanvas; QgsMapCanvas* mMapCanvas;


/**Map that keeps track of which checkboxes are in which check state. This is necessary because QTreeView does not emit
a signal for check state changes*/
std::map<QTreeWidgetItem*, Qt::CheckState> mStateOfCheckBoxes;

/**Stores the width values of the LegendSymbologyItem pixmaps. The purpose of this is that the legend may automatically change /**Stores the width values of the LegendSymbologyItem pixmaps. The purpose of this is that the legend may automatically change
the global IconWidth when items are added or removed*/ the global IconWidth when items are added or removed*/
std::multiset<int> mPixmapWidthValues; std::multiset<int> mPixmapWidthValues;
Expand Down
4 changes: 2 additions & 2 deletions src/app/legend/qgslegendlayer.cpp
Expand Up @@ -385,7 +385,7 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu, QAction* toggleEditingActio
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( lyr ); QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( lyr );


// attribute table // attribute table
QAction* tableAction = theMenu.addAction( tr( "&Open attribute table" ), this, SLOT( table() ) ); theMenu.addAction( tr( "&Open attribute table" ), this, SLOT( table() ) );


// allow editing // allow editing
int cap = vlayer->dataProvider()->capabilities(); int cap = vlayer->dataProvider()->capabilities();
Expand All @@ -399,7 +399,7 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu, QAction* toggleEditingActio
} }


// save as shapefile // save as shapefile
QAction* saveShpAction = theMenu.addAction( tr( "Save as shapefile..." ), this, SLOT( saveAsShapefile() ) ); theMenu.addAction( tr( "Save as shapefile..." ), this, SLOT( saveAsShapefile() ) );


// save selection as shapefile // save selection as shapefile
QAction* saveSelectionAction = theMenu.addAction( tr( "Save selection as shapefile..." ), this, SLOT( saveSelectionAsShapefile() ) ); QAction* saveSelectionAction = theMenu.addAction( tr( "Save selection as shapefile..." ), this, SLOT( saveSelectionAsShapefile() ) );
Expand Down

0 comments on commit 64eeec8

Please sign in to comment.