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"
27 
28 #include "qgsapplication.h"
29 #include "qgslogger.h"
30 #include "qgsvectorlayer.h"
31 
32 #include <QKeyEvent>
33 #include <QMessageBox>
34 
35 static bool _initRenderer( QString name, QgsRendererV2WidgetFunc f, QString iconName = QString() )
36 {
39  if ( am == NULL )
40  return false;
41  QgsRendererV2Metadata* m = dynamic_cast<QgsRendererV2Metadata*>( am );
42  if ( m == NULL )
43  return false;
44 
45  m->setWidgetFunction( f );
46 
47  if ( !iconName.isEmpty() )
48  {
49  QString iconPath = QgsApplication::defaultThemePath() + iconName;
50  QPixmap pix;
51  if ( pix.load( iconPath, "png" ) )
52  m->setIcon( pix );
53  }
54 
55  QgsDebugMsg( "Set for " + name );
56  return true;
57 }
58 
60 {
61  static bool initialized = false;
62  if ( initialized )
63  return;
64 
65  _initRenderer( "singleSymbol", QgsSingleSymbolRendererV2Widget::create, "rendererSingleSymbol.png" );
66  _initRenderer( "categorizedSymbol", QgsCategorizedSymbolRendererV2Widget::create, "rendererCategorizedSymbol.png" );
67  _initRenderer( "graduatedSymbol", QgsGraduatedSymbolRendererV2Widget::create, "rendererGraduatedSymbol.png" );
70  _initRenderer( "invertedPolygonRenderer", QgsInvertedPolygonRendererWidget::create );
71  initialized = true;
72 }
73 
75  : mLayer( layer ), mStyle( style ), mActiveWidget( NULL )
76 {
77  setupUi( this );
78 
79  // can be embedded in vector layer properties
80  if ( embedded )
81  {
82  buttonBox->hide();
83  layout()->setContentsMargins( 0, 0, 0, 0 );
84  }
85 
86  connect( buttonBox, SIGNAL( accepted() ), this, SLOT( onOK() ) );
87 
88  // initialize registry's widget functions
90 
91  // Blend mode
92  mBlendModeComboBox->setBlendMode( mLayer->blendMode() );
93 
94  // Feature blend mode
95  mFeatureBlendComboBox->setBlendMode( mLayer->featureBlendMode() );
96 
97  // Layer transparency
98  mLayerTransparencySlider->setValue( mLayer->layerTransparency() );
99  mLayerTransparencySpnBx->setValue( mLayer->layerTransparency() );
100 
101  // connect layer transparency slider and spin box
102  connect( mLayerTransparencySlider, SIGNAL( valueChanged( int ) ), mLayerTransparencySpnBx, SLOT( setValue( int ) ) );
103  connect( mLayerTransparencySpnBx, SIGNAL( valueChanged( int ) ), mLayerTransparencySlider, SLOT( setValue( int ) ) );
104 
105  QPixmap pix;
107  QStringList renderers = reg->renderersList();
108  foreach ( QString name, renderers )
109  {
111  cboRenderers->addItem( m->icon(), m->visibleName(), name );
112  }
113 
114  cboRenderers->setCurrentIndex( -1 ); // set no current renderer
115 
116  // setup slot rendererChanged()
117  connect( cboRenderers, SIGNAL( currentIndexChanged( int ) ), this, SLOT( rendererChanged() ) );
118 
119  // set current renderer from layer
120  QString rendererName = mLayer->rendererV2()->type();
121  for ( int i = 0; i < cboRenderers->count(); i++ )
122  {
123  if ( cboRenderers->itemData( i ).toString() == rendererName )
124  {
125  cboRenderers->setCurrentIndex( i );
126  return;
127  }
128  }
129 
130  // no renderer found... this mustn't happen
131  Q_ASSERT( false && "there must be a renderer!" );
132 
133 }
134 
135 
137 {
138 
139  if ( cboRenderers->currentIndex() == -1 )
140  {
141  QgsDebugMsg( "No current item -- this should never happen!" );
142  return;
143  }
144 
145  QString rendererName = cboRenderers->itemData( cboRenderers->currentIndex() ).toString();
146 
147  //Retrieve the previous renderer: from the old active widget if possible, otherwise from the layer
148  QgsFeatureRendererV2* oldRenderer;
150  {
151  oldRenderer = mActiveWidget->renderer()->clone();
152  }
153  else
154  {
155  oldRenderer = mLayer->rendererV2()->clone();
156  }
157 
158  // get rid of old active widget (if any)
159  if ( mActiveWidget )
160  {
161  stackedWidget->removeWidget( mActiveWidget );
162 
163  delete mActiveWidget;
164  mActiveWidget = NULL;
165  }
166 
167  QgsRendererV2Widget* w = NULL;
169  if ( m != NULL )
170  w = m->createRendererWidget( mLayer, mStyle, oldRenderer );
171 
172  if ( w != NULL )
173  {
174  // instantiate the widget and set as active
175  mActiveWidget = w;
176  stackedWidget->addWidget( mActiveWidget );
177  stackedWidget->setCurrentWidget( mActiveWidget );
178  }
179  else
180  {
181  // set default "no edit widget available" page
182  stackedWidget->setCurrentWidget( pageNoWidget );
183  }
184 
185 }
186 
188 {
189  if ( !mActiveWidget || !mLayer )
190  {
191  return;
192  }
193 
195  if ( renderer )
196  {
197  mLayer->setRendererV2( renderer->clone() );
198  }
199 
200  // set the blend modes for the layer
201  mLayer->setBlendMode( mBlendModeComboBox->blendMode() );
202  mLayer->setFeatureBlendMode( mFeatureBlendComboBox->blendMode() );
203 
204  // set transparency for the layer
205  mLayer->setLayerTransparency( mLayerTransparencySlider->value() );
206 }
207 
209 {
210  apply();
211  accept();
212 }
213 
214 
216 {
217  // Ignore the ESC key to avoid close the dialog without the properties window
218  if ( !isWindow() && e->key() == Qt::Key_Escape )
219  {
220  e->ignore();
221  }
222  else
223  {
224  QDialog::keyPressEvent( e );
225  }
226 }
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:80
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.
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