int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer ) { if ( !layerItem || !vlayer ) { return 1; } QgsFeatureRendererV2* renderer = vlayer->rendererV2(); if ( !renderer ) { return 2; } QgsLegendSymbolList lst = renderer->legendSymbolItems(); QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin(); for ( ; symbolIt != lst.constEnd(); ++symbolIt ) { QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( symbolIt->first ); currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable ); if ( symbolIt->second ) { if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server { currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) ); } currentSymbolItem->setSymbolV2( symbolIt->second->clone() ); } layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem ); } return 0; }
void QgsLegendModel::updateSymbolV2ItemText( QStandardItem* symbolItem ) { QgsComposerSymbolV2Item* sv2Item = dynamic_cast<QgsComposerSymbolV2Item*>( symbolItem ); if ( !sv2Item ) return; QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( sv2Item->parent() ); if ( !lItem ) return; QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() ); if ( !mapLayer ) return; QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer ); if ( !vLayer ) return; QgsFeatureRendererV2* renderer = vLayer->rendererV2(); if ( !renderer ) return; if ( lItem->showFeatureCount() ) vLayer->countSymbolFeatures(); QgsLegendSymbolList symbolList = renderer->legendSymbolItems(); QPair<QString, QgsSymbolV2*> symbol = symbolList.value( symbolItem->row() ); QString label = sv2Item->userText().isEmpty() ? symbol.first : sv2Item->userText(); if ( renderer->type() == "singleSymbol" ) { if ( !sv2Item->userText().isEmpty() ) { label = sv2Item->userText(); } else if ( !lItem->userText().isEmpty() ) { label = lItem->userText(); } else if ( !vLayer->title().isEmpty() ) { label = vLayer->title(); } else { label = vLayer->name(); } } if ( lItem->showFeatureCount() ) { // Add counts to multi symbols layers only or labeled single symbols, // so that single symbol layers are still drawn on single line if ( symbolList.size() > 1 || !label.isEmpty() ) { label += QString( " [%1]" ).arg( vLayer->featureCount( symbol.second ) ); } } symbolItem->setText( label ); }
int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer ) { QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem ); if ( !layerItem || !lItem || !vlayer ) { return 1; } QgsFeatureRendererV2* renderer = vlayer->rendererV2(); if ( !renderer ) { return 2; } if ( lItem->showFeatureCount() ) { if ( !vlayer->countSymbolFeatures() ) { QgsDebugMsg( "Cannot get feature counts" ); } } QgsLegendSymbolList lst = renderer->legendSymbolItems(); QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin(); for ( ; symbolIt != lst.constEnd(); ++symbolIt ) { QString label = symbolIt->first; if ( lItem->showFeatureCount() ) { // Add counts to multi symbols layers only or labeled single symbols, // so that single symbol layers are still drawn on single line if ( lst.size() > 1 || !label.isEmpty() ) { label += QString( " [%1]" ).arg( vlayer->featureCount( symbolIt->second ) ); } } QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( label ); currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable ); if ( symbolIt->second ) { if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server { currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) ); } currentSymbolItem->setSymbolV2( symbolIt->second->clone() ); } layerItem->setChild( layerItem->rowCount(), 0, currentSymbolItem ); } return 0; }
void QgsComposerLayerItem::setDefaultStyle( double scaleDenominator, const QString& rule ) { // set default style according to number of symbols QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerID() ) ); if ( vLayer ) { QgsFeatureRendererV2* renderer = vLayer->rendererV2(); if ( renderer ) { QPair<QString, QgsSymbolV2*> symbolItem = renderer->legendSymbolItems( scaleDenominator, rule ).value( 0 ); if ( renderer->legendSymbolItems( scaleDenominator, rule ).size() > 1 || !symbolItem.first.isEmpty() ) { setStyle( QgsComposerLegendStyle::Subgroup ); } else { // Hide title by default for single symbol setStyle( QgsComposerLegendStyle::Hidden ); } } } }
int QgsLegendModel::addVectorLayerItemsV2( QStandardItem* layerItem, QgsVectorLayer* vlayer, double scaleDenominator ) { QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem ); if ( !layerItem || !lItem || !vlayer ) { return 1; } QgsFeatureRendererV2* renderer = vlayer->rendererV2(); if ( !renderer ) { return 2; } if ( lItem->showFeatureCount() ) { if ( !vlayer->countSymbolFeatures() ) { QgsDebugMsg( "Cannot get feature counts" ); } } QgsLegendSymbolList lst = renderer->legendSymbolItems( scaleDenominator ); QgsLegendSymbolList::const_iterator symbolIt = lst.constBegin(); int row = 0; for ( ; symbolIt != lst.constEnd(); ++symbolIt ) { if ( scaleDenominator == -1 ) { QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" ); // Get userText from old item if exists QgsComposerSymbolV2Item* oldSymbolItem = dynamic_cast<QgsComposerSymbolV2Item*>( layerItem->child( row, 0 ) ); if ( oldSymbolItem ) { currentSymbolItem->setUserText( oldSymbolItem->userText() ); } currentSymbolItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable ); if ( symbolIt->second ) { if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server { currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) ); } currentSymbolItem->setSymbolV2( symbolIt->second->clone() ); } layerItem->setChild( row, 0, currentSymbolItem ); // updateSymbolV2ItemText needs layer set updateSymbolV2ItemText( currentSymbolItem ); row++; } else { QgsComposerSymbolV2Item* currentSymbolItem = new QgsComposerSymbolV2Item( "" ); currentSymbolItem->setIcon( QgsSymbolLayerV2Utils::symbolPreviewIcon( symbolIt->second, QSize( 30, 30 ) ) ); currentSymbolItem->setSymbolV2( symbolIt->second ); layerItem->setChild( 0, 0, currentSymbolItem ); currentSymbolItem->setText( symbolIt->first ); } } if ( scaleDenominator == -1 ) { // Delete following old items (if current number of items decreased) for ( int i = layerItem->rowCount() - 1; i >= row; --i ) { layerItem->removeRow( i ); } } return 0; }