Skip to content

Commit d550f70

Browse files
author
mhugent
committedMay 19, 2010
Reenabled update for composer legend layers
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13532 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+36
-191
lines changed

2 files changed

+36
-191
lines changed
 

‎src/core/composer/qgslegendmodel.cpp

Lines changed: 33 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -271,218 +271,63 @@ int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* r
271271

272272
void QgsLegendModel::updateItem( QStandardItem* item )
273273
{
274-
#if 0
275274
if ( !item )
276275
{
277276
return;
278277
}
279278

280-
//is it a toplevel layer item?
281-
QModelIndex itemIndex = indexFromItem( item );
282-
QModelIndex parentIndex = itemIndex.parent();
283-
if ( !parentIndex.isValid() ) // a layer item?
284-
{
285-
updateLayer( item );
286-
}
287-
288-
//take QgsSymbol* from user data
289-
QVariant symbolVariant = item->data( Qt::UserRole + 2 );
290-
QgsSymbol* symbol = 0;
291-
if ( symbolVariant.canConvert<void*>() )
292-
{
293-
void* symbolData = symbolVariant.value<void*>();
294-
symbol = ( QgsSymbol* )( symbolData );
295-
}
296-
297-
QVariant symbolNgVariant = item->data( Qt::UserRole + 3 );
298-
QgsSymbolV2* symbolNg = 0;
299-
if ( symbolNgVariant.canConvert<void*>() )
279+
//only layer items are supported for update
280+
QgsComposerLegendItem* cItem = dynamic_cast<QgsComposerLegendItem*>( item );
281+
if ( ! cItem )
300282
{
301-
void* symbolNgData = symbolVariant.value<void*>();
302-
symbolNg = ( QgsSymbolV2* )symbolNgData;
283+
return;
303284
}
304285

305-
if ( symbol ) //vector classification item
306-
{
307-
updateVectorClassificationItem( item, symbol, item->text() );
308-
}
309-
else if ( symbolNg )
286+
QgsComposerLegendItem::ItemType type = cItem->itemType();
287+
if ( type == QgsComposerLegendItem::LayerItem )
310288
{
311-
updateVectorV2ClassificationItem( item, symbolNg, item->text() );
289+
updateLayer( cItem );
312290
}
313-
else if ( !item->icon().isNull() ) //raster classification item
314-
{
315-
updateRasterClassificationItem( item );
316-
}
317-
#endif //0
318291
}
319292

