Exemplo n.º 1
0
void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
{
  SymbologyList itemList;
  QList< QPair< QString, QColor > > rasterItemList = layer->legendSymbologyItems();
  QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
#if QT_VERSION >= 0x40700
  itemList.reserve( rasterItemList.size() );
#endif
  // Paletted raster may have many colors, for example UInt16 may have 65536 colors
  // and it is very slow, so we limit max count
  QSize iconSize = treeWidget()->iconSize();
  int count = 0;
  int max_count = 1000;
  for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
  {
    QPixmap itemPixmap( iconSize );
    itemPixmap.fill( itemIt->second );
    // This is very slow, not clear why, it should not be, probably realloc,
    // but it seems to be non linear
    itemList.append( qMakePair( itemIt->first, itemPixmap ) );
    count++;
    if ( count == max_count )
    {
      itemPixmap = QPixmap( iconSize );
      itemPixmap.fill( Qt::transparent );
      QString label = tr( "following %1 items\nnot displayed" ).arg( rasterItemList.size() - max_count );
      itemList.append( qMakePair( label, itemPixmap ) );
      break;
    }
  }
  changeSymbologySettings( layer, itemList );
}
Exemplo n.º 2
0
void QgsLegendLayer::refreshSymbology( const QString& key )
{
  QgsMapLayer* theMapLayer = QgsMapLayerRegistry::instance()->mapLayer( key );
  if ( !theMapLayer )
  {
    return;
  }

  if ( theMapLayer->type() == QgsMapLayer::VectorLayer ) // VECTOR
  {
    QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( theMapLayer );
    vectorLayerSymbologyV2( vlayer );
  }
  else if ( theMapLayer->type() == QgsMapLayer::RasterLayer ) // RASTER
  {
    QgsRasterLayer* rlayer = qobject_cast<QgsRasterLayer *>( theMapLayer );
    rasterLayerSymbology( rlayer ); // get and change symbology
  }
  else if ( theMapLayer->type() == QgsMapLayer::PluginLayer )
  {
    QgsPluginLayer* player = qobject_cast<QgsPluginLayer *>( theMapLayer );

    QSize iconSize( 16, 16 );
    SymbologyList itemList = player->legendSymbologyItems( iconSize );

    changeSymbologySettings( theMapLayer, itemList );
  }

  updateIcon();
}
void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
{
  SymbologyList itemList;
  QPixmap legendpixmap = layer->legendAsPixmap( true ).scaled( 20, 20, Qt::KeepAspectRatio );
  itemList.append( qMakePair( QString(), legendpixmap ) );

  changeSymbologySettings( layer, itemList );
}
Exemplo n.º 4
0
void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
{
  SymbologyList itemList;
  QList< QPair< QString, QColor > > rasterItemList = layer->legendSymbologyItems();
  QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
#if QT_VERSION >= 0x40700
  itemList.reserve( rasterItemList.size() );
#endif

  // GetLegendGraphics in case of WMS service... pixmap can return null if GetLegendGraphics
  // is not supported by the server
  QgsDebugMsg( QString( "layer providertype:: %1" ).arg( layer->providerType() ) );
  if ( layer->providerType() == "wms" )
  {
    double currentScale = legend()->canvas()->scale();

    QImage legendGraphic = layer->dataProvider()->getLegendGraphic( currentScale );
    if ( !legendGraphic.isNull() )
    {
      QgsDebugMsg( QString( "downloaded legend with dimension width:" ) + QString::number( legendGraphic.width() ) + QString( " and Height:" ) + QString::number( legendGraphic.height() ) );

#if QT_VERSION >= 0x40700
      if ( rasterItemList.size() == 0 ) itemList.reserve( 1 );
#endif
      itemList.append( qMakePair( QString( "" ), QPixmap::fromImage( legendGraphic ) ) );
    }
  }

  // Paletted raster may have many colors, for example UInt16 may have 65536 colors
  // and it is very slow, so we limit max count
  QSize iconSize = treeWidget()->iconSize();
  int count = 0;
  int max_count = 1000;
  for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
  {
    QPixmap itemPixmap( iconSize );
    itemPixmap.fill( itemIt->second );
    // This is very slow, not clear why, it should not be, probably realloc,
    // but it seems to be non linear
    itemList.append( qMakePair( itemIt->first, itemPixmap ) );
    count++;
    if ( count == max_count )
    {
      itemPixmap = QPixmap( iconSize );
      itemPixmap.fill( Qt::transparent );
      QString label = tr( "following %1 items\nnot displayed" ).arg( rasterItemList.size() - max_count );
      itemList.append( qMakePair( label, itemPixmap ) );
      break;
    }
  }
  changeSymbologySettings( layer, itemList );
}
Exemplo n.º 5
0
void QgsLegendLayer::rasterLayerSymbology( QgsRasterLayer* layer )
{
  SymbologyList itemList;
  QList< QPair< QString, QColor > > rasterItemList = layer->legendSymbologyItems();
  QList< QPair< QString, QColor > >::const_iterator itemIt = rasterItemList.constBegin();
  for ( ; itemIt != rasterItemList.constEnd(); ++itemIt )
  {
    QPixmap itemPixmap( treeWidget()->iconSize() );
    itemPixmap.fill( itemIt->second );
    itemList.append( qMakePair( itemIt->first, itemPixmap ) );
  }
  changeSymbologySettings( layer, itemList );
}
Exemplo n.º 6
0
void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
{
  QSize iconSize( 16, 16 );

  QgsFeatureRendererV2* renderer = layer->rendererV2();
  if ( renderer )
  {
    SymbologyList itemList = renderer->legendSymbologyItems( iconSize );
    if ( mShowFeatureCount )
    {
      updateItemListCountV2( itemList, layer );
    }

    changeSymbologySettings( layer, itemList );
  }
}
Exemplo n.º 7
0
void QgsLegendLayer::vectorLayerSymbology( QgsVectorLayer* layer, double widthScale )
{
  if ( !layer )
  {
    return;
  }

  SymbologyList itemList;
  if ( layer->hasGeometryType() )
  {
    //add the new items
    QString lw, uv, label;
    const QgsRenderer* renderer = layer->renderer();
    const QList<QgsSymbol*> sym = renderer->symbols();

    //create an item for each classification field (only one for most renderers)
    QSettings settings;
    if ( settings.value( "/qgis/showLegendClassifiers", false ).toBool() )
    {
      if ( renderer->needsAttributes() )
      {
        QgsAttributeList classfieldlist = renderer->classificationAttributes();
        const QgsFieldMap& fields = layer->pendingFields();
        for ( QgsAttributeList::iterator it = classfieldlist.begin(); it != classfieldlist.end(); ++it )
        {
          QString classfieldname = layer->attributeAlias( *it );
          if ( classfieldname.isEmpty() )
          {
            classfieldname = fields[*it].name();
          }
          itemList.append( qMakePair( classfieldname, QPixmap() ) );
        }
      }
    }

    QMap< QgsSymbol*, int > featureCountMap;
    if ( mShowFeatureCount )
    {
      updateItemListCount( layer, sym, featureCountMap );
    }

    for ( QList<QgsSymbol*>::const_iterator it = sym.begin(); it != sym.end(); ++it )
    {
      QImage img;
      if (( *it )->type() == QGis::Point )
      {
        img = ( *it )->getPointSymbolAsImage( widthScale );
      }
      else if (( *it )->type() == QGis::Line )
      {
        img = ( *it )->getLineSymbolAsImage();
      }
      else  if (( *it )->type() == QGis::Polygon )
      {
        img = ( *it )->getPolygonSymbolAsImage();
      }
      else
      {
        // must be a layer without geometry then
      }

      QString values;
      lw = ( *it )->lowerValue();
      if ( !lw.isEmpty() )
      {
        values += lw;
      }
      uv = ( *it )->upperValue();
      if ( !uv.isEmpty() && lw != uv )
      {
        values += " - ";
        values += uv;
      }
      label = ( *it )->label();
      if ( !label.isEmpty() )
      {
        values += " ";
        values += label;
      }

      if ( mShowFeatureCount )
      {
        int fCount = featureCountMap[*it];
        if ( fCount >= 0 )
        {
          values += ( " [" + QString::number( fCount ) + "]" );
        }
      }

      QPixmap pix = QPixmap::fromImage( img ); // convert to pixmap
      itemList.append( qMakePair( values, pix ) );
    }
  }
  changeSymbologySettings( layer, itemList );
}