QGIS API Documentation  2.15.0-Master (02a0ebe)
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 
38 
39  // the renderer only applies to polygon vector layers
40  if ( type != QgsWKBTypes::Polygon && type != QgsWKBTypes::CurvePolygon )
41  {
42  //setup blank dialog
43  mRenderer.reset( nullptr );
44  QGridLayout* layout = new QGridLayout( this );
45  QLabel* label = new QLabel( tr( "The inverted polygon renderer only applies to polygon and multipolygon layers. \n"
46  "'%1' is not a polygon layer and then cannot be displayed" )
47  .arg( layer->name() ), this );
48  this->setLayout( layout );
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 
57  if ( renderer )
58  {
60  }
61  if ( ! mRenderer )
62  {
64  }
65  mMergePolygonsCheckBox->blockSignals( true );
66  mMergePolygonsCheckBox->setCheckState( mRenderer->preprocessingEnabled() ? Qt::Checked : Qt::Unchecked );
67  mMergePolygonsCheckBox->blockSignals( false );
68 
69  int currentEmbeddedIdx = 0;
70  //insert possible renderer types
72  QStringList::const_iterator it = rendererList.constBegin();
73  int idx = 0;
74  mRendererComboBox->blockSignals( true );
75  for ( ; it != rendererList.constEnd(); ++it, ++idx )
76  {
77  if ( *it != "invertedPolygonRenderer" ) //< an inverted renderer cannot contain another inverted renderer
78  {
80  mRendererComboBox->addItem( m->icon(), m->visibleName(), /* data */ *it );
81  const QgsFeatureRendererV2* embeddedRenderer = mRenderer->embeddedRenderer();
82  if ( embeddedRenderer && embeddedRenderer->type() == m->name() )
83  {
84  // store the combo box index of the current renderer
85  currentEmbeddedIdx = idx;
86  }
87  }
88  }
89  mRendererComboBox->blockSignals( false );
90 
91  int oldIdx = mRendererComboBox->currentIndex();
92  mRendererComboBox->setCurrentIndex( currentEmbeddedIdx );
93  if ( oldIdx == currentEmbeddedIdx )
94  {
95  // force update
96  on_mRendererComboBox_currentIndexChanged( currentEmbeddedIdx );
97  }
98 }
99 
101 {
103  {
104  QgsFeatureRendererV2* embeddedRenderer = mEmbeddedRendererWidget->renderer();
105  if ( embeddedRenderer )
106  {
107  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
108  }
109  }
110  return mRenderer.data();
111 }
112 
114 {
117  mEmbeddedRendererWidget->setMapCanvas( canvas );
118 }
119 
120 void QgsInvertedPolygonRendererWidget::on_mRendererComboBox_currentIndexChanged( int index )
121 {
122  QString rendererId = mRendererComboBox->itemData( index ).toString();
124  if ( m )
125  {
126  mEmbeddedRendererWidget.reset( m->createRendererWidget( mLayer, mStyle, const_cast<QgsFeatureRendererV2*>( mRenderer->embeddedRenderer() )->clone() ) );
127  connect( mEmbeddedRendererWidget.data(), SIGNAL( widgetChanged() ), this, SIGNAL( widgetChanged() ) );
128  mEmbeddedRendererWidget->setMapCanvas( mMapCanvas );
129 
130  if ( layout()->count() > 2 )
131  {
132  // remove the current renderer widget
133  layout()->takeAt( 2 );
134  }
136  }
137 }
138 
139 void QgsInvertedPolygonRendererWidget::on_mMergePolygonsCheckBox_stateChanged( int state )
140 {
141  mRenderer->setPreprocessingEnabled( state == Qt::Checked );
142  emit widgetChanged();
143 }
QLayout * layout() const
static QgsRendererV2Registry * instance()
Returns a pointer to the QgsRendererV2Registry singleton.
static unsigned index
void setupUi(QWidget *widget)
QString name() const
Get the display name of the layer.
virtual QgsFeatureRendererV2 * renderer() override
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QScopedPointer< QgsInvertedPolygonRenderer > mRenderer
The mask renderer.
QStyle * style() const
static QgsInvertedPolygonRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
Creates a QgsInvertedPolygonRenderer by a conversion from an existing renderer.
void setMapCanvas(QgsMapCanvas *canvas) override
Sets the map canvas associated with the widget.
QgsMapCanvas * mMapCanvas
Stores metadata about one renderer class.
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
QgsRendererV2AbstractMetadata * rendererMetadata(const QString &rendererName)
Returns the metadata for a specified renderer.
QgsInvertedPolygonRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Constructor.
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
void reset(T *other)
QString type() const
Definition: qgsrendererv2.h:86
static QgsWKBTypes::Type fromOldWkbType(QGis::WkbType type)
Converts from old (pre 2.10) WKB type (OGR) to new WKB type.
Definition: qgis.cpp:106
virtual QgsFeatureRendererV2 * clone() const =0
QScopedPointer< QgsRendererV2Widget > mEmbeddedRendererWidget
The widget used to represent the mask&#39;s embedded renderer.
QgsVectorLayer * mLayer
void setLayout(QLayout *layout)
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted...
void addWidget(QWidget *w)
void widgetChanged()
Emiited when the widget state changes.
T * data() const
virtual QgsRendererV2Widget * createRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *oldRenderer)
Return new instance of settings widget for the renderer.
Base class for renderer settings widgets.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:366
const_iterator constEnd() const
const_iterator constBegin() const
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 void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
virtual QLayoutItem * takeAt(int index)=0
QStringList renderersList(QgsRendererV2AbstractMetadata::LayerTypes layerTypes=QgsRendererV2AbstractMetadata::All) const
Returns a list of available renderers.
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:114
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Static creation method.