QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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  {
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 ), mStyle( style ), mActiveWidget( NULL )
78 {
79  setupUi( this );
80 
81  // can be embedded in vector layer properties
82  if ( embedded )
83  {
84  buttonBox->hide();
85  layout()->setContentsMargins( 0, 0, 0, 0 );
86  }
87 
88  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( onOK() ) );
89 
90  // initialize registry's widget functions
92 
93  // Blend mode
94  mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
95 
96  // Feature blend mode
97  mFeatureBlendComboBox->setBlendMode( mLayer->featureBlendMode() );
98 
99  // Layer transparency
100  mLayerTransparencySlider->setValue( mLayer->layerTransparency() );
101  mLayerTransparencySpnBx->setValue( mLayer->layerTransparency() );
102 
103  // connect layer transparency slider and spin box
104  connect( mLayerTransparencySlider, SIGNAL( valueChanged( int ) ), mLayerTransparencySpnBx, SLOT( setValue( int ) ) );
105  connect( mLayerTransparencySpnBx, SIGNAL( valueChanged( int ) ), mLayerTransparencySlider, SLOT( setValue( int ) ) );
106 
107  QPixmap pix;
109  QStringList renderers = reg->renderersList();
110  foreach ( QString name, renderers )
111  {
113  cboRenderers->addItem( m->icon(), m->visibleName(), name );
114  }
115 
116  cboRenderers->setCurrentIndex( -1 ); // set no current renderer
117 
118  // setup slot rendererChanged()
119  connect( cboRenderers, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rendererChanged() ) );
120 
121  // set current renderer from layer
122  QString rendererName = mLayer->rendererV2()->type();
123  for ( int i = 0; i < cboRenderers->count(); i++ )
124  {
125  if ( cboRenderers->itemData( i ).toString() == rendererName )
126  {
127  cboRenderers->setCurrentIndex( i );
128  return;
129  }
130  }
131 
132  // no renderer found... this mustn't happen
133  Q_ASSERT( false && "there must be a renderer!" );
134 
135 }
136 
137 
139 {
140 
141  if ( cboRenderers->currentIndex() == -1 )
142  {
143  QgsDebugMsg( "No current item -- this should never happen!" );
144  return;
145  }
146 
147  QString rendererName = cboRenderers->itemData( cboRenderers->currentIndex() ).toString();
148 
149  //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
150  QgsFeatureRendererV2* oldRenderer;
152  {
153  oldRenderer = mActiveWidget->renderer()->clone();
154  }
155  else
156  {
157  oldRenderer = mLayer->rendererV2()->clone();
158  }
159 
160  // get rid of old active widget (if any)
161  if ( mActiveWidget )
162  {
163  stackedWidget->removeWidget( mActiveWidget );
164 
165  delete mActiveWidget;
166  mActiveWidget = NULL;
167  }
168 
169  QgsRendererV2Widget* w = NULL;
171  if ( m != NULL )
172  w = m->createRendererWidget( mLayer, mStyle, oldRenderer );
173 
174  if ( w != NULL )
175  {
176  // instantiate the widget and set as active
177  mActiveWidget = w;
178  stackedWidget->addWidget( mActiveWidget );
179  stackedWidget->setCurrentWidget( mActiveWidget );
180  }
181  else
182  {
183  // set default "no edit widget available" page
184  stackedWidget->setCurrentWidget( pageNoWidget );
185  }
186 
187 }
188 
190 {
191  if ( !mActiveWidget || !mLayer )
192  {
193  return;
194  }
195 
197  if ( renderer )
198  {
199  mLayer->setRendererV2( renderer->clone() );
200  }
201 
202  // set the blend modes for the layer
203  mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
204  mLayer->setFeatureBlendMode( mFeatureBlendComboBox->blendMode() );
205 
206  // set transparency for the layer
207  mLayer->setLayerTransparency( mLayerTransparencySlider->value() );
208 }
209 
211 {
212  apply();
213  accept();
214 }
215 
216 
218 {
219  // Ignore the ESC key to avoid close the dialog without the properties window
220  if ( !isWindow() && e->key() == Qt::Key_Escape )
221  {
222  e->ignore();
223  }
224  else
225  {
226  QDialog::keyPressEvent( e );
227  }
228 }
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)
Write transparency for layer.
Stores metadata about one renderer class.
void setBlendMode(const QPainter::CompositionMode &blendMode)
Write blend mode for layer.
Registry of renderers.
QString iconPath(QString iconFile)
void setIcon(const QIcon &icon)
static void _initRendererWidgetFunctions()
QString type() const
Definition: qgsrendererv2.h:81
void setWidgetFunction(QgsRendererV2WidgetFunc f)
virtual QgsFeatureRendererV2 * clone() const =0
QPainter::CompositionMode blendMode() const
Read blend mode for layer.
QPainter::CompositionMode featureBlendMode() const
Read blend mode for layer.
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 setFeatureBlendMode(const QPainter::CompositionMode &blendMode)
Write blend mode for features.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
void keyPressEvent(QKeyEvent *event)
Reimplements dialog keyPress event so we can ignore it.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
QgsRendererV2Widget *(* QgsRendererV2WidgetFunc)(QgsVectorLayer *, QgsStyleV2 *, QgsFeatureRendererV2 *)
QgsRendererV2PropertiesDialog(QgsVectorLayer *layer, QgsStyleV2 *style, bool embedded=false)
void rendererChanged()
called when user changes renderer type
int layerTransparency() const
Read transparency for 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.
static bool _initRenderer(QString name, QgsRendererV2WidgetFunc f, QString iconName=QString())
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)
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
static creation method