320293
void QgsLegendModel::updateLayer( QStandardItem* layerItem )
321294
{
322-
#if 0
323-
if ( !layerItem )
295+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
296+
if ( lItem )
324297
{
325-
return;
326-
}
327-
328-
QString layerId = layerItem->data( Qt::UserRole + 2 ).toString();
329-
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( layerId );
330-
if ( mapLayer )
331-
{
332-
//delete all the entries under layer item
333-
int currentRowCount = layerItem->rowCount();
334-
for ( int i = currentRowCount - 1; i >= 0; --i )
298+
QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
299+
if ( mapLayer )
335300
{
336-
layerItem->removeRow( i );
337-
}
301+
//delete all the entries under layer item
302+
int currentRowCount = lItem->rowCount();
303+
for ( int i = currentRowCount - 1; i >= 0; --i )
304+
{
305+
lItem->removeRow( i );
306+
}
338307

339-
//and add the new ones...
340-
switch ( mapLayer->type() )
341-
{
342-
case QgsMapLayer::VectorLayer:
308+
//set layer name as item text
309+
layerItem->setText( mapLayer->name() );
310+
311+
QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
312+
if ( vLayer )
343313
{
344-
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>( mapLayer );
345-
if ( vLayer )
314+
if ( vLayer->isUsingRendererV2() )
346315
{
347-
if ( vLayer->isUsingRendererV2() )
348-
{
349-
addVectorLayerItemsV2( layerItem, vLayer );
350-
}
351-
else
352-
{
353-
addVectorLayerItems( layerItem, vLayer );
354-
}
316+
addVectorLayerItemsV2( lItem, vLayer );
317+
}
318+
else
319+
{
320+
addVectorLayerItems( lItem, vLayer );
355321
}
356322
}
357-
break;
358-
case QgsMapLayer::RasterLayer:
359-
addRasterLayerItem( layerItem, mapLayer );
360-
break;
361-
default:
362-
break;
363-
}
364-
}
365-
#endif //0
366-
}
367-
368-
void QgsLegendModel::updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText )
369-
{
370-
#if 0
371-
//this function uses the following logic to find a classification match:
372-
//first test if there is a symbol where lowerbound - upperbound equels itemText
373-
//if no match found, test if there is a symbol where label equals itemText
374-
//still no match found. Test, if there is a symbol with same pen/brush/point symbol
375-
376-
//get parent item
377-
QStandardItem* parentItem = classificationItem->parent();
378-
if ( !parentItem )
379-
{
380-
return;
381-
}
382323

383-
//get maplayer object from parent item
384-
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( parentItem->data( Qt::UserRole + 2 ).toString() );
385-
if ( !ml )
386-
{
387-
return;
388-
}
389-
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer *>( ml );
390-
if ( !vl )
391-
{
392-
return;
393-
}
394-
int opacity = vl->getTransparency();
395-
396-
const QgsRenderer* layerRenderer = vl->renderer();
397-
if ( !layerRenderer )
398-
{
399-
return;
400-
}
401-
402-
QList<QgsSymbol*> symbolList = layerRenderer->symbols();
403-
QList<QgsSymbol*>::iterator symbolIt;
404-
QgsSymbol* currentSymbol = 0;
405-
406-
//try to find a symbol where lowerbound - upperbound matches item text
407-
symbolIt = symbolList.begin();
408-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
409-
{
410-
currentSymbol = *symbolIt;
411-
if ( currentSymbol->lowerValue() + " - " + currentSymbol->upperValue() == itemText )
412-
{
413-
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity, vl->getLayerID() ) );
414-
parentItem->removeRow( classificationItem->row() );
415-
return;
416-
}
417-
}
418-
419-
//try to find a symbol where lower value matches item text (non-numeric classifications)
420-
symbolIt = symbolList.begin();
421-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
422-
{
423-
currentSymbol = *symbolIt;
424-
if ( currentSymbol->lowerValue() == itemText )
425-
{
426-
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity, vl->getLayerID() ) );
427-
parentItem->removeRow( classificationItem->row() );
428-
return;
429-
}
430-
}
431-
432-
//try to find a symbol where label matches item text
433-
symbolIt = symbolList.begin();
434-
for ( ; symbolIt != symbolList.end(); ++symbolIt )
435-
{
436-
currentSymbol = *symbolIt;
437-
if ( currentSymbol->label() == itemText )
438-
{
439-
removeSymbol( symbol );
440-
parentItem->insertRow( classificationItem->row(), itemFromSymbol( currentSymbol, opacity, vl->getLayerID() ) );
441-
parentItem->removeRow( classificationItem->row() );
442-
return;
324+
QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
325+
if ( rLayer )
326+
{
327+
addRasterLayerItem( lItem, rLayer );
328+
}
443329
}
444330
}
445-
#endif //0
446-
}
447-
448-
void QgsLegendModel::updateVectorV2ClassificationItem( QStandardItem* classificationItem, QgsSymbolV2* symbol, QString itemText )
449-
{
450-
//todo...
451-
}
452-
453-
454-
void QgsLegendModel::updateRasterClassificationItem( QStandardItem* classificationItem )
455-
{
456-
#if 0
457-
if ( !classificationItem )
458-
{
459-
return;
460-
}
461-
462-
QStandardItem* parentItem = classificationItem->parent();
463-
if ( !parentItem )
464-
{
465-
return;
466-
}
467-
468-
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( parentItem->data( Qt::UserRole + 2 ).toString() );
469-
if ( !ml )
470-
{
471-
return;
472-
}
473-
474-
QgsRasterLayer* rl = qobject_cast<QgsRasterLayer *>( ml );
475-
if ( !rl )
476-
{
477-
return;
478-
}
479-
480-
QStandardItem* currentSymbolItem = new QStandardItem( QIcon( rl->legendAsPixmap( true ) ), "" );
481-
482-
currentSymbolItem->setData( QgsLegendModel::ClassificationItem, Qt::UserRole + 1 ); //first user data stores the item type
483-
parentItem->insertRow( 0, currentSymbolItem );
484-
parentItem->removeRow( 1 );
485-
#endif //0
486331
}
487332

488333
void QgsLegendModel::removeLayer( const QString& layerId )

‎src/core/composer/qgslegendmodel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
6464
/**Updates the whole symbology of a layer*/
6565
void updateLayer( QStandardItem* layerItem );
6666
/**Tries to update a single classification item*/
67-
void updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText );
68-
void updateVectorV2ClassificationItem( QStandardItem* classificationItem, QgsSymbolV2* symbol, QString itemText );
69-
void updateRasterClassificationItem( QStandardItem* classificationItem );
67+
void updateVectorClassificationItem( QStandardItem* classificationItem, QgsSymbol* symbol, QString itemText ) {}
68+
void updateVectorV2ClassificationItem( QStandardItem* classificationItem, QgsSymbolV2* symbol, QString itemText ) {}
69+
void updateRasterClassificationItem( QStandardItem* classificationItem ) {}
7070

7171
bool writeXML( QDomElement& composerLegendElem, QDomDocument& doc ) const;
7272
bool readXML( const QDomElement& legendModelElem, const QDomDocument& doc );

0 commit comments

Comments
 (0)
Please sign in to comment.