Skip to content

Commit

Permalink
update composer legend on layer level
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@9190 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Aug 27, 2008
1 parent 874b29b commit fc29941
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 13 deletions.
39 changes: 39 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.cpp
Expand Up @@ -340,3 +340,42 @@ void QgsComposerLegendWidget::on_mEditPushButton_clicked()
mLegend->update();
}
}

void QgsComposerLegendWidget::on_mUpdatePushButton_clicked()
{
//get current item
QStandardItemModel* itemModel = dynamic_cast<QStandardItemModel*>( mItemTreeView->model() );
if ( !itemModel )
{
return;
}

//get current item
QModelIndex currentIndex = mItemTreeView->currentIndex();
if ( !currentIndex.isValid() )
{
return;
}

QStandardItem* currentItem = itemModel->itemFromIndex( currentIndex );
if ( !currentItem )
{
return;
}

QModelIndex parentIndex = currentIndex.parent();
if ( !parentIndex.isValid() ) // a layer item
{
QString mapLayerId = currentItem->data().toString();
mLegend->model()->updateLayer(mapLayerId);
mLegend->update();
}
}

void QgsComposerLegendWidget::on_mUpdateAllPushButton_clicked()
{
if(mLegend)
{
mLegend->updateLegend();
}
}
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.h
Expand Up @@ -52,6 +52,8 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
void on_mMoveUpPushButton_clicked();
void on_mRemovePushButton_clicked();
void on_mEditPushButton_clicked();
void on_mUpdatePushButton_clicked();
void on_mUpdateAllPushButton_clicked();

private:
QgsComposerLegendWidget();
Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -438,6 +438,13 @@ QFont QgsComposerLegend::itemFont() const
return mItemFont;
}

void QgsComposerLegend::updateLegend()
{
mLegendModel.setLayerSet(layerIdList());
adjustBoxSize();
update();
}

bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc )
{
if ( elem.isNull() )
Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposerlegend.h
Expand Up @@ -78,6 +78,9 @@ class CORE_EXPORT QgsComposerLegend: public QObject, public QgsComposerItem
double symbolHeight() const {return mSymbolHeight;}
void setSymbolHeight( double h ) {mSymbolHeight = h;}

/**Updates the model and all legend entries*/
void updateLegend();

/** stores state in Dom node
* @param elem is Dom element corresponding to 'Composer' tag
* @param temp write template file
Expand Down
52 changes: 52 additions & 0 deletions src/core/composer/qgslegendmodel.cpp
Expand Up @@ -230,6 +230,7 @@ void QgsLegendModel::removeAllSymbols()
mSymbols.clear();
}

#if 0
void QgsLegendModel::updateLayerEntries( const QStringList& newLayerIds )
{
if ( !invisibleRootItem() )
Expand Down Expand Up @@ -276,6 +277,57 @@ void QgsLegendModel::updateLayerEntries( const QStringList& newLayerIds )

mLayerIds = newLayerIds;
}
#endif //0

void QgsLegendModel::updateLayer(const QString& layerId)
{
//find layer item
QStandardItem* layerItem = 0;
QStandardItem* currentLayerItem = 0;

int numRootItems = rowCount();
for ( int i = 0; i < numRootItems ; ++i )
{
currentLayerItem = item( i );
if ( !currentLayerItem )
{
continue;
}

QString currentId = currentLayerItem->data().toString();
if ( currentId == layerId )
{
layerItem = currentLayerItem;
break;
}
}

if(layerItem)
{
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer(layerId);
if(mapLayer)
{
//delete all the entries under layer item
for(int i = rowCount() - 1; i >= 0; --i)
{
layerItem->removeRow(i);
}

//and add the new ones...
switch(mapLayer->type())
{
case QgsMapLayer::VECTOR:
addVectorLayerItems(layerItem, mapLayer);
break;
case QgsMapLayer::RASTER:
addRasterLayerItem(layerItem, mapLayer);
break;
default:
break;
}
}
}
}

void QgsLegendModel::removeLayer( const QString& layerId )
{
Expand Down
5 changes: 4 additions & 1 deletion src/core/composer/qgslegendmodel.h
Expand Up @@ -41,7 +41,10 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel

void setLayerSet( const QStringList& layerIds );
/**Apply added or removed layers to the model*/
void updateLayerEntries( const QStringList& newLayerIds );
//void updateLayerEntries( const QStringList& newLayerIds );

/**Updates the symbology of a single layer*/
void updateLayer(const QString& layerId);

bool writeXML( QDomElement& composerLegendElem, QDomDocument& doc );
bool readXML( const QDomElement& legendModelElem, const QDomDocument& doc );
Expand Down
25 changes: 13 additions & 12 deletions src/ui/qgscomposerlegendwidgetbase.ui
Expand Up @@ -5,8 +5,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>496</width>
<height>626</height>
<width>560</width>
<height>537</height>
</rect>
</property>
<property name="sizePolicy" >
Expand Down Expand Up @@ -183,19 +183,20 @@
</widget>
</item>
<item row="0" column="4" >
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
<widget class="QPushButton" name="mUpdatePushButton" >
<property name="text" >
<string>update</string>
</property>
<property name="sizeHint" >
<size>
<width>121</width>
<height>33</height>
</size>
</widget>
</item>
<item row="0" column="5" >
<widget class="QPushButton" name="mUpdateAllPushButton" >
<property name="text" >
<string>update all</string>
</property>
</spacer>
</widget>
</item>
<item row="1" column="0" colspan="5" >
<item row="1" column="0" colspan="6" >
<widget class="QTreeView" name="mItemTreeView" >
<property name="headerHidden" stdset="0" >
<bool>true</bool>
Expand Down

0 comments on commit fc29941

Please sign in to comment.