QGIS API Documentation  2.11.0-Master
qgspointdisplacementrendererwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointdisplacementrendererwidget.cpp
3  --------------------------------------
4  begin : January 26, 2010
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco at hugis dot net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
20 #include "qgsrendererv2registry.h"
21 #include "qgsfield.h"
22 #include "qgsstylev2.h"
24 #include "qgssymbollayerv2utils.h"
25 #include "qgsvectorlayer.h"
26 #include "qgisgui.h"
27 
29 {
30  return new QgsPointDisplacementRendererWidget( layer, style, renderer );
31 }
32 
34  : QgsRendererV2Widget( layer, style )
35  , mRenderer( NULL )
36  , mEmbeddedRendererWidget( 0 )
37 {
38  if ( !layer )
39  {
40  return;
41  }
42 
43  //the renderer only applies to point vector layers
44  if ( layer->wkbType() != QGis::WKBPoint && layer->wkbType() != QGis::WKBPoint25D )
45  {
46  //setup blank dialog
47  mRenderer = 0;
48  setupBlankUi( layer->name() );
49  return;
50  }
51  setupUi( this );
52 
53 
54  if ( renderer )
55  {
57  }
58  if ( !mRenderer )
59  {
60  mRenderer = new QgsPointDisplacementRenderer();
61  }
62 
63  blockAllSignals( true );
64 
65  //insert attributes into combo box
66  if ( layer )
67  {
68  const QgsFields& layerAttributes = layer->pendingFields();
69  for ( int idx = 0; idx < layerAttributes.count(); ++idx )
70  {
71  mLabelFieldComboBox->addItem( layerAttributes[idx].name() );
72  }
73  mLabelFieldComboBox->addItem( tr( "None" ) );
74 
75  QString currentLabelAttribute = mRenderer->labelAttributeName();
76  if ( !currentLabelAttribute.isEmpty() )
77  {
78  mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( currentLabelAttribute ) );
79  }
80  else
81  {
82  mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( tr( "None" ) ) );
83  }
84  }
85 
86  //insert possible renderer types
88  QStringList::const_iterator it = rendererList.constBegin();
89  for ( ; it != rendererList.constEnd(); ++it )
90  {
91  if ( *it != "pointDisplacement" )
92  {
94  mRendererComboBox->addItem( m->icon(), m->visibleName(), *it );
95  }
96  }
97 
98  mCircleColorButton->setColorDialogTitle( tr( "Select color" ) );
99  mCircleColorButton->setContext( "symbology" );
100  mLabelColorButton->setContext( "symbology" );
101  mLabelColorButton->setColorDialogTitle( tr( "Select color" ) );
102 
103  mCircleWidthSpinBox->setValue( mRenderer->circleWidth() );
104  mCircleColorButton->setColor( mRenderer->circleColor() );
105  mLabelColorButton->setColor( mRenderer->labelColor() );
106  mCircleModificationSpinBox->setValue( mRenderer->circleRadiusAddition() );
107  mDistanceSpinBox->setValue( mRenderer->tolerance() );
108 
109  //scale dependent labelling
110  mMaxScaleDenominatorEdit->setText( QString::number( mRenderer->maxLabelScaleDenominator() ) );
111  mMaxScaleDenominatorEdit->setValidator( new QDoubleValidator( mMaxScaleDenominatorEdit ) );
112  if ( mRenderer->maxLabelScaleDenominator() > 0 )
113  {
114  mScaleDependentLabelsCheckBox->setCheckState( Qt::Checked );
115  }
116  else
117  {
118  mScaleDependentLabelsCheckBox->setCheckState( Qt::Unchecked );
119  mMaxScaleDenominatorEdit->setEnabled( false );
120  }
121 
122 
123  blockAllSignals( false );
124 
125  //set the appropriate renderer dialog
126  if ( mRenderer->embeddedRenderer() )
127  {
128  QString rendererName = mRenderer->embeddedRenderer()->type();
129  int rendererIndex = mRendererComboBox->findData( rendererName );
130  if ( rendererIndex != -1 )
131  {
132  mRendererComboBox->setCurrentIndex( rendererIndex );
133  on_mRendererComboBox_currentIndexChanged( rendererIndex );
134  }
135  }
136 
137  updateCenterIcon();
138 }
139 
141 {
142  delete mRenderer;
143  delete mEmbeddedRendererWidget;
144 }
145 
147 {
148  if ( mRenderer && mEmbeddedRendererWidget )
149  {
150  QgsFeatureRendererV2* embeddedRenderer = mEmbeddedRendererWidget->renderer();
151  if ( embeddedRenderer )
152  {
153  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
154  }
155  }
156  return mRenderer;
157 }
158 
159 void QgsPointDisplacementRendererWidget::on_mLabelFieldComboBox_currentIndexChanged( const QString& text )
160 {
161  if ( mRenderer )
162  {
163  if ( text == tr( "None" ) )
164  {
165  mRenderer->setLabelAttributeName( "" );
166  }
167  else
168  {
169  mRenderer->setLabelAttributeName( text );
170  }
171  }
172 }
173 
174 void QgsPointDisplacementRendererWidget::on_mRendererComboBox_currentIndexChanged( int index )
175 {
176  QString rendererId = mRendererComboBox->itemData( index ).toString();
178  if ( m )
179  {
180  delete mEmbeddedRendererWidget;
181  mEmbeddedRendererWidget = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
182  }
183 }
184 
185 void QgsPointDisplacementRendererWidget::on_mRendererSettingsButton_clicked()
186 {
187  if ( mEmbeddedRendererWidget )
188  {
189  //create a dialog with the embedded widget
190 #ifdef Q_OS_MAC
191  QDialog* d = new QDialog( this->window() );
192  d->setWindowModality( Qt::WindowModal );
193 #else
194  QDialog* d = new QDialog();
195 #endif
196  QGridLayout* layout = new QGridLayout( d );
197  mEmbeddedRendererWidget->setParent( d );
198  QDialogButtonBox* buttonBox = new QDialogButtonBox( d );
199  buttonBox->addButton( QDialogButtonBox::Ok );
200  QObject::connect( buttonBox, SIGNAL( accepted() ), d, SLOT( accept() ) );
201  layout->addWidget( mEmbeddedRendererWidget, 0, 0 );
202  layout->addWidget( buttonBox, 1, 0 );
203  d->exec();
204  mEmbeddedRendererWidget->setParent( 0 );
205  delete d;
206  }
207 }
208 
209 void QgsPointDisplacementRendererWidget::on_mLabelFontButton_clicked()
210 {
211  if ( !mRenderer )
212  {
213  return;
214  }
215 
216  bool ok;
217  QFont newFont = QgisGui::getFont( ok, mRenderer->labelFont(), tr( "Label Font" ) );
218  if ( ok )
219  {
220  mRenderer->setLabelFont( newFont );
221  }
222 }
223 
224 void QgsPointDisplacementRendererWidget::on_mCircleWidthSpinBox_valueChanged( double d )
225 {
226  if ( mRenderer )
227  {
228  mRenderer->setCircleWidth( d );
229  }
230 }
231 
232 void QgsPointDisplacementRendererWidget::on_mCircleColorButton_colorChanged( const QColor& newColor )
233 {
234  if ( !mRenderer )
235  {
236  return;
237  }
238 
239  mRenderer->setCircleColor( newColor );
240 }
241 
242 void QgsPointDisplacementRendererWidget::on_mLabelColorButton_colorChanged( const QColor& newColor )
243 {
244  if ( !mRenderer )
245  {
246  return;
247  }
248 
249  mRenderer->setLabelColor( newColor );
250 }
251 
252 void QgsPointDisplacementRendererWidget::on_mCircleModificationSpinBox_valueChanged( double d )
253 {
254  if ( !mRenderer )
255  {
256  return;
257  }
258 
259  mRenderer->setCircleRadiusAddition( d );
260 }
261 
262 void QgsPointDisplacementRendererWidget::on_mDistanceSpinBox_valueChanged( double d )
263 {
264  if ( mRenderer )
265  {
266  mRenderer->setTolerance( d );
267  }
268 }
269 
270 void QgsPointDisplacementRendererWidget::on_mScaleDependentLabelsCheckBox_stateChanged( int state )
271 {
272  if ( state == Qt::Unchecked )
273  {
274  mMaxScaleDenominatorEdit->setText( "-1" );
275  mMaxScaleDenominatorEdit->setEnabled( false );
276  }
277  else
278  {
279  mMaxScaleDenominatorEdit->setEnabled( true );
280  }
281 }
282 
283 void QgsPointDisplacementRendererWidget::on_mMaxScaleDenominatorEdit_textChanged( const QString & text )
284 {
285  if ( !mRenderer )
286  {
287  return;
288  }
289 
290  bool ok;
291  double scaleDenominator = text.toDouble( &ok );
292  if ( ok )
293  {
294  mRenderer->setMaxLabelScaleDenominator( scaleDenominator );
295  }
296 }
297 
298 void QgsPointDisplacementRendererWidget::blockAllSignals( bool block )
299 {
300  mLabelFieldComboBox->blockSignals( block );
301  mLabelFontButton->blockSignals( block );
302  mCircleWidthSpinBox->blockSignals( block );
303  mCircleColorButton->blockSignals( block );
304  mRendererComboBox->blockSignals( block );
305  mLabelColorButton->blockSignals( block );
306  mCircleModificationSpinBox->blockSignals( block );
307  mScaleDependentLabelsCheckBox->blockSignals( block );
308  mMaxScaleDenominatorEdit->blockSignals( block );
309  mCenterSymbolPushButton->blockSignals( block );
310  mDistanceSpinBox->blockSignals( block );
311 }
312 
313 void QgsPointDisplacementRendererWidget::on_mCenterSymbolPushButton_clicked()
314 {
315  if ( !mRenderer || !mRenderer->centerSymbol() )
316  {
317  return;
318  }
319  QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( mRenderer->centerSymbol()->clone() );
320  QgsSymbolV2SelectorDialog dlg( markerSymbol, QgsStyleV2::defaultStyle(), mLayer, this );
321  if ( dlg.exec() == QDialog::Rejected )
322  {
323  delete markerSymbol;
324  return;
325  }
326  mRenderer->setCenterSymbol( markerSymbol );
327  updateCenterIcon();
328 }
329 
330 void QgsPointDisplacementRendererWidget::updateCenterIcon()
331 {
332  QgsMarkerSymbolV2* symbol = mRenderer->centerSymbol();
333  if ( !symbol )
334  {
335  return;
336  }
337  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, mCenterSymbolPushButton->iconSize() );
338  mCenterSymbolPushButton->setIcon( icon );
339 }
340 
341 void QgsPointDisplacementRendererWidget::setupBlankUi( const QString& layerName )
342 {
343  QGridLayout* layout = new QGridLayout( this );
344  QLabel* label = new QLabel( tr( "The point displacement renderer only applies to (single) point layers. \n'%1' is not a point layer and cannot be displayed by the point displacement renderer" ).arg( layerName ), this );
345  layout->addWidget( label );
346 }
QLayout * layout() const
static QgsRendererV2Registry * instance()
static unsigned index
void setupUi(QWidget *widget)
QgsRendererV2AbstractMetadata * rendererMetadata(QString rendererName)
get metadata for particular renderer. Returns NULL if not found in registry.
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QWidget * window() const
void setWindowModality(Qt::WindowModality windowModality)
void setLabelAttributeName(const QString &name)
QFont getFont(bool &ok, const QFont &initial, const QString &title)
Show font selection dialog.
Definition: qgisgui.cpp:191
void setCenterSymbol(QgsMarkerSymbolV2 *symbol)
Sets the center symbol (takes ownership)
void addButton(QAbstractButton *button, ButtonRole role)
int exec()
Container of fields for a vector layer.
Definition: qgsfield.h:173
static QgsPointDisplacementRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsPointDisplacementRenderer from an existing renderer.
Stores metadata about one renderer class.
const QPixmap * icon() const
double toDouble(bool *ok) const
QString tr(const char *sourceText, const char *disambiguation, int n)
void setParent(QWidget *parent)
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
QString type() const
Definition: qgsrendererv2.h:82
const QString & name() const
Get the display name of the layer.
const char * name() const
virtual QgsFeatureRendererV2 * clone() const =0
QString number(int n, int base)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
QgsVectorLayer * mLayer
void setEmbeddedRenderer(QgsFeatureRendererV2 *r)
Sets embedded renderer (takes ownership)
QStringList renderersList()
return a list of available renderers
static QgsStyleV2 * defaultStyle()
return default application-wide style
Definition: qgsstylev2.cpp:51
bool isEmpty() const
virtual QgsSymbolV2 * clone() const override
QgsFeatureRendererV2 * renderer() override
return pointer to the renderer (no transfer of ownership)
int count() const
Return number of items.
Definition: qgsfield.cpp:283
QgsPointDisplacementRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
QgsFeatureRendererV2 * embeddedRenderer() const
A renderer that automatically displaces points with the same position.
Base class for renderer settings widgets.
virtual QgsRendererV2Widget * createRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
Return new instance of settings widget for the renderer.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
static QgsRendererV2Widget * create(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
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)
QgsMarkerSymbolV2 * centerSymbol()
Returns the symbol for the center of a displacement group (but not ownership of the symbol) ...
Represents a vector layer which manages a vector based data sets.
virtual QgsFeatureRendererV2 * renderer()=0
return pointer to the renderer (no transfer of ownership)