QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget( QgsMapLayer *layer )
  : mLayer( layer )
{
  setAutoFillBackground( true ); // override the content from model
  QLabel *l = new QLabel( QStringLiteral( "Opacity" ), this );
  mSlider = new QSlider( Qt::Horizontal, this );
  mSlider->setRange( 0, 1000 );
  QHBoxLayout *lay = new QHBoxLayout();
  lay->addWidget( l );
  lay->addWidget( mSlider );
  setLayout( lay );

  // timer for delayed transparency update - for more responsive GUI
  mTimer = new QTimer( this );
  mTimer->setSingleShot( true );
  mTimer->setInterval( 100 );
  connect( mTimer, &QTimer::timeout, this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );

  connect( mSlider, &QAbstractSlider::valueChanged, this, &QgsLayerTreeOpacityWidget::sliderValueChanged );

  // init from layer
  if ( mLayer->type() == QgsMapLayer::VectorLayer )
  {
    QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayer );
    mSlider->setValue( vl->opacity() * 1000.0 );
    connect( vl, &QgsVectorLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
  }
  else if ( mLayer->type() == QgsMapLayer::RasterLayer )
  {
    mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
    // TODO: there is no signal for raster layers
  }
}
示例#2
0
QgsLayerRestorer::QgsLayerRestorer( const QList<QgsMapLayer *> &layers )
{
  for ( QgsMapLayer *layer : layers )
  {
    QgsLayerSettings settings;
    settings.name = layer->name();

    QString style = layer->styleManager()->currentStyle();
    settings.mNamedStyle = layer->styleManager()->currentStyle();

    // set a custom property allowing to keep in memory if a SLD file has
    // been loaded for rendering
    layer->setCustomProperty( "readSLD", false );

    QString errMsg;
    QDomDocument sldDoc;
    layer->exportSldStyle( sldDoc, errMsg );
    ( void )settings.mSldStyle.setContent( sldDoc.toString(), true ); // for namespace processing

    switch ( layer->type() )
    {
      case QgsMapLayer::VectorLayer:
      {
        QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layer );

        if ( vLayer )
        {
          settings.mOpacity = vLayer->opacity();
          settings.mSelectedFeatureIds = vLayer->selectedFeatureIds();
          settings.mFilter = vLayer->subsetString();
        }
        break;
      }
      case QgsMapLayer::LayerType::RasterLayer:
      {
        QgsRasterLayer *rLayer = qobject_cast<QgsRasterLayer *>( layer );

        if ( rLayer )
        {
          settings.mOpacity = rLayer->renderer()->opacity();
        }
        break;
      }

      case QgsMapLayer::MeshLayer:
      case QgsMapLayer::PluginLayer:
        break;
    }

    mLayerSettings[layer] = settings;
  }
}