QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  {
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  delete oldRenderer;
189 
190  if ( w != NULL )
191  {
192  // instantiate the widget and set as active
193  mActiveWidget = w;
194  stackedWidget->addWidget( mActiveWidget );
195  stackedWidget->setCurrentWidget( mActiveWidget );
196  }
197  else
198  {
199  // set default "no edit widget available" page
200  stackedWidget->setCurrentWidget( pageNoWidget );
201  }
202 
203 }
204 
206 {
207  if ( !mActiveWidget || !mLayer )
208  {
209  return;
210  }
211 
213  if ( renderer )
214  {
215  renderer->setPaintEffect( mPaintEffect->clone() );
216  mLayer->setRendererV2( renderer->clone() );
217  }
218 
219  // set the blend modes for the layer
220  mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
221  mLayer->setFeatureBlendMode( mFeatureBlendComboBox->blendMode() );
222 
223  // set transparency for the layer
224  mLayer->setLayerTransparency( mLayerTransparencySlider->value() );
225 }
226 
228 {
229  apply();
230  accept();
231 }
232 
233 
235 {
236  // Ignore the ESC key to avoid close the dialog without the properties window
237  if ( !isWindow() && e->key() == Qt::Key_Escape )
238  {
239  e->ignore();
240  }
241  else
242  {
244  }
245 }
QLayout * layout() const
static QgsRendererV2Registry * instance()
void setContentsMargins(int left, int top, int right, int bottom)
void setupUi(QWidget *widget)
QgsRendererV2AbstractMetadata * rendererMetadata(QString rendererName)
get metadata for particular renderer. Returns NULL if not found in registry.
static QString defaultThemePath()
Returns the path to the default theme directory.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void setRendererV2(QgsFeatureRendererV2 *r)
Set renderer V2.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
void accepted()
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)
const char * name() const
virtual QgsFeatureRendererV2 * clone() const =0
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
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
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
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)
bool load(const QString &fileName, const char *format, QFlags< Qt::ImageConversionFlag > flags)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
virtual void accept()
QgsRendererV2Widget *(* QgsRendererV2WidgetFunc)(QgsVectorLayer *, QgsStyleV2 *, QgsFeatureRendererV2 *)
bool isWindow() const
QgsRendererV2PropertiesDialog(QgsVectorLayer *layer, QgsStyleV2 *style, bool embedded=false)
int key() const
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)
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)
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)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.