Exemple #1
0
QModelIndex QgsLayerTreeModel::parent( const QModelIndex &child ) const
{
  if ( !child.isValid() )
    return QModelIndex();

  QgsLayerTreeNode* n = index2node( child );

  QgsLayerTreeNode* parentNode = 0;

  if ( !n )
  {
    QgsLayerTreeModelLegendNode* sym = index2symnode( child );
    Q_ASSERT( sym );
    parentNode = sym->parent();
  }
  else
    parentNode = n->parent(); // must not be null
  Q_ASSERT( parentNode );

  QgsLayerTreeNode* grandParentNode = parentNode->parent();
  if ( !grandParentNode )
    return QModelIndex();  // root node -> invalid index

  int row = grandParentNode->children().indexOf( parentNode );
  Q_ASSERT( row >= 0 );
  return createIndex( row, 0, static_cast<QObject*>( parentNode ) );
}
Exemple #2
0
int QgsLayerTreeModel::rowCount( const QModelIndex &parent ) const
{
  if ( index2legendNode( parent ) )
    return 0; // they are leaves

  QgsLayerTreeNode* n = index2node( parent );
  if ( !n )
    return 0;

  if ( QgsLayerTree::isLayer( n ) )
  {
    if ( !testFlag( ShowLegend ) )
      return 0;

    QgsLayerTreeLayer* nL = QgsLayerTree::toLayer( n );
    if ( mLegendNodes[nL].count() == 1 && mLegendNodes[nL][0]->isEmbeddedInParent() )
      return 0;

    return mLegendNodes[nL].count();
  }

  return n->children().count();
}
QList<QDomElement> QgsServerProjectParser::setLegendGroupElementsWithLayerTree( QgsLayerTreeGroup* layerTreeGroup, const QDomElement& legendElement ) const
{
  QList<QDomElement> LegendGroupElemList;
  QList< QgsLayerTreeNode * > layerTreeGroupChildren = layerTreeGroup->children();
  QDomNodeList legendElementChildNodes = legendElement.childNodes();
  int g = 0; // index of the last child layer tree group
  for ( int i = 0; i < legendElementChildNodes.size(); ++i )
  {
    QDomNode legendElementNode = legendElementChildNodes.at( i );
    if ( !legendElementNode.isElement() )
      continue;
    QDomElement legendElement = legendElementNode.toElement();
    if ( legendElement.tagName() != "legendgroup" )
      continue;
    for ( int j = g; j < i + 1; ++j )
    {
      QgsLayerTreeNode* layerTreeNode = layerTreeGroupChildren.at( j );
      if ( layerTreeNode->nodeType() != QgsLayerTreeNode::NodeGroup )
        continue;
      QgsLayerTreeGroup* layerTreeGroup = static_cast<QgsLayerTreeGroup *>( layerTreeNode );
      if ( layerTreeGroup->name() == legendElement.attribute( "name" ) )
      {
        g = j;
        QString shortName = layerTreeGroup->customProperty( "wmsShortName" ).toString();
        if ( !shortName.isEmpty() )
          legendElement.setAttribute( "shortName", shortName );
        QString title = layerTreeGroup->customProperty( "wmsTitle" ).toString();
        if ( !title.isEmpty() )
          legendElement.setAttribute( "title", title );
        LegendGroupElemList.append( setLegendGroupElementsWithLayerTree( layerTreeGroup, legendElement ) );
      }
    }
    LegendGroupElemList.push_back( legendElement );
  }
  return LegendGroupElemList;
}
Exemple #4
0
int QgsLayerTreeModel::rowCount( const QModelIndex &parent ) const
{
  if ( index2symnode( parent ) )
  {
    return 0; // they are leaves
  }

  QgsLayerTreeNode* n = index2node( parent );

  if ( parent.isValid() && parent.column() != 0 )
    return 0;

  if ( QgsLayerTree::isLayer( n ) )
  {
    QgsLayerTreeLayer* nL = QgsLayerTree::toLayer( n );

    if ( mSymbologyNodes[nL].count() == 1 && mSymbologyNodes[nL][0]->isEmbeddedInParent() )
      return 0;

    return mSymbologyNodes[nL].count();
  }

  return n->children().count();
}
Exemple #5
0
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 ( index2symnode( index ) )
    return Qt::ItemIsEnabled; // | Qt::ItemIsSelectable;

  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;
}
Exemple #6
0
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();
}
Exemple #7
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();
}