예제 #1
0
int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer )
{
  if ( !layerItem || !rlayer )
  {
    return 1;
  }

  QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( rlayer );
  if ( !rasterLayer )
  {
    return 2;
  }

  QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
  QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
  for(; itemIt != rasterItemList.constEnd(); ++itemIt )
  {
    QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );
    if( mHasTopLevelWindow )
    {
      QPixmap itemPixmap( 20, 20 );
      itemPixmap.fill( itemIt->second );
      currentSymbolItem->setIcon( QIcon( itemPixmap ) );
    }
    currentSymbolItem->setLayerID( rasterLayer->id() );
    currentSymbolItem->setColor( itemIt->second );
    int currentRowCount = layerItem->rowCount();
    layerItem->setChild( currentRowCount, 0, currentSymbolItem );
  }

  return 0;
}
int QgsLegendModel::addRasterLayerItem( QStandardItem* layerItem, QgsMapLayer* rlayer )
{
    if ( !layerItem || !rlayer )
    {
        return 1;
    }

    QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( rlayer );
    if ( !rasterLayer )
    {
        return 2;
    }

    QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem();
    //use a vector symbol item without symbol
    if ( mHasTopLevelWindow ) //only use QIcon / QPixmap if we have a running x-server
    {
        currentSymbolItem->setIcon( QIcon( rasterLayer->legendAsPixmap( true ) ) );
    }
    currentSymbolItem->setLayerID( rasterLayer->id() );
    int currentRowCount = layerItem->rowCount();
    layerItem->setChild( currentRowCount, 0, currentSymbolItem );

    return 0;
}
예제 #3
0
QStandardItem* QgsComposerRasterSymbolItem::clone() const
{
  QgsComposerRasterSymbolItem* cloneItem  = new QgsComposerRasterSymbolItem();
  *cloneItem = *this;
  cloneItem->setLayerID( mLayerID );
  return cloneItem;
}
예제 #4
0
int QgsLegendModel::addRasterLayerItems( QStandardItem* layerItem, QgsMapLayer* rlayer )
{
  if ( !layerItem || !rlayer )
  {
    return 1;
  }

  QgsRasterLayer* rasterLayer = qobject_cast<QgsRasterLayer *>( rlayer );
  if ( !rasterLayer )
  {
    return 2;
  }

  QList< QPair< QString, QColor > > rasterItemList = rasterLayer->legendSymbologyItems();
  QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
  int row = 0;
  for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
  {
    QgsComposerRasterSymbolItem* currentSymbolItem = new QgsComposerRasterSymbolItem( itemIt->first );

    QgsComposerRasterSymbolItem* oldSymbolItem = dynamic_cast<QgsComposerRasterSymbolItem*>( layerItem->child( row, 0 ) );
    if ( oldSymbolItem )
    {
      currentSymbolItem->setUserText( oldSymbolItem->userText() );
      currentSymbolItem->setText( currentSymbolItem->userText() );
    }

    if ( mHasTopLevelWindow )
    {
      QPixmap itemPixmap( 20, 20 );
      itemPixmap.fill( itemIt->second );
      currentSymbolItem->setIcon( QIcon( itemPixmap ) );
    }
    currentSymbolItem->setLayerID( rasterLayer->id() );
    currentSymbolItem->setColor( itemIt->second );
    int currentRowCount = layerItem->rowCount();
    layerItem->setChild( currentRowCount, 0, currentSymbolItem );
    row++;
  }

  // Delete following old items (if current number of items decreased)
  for ( int i = layerItem->rowCount() - 1; i >= row; --i )
  {
    layerItem->removeRow( i );
  }

  return 0;
}
예제 #5
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 );
}
void QgsComposerLegend::drawLayerChildItems( QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord, int layerOpacity )
{
  if ( !layerItem )
  {
    return;
  }

  //Draw all symbols first and the texts after (to find out the x coordinate to have the text aligned)
  QList<double> childYCoords;
  QList<double> realItemHeights;

  double textHeight = fontHeightCharacterMM( mItemFont, QChar( '0' ) );
  double itemHeight = qMax( mSymbolHeight, textHeight );

  double textAlignCoord = 0; //alignment for legend text

  QStandardItem* currentItem;

  int numChildren = layerItem->rowCount();

  for ( int i = 0; i < numChildren; ++i )
  {
    //real symbol height. Can be different from standard height in case of point symbols
    double realSymbolHeight;
    double realItemHeight = itemHeight; //will be adjusted if realSymbolHeight turns out to be larger

    currentYCoord += mSymbolSpace;
    double currentXCoord = mBoxSpace;

    currentItem = layerItem->child( i, 0 );

    if ( !currentItem )
    {
      continue;
    }

    QgsSymbol* symbol = 0;
    QgsComposerSymbolItem* symbolItem = dynamic_cast<QgsComposerSymbolItem*>( currentItem );
    if ( symbolItem )
    {
      symbol = symbolItem->symbol();
    }

    QgsSymbolV2* symbolNg = 0;
    QgsComposerSymbolV2Item* symbolV2Item = dynamic_cast<QgsComposerSymbolV2Item*>( currentItem );
    if ( symbolV2Item )
    {
      symbolNg = symbolV2Item->symbolV2();
    }
    QgsComposerRasterSymbolItem* rasterItem = dynamic_cast<QgsComposerRasterSymbolItem*>( currentItem );

    if ( symbol )  //item with symbol?
    {
      //draw symbol
      drawSymbol( p, symbol, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
      realItemHeight = qMax( realSymbolHeight, itemHeight );
      currentXCoord += mIconLabelSpace;
    }
    else if ( symbolNg ) //item with symbol NG?
    {
      drawSymbolV2( p, symbolNg, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, currentXCoord, realSymbolHeight, layerOpacity );
      realItemHeight = qMax( realSymbolHeight, itemHeight );
      currentXCoord += mIconLabelSpace;
    }
    else if ( rasterItem )
    {
      if ( p )
      {
        p->setBrush( rasterItem->color() );
        p->drawRect( QRectF( currentXCoord, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight ) );
      }
      currentXCoord += mSymbolWidth;
      currentXCoord += mIconLabelSpace;
    }
    else //item with icon?
    {
      QIcon symbolIcon = currentItem->icon();
      if ( !symbolIcon.isNull() && p )
      {
        symbolIcon.paint( p, currentXCoord, currentYCoord + ( itemHeight - mSymbolHeight ) / 2, mSymbolWidth, mSymbolHeight );
        currentXCoord += mSymbolWidth;
        currentXCoord += mIconLabelSpace;
      }
    }

    childYCoords.push_back( currentYCoord );
    realItemHeights.push_back( realItemHeight );
    currentYCoord += realItemHeight;
    textAlignCoord = qMax( currentXCoord, textAlignCoord );
  }

  maxXCoord = qMax( maxXCoord, textAlignCoord );
  for ( int i = 0; i < numChildren; ++i )
  {
    if ( p )
    {
      p->setPen( QColor( 0, 0, 0 ) );
      drawText( p, textAlignCoord, childYCoords.at( i ) + textHeight + ( realItemHeights.at( i ) - textHeight ) / 2, layerItem->child( i, 0 )->text(), mItemFont );
      maxXCoord = qMax( maxXCoord, textAlignCoord + mBoxSpace + textWidthMillimeters( mItemFont,  layerItem->child( i, 0 )->text() ) );
    }
  }
}