Exemplo n.º 1
0
QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
{
  if ( !index.isValid() )
    return QVariant();

  if ( QgsLayerTreeModelSymbologyNode* sym = index2symnode( index ) )
  {
    if ( role == Qt::DisplayRole )
      return sym->name();
    else if ( role == Qt::DecorationRole )
      return sym->icon();
    return QVariant();
  }

  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 ) )
    {
      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();
}
Exemplo n.º 2
0
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();
}