示例#1
0
QStandardItem* QgsComposerLayerItem::clone() const
{
  QgsComposerLayerItem* cloneItem  = new QgsComposerLayerItem();
  *cloneItem = *this;
  cloneItem->setLayerID( mLayerID );
  return cloneItem;
}
示例#2
0
void QgsLegendModel::updateLayer( QStandardItem* layerItem )
{
  QgsDebugMsg( "Entered." );
  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
  if ( lItem )
  {
    QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
    if ( mapLayer )
    {
      updateLayerItemText( lItem );

      QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
      if ( vLayer )
      {
        addVectorLayerItemsV2( lItem, vLayer );
      }

      QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
      if ( rLayer )
      {
        addRasterLayerItems( lItem, rLayer );
      }
    }
  }
}
示例#3
0
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer )
{
    if ( !theMapLayer )
    {
        return;
    }

    QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() );
    layerItem->setLayerID( theMapLayer->id() );
    layerItem->setDefaultStyle();
    layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );

    QList<QStandardItem *> itemsList;
    itemsList << layerItem << new QgsComposerStyleItem( layerItem );
    invisibleRootItem()->appendRow( itemsList );

    switch ( theMapLayer->type() )
    {
    case QgsMapLayer::VectorLayer:
    {
        QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( theMapLayer );
        if ( vl )
        {
            addVectorLayerItemsV2( layerItem, vl );
        }
        break;
    }
    case QgsMapLayer::RasterLayer:
        addRasterLayerItems( layerItem, theMapLayer );
        break;
    default:
        break;
    }
    emit layersChanged();
}
示例#4
0
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;
}
示例#5
0
void QgsLegendModel::setLayerSetAndGroups( const QStringList& layerIds, const QList< GroupLayerInfo >& groupInfo )
{
  setLayerSet( layerIds );

  QStandardItem* currentItem = 0;
  QStandardItem* currentGroupItem = 0;
  int i = 0;

  QList< GroupLayerInfo >::const_iterator infoIt = groupInfo.constBegin();
  for ( ; infoIt != groupInfo.constEnd() && i < invisibleRootItem()->rowCount(); )
  {
    currentItem = invisibleRootItem()->child( i, 0 );
    QString infoKey = infoIt->first;
    if ( infoKey.isNull() ) //a toplevel layer
    {
      ++i;
    }
    else //a group
    {
      currentGroupItem = addGroup( infoKey, i );
      ++i;
      QList<QString> layerList = infoIt->second;
      QList<QString>::const_iterator groupLayerIt = layerList.constBegin();
      for ( ; currentItem && ( groupLayerIt != layerList.constEnd() ); ++groupLayerIt )
      {
        //check if current item is contained in this group
        QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem*>( currentItem );
        if ( !layerItem )
        {
          return; //should never happen
        }
        //QString layerID = currentItem->data(Qt::UserRole + 2).toString();
        QString layerID = layerItem->layerID();
        if ( layerList.contains( layerID ) )
        {
          takeRow( i );
          currentGroupItem->setChild( currentGroupItem->rowCount(), 0, currentItem );
        }
        else
        {
          ++i;
        }
        currentItem = invisibleRootItem()->child( i, 0 );
      }
    }
    ++infoIt;
  }
}
示例#6
0
void QgsLegendModel::updateLayer( QStandardItem* layerItem )
{
  QgsDebugMsg( "Entered." );
  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
  if ( lItem )
  {
    QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
    if ( mapLayer )
    {
      //delete all the entries under layer item
      int currentRowCount = lItem->rowCount();
      for ( int i = currentRowCount - 1; i >= 0; --i )
      {
        lItem->removeRow( i );
      }

      QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );

      //set layer name as item text
      QString label = mapLayer->name();
      if ( vLayer && lItem->showFeatureCount() )
      {
        label += QString( " [%1]" ).arg( vLayer->featureCount() );
      }
      layerItem->setText( label );

      if ( vLayer )
      {
        if ( vLayer->isUsingRendererV2() )
        {
          addVectorLayerItemsV2( lItem, vLayer );
        }
        else
        {
          addVectorLayerItems( lItem, vLayer );
        }
      }

      QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
      if ( rLayer )
      {
        addRasterLayerItems( lItem, rLayer );
      }
    }
  }
}
void QgsComposerLegendWidget::on_mCountToolButton_clicked( bool checked )
{
  QgsDebugMsg( "Entered." );
  if ( !mLegend )
  {
    return;
  }

  //get current item
  QStandardItemModel* itemModel = qobject_cast<QStandardItemModel *>( mItemTreeView->model() );
  if ( !itemModel )
  {
    return;
  }

  //get current item
  QModelIndex currentIndex = mItemTreeView->currentIndex();
  if ( !currentIndex.isValid() )
  {
    return;
  }

  QStandardItem* currentItem = itemModel->itemFromIndex( currentIndex );
  if ( !currentItem )
  {
    return;
  }

  QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem *>( currentItem );

  if ( !layerItem )
  {
    return;
  }

  mLegend->beginCommand( tr( "Legend updated" ) );
  layerItem->setShowFeatureCount( checked );
  if ( mLegend->model() )
  {
    mLegend->model()->updateItem( currentItem );
  }
  mLegend->update();
  mLegend->adjustBoxSize();
  mLegend->endCommand();
}
示例#8
0
void QgsLegendModel::removeLayer( const QString& layerId )
{
  int numRootItems = rowCount();
  for ( int i = 0; i < numRootItems ; ++i )
  {
    QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) );
    if ( !lItem )
    {
      continue;
    }

    if ( layerId == lItem->layerID() )
    {
      removeRow( i ); //todo: also remove the subitems and their symbols...
      emit layersChanged();
      return;
    }
  }
}
示例#9
0
void QgsLegendModel::updateRasterSymbolItemText( QStandardItem* symbolItem )
{
  QgsComposerRasterSymbolItem* rItem = dynamic_cast<QgsComposerRasterSymbolItem*>( symbolItem );
  if ( !rItem ) return;

  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( rItem->parent() );
  if ( !lItem ) return;

  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
  if ( !mapLayer ) return;

  QgsRasterLayer* rLayer = qobject_cast<QgsRasterLayer*>( mapLayer );
  if ( !rLayer ) return;

  QPair< QString, QColor> symbol = rLayer->legendSymbologyItems().value( symbolItem->row() );

  QString label = rItem->userText().isEmpty() ? symbol.first : rItem->userText();

  symbolItem->setText( label );
}
示例#10
0
void QgsLegendModel::addLayer( QgsMapLayer* theMapLayer )
{
  if ( !theMapLayer )
  {
    return;
  }

  QgsComposerLayerItem* layerItem = new QgsComposerLayerItem( theMapLayer->name() );
  layerItem->setLayerID( theMapLayer->id() );
  layerItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );

  invisibleRootItem()->setChild( invisibleRootItem()->rowCount(), layerItem );

  switch ( theMapLayer->type() )
  {
    case QgsMapLayer::VectorLayer:
    {
      QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( theMapLayer );
      if ( vl )
      {
        if ( vl->isUsingRendererV2() )
        {
          addVectorLayerItemsV2( layerItem, vl );
        }
        else
        {
          addVectorLayerItems( layerItem, vl );
        }
      }
      break;
    }
    case QgsMapLayer::RasterLayer:
      addRasterLayerItems( layerItem, theMapLayer );
      break;
    default:
      break;
  }
  emit layersChanged();
}
void QgsComposerLegendWidget::selectedChanged( const QModelIndex & current, const QModelIndex & previous )
{
  Q_UNUSED( previous );
  QgsDebugMsg( "Entered" );

  mCountToolButton->setChecked( false );
  mCountToolButton->setEnabled( false );

  QStandardItemModel* itemModel = qobject_cast<QStandardItemModel *>( mItemTreeView->model() );
  if ( !itemModel ) return;

  QStandardItem* currentItem = itemModel->itemFromIndex( current );
  if ( !currentItem ) return;

  QgsComposerLayerItem* layerItem = dynamic_cast<QgsComposerLayerItem *>( currentItem );
  if ( !layerItem ) return;

  QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerItem->layerID() ) );
  if ( !vectorLayer ) return;

  mCountToolButton->setChecked( layerItem->showFeatureCount() );
  mCountToolButton->setEnabled( true );
}
示例#12
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 );
}
示例#13
0
void QgsLegendModel::updateLayerItemText( QStandardItem* layerItem )
{
  QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( layerItem );
  if ( !lItem ) return;

  QgsMapLayer* mapLayer = QgsMapLayerRegistry::instance()->mapLayer( lItem->layerID() );
  if ( !mapLayer ) return;

  QString label = lItem->userText().isEmpty() ? mapLayer->name() : lItem->userText();

  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
  if ( vLayer )
  {
    addVectorLayerItemsV2( lItem, vLayer );
    if ( lItem->showFeatureCount() )
    {
      label += QString( " [%1]" ).arg( vLayer->featureCount() );
    }
  }
  lItem->setText( label );
}
示例#14
0
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;
}