QGIS API Documentation  2.9.0-Master
qgsrendererv2propertiesdialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrendererv2propertiesdialog.cpp
3  ---------------------
4  begin : December 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
16 
17 #include "qgsrendererv2.h"
18 #include "qgsrendererv2registry.h"
19 
20 #include "qgsrendererv2widget.h"
28 
29 #include "qgsapplication.h"
30 #include "qgslogger.h"
31 #include "qgsvectorlayer.h"
32 
33 #include <QKeyEvent>
34 #include <QMessageBox>
35 
36 static bool _initRenderer( QString name, QgsRendererV2WidgetFunc f, QString iconName = QString() )
37 {
40  if ( am == NULL )
41  return false;
42  QgsRendererV2Metadata* m = dynamic_cast<QgsRendererV2Metadata*>( am );
43  if ( m == NULL )
44  return false;
45 
46  m->setWidgetFunction( f );
47 
48  if ( !iconName.isEmpty() )
49  {
50  QString iconPath = QgsApplication::defaultThemePath() + iconName;
51  QPixmap pix;
52  if ( pix.load( iconPath, "png" ) )
53  m->setIcon( pix );
54  }
55 
56  QgsDebugMsg( "Set for " + name );
57  return true;
58 }
59 
61 {
62  static bool initialized = false;
63  if ( initialized )
64  return;
65 
66  _initRenderer( "singleSymbol", QgsSingleSymbolRendererV2Widget::create, "rendererSingleSymbol.png" );
67  _initRenderer( "categorizedSymbol", QgsCategorizedSymbolRendererV2Widget::create, "rendererCategorizedSymbol.png" );
68  _initRenderer( "graduatedSymbol", QgsGraduatedSymbolRendererV2Widget::create, "rendererGraduatedSymbol.png" );
71  _initRenderer( "invertedPolygonRenderer", QgsInvertedPolygonRendererWidget::create );
72  _initRenderer( "heatmapRenderer", QgsHeatmapRendererWidget::create );
73  initialized = true;
74 }
75 
77  : mLayer( layer )
78  , mStyle( style )
79  , mActiveWidget( NULL )
80  , mPaintEffect( 0 )
81 {
82  setupUi( this );
83 
84  // can be embedded in vector layer properties
85  if ( embedded )
86  {
87  buttonBox->hide();
88  layout()->setContentsMargins( 0, 0, 0, 0 );
89  }
90 
91  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( onOK() ) );
92 
93  // initialize registry's widget functions
95 
96  // Blend mode
97  mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
98 
99  // Feature blend mode
100  mFeatureBlendComboBox->setBlendMode( mLayer->featureBlendMode() );
101 
102  // Layer transparency
103  mLayerTransparencySlider->setValue( mLayer->layerTransparency() );
104  mLayerTransparencySpnBx->setValue( mLayer->layerTransparency() );
105 
106  // connect layer transparency slider and spin box
107  connect( mLayerTransparencySlider, SIGNAL( valueChanged( int ) ), mLayerTransparencySpnBx, SLOT( setValue( int ) ) );
108  connect( mLayerTransparencySpnBx, SIGNAL( valueChanged( int ) ), mLayerTransparencySlider, SLOT( setValue( int ) ) );
109 
110  //paint effect widget
111  if ( mLayer->rendererV2() && mLayer->rendererV2()->paintEffect() )
112  {
114  mEffectWidget->setPaintEffect( mPaintEffect );
115  }
116 
117  QPixmap pix;
119  QStringList renderers = reg->renderersList();
120  foreach ( QString name, renderers )
121  {
123  cboRenderers->addItem( m->icon(), m->visibleName(), name );
124  }
125 
126  cboRenderers->setCurrentIndex( -1 ); // set no current renderer
127 
128  // setup slot rendererChanged()
129  connect( cboRenderers, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rendererChanged() ) );
130 
131  // set current renderer from layer
132  QString rendererName = mLayer->rendererV2()->type();
133  for ( int i = 0; i < cboRenderers->count(); i++ )
134  {
135  if ( cboRenderers->itemData( i ).toString() == rendererName )
136  {
137  cboRenderers->setCurrentIndex( i );
138  return;
139  }
140  }
141 
142  // no renderer found... this mustn't happen
143  Q_ASSERT( false && "there must be a renderer!" );
144 
145 }
146 
148 {
149  delete mPaintEffect;
150 }
151 
152 
154 {
155 
156  if ( cboRenderers->currentIndex() == -1 )
157  {
158  QgsDebugMsg( "No current item -- this should never happen!" );
159  return;
160  }
161 
162  QString rendererName = cboRenderers->itemData( cboRenderers->currentIndex() ).toString();
163 
164  //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
165  QgsFeatureRendererV2* oldRenderer;
167  {
168  oldRenderer = mActiveWidget->renderer()->clone();
169  }
170  else
171  {
172  oldRenderer = mLayer->rendererV2()->clone();
173  }
174 
175  // get rid of old active widget (if any)
176  if ( mActiveWidget )
177  {
178  stackedWidget->removeWidget( mActiveWidget );
179 
180  delete mActiveWidget;
181  mActiveWidget = NULL;
182  }
183 
184  QgsRendererV2Widget* w = NULL;
186  if ( m != NULL )
187  w = m->createRendererWidget( mLayer, mStyle, oldRenderer );
188 
189  if ( w != NULL )
190  {
191  // instantiate the widget and set as active
192  mActiveWidget = w;
193  stackedWidget->addWidget( mActiveWidget );
194  stackedWidget->setCurrentWidget( mActiveWidget );
195  }
196  else
197  {
198  // set default "no edit widget available" page
199  stackedWidget->setCurrentWidget( pageNoWidget );
200  }
201 
202 }
203 
205 {
206  if ( !mActiveWidget || !mLayer )
207  {
208  return;
209  }
210 
212  if ( renderer )
213  {
214  renderer->setPaintEffect( mPaintEffect->clone() );
215  mLayer->setRendererV2( renderer->clone() );
216  }
217 
218  // set the blend modes for the layer
219  mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
220  mLayer->setFeatureBlendMode( mFeatureBlendComboBox->blendMode() );
221 
222  // set transparency for the layer
223  mLayer->setLayerTransparency( mLayerTransparencySlider->value() );
224 }
225 
227 {
228  apply();
229  accept();
230 }
231 
232 
234 {
235  // Ignore the ESC key to avoid close the dialog without the properties window
236  if ( !isWindow() && e->key() == Qt::Key_Escape )
237  {
238  e->ignore();
239  }
240  else
241  {
242  QDialog::keyPressEvent( e );
243  }
244 }
static QgsRendererV2Registry * instance()
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
QgsRendererV2AbstractMetadata * rendererMetadata(QString rendererName)
get metadata for particular renderer. Returns NULL if not found in registry.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void setRendererV2(QgsFeatureRendererV2 *r)
Set renderer V2.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
void setLayerTransparency(int layerTransparency)
Set the transparency for the vector layer.
Stores metadata about one renderer class.
void setBlendMode(const QPainter::CompositionMode &blendMode)
Set the blending mode used for rendering a layer.
Registry of renderers.
QString iconPath(QString iconFile)
virtual QgsPaintEffect * clone() const =0
Duplicates an effect by creating a deep copy of the effect.
void setIcon(const QIcon &icon)
static void _initRendererWidgetFunctions()
QString type() const
Definition: qgsrendererv2.h:82
void setWidgetFunction(QgsRendererV2WidgetFunc f)
virtual QgsFeatureRendererV2 * clone() const =0
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
QPainter::CompositionMode featureBlendMode() const
Returns the current blending mode for features.
QStringList renderersList()
return a list of available renderers
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
static const QString defaultThemePath()
Returns the path to the default theme directory.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the renderer.
void setFeatureBlendMode(const QPainter::CompositionMode &blendMode)
Set the blending mode used for rendering each feature.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
QgsRendererV2PropertiesDialog(QgsVectorLayer *layer, QgsStyleV2 *style, bool embedded=false)
void rendererChanged()
called when user changes renderer type
int layerTransparency() const
Returns the current transparency for the vector layer.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
static creation method
Convenience metadata class that uses static functions to create renderer and its widget.
void keyPressEvent(QKeyEvent *event) override
Reimplements dialog keyPress event so we can ignore it.
static bool _initRenderer(QString name, QgsRendererV2WidgetFunc f, QString iconName=QString())
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the renderer.
Base class for renderer settings widgets.
virtual QgsRendererV2Widget * createRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Return new instance of settings widget for the renderer.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Represents a vector layer which manages a vector based data sets.
virtual QgsFeatureRendererV2 * renderer()=0
return pointer to the renderer (no transfer of ownership)
QgsRendererV2Widget *(* QgsRendererV2WidgetFunc)(QgsVectorLayer *, QgsStyleV2 *, QgsFeatureRendererV2 *)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
static creation method