QGIS API Documentation
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 #include "qgs25drendererwidget.h"
30 
31 #include "qgsorderbydialog.h"
32 #include "qgsapplication.h"
33 #include "qgslogger.h"
34 #include "qgsvectorlayer.h"
35 
36 #include <QKeyEvent>
37 #include <QMessageBox>
38 
39 static bool _initRenderer( const QString& name, QgsRendererV2WidgetFunc f, const QString& iconName = QString() )
40 {
43  if ( !am )
44  return false;
45  QgsRendererV2Metadata* m = dynamic_cast<QgsRendererV2Metadata*>( am );
46  if ( !m )
47  return false;
48 
49  m->setWidgetFunction( f );
50 
51  if ( !iconName.isEmpty() )
52  {
54  QPixmap pix;
55  if ( pix.load( iconPath ) )
56  m->setIcon( pix );
57  }
58 
59  QgsDebugMsg( "Set for " + name );
60  return true;
61 }
62 
64 {
65  static bool initialized = false;
66  if ( initialized )
67  return;
68 
69  _initRenderer( "singleSymbol", QgsSingleSymbolRendererV2Widget::create, "rendererSingleSymbol.svg" );
70  _initRenderer( "categorizedSymbol", QgsCategorizedSymbolRendererV2Widget::create, "rendererCategorizedSymbol.svg" );
71  _initRenderer( "graduatedSymbol", QgsGraduatedSymbolRendererV2Widget::create, "rendererGraduatedSymbol.svg" );
72  _initRenderer( "RuleRenderer", QgsRuleBasedRendererV2Widget::create, "rendererRuleBasedSymbol.svg" );
73  _initRenderer( "pointDisplacement", QgsPointDisplacementRendererWidget::create, "rendererPointDisplacementSymbol.svg" );
74  _initRenderer( "invertedPolygonRenderer", QgsInvertedPolygonRendererWidget::create, "rendererInvertedSymbol.svg" );
75  _initRenderer( "heatmapRenderer", QgsHeatmapRendererWidget::create, "rendererHeatmapSymbol.svg" );
76  _initRenderer( "25dRenderer", Qgs25DRendererWidget::create, "renderer25dSymbol.svg" );
77  _initRenderer( "nullSymbol", QgsNullSymbolRendererWidget::create, "rendererNullSymbol.svg" );
78  initialized = true;
79 }
80 
82  : mLayer( layer )
83  , mStyle( style )
84  , mActiveWidget( nullptr )
85  , mPaintEffect( nullptr )
86  , mMapCanvas( nullptr )
87 {
88  setupUi( this );
89 
90  // can be embedded in vector layer properties
91  if ( embedded )
92  {
93  buttonBox->hide();
94  layout()->setContentsMargins( 0, 0, 0, 0 );
95  }
96 
97  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( onOK() ) );
98 
99  // initialize registry's widget functions
101 
102  // Blend mode
103  mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
104 
105  // Feature blend mode
106  mFeatureBlendComboBox->setBlendMode( mLayer->featureBlendMode() );
107 
108  // Layer transparency
109  mLayerTransparencySlider->setValue( mLayer->layerTransparency() );
110  mLayerTransparencySpnBx->setValue( mLayer->layerTransparency() );
111 
112  // connect layer transparency slider and spin box
113  connect( mLayerTransparencySlider, SIGNAL( valueChanged( int ) ), mLayerTransparencySpnBx, SLOT( setValue( int ) ) );
114  connect( mLayerTransparencySpnBx, SIGNAL( valueChanged( int ) ), mLayerTransparencySlider, SLOT( setValue( int ) ) );
115 
116  //paint effect widget
117  if ( mLayer->rendererV2() )
118  {
119  if ( mLayer->rendererV2()->paintEffect() )
120  {
122  mEffectWidget->setPaintEffect( mPaintEffect );
123  }
124 
126  }
127 
129  QStringList renderers = reg->renderersList();
130  Q_FOREACH ( const QString& name, renderers )
131  {
133  cboRenderers->addItem( m->icon(), m->visibleName(), name );
134  }
135 
136  cboRenderers->setCurrentIndex( -1 ); // set no current renderer
137 
138  // setup slot rendererChanged()
139  connect( cboRenderers, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rendererChanged() ) );
140  //setup order by
141  if ( mLayer->rendererV2()->orderByEnabled() )
142  {
143  checkboxEnableOrderBy->setChecked( true );
144  }
145  else
146  {
147  btnOrderBy->setEnabled( false );
148  checkboxEnableOrderBy->setChecked( false );
149  lineEditOrderBy->setEnabled( false );
150  }
151  lineEditOrderBy->setReadOnly( true );
152  connect( checkboxEnableOrderBy, SIGNAL( toggled( bool ) ), btnOrderBy, SLOT( setEnabled( bool ) ) );
153  connect( checkboxEnableOrderBy, SIGNAL( toggled( bool ) ), lineEditOrderBy, SLOT( setEnabled( bool ) ) );
154  connect( btnOrderBy, SIGNAL( clicked( bool ) ), this, SLOT( showOrderByDialog() ) );
155  lineEditOrderBy->setText( mOrderBy.dump() );
156 
157  // set current renderer from layer
158  QString rendererName = mLayer->rendererV2()->type();
159 
160  int rendererIdx = cboRenderers->findData( rendererName );
161  cboRenderers->setCurrentIndex( rendererIdx );
162 
163  // no renderer found... this mustn't happen
164  Q_ASSERT( rendererIdx != -1 && "there must be a renderer!" );
165 }
166 
168 {
169  delete mPaintEffect;
170 }
171 
173 {
174  mMapCanvas = canvas;
175  if ( mActiveWidget )
177 }
178 
179 
181 {
182 
183  if ( cboRenderers->currentIndex() == -1 )
184  {
185  QgsDebugMsg( "No current item -- this should never happen!" );
186  return;
187  }
188 
189  QString rendererName = cboRenderers->itemData( cboRenderers->currentIndex() ).toString();
190 
191  //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
192  QgsFeatureRendererV2* oldRenderer;
194  {
195  oldRenderer = mActiveWidget->renderer()->clone();
196  }
197  else
198  {
199  oldRenderer = mLayer->rendererV2()->clone();
200  }
201 
202  // get rid of old active widget (if any)
203  if ( mActiveWidget )
204  {
205  stackedWidget->removeWidget( mActiveWidget );
206 
207  delete mActiveWidget;
208  mActiveWidget = nullptr;
209  }
210 
211  QgsRendererV2Widget* w = nullptr;
213  if ( m )
214  w = m->createRendererWidget( mLayer, mStyle, oldRenderer );
215  delete oldRenderer;
216 
217  if ( w )
218  {
219  // instantiate the widget and set as active
220  mActiveWidget = w;
221  stackedWidget->addWidget( mActiveWidget );
222  stackedWidget->setCurrentWidget( mActiveWidget );
223  if ( mActiveWidget->renderer() )
224  {
225  if ( mMapCanvas )
227  changeOrderBy( mActiveWidget->renderer()->orderBy(), mActiveWidget->renderer()->orderByEnabled() );
228  connect( mActiveWidget, SIGNAL( layerVariablesChanged() ), this, SIGNAL( layerVariablesChanged() ) );
229  }
230  }
231  else
232  {
233  // set default "no edit widget available" page
234  stackedWidget->setCurrentWidget( pageNoWidget );
235  }
236 }
237 
239 {
240  if ( !mActiveWidget || !mLayer )
241  {
242  return;
243  }
244 
246 
248  if ( renderer )
249  {
250  renderer->setPaintEffect( mPaintEffect->clone() );
251  // set the order by
252  renderer->setOrderBy( mOrderBy );
253  renderer->setOrderByEnabled( checkboxEnableOrderBy->isChecked() );
254 
255  mLayer->setRendererV2( renderer->clone() );
256  }
257 
258  // set the blend modes for the layer
259  mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
260  mLayer->setFeatureBlendMode( mFeatureBlendComboBox->blendMode() );
261 
262  // set transparency for the layer
263  mLayer->setLayerTransparency( mLayerTransparencySlider->value() );
264 }
265 
267 {
268  apply();
269  accept();
270 }
271 
272 void QgsRendererV2PropertiesDialog::showOrderByDialog()
273 {
274  QgsOrderByDialog dlg( mLayer, this );
275 
276  dlg.setOrderBy( mOrderBy );
277  if ( dlg.exec() )
278  {
279  mOrderBy = dlg.orderBy();
280  lineEditOrderBy->setText( mOrderBy.dump() );
281  }
282 }
283 
284 void QgsRendererV2PropertiesDialog::changeOrderBy( const QgsFeatureRequest::OrderBy& orderBy, bool orderByEnabled )
285 {
286  mOrderBy = orderBy;
287  lineEditOrderBy->setText( mOrderBy.dump() );
288  checkboxEnableOrderBy->setChecked( orderByEnabled );
289 }
290 
291 
293 {
294  // Ignore the ESC key to avoid close the dialog without the properties window
295  if ( !isWindow() && e->key() == Qt::Key_Escape )
296  {
297  e->ignore();
298  }
299  else
300  {
302  }
303 }
QLayout * layout() const
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the dialog.
static QgsRendererV2Registry * instance()
This is a dialog to build and manage a list of order by clauses.
void setContentsMargins(int left, int top, int right, int bottom)
void setupUi(QWidget *widget)
void setOrderBy(const QgsFeatureRequest::OrderBy &orderBy)
Set the order by to manage.
QgsFeatureRequest::OrderBy orderBy() const
Get the order in which features shall be processed by this renderer.
static QString defaultThemePath()
Returns the path to the default theme directory.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
bool orderByEnabled() const
Returns whether custom ordering will be applied before features are processed by this renderer...
void setRendererV2(QgsFeatureRendererV2 *r)
Set renderer which will be invoked to represent this layer.
void setFeatureBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering each feature.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
void accepted()
int exec()
void setLayerTransparency(int layerTransparency)
Set the transparency for the vector layer.
Stores metadata about one renderer class.
QString iconPath(const QString &iconFile)
void setBlendMode(QPainter::CompositionMode blendMode)
Set the blending mode used for rendering a layer.
Registry of renderers.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
QgsRendererV2AbstractMetadata * rendererMetadata(const QString &rendererName)
get metadata for particular renderer. Returns NULL if not found in registry.
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:83
void setWidgetFunction(QgsRendererV2WidgetFunc f)
QString CORE_EXPORT dump() const
Dumps the content to an SQL equivalent syntax.
const char * name() const
virtual QgsFeatureRendererV2 * clone() const =0
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
void setEnabled(bool)
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
void ignore()
QPainter::CompositionMode featureBlendMode() const
Returns the current blending mode for features.
QStringList renderersList()
return a list of available renderers
void layerVariablesChanged()
Emitted when expression context variables on the associated vector layers have been changed...
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
void setOrderByEnabled(bool enabled)
Sets whether custom ordering should be applied before features are processed by this renderer...
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the renderer.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
void applyChanges()
This method should be called whenever the renderer is actually set on the layer.
bool load(const QString &fileName, const char *format, QFlags< Qt::ImageConversionFlag > flags)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
virtual void accept()
bool isWindow() const
QgsRendererV2PropertiesDialog(QgsVectorLayer *layer, QgsStyleV2 *style, bool embedded=false)
int key() const
static bool _initRenderer(const QString &name, QgsRendererV2WidgetFunc f, const QString &iconName=QString())
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.
void rendererChanged()
called when user changes renderer type
int layerTransparency() const
Returns the current transparency for the vector layer.
virtual void keyPressEvent(QKeyEvent *e)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Creates a new QgsNullSymbolRendererWidget object.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.
void setOrderBy(const QgsFeatureRequest::OrderBy &orderBy)
Define the order in which features shall be processed by this renderer.
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.
virtual QgsRendererV2Widget * createRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *oldRenderer)
Return new instance of settings widget for the renderer.
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the renderer.
Base class for renderer settings widgets.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
QgsFeatureRequest::OrderBy orderBy()
Get the order by defined in the dialog.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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 *)
virtual void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
Represents a list of OrderByClauses, with the most important first and the least important last...
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.