Skip to content

Commit

Permalink
Reenabled update for composer legend layers
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13532 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed May 19, 2010
1 parent 16feeeb commit d550f70
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 191 deletions.
221 changes: 33 additions & 188 deletions src/core/composer/qgslegendmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,218 +271,63 @@ int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* r

void QgsLegendModel::updateItem( QStandardItem* item )
{
#if 0
if ( !item )
{
return;
}

//is it a toplevel layer item?
QModelIndex itemIndex = indexFromItem( item );
QModelIndex parentIndex = itemIndex.parent();
if ( !parentIndex.isValid() ) // a layer item?
{
updateLayer( item );
}

//take QgsSymbol* from user data
QVariant symbolVariant = item->data( Qt::UserRole + 2 );
QgsSymbol* symbol = 0;
if ( symbolVariant.canConvert<void*>() )
{
void* symbolData = symbolVariant.value<void*>();
symbol = ( QgsSymbol* )( symbolData );
}

QVariant symbolNgVariant = item->data( Qt::UserRole + 3 );
QgsSymbolV2* symbolNg = 0;
if ( symbolNgVariant.canConvert<void*>() )
//only layer items are supported for update
QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item );
if ( ! cItem )
{
void* symbolNgData = symbolVariant.value<void*>();
symbolNg = ( QgsSymbolV2* )symbolNgData;
return;
}

if ( symbol ) //vector classification item
{
updateVectorClassificationItem( item, symbol, item->text() );
}
else if ( symbolNg )
QgsComposerLegendItem::ItemType type = cItem->itemType();
if ( type == QgsComposerLegendItem::LayerItem )
{
updateVectorV2ClassificationItem( item, symbolNg, item->text() );
updateLayer( cItem );
}
else if ( !item->icon().isNull() ) //raster classification item
{
updateRasterClassificationItem( item );
}
#endif //0
}

void QgsLegendModel::updateLayer( QStandardItem* layerItem )
{
#if 0
if ( !layerItem )
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
if ( lItem )
{
return;
}

QString layerId = layerItem->data( Qt::UserRole + 2 ).toString();
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( mapLayer )
{
//delete all the entries under layer item
int currentRowCount = layerItem->rowCount();
for ( int i = currentRowCount - 1; i >= 0; --i )
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
if ( mapLayer )
{
layerItem->removeRow( i );
}
//delete all the entries under layer item
int currentRowCount = lItem->rowCount();
for ( int i = currentRowCount - 1; i >= 0; --i )
{
lItem->removeRow( i );
}

//and add the new ones...
switch ( mapLayer->type() )
{
case QgsMapLayer::VectorLayer:
//set layer name as item text
layerItem->setText( mapLayer->name() );

QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
if ( vLayer )
{
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>( mapLayer );
if ( vLayer )
if ( vLayer->isUsingRendererV2() )
{
if ( vLayer->isUsingRendererV2() )
{
addVectorLayerItemsV2( layerItem, vLayer );
}
else
{
addVectorLayerItems( layerItem, vLayer );
}
addVectorLayerItemsV2( lItem, vLayer );
}
else
{
addVectorLayerItems( lItem, vLayer );
}
}
break;
case QgsMapLayer::RasterLayer:
addRasterLayerItem( layerItem, mapLayer );
break;
default:
break;
}
}
#endif //0
}

void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText )
{
#if 0
//this function uses the following logic to find a classification match:
//first test if there is a symbol where lowerbound - upperbound equels itemText
//if no match found, test if there is a symbol where label equals itemText
//still no match found. Test, if there is a symbol with same pen/brush/point symbol

//get parent item
QStandardItem* parentItem = classificationItem->parent();
if ( !parentItem )
{
return;
}

//get maplayer object from parent item
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( parentItem->data( Qt::UserRole + 2 ).toString() );
if ( !ml )
{
return;
}
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( ml );
if ( !vl )
{
return;
}
int opacity = vl->getTransparency();

const QgsRenderer* layerRenderer = vl->renderer();
if ( !layerRenderer )
{
return;
}

QList<QgsSymbol*> symbolList = layerRenderer->symbols();
QList<QgsSymbol*>::iterator symbolIt;
QgsSymbol* currentSymbol = 0;

//try to find a symbol where lowerbound - upperbound matches item text
symbolIt = symbolList.begin();
for ( ; symbolIt != symbolList.end(); ++symbolIt )
{
currentSymbol = *symbolIt;
if ( currentSymbol->lowerValue() + " - " + currentSymbol->upperValue() == itemText )
{
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity, vl->getLayerID() ) );
parentItem->removeRow( classificationItem->row() );
return;
}
}

//try to find a symbol where lower value matches item text (non-numeric classifications)
symbolIt = symbolList.begin();
for ( ; symbolIt != symbolList.end(); ++symbolIt )
{
currentSymbol = *symbolIt;
if ( currentSymbol->lowerValue() == itemText )
{
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity, vl->getLayerID() ) );
parentItem->removeRow( classificationItem->row() );
return;
}
}

//try to find a symbol where label matches item text
symbolIt = symbolList.begin();
for ( ; symbolIt != symbolList.end(); ++symbolIt )
{
currentSymbol = *symbolIt;
if ( currentSymbol->label() == itemText )
{
removeSymbol( symbol );
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity, vl->getLayerID() ) );
parentItem->removeRow( classificationItem->row() );
return;
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
if ( rLayer )
{
addRasterLayerItem( lItem, rLayer );
}
}
}
#endif //0
}

void QgsLegendModel::updateVectorV2ClassificationItem( QStandardItem* classificationItem, QgsSymbolV2* symbol, QString itemText )
{
//todo...
}


void QgsLegendModel::updateRasterClassificationItem( QStandardItem* classificationItem )
{
#if 0
if ( !classificationItem )
{
return;
}

QStandardItem* parentItem = classificationItem->parent();
if ( !parentItem )
{
return;
}

QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( parentItem->data( Qt::UserRole + 2 ).toString() );
if ( !ml )
{
return;
}

QgsRasterLayer* rl = qobject_cast<QgsRasterLayer *>( ml );
if ( !rl )
{
return;
}

QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rl->legendAsPixmap( true ) ), "" );

currentSymbolItem->setData( QgsLegendModel::ClassificationItem, Qt::UserRole + 1 ); //first user data stores the item type
parentItem->insertRow( 0, currentSymbolItem );
parentItem->removeRow( 1 );
#endif //0
}

void QgsLegendModel::removeLayer( const QString& layerId )
Expand Down
6 changes: 3 additions & 3 deletions src/core/composer/qgslegendmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
/**Updates the whole symbology of a layer*/
void updateLayer( QStandardItem* layerItem );
/**Tries to update a single classification item*/
void updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText );
void updateVectorV2ClassificationItem( QStandardItem* classificationItem, QgsSymbolV2* symbol, QString itemText );
void updateRasterClassificationItem( QStandardItem* classificationItem );
void updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText ) {}
void updateVectorV2ClassificationItem( QStandardItem* classificationItem, QgsSymbolV2* symbol, QString itemText ) {}
void updateRasterClassificationItem( QStandardItem* classificationItem ) {}

bool writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const;
bool readXML( const QDomElement& legendModelElem, const QDomDocument& doc );
Expand Down

0 comments on commit d550f70

Please sign in to comment.