[QGIS Commit] r12811 - in trunk/qgis/src: core/symbology-ng gui/symbology-ng ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Thu Jan 21 11:14:05 EST 2010


Author: mhugent
Date: 2010-01-21 11:14:04 -0500 (Thu, 21 Jan 2010)
New Revision: 12811

Modified:
   trunk/qgis/src/core/symbology-ng/qgsfillsymbollayerv2.cpp
   trunk/qgis/src/core/symbology-ng/qgslinesymbollayerv2.cpp
   trunk/qgis/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
   trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.cpp
   trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.h
   trunk/qgis/src/core/symbology-ng/qgssymbolv2.cpp
   trunk/qgis/src/core/symbology-ng/qgssymbolv2.h
   trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.cpp
   trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.h
   trunk/qgis/src/ui/qgssymbolv2selectordialogbase.ui
Log:
Added layer transparency to symbol classes (with the exception of svg marker, where I'm still looking for a solution with QPicture)

Modified: trunk/qgis/src/core/symbology-ng/qgsfillsymbollayerv2.cpp
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgsfillsymbollayerv2.cpp	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgsfillsymbollayerv2.cpp	2010-01-21 16:14:04 UTC (rev 12811)
@@ -43,7 +43,9 @@
 
 void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
 {
+  mColor.setAlphaF( context.alpha() );
   mBrush = QBrush( mColor, mBrushStyle );
+  mBorderColor.setAlphaF( context.alpha() );
   mPen = QPen( mBorderColor );
   mPen.setStyle( mBorderStyle );
   mPen.setWidthF( context.outputLineWidth( mBorderWidth ) );
@@ -162,10 +164,10 @@
   }
 
   //create QImage with appropriate dimensions
-  int pixelWidth = context.outputPixelSize( mPatternWidth );//mPatternWidth.value( context, QgsOutputUnit::Pixel );
+  int pixelWidth = context.outputPixelSize( mPatternWidth );
   int pixelHeight = pixelWidth / mSvgViewBox.width() * mSvgViewBox.height();
 
-  QImage textureImage( pixelWidth, pixelHeight, QImage::Format_ARGB32_Premultiplied );
+  QImage textureImage( pixelWidth, pixelHeight, QImage::Format_ARGB32 );
   textureImage.fill( 0 ); // transparent background
 
   //rasterise byte array to image
@@ -177,6 +179,11 @@
   }
   r.render( &p );
 
+  if ( context.alpha() < 1.0 )
+  {
+    QgsSymbolLayerV2Utils::multiplyImageOpacity( &textureImage, context.alpha() );
+  }
+
   QTransform brushTransform;
   brushTransform.scale( 1.0 / context.renderContext().rasterScaleFactor(), 1.0 / context.renderContext().rasterScaleFactor() );
   mBrush.setTextureImage( textureImage );

Modified: trunk/qgis/src/core/symbology-ng/qgslinesymbollayerv2.cpp
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgslinesymbollayerv2.cpp	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgslinesymbollayerv2.cpp	2010-01-21 16:14:04 UTC (rev 12811)
@@ -58,7 +58,9 @@
 
 void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
 {
-  mPen.setColor( mColor );
+  QColor penColor = mColor;
+  penColor.setAlphaF( context.alpha() );
+  mPen.setColor( penColor );
   double scaledWidth = context.outputLineWidth( mWidth );
   mPen.setWidthF( scaledWidth );
   if ( mUseCustomDashPattern && scaledWidth != 0 )
@@ -242,6 +244,7 @@
   // if being rotated, it gets initialized with every line segment
   if ( !mRotateMarker )
   {
+    mMarker->setAlpha( context.alpha() );
     mMarker->startRender( context.renderContext() );
   }
 }
@@ -298,6 +301,7 @@
     // rotate marker (if desired)
     if ( mRotateMarker )
     {
+      mMarker->setAlpha( context.alpha() );
       mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) );
       mMarker->startRender( rc );
     }
@@ -394,7 +398,9 @@
 
 void QgsLineDecorationSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
 {
-  mPen.setColor( mColor );
+  QColor penColor = mColor;
+  penColor.setAlphaF( context.alpha() );
+  mPen.setColor( penColor );
 }
 
 void QgsLineDecorationSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )

Modified: trunk/qgis/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp	2010-01-21 16:14:04 UTC (rev 12811)
@@ -164,7 +164,7 @@
 
   double center = (( double ) imageSize / 2 ) + 0.5; // add 1/2 pixel for proper rounding when the figure's coordinates are added
 
-  mCache = QImage( QSize( imageSize, imageSize ), QImage::Format_ARGB32_Premultiplied );
+  mCache = QImage( QSize( imageSize, imageSize ), QImage::Format_ARGB32 );
   mCache.fill( 0 );
 
   QPainter p;
@@ -175,6 +175,12 @@
   p.translate( QPointF( center, center ) );
   drawMarker( &p, context );
   p.end();
+
+  //opacity
+  if ( context.alpha() < 1.0 )
+  {
+    QgsSymbolLayerV2Utils::multiplyImageOpacity( &mCache, context.alpha() );
+  }
 }
 
 void QgsSimpleMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
@@ -512,6 +518,8 @@
   mFont = QFont( mFontFamily, MM2POINT( mSize ) );
   QFontMetrics fm( mFont );
   mChrOffset = QPointF( fm.width( mChr ) / 2, -fm.ascent() / 2 );
+
+
 }
 
 void QgsFontMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
@@ -521,15 +529,17 @@
 void QgsFontMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
 {
   QPainter* p = context.renderContext().painter();
-  p->setPen( mColor );
+  QColor penColor = mColor;
+  penColor.setAlphaF( context.alpha() );
+  p->setPen( penColor );
   p->setFont( mFont );
 
   p->save();
-  p->translate(point);
+  p->translate( point );
   if ( mAngle != 0 )
     p->rotate( mAngle );
 
-  p->drawText(-mChrOffset, mChr );
+  p->drawText( -mChrOffset, mChr );
   p->restore();
 }
 

Modified: trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.cpp
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.cpp	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.cpp	2010-01-21 16:14:04 UTC (rev 12811)
@@ -414,6 +414,7 @@
   }
 
   symbol->setOutputUnit( decodeOutputUnit( element.attribute( "outputUnit" ) ) );
+  symbol->setAlpha( element.attribute( "alpha", "1.0" ).toDouble() );
 
   return symbol;
 }
@@ -459,6 +460,7 @@
   symEl.setAttribute( "type", _nameForSymbolType( symbol->type() ) );
   symEl.setAttribute( "name", name );
   symEl.setAttribute( "outputUnit", encodeOutputUnit( symbol->outputUnit() ) );
+  symEl.setAttribute( "alpha", symbol->alpha() );
   QgsDebugMsg( "num layers " + QString::number( symbol->symbolLayerCount() ) );
   for ( int i = 0; i < symbol->symbolLayerCount(); i++ )
   {
@@ -712,3 +714,28 @@
   }
   return context;
 }
+
+void QgsSymbolLayerV2Utils::multiplyImageOpacity( QImage* image, qreal alpha )
+{
+  if ( !image )
+  {
+    return;
+  }
+
+  //change the alpha component of every pixel
+  int widthIndex = 0;
+  int heightIndex = 0;
+  QRgb* scanLine = 0;
+  QRgb myRgb;
+
+  for ( ; heightIndex < image->height(); ++heightIndex )
+  {
+    scanLine = ( QRgb* )image->scanLine( heightIndex );
+    widthIndex = 0;
+    for ( ; widthIndex < image->width(); ++widthIndex )
+    {
+      myRgb = image->pixel( widthIndex, heightIndex );
+      scanLine[widthIndex] = qRgba( qRed( myRgb ), qGreen( myRgb ), qBlue( myRgb ), qAlpha( myRgb ) * alpha );
+    }
+  }
+}

Modified: trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.h
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.h	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgssymbollayerv2utils.h	2010-01-21 16:14:04 UTC (rev 12811)
@@ -80,6 +80,9 @@
     static QgsRenderContext createRenderContext( QPainter* p );
 
     static QString iconPath( QString iconFile );
+
+    /**Multiplies opacity of image pixel values with a (global) transparency value*/
+    static void multiplyImageOpacity( QImage* image, qreal alpha );
 };
 
 class QPolygonF;

Modified: trunk/qgis/src/core/symbology-ng/qgssymbolv2.cpp
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgssymbolv2.cpp	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgssymbolv2.cpp	2010-01-21 16:14:04 UTC (rev 12811)
@@ -17,7 +17,7 @@
 #include <cmath>
 
 QgsSymbolV2::QgsSymbolV2( SymbolType type, QgsSymbolLayerV2List layers )
-    : mType( type ), mLayers( layers ), mOutputUnit( MM )
+    : mType( type ), mLayers( layers ), mOutputUnit( MM ), mAlpha( 1.0 )
 {
 
   // check they're all correct symbol layers
@@ -126,14 +126,14 @@
 
 void QgsSymbolV2::startRender( QgsRenderContext& context )
 {
-  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
+  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
   for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
     ( *it )->startRender( symbolContext );
 }
 
 void QgsSymbolV2::stopRender( QgsRenderContext& context )
 {
-  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
+  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
   for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
     ( *it )->stopRender( symbolContext );
 }
@@ -244,8 +244,8 @@
 
 ////////////////////
 
-QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u )
-    : mRenderContext( c ), mOutputUnit( u )
+QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u, qreal alpha )
+    : mRenderContext( c ), mOutputUnit( u ), mAlpha( alpha )
 {
 
 }
@@ -331,7 +331,7 @@
 
 void QgsMarkerSymbolV2::renderPoint( const QPointF& point, QgsRenderContext& context, int layer )
 {
-  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
+  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
   if ( layer != -1 )
   {
     if ( layer >= 0 && layer < mLayers.count() )
@@ -350,6 +350,7 @@
 {
   QgsSymbolV2* cloneSymbol = new QgsMarkerSymbolV2( cloneLayers() );
   cloneSymbol->setOutputUnit( mOutputUnit );
+  cloneSymbol->setAlpha( mAlpha );
   return cloneSymbol;
 }
 
@@ -399,7 +400,7 @@
 
 void QgsLineSymbolV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer )
 {
-  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
+  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
   if ( layer != -1 )
   {
     if ( layer >= 0 && layer < mLayers.count() )
@@ -419,6 +420,7 @@
 {
   QgsSymbolV2* cloneSymbol = new QgsLineSymbolV2( cloneLayers() );
   cloneSymbol->setOutputUnit( mOutputUnit );
+  cloneSymbol->setAlpha( mAlpha );
   return cloneSymbol;
 }
 
@@ -434,7 +436,7 @@
 
 void QgsFillSymbolV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context, int layer )
 {
-  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
+  QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha );
   if ( layer != -1 )
   {
     if ( layer >= 0 && layer < mLayers.count() )
@@ -454,5 +456,6 @@
 {
   QgsSymbolV2* cloneSymbol = new QgsFillSymbolV2( cloneLayers() );
   cloneSymbol->setOutputUnit( mOutputUnit );
+  cloneSymbol->setAlpha( mAlpha );
   return cloneSymbol;
 }

Modified: trunk/qgis/src/core/symbology-ng/qgssymbolv2.h
===================================================================
--- trunk/qgis/src/core/symbology-ng/qgssymbolv2.h	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/core/symbology-ng/qgssymbolv2.h	2010-01-21 16:14:04 UTC (rev 12811)
@@ -81,6 +81,9 @@
     OutputUnit outputUnit() const { return mOutputUnit; }
     void setOutputUnit( OutputUnit u ) { mOutputUnit = u; }
 
+    qreal alpha() const { return mAlpha; }
+    void setAlpha( qreal alpha ) { mAlpha = alpha; }
+
   protected:
     QgsSymbolV2( SymbolType type, QgsSymbolLayerV2List layers ); // can't be instantiated
 
@@ -90,6 +93,9 @@
     QgsSymbolLayerV2List mLayers;
 
     OutputUnit mOutputUnit;
+
+    /**Symbol opacity (in the range 0 - 1)*/
+    qreal mAlpha;
 };
 
 ///////////////////////
@@ -97,7 +103,7 @@
 class CORE_EXPORT QgsSymbolV2RenderContext
 {
   public:
-    QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u );
+    QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u , qreal alpha = 1.0 );
     ~QgsSymbolV2RenderContext();
 
     QgsRenderContext& renderContext() { return mRenderContext; }
@@ -106,12 +112,16 @@
     QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
     void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
 
-    double outputLineWidth(double width) const;
-    double outputPixelSize(double size) const;
+    qreal alpha() const { return mAlpha; }
+    void setAlpha( qreal alpha ) { mAlpha = alpha; }
 
+    double outputLineWidth( double width ) const;
+    double outputPixelSize( double size ) const;
+
   private:
     QgsRenderContext& mRenderContext;
     QgsSymbolV2::OutputUnit mOutputUnit;
+    qreal mAlpha;
 };
 
 

Modified: trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.cpp
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.cpp	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.cpp	2010-01-21 16:14:04 UTC (rev 12811)
@@ -41,11 +41,18 @@
   updateSymbolPreview();
   updateSymbolInfo();
 
-  // output unit
-  mSymbolUnitComboBox->blockSignals( true );
-  mSymbolUnitComboBox->setCurrentIndex( mSymbol->outputUnit() );
-  mSymbolUnitComboBox->blockSignals( false );
+  if ( mSymbol )
+  {
+    // output unit
+    mSymbolUnitComboBox->blockSignals( true );
+    mSymbolUnitComboBox->setCurrentIndex( mSymbol->outputUnit() );
+    mSymbolUnitComboBox->blockSignals( false );
 
+    mOpacitySlider->blockSignals( true );
+    mOpacitySlider->setValue( symbol->alpha() * 255 );
+    mOpacitySlider->blockSignals( false );
+  }
+
   // select correct page in stacked widget
   // there's a correspondence between symbol type number and page numbering => exploit it!
   stackedWidget->setCurrentIndex( symbol->type() );
@@ -232,8 +239,21 @@
 
 void QgsSymbolV2SelectorDialog::on_mSymbolUnitComboBox_currentIndexChanged( const QString & text )
 {
-  mSymbol->setOutputUnit( (QgsSymbolV2::OutputUnit) mSymbolUnitComboBox->currentIndex() );
+  if ( mSymbol )
+  {
+    mSymbol->setOutputUnit(( QgsSymbolV2::OutputUnit ) mSymbolUnitComboBox->currentIndex() );
 
-  updateSymbolPreview();
-  emit symbolModified();
+    updateSymbolPreview();
+    emit symbolModified();
+  }
 }
+
+void QgsSymbolV2SelectorDialog::on_mOpacitySlider_valueChanged( int value )
+{
+  if ( mSymbol )
+  {
+    mSymbol->setAlpha( value / 255.0 );
+    updateSymbolPreview();
+    emit symbolModified();
+  }
+}

Modified: trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.h
===================================================================
--- trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.h	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/gui/symbology-ng/qgssymbolv2selectordialog.h	2010-01-21 16:14:04 UTC (rev 12811)
@@ -34,6 +34,7 @@
     void setLineWidth( double width );
     void addSymbolToStyle();
     void on_mSymbolUnitComboBox_currentIndexChanged( const QString & text );
+    void on_mOpacitySlider_valueChanged( int value );
 
   signals:
     void symbolModified();

Modified: trunk/qgis/src/ui/qgssymbolv2selectordialogbase.ui
===================================================================
--- trunk/qgis/src/ui/qgssymbolv2selectordialogbase.ui	2010-01-21 15:13:56 UTC (rev 12810)
+++ trunk/qgis/src/ui/qgssymbolv2selectordialogbase.ui	2010-01-21 16:14:04 UTC (rev 12811)
@@ -6,16 +6,16 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>331</width>
-    <height>404</height>
+    <width>278</width>
+    <height>416</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>Symbol selector</string>
   </property>
-  <layout class="QGridLayout" name="gridLayout_3">
+  <layout class="QGridLayout" name="gridLayout_4">
    <item row="0" column="0">
-    <layout class="QHBoxLayout" name="horizontalLayout_3">
+    <layout class="QHBoxLayout" name="horizontalLayout_5">
      <item>
       <widget class="QLabel" name="lblPreview">
        <property name="sizePolicy">
@@ -45,8 +45,8 @@
       </widget>
      </item>
      <item>
-      <layout class="QVBoxLayout" name="verticalLayout">
-       <item>
+      <layout class="QGridLayout" name="gridLayout_3">
+       <item row="0" column="0">
         <layout class="QHBoxLayout" name="horizontalLayout_4">
          <item>
           <widget class="QLabel" name="mSymbolUnitLabel">
@@ -71,7 +71,28 @@
          </item>
         </layout>
        </item>
-       <item>
+       <item row="1" column="0">
+        <layout class="QHBoxLayout" name="horizontalLayout_3">
+         <item>
+          <widget class="QLabel" name="mOpacityLabel">
+           <property name="text">
+            <string>Opacity</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QSlider" name="mOpacitySlider">
+           <property name="maximum">
+            <number>255</number>
+           </property>
+           <property name="orientation">
+            <enum>Qt::Horizontal</enum>
+           </property>
+          </widget>
+         </item>
+        </layout>
+       </item>
+       <item row="2" column="0">
         <widget class="QStackedWidget" name="stackedWidget">
          <property name="currentIndex">
           <number>0</number>



More information about the QGIS-commit mailing list