QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsinvertedpolygonrendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsinvertedpolygonrendererwidget.cpp
3  ---------------------
4  begin : April 2014
5  copyright : (C) 2014 Hugo Mercier / Oslandia
6  email : hugo dot mercier at oslandia 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  ***************************************************************************/
17 #include "qgsrendererv2registry.h"
18 
19 #include "qgssymbolv2.h"
20 
21 #include "qgslogger.h"
22 #include "qgsvectorlayer.h"
23 
25 {
26  return new QgsInvertedPolygonRendererWidget( layer, style, renderer );
27 }
28 
30  : QgsRendererV2Widget( layer, style )
31 {
32  if ( !layer )
33  {
34  return;
35  }
36 
37  // the renderer only applies to polygon vector layers
38  if ( layer->wkbType() != QGis::WKBPolygon &&
39  layer->wkbType() != QGis::WKBPolygon25D &&
40  layer->wkbType() != QGis::WKBMultiPolygon &&
41  layer->wkbType() != QGis::WKBMultiPolygon25D )
42  {
43  //setup blank dialog
44  mRenderer.reset( 0 );
45  QGridLayout* layout = new QGridLayout( this );
46  QLabel* label = new QLabel( tr( "The inverted polygon renderer only applies to polygon and multipolygon layers. \n"
47  "'%1' is not a polygon layer and then cannot be displayed" )
48  .arg( layer->name() ), this );
49  layout->addWidget( label );
50  return;
51  }
52  setupUi( this );
53 
54  // try to recognize the previous renderer
55  // (null renderer means "no previous renderer")
56  if ( !renderer )
57  {
58  // a new renderer
60  }
61  else if ( renderer && renderer->type() != "invertedPolygonRenderer" )
62  {
63  // an existing renderer, but not an inverted renderer
64  // create an inverted renderer, with the existing renderer embedded
65  mRenderer.reset( new QgsInvertedPolygonRenderer( renderer ) );
66  }
67  else
68  {
69  // an existing inverted renderer
70  mRenderer.reset( static_cast<QgsInvertedPolygonRenderer*>( renderer ) );
71  mMergePolygonsCheckBox->blockSignals( true );
72  mMergePolygonsCheckBox->setCheckState( mRenderer->preprocessingEnabled() ? Qt::Checked : Qt::Unchecked );
73  mMergePolygonsCheckBox->blockSignals( false );
74  }
75 
76  int currentEmbeddedIdx = 0;
77  //insert possible renderer types
78  QStringList rendererList = QgsRendererV2Registry::instance()->renderersList();
79  QStringList::const_iterator it = rendererList.constBegin();
80  int idx = 0;
81  mRendererComboBox->blockSignals( true );
82  for ( ; it != rendererList.constEnd(); ++it, ++idx )
83  {
84  if (( *it != "invertedPolygonRenderer" ) && //< an inverted renderer cannot contain another inverted renderer
85  ( *it != "pointDisplacement" ) ) //< an inverted renderer can only contain a polygon renderer
86  {
88  mRendererComboBox->addItem( m->icon(), m->visibleName(), /* data */ *it );
89  const QgsFeatureRendererV2* embeddedRenderer = mRenderer->embeddedRenderer();
90  if ( embeddedRenderer && embeddedRenderer->type() == m->name() )
91  {
92  // store the combo box index of the current renderer
93  currentEmbeddedIdx = idx;
94  }
95  }
96  }
97  mRendererComboBox->blockSignals( false );
98 
99  int oldIdx = mRendererComboBox->currentIndex();
100  mRendererComboBox->setCurrentIndex( currentEmbeddedIdx );
101  if ( oldIdx == currentEmbeddedIdx )
102  {
103  // force update
104  on_mRendererComboBox_currentIndexChanged( currentEmbeddedIdx );
105  }
106 }
107 
109 {
111  {
112  QgsFeatureRendererV2* embeddedRenderer = mEmbeddedRendererWidget->renderer();
113  if ( embeddedRenderer )
114  {
115  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
116  }
117  }
118  return mRenderer.data();
119 }
120 
122 {
123  QString rendererId = mRendererComboBox->itemData( index ).toString();
125  if ( m )
126  {
127  mEmbeddedRendererWidget.reset( m->createRendererWidget( mLayer, mStyle, const_cast<QgsFeatureRendererV2*>( mRenderer->embeddedRenderer() )->clone() ) );
128 
129  if ( mLayout->count() > 2 )
130  {
131  // remove the current renderer widget
132  mLayout->takeAt( 2 );
133  }
134  mLayout->addWidget( mEmbeddedRendererWidget.data() );
135  }
136 }
137 
139 {
140  mRenderer->setPreprocessingEnabled( state == Qt::Checked );
141 }
static QgsRendererV2Registry * instance()
static unsigned index
QgsRendererV2AbstractMetadata * rendererMetadata(QString rendererName)
get metadata for particular renderer. Returns NULL if not found in registry.
QScopedPointer< QgsInvertedPolygonRenderer > mRenderer
the mask renderer
Stores metadata about one renderer class.
QgsInvertedPolygonRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Constructor.
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
QString type() const
Definition: qgsrendererv2.h:80
const QString & name() const
Get the display name of the layer.
QScopedPointer< QgsRendererV2Widget > mEmbeddedRendererWidget
the widget used to represent the mask's embedded renderer
virtual QgsFeatureRendererV2 * clone()=0
QgsVectorLayer * mLayer
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted...
QStringList renderersList()
return a list of available renderers
Base class for renderer settings widgets.
virtual QgsRendererV2Widget * createRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Return new instance of settings widget for the renderer.
Represents a vector layer which manages a vector based data sets.
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
static creation method
#define tr(sourceText)