QAction* QgsLayerTreeViewDefaultActions::actionShowFeatureCount( QObject* parent ) { QgsLayerTreeNode* node = mView->currentNode(); if ( !node ) return 0; QAction* a = new QAction( tr( "Show Feature Count" ), parent ); connect( a, SIGNAL( triggered() ), this, SLOT( showFeatureCount() ) ); a->setCheckable( true ); a->setChecked( node->customProperty( "showFeatureCount", 0 ).toInt() ); return a; }
QAction* QgsLayerTreeViewDefaultActions::actionShowInOverview( QObject* parent ) { QgsLayerTreeNode* node = mView->currentNode(); if ( !node ) return nullptr; QAction* a = new QAction( tr( "&Show in Overview" ), parent ); connect( a, SIGNAL( triggered() ), this, SLOT( showInOverview() ) ); a->setCheckable( true ); a->setChecked( node->customProperty( QStringLiteral( "overview" ), 0 ).toInt() ); return a; }
QAction *QgsLayerTreeViewDefaultActions::actionShowFeatureCount( QObject *parent ) { QgsLayerTreeNode *node = mView->currentNode(); if ( !node ) return nullptr; QAction *a = new QAction( tr( "Show Feature Count" ), parent ); connect( a, &QAction::triggered, this, &QgsLayerTreeViewDefaultActions::showFeatureCount ); a->setCheckable( true ); a->setChecked( node->customProperty( QStringLiteral( "showFeatureCount" ), 0 ).toInt() ); return a; }
QAction *QgsLayerTreeViewDefaultActions::actionShowInOverview( QObject *parent ) { QgsLayerTreeNode *node = mView->currentNode(); if ( !node ) return nullptr; QAction *a = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionInOverview.svg" ) ), tr( "&Show in Overview" ), parent ); connect( a, &QAction::triggered, this, &QgsLayerTreeViewDefaultActions::showInOverview ); a->setCheckable( true ); a->setChecked( node->customProperty( QStringLiteral( "overview" ), 0 ).toInt() ); return a; }
void QgsLayerTreeView::modelRowsInserted( const QModelIndex& index, int start, int end ) { QgsLayerTreeNode* parentNode = layerTreeModel()->index2node( index ); if ( !parentNode ) return; // Embedded widgets - replace placeholders in the model by actual widgets if ( layerTreeModel()->testFlag( QgsLayerTreeModel::UseEmbeddedWidgets ) && QgsLayerTree::isLayer( parentNode ) ) { QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( parentNode ); if ( QgsMapLayer* layer = nodeLayer->layer() ) { int widgetsCount = layer->customProperty( "embeddedWidgets/count", 0 ).toInt(); QList<QgsLayerTreeModelLegendNode*> legendNodes = layerTreeModel()->layerLegendNodes( nodeLayer ); for ( int i = 0; i < widgetsCount; ++i ) { QString providerId = layer->customProperty( QString( "embeddedWidgets/%1/id" ).arg( i ) ).toString(); if ( QgsLayerTreeEmbeddedWidgetProvider* provider = QgsLayerTreeEmbeddedWidgetRegistry::instance()->provider( providerId ) ) { QModelIndex index = layerTreeModel()->legendNode2index( legendNodes[i] ); setIndexWidget( index, provider->createWidget( layer, i ) ); } } } } if ( QgsLayerTree::isLayer( parentNode ) ) { // if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes QStringList expandedNodeKeys = parentNode->customProperty( "expandedLegendNodes" ).toStringList(); if ( expandedNodeKeys.isEmpty() ) return; Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ) ) ) { QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString(); if ( expandedNodeKeys.contains( ruleKey ) ) setExpanded( layerTreeModel()->legendNode2index( legendNode ), true ); } return; }
Qt::ItemFlags QgsLayerTreeModel::flags( const QModelIndex& index ) const { if ( !index.isValid() ) { Qt::ItemFlags rootFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if ( testFlag( AllowNodeReorder ) ) rootFlags |= Qt::ItemIsDropEnabled; return rootFlags; } if ( QgsLayerTreeModelLegendNode* symn = index2symnode( index ) ) { Qt::ItemFlags f = symn->flags(); if ( !testFlag( AllowSymbologyChangeState ) ) f &= ~Qt::ItemIsUserCheckable; return f; } Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable; if ( testFlag( AllowNodeRename ) ) f |= Qt::ItemIsEditable; QgsLayerTreeNode* node = index2node( index ); bool isEmbedded = node->customProperty( "embedded" ).toInt(); if ( testFlag( AllowNodeReorder ) ) { // only root embedded nodes can be reordered if ( !isEmbedded || ( isEmbedded && node->parent() && !node->parent()->customProperty( "embedded" ).toInt() ) ) f |= Qt::ItemIsDragEnabled; } if ( testFlag( AllowNodeChangeVisibility ) && ( QgsLayerTree::isLayer( node ) || QgsLayerTree::isGroup( node ) ) ) f |= Qt::ItemIsUserCheckable; if ( testFlag( AllowNodeReorder ) && QgsLayerTree::isGroup( node ) && !isEmbedded ) f |= Qt::ItemIsDropEnabled; return f; }
void QgsLayerTreeView::modelRowsInserted( const QModelIndex& index, int start, int end ) { QgsLayerTreeNode* parentNode = layerTreeModel()->index2node( index ); if ( !parentNode ) return; if ( QgsLayerTree::isLayer( parentNode ) ) { // if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes QStringList expandedNodeKeys = parentNode->customProperty( "expandedLegendNodes" ).toStringList(); if ( expandedNodeKeys.isEmpty() ) return; Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ) ) ) { QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString(); if ( expandedNodeKeys.contains( ruleKey ) ) setExpanded( layerTreeModel()->legendNode2index( legendNode ), true ); } return; }
QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const { if ( !index.isValid() ) return QVariant(); if ( QgsLayerTreeModelLegendNode* sym = index2symnode( index ) ) { if ( role == Qt::CheckStateRole && !testFlag( AllowSymbologyChangeState ) ) return QVariant(); return sym->data( role ); } QgsLayerTreeNode* node = index2node( index ); if ( role == Qt::DisplayRole || role == Qt::EditRole ) { if ( QgsLayerTree::isGroup( node ) ) return QgsLayerTree::toGroup( node )->name(); else if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node ); QString name = nodeLayer->layerName(); if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole ) { QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() ); if ( vlayer && vlayer->pendingFeatureCount() >= 0 ) name += QString( " [%1]" ).arg( vlayer->pendingFeatureCount() ); } return name; } } else if ( role == Qt::DecorationRole && index.column() == 0 ) { if ( QgsLayerTree::isGroup( node ) ) return iconGroup(); else if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node ); // if there's just on legend entry that should be embedded in layer - do that! if ( testFlag( ShowSymbology ) && mSymbologyNodes[nodeLayer].count() == 1 && mSymbologyNodes[nodeLayer][0]->isEmbeddedInParent() ) return mSymbologyNodes[nodeLayer][0]->data( Qt::DecorationRole ); QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer(); if ( !layer ) return QVariant(); if ( layer->type() == QgsMapLayer::RasterLayer ) { if ( testFlag( ShowRasterPreviewIcon ) ) { QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( layer ); return QIcon( rlayer->previewAsPixmap( QSize( 32, 32 ) ) ); } else return QgsLayerItem::iconRaster(); } else if ( layer->type() == QgsMapLayer::VectorLayer ) { QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>( layer ); if ( vlayer->isEditable() ) { if ( vlayer->isModified() ) return QIcon( QgsApplication::getThemePixmap( "/mIconEditableEdits.png" ) ); else return QIcon( QgsApplication::getThemePixmap( "/mIconEditable.png" ) ); } if ( vlayer->geometryType() == QGis::Point ) return QgsLayerItem::iconPoint(); else if ( vlayer->geometryType() == QGis::Line ) return QgsLayerItem::iconLine(); else if ( vlayer->geometryType() == QGis::Polygon ) return QgsLayerItem::iconPolygon(); else if ( vlayer->geometryType() == QGis::NoGeometry ) return QgsLayerItem::iconTable(); } return QgsLayerItem::iconDefault(); } } else if ( role == Qt::CheckStateRole ) { if ( !testFlag( AllowNodeChangeVisibility ) ) return QVariant(); if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node ); if ( nodeLayer->layer() && nodeLayer->layer()->type() == QgsMapLayer::VectorLayer ) { if ( qobject_cast<QgsVectorLayer*>( nodeLayer->layer() )->geometryType() == QGis::NoGeometry ) return QVariant(); // do not show checkbox for non-spatial tables } return nodeLayer->isVisible(); } else if ( QgsLayerTree::isGroup( node ) ) { QgsLayerTreeGroup* nodeGroup = QgsLayerTree::toGroup( node ); return nodeGroup->isVisible(); } } else if ( role == Qt::FontRole ) { QFont f( QgsLayerTree::isLayer( node ) ? mFontLayer : ( QgsLayerTree::isGroup( node ) ? mFontGroup : QFont() ) ); if ( node->customProperty( "embedded" ).toInt() ) f.setItalic( true ); if ( index == mCurrentIndex ) f.setUnderline( true ); return f; } else if ( role == Qt::ToolTipRole ) { if ( QgsLayerTree::isLayer( node ) ) { if ( QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer() ) return layer->publicSource(); } } return QVariant(); }
QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const { if ( !index.isValid() || index.column() > 1 ) return QVariant(); if ( QgsLayerTreeModelLegendNode* sym = index2legendNode( index ) ) return legendNodeData( sym, role ); QgsLayerTreeNode* node = index2node( index ); if ( role == Qt::DisplayRole || role == Qt::EditRole ) { if ( QgsLayerTree::isGroup( node ) ) return QgsLayerTree::toGroup( node )->name(); if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node ); QString name = nodeLayer->layerName(); if ( nodeLayer->customProperty( "showFeatureCount", 0 ).toInt() && role == Qt::DisplayRole ) { QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer*>( nodeLayer->layer() ); if ( vlayer && vlayer->featureCount() >= 0 ) name += QString( " [%1]" ).arg( vlayer->featureCount() ); } return name; } } else if ( role == Qt::DecorationRole && index.column() == 0 ) { if ( QgsLayerTree::isGroup( node ) ) return iconGroup(); if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node ); QgsMapLayer *layer = nodeLayer->layer(); if ( !layer ) return QVariant(); // icons possibly overriding default icon if ( layer->type() == QgsMapLayer::RasterLayer ) { if ( testFlag( ShowRasterPreviewIcon ) ) { QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( layer ); return QIcon( QPixmap::fromImage( rlayer->previewAsImage( QSize( 32, 32 ) ) ) ); } else { return QgsLayerItem::iconRaster(); } } QgsVectorLayer *vlayer = dynamic_cast<QgsVectorLayer*>( layer ); QIcon icon; // if there's just on legend entry that should be embedded in layer - do that! if ( testFlag( ShowLegend ) && legendEmbeddedInParent( nodeLayer ) ) { icon = legendIconEmbeddedInParent( nodeLayer ); } else if ( vlayer && layer->type() == QgsMapLayer::VectorLayer ) { if ( vlayer->geometryType() == QGis::Point ) icon = QgsLayerItem::iconPoint(); else if ( vlayer->geometryType() == QGis::Line ) icon = QgsLayerItem::iconLine(); else if ( vlayer->geometryType() == QGis::Polygon ) icon = QgsLayerItem::iconPolygon(); else if ( vlayer->geometryType() == QGis::NoGeometry ) icon = QgsLayerItem::iconTable(); else icon = QgsLayerItem::iconDefault(); } if ( vlayer && vlayer->isEditable() ) { QPixmap pixmap( icon.pixmap( 16, 16 ) ); QPainter painter( &pixmap ); painter.drawPixmap( 0, 0, 16, 16, QgsApplication::getThemePixmap( vlayer->isModified() ? "/mIconEditableEdits.png" : "/mIconEditable.png" ) ); painter.end(); icon = QIcon( pixmap ); } return icon; } } else if ( role == Qt::CheckStateRole ) { if ( !testFlag( AllowNodeChangeVisibility ) ) return QVariant(); if ( QgsLayerTree::isLayer( node ) ) { QgsLayerTreeLayer* nodeLayer = QgsLayerTree::toLayer( node ); if ( nodeLayer->layer() && nodeLayer->layer()->type() == QgsMapLayer::VectorLayer ) { if ( qobject_cast<QgsVectorLayer*>( nodeLayer->layer() )->geometryType() == QGis::NoGeometry ) return QVariant(); // do not show checkbox for non-spatial tables } return nodeLayer->isVisible(); } else if ( QgsLayerTree::isGroup( node ) ) { QgsLayerTreeGroup* nodeGroup = QgsLayerTree::toGroup( node ); return nodeGroup->isVisible(); } } else if ( role == Qt::FontRole ) { QFont f( QgsLayerTree::isLayer( node ) ? mFontLayer : ( QgsLayerTree::isGroup( node ) ? mFontGroup : QFont() ) ); if ( node->customProperty( "embedded" ).toInt() ) f.setItalic( true ); if ( index == mCurrentIndex ) f.setUnderline( true ); return f; } else if ( role == Qt::ToolTipRole ) { if ( QgsLayerTree::isLayer( node ) ) { if ( QgsMapLayer* layer = QgsLayerTree::toLayer( node )->layer() ) return layer->publicSource(); } } return QVariant(); }