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@12076 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 10, 2009
1 parent 9640358 commit be95d79
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ void QgsLegend::handleCurrentItemChanged( QTreeWidgetItem* current, QTreeWidgetI
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
group->setData( 0, Qt::UserRole, Qt::Checked );
setExpanded( indexFromItem( group ), true );
}

void QgsLegend::removeAll()
{
mStateOfCheckBoxes.clear();
clear();
mPixmapWidthValues.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 )
{
//remove the map entry for the checkbox
mStateOfCheckBoxes.erase( ll );
removeItem( ll );
delete ll;

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

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

do
{
Expand All @@ -927,24 +923,24 @@ bool QgsLegend::readXML( QDomNode& legendnode )
if ( checked == "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" )
{
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" )
{
theGroup->setCheckState( 0, Qt::PartiallyChecked );
mStateOfCheckBoxes.insert( std::make_pair( theGroup, Qt::PartiallyChecked ) );
theGroup->setData( 0, Qt::UserRole, Qt::PartiallyChecked );
}
blockSignals( false );
lastGroup = theGroup;
}
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);

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

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

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

QList<QgsMapCanvasLayer> 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 ) );
}

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

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

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 );
( *iter )->setCheckState( 0, item->checkState( 0 ) );
blockSignals( false );
mStateOfCheckBoxes[( *iter )] = item->checkState( 0 );
item->setData( 0, Qt::UserRole, item->checkState( 0 ) );
if (( *iter )->layer() )
{
( *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.
if ( renderFlagState )
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
Expand All @@ -1538,7 +1528,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
blockSignals( true );
ll->setCheckState( 0, item->checkState( 0 ) );
blockSignals( false );
mStateOfCheckBoxes[ll] = item->checkState( 0 );
ll->setData( 0, Qt::UserRole, ll->checkState( 0 ) );
if (ll->layer() )
{
ll->setVisible( item->checkState( 0 ) == Qt::Checked );
Expand All @@ -1547,7 +1537,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
if ( ll->parent() )
{
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
// off, as turning it on causes a refresh.
Expand All @@ -1557,7 +1547,7 @@ void QgsLegend::handleItemChange( QTreeWidgetItem* item, int row )
}
mMapCanvas->freeze( false );
//update check state of the legend group
mStateOfCheckBoxes[item] = item->checkState( 0 );
item->setData( 0, Qt::UserRole, item->checkState( 0 ) );
}

// update layer set
Expand Down
6 changes: 1 addition & 5 deletions src/app/legend/qgslegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class QgsLegend : public QTreeWidget
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:

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*/
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
the global IconWidth when items are added or removed*/
std::multiset<int> mPixmapWidthValues;
Expand Down
4 changes: 2 additions & 2 deletions src/app/legend/qgslegendlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ void QgsLegendLayer::addToPopupMenu( QMenu& theMenu, QAction* toggleEditingActio
QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( lyr );

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

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

// 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
QAction* saveSelectionAction = theMenu.addAction( tr( "Save selection as shapefile..." ), this, SLOT( saveSelectionAsShapefile() ) );
Expand Down

0 comments on commit be95d79

Please sign in to comment.