QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <QColorDialog>
27 #include <QFontDialog>
28 
30 {
31  return new QgsPointDisplacementRendererWidget( layer, style, renderer );
32 }
33 
35  : QgsRendererV2Widget( layer, style ), mEmbeddedRendererWidget( 0 )
36 {
37  if ( !layer )
38  {
39  return;
40  }
41 
42  //the renderer only applies to point vector layers
43  if ( layer->wkbType() != QGis::WKBPoint && layer->wkbType() != QGis::WKBPoint25D )
44  {
45  //setup blank dialog
46  mRenderer = 0;
47  setupBlankUi( layer->name() );
48  return;
49  }
50  setupUi( this );
51 
52 
53  if ( renderer )
54  {
56  }
57  if ( !mRenderer )
58  {
59  mRenderer = new QgsPointDisplacementRenderer();
60  }
61 
62  blockAllSignals( true );
63 
64  //insert attributes into combo box
65  if ( layer )
66  {
67  const QgsFields& layerAttributes = layer->pendingFields();
68  for ( int idx = 0; idx < layerAttributes.count(); ++idx )
69  {
70  mLabelFieldComboBox->addItem( layerAttributes[idx].name() );
71  }
72  mLabelFieldComboBox->addItem( tr( "None" ) );
73 
74  QString currentLabelAttribute = mRenderer->labelAttributeName();
75  if ( !currentLabelAttribute.isEmpty() )
76  {
77  mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( currentLabelAttribute ) );
78  }
79  else
80  {
81  mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( tr( "None" ) ) );
82  }
83  }
84 
85  //insert possible renderer types
86  QStringList rendererList = QgsRendererV2Registry::instance()->renderersList();
87  QStringList::const_iterator it = rendererList.constBegin();
88  for ( ; it != rendererList.constEnd(); ++it )
89  {
90  if ( *it != "pointDisplacement" )
91  {
93  mRendererComboBox->addItem( m->icon(), m->visibleName(), *it );
94  }
95  }
96 
97  mCircleColorButton->setColorDialogTitle( tr( "Select color" ) );
98  mCircleColorButton->setContext( "symbology" );
99  mLabelColorButton->setContext( "symbology" );
100  mLabelColorButton->setColorDialogTitle( tr( "Select color" ) );
101 
102  mCircleWidthSpinBox->setValue( mRenderer->circleWidth() );
103  mCircleColorButton->setColor( mRenderer->circleColor() );
104  mLabelColorButton->setColor( mRenderer->labelColor() );
105  mCircleModificationSpinBox->setValue( mRenderer->circleRadiusAddition() );
106  mDistanceSpinBox->setValue( mRenderer->tolerance() );
107 
108  //scale dependent labelling
109  mMaxScaleDenominatorEdit->setText( QString::number( mRenderer->maxLabelScaleDenominator() ) );
110  mMaxScaleDenominatorEdit->setValidator( new QDoubleValidator( mMaxScaleDenominatorEdit ) );
111  if ( mRenderer->maxLabelScaleDenominator() > 0 )
112  {
113  mScaleDependentLabelsCheckBox->setCheckState( Qt::Checked );
114  }
115  else
116  {
117  mScaleDependentLabelsCheckBox->setCheckState( Qt::Unchecked );
118  mMaxScaleDenominatorEdit->setEnabled( false );
119  }
120 
121 
122  blockAllSignals( false );
123 
124  //set the appropriate renderer dialog
125  if ( mRenderer && mRenderer->embeddedRenderer() )
126  {
127  QString rendererName = mRenderer->embeddedRenderer()->type();
128  int rendererIndex = mRendererComboBox->findData( rendererName );
129  if ( rendererIndex != -1 )
130  {
131  mRendererComboBox->setCurrentIndex( rendererIndex );
132  on_mRendererComboBox_currentIndexChanged( rendererIndex );
133  }
134  }
135 
136  updateCenterIcon();
137 }
138 
140 {
141  delete mRenderer;
142  delete mEmbeddedRendererWidget;
143 }
144 
146 {
147  if ( mRenderer && mEmbeddedRendererWidget )
148  {
149  QgsFeatureRendererV2* embeddedRenderer = mEmbeddedRendererWidget->renderer();
150  if ( embeddedRenderer )
151  {
152  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
153  }
154  }
155  return mRenderer;
156 }
157 
158 void QgsPointDisplacementRendererWidget::on_mLabelFieldComboBox_currentIndexChanged( const QString& text )
159 {
160  if ( mRenderer )
161  {
162  if ( text == tr( "None" ) )
163  {
164  mRenderer->setLabelAttributeName( "" );
165  }
166  else
167  {
168  mRenderer->setLabelAttributeName( text );
169  }
170  }
171 }
172 
173 void QgsPointDisplacementRendererWidget::on_mRendererComboBox_currentIndexChanged( int index )
174 {
175  QString rendererId = mRendererComboBox->itemData( index ).toString();
177  if ( m )
178  {
179  delete mEmbeddedRendererWidget;
180  mEmbeddedRendererWidget = m->createRendererWidget( mLayer, mStyle, mRenderer->embeddedRenderer()->clone() );
181  }
182 }
183 
184 void QgsPointDisplacementRendererWidget::on_mRendererSettingsButton_clicked()
185 {
186  if ( mEmbeddedRendererWidget )
187  {
188  //create a dialog with the embedded widget
189 #ifdef Q_WS_MAC
190  QDialog* d = new QDialog( this->window() );
191  d->setWindowModality( Qt::WindowModal );
192 #else
193  QDialog* d = new QDialog();
194 #endif
195  QGridLayout* layout = new QGridLayout( d );
196  mEmbeddedRendererWidget->setParent( d );
197  QDialogButtonBox* buttonBox = new QDialogButtonBox( d );
198  buttonBox->addButton( QDialogButtonBox::Ok );
199  QObject::connect( buttonBox, SIGNAL( accepted() ), d, SLOT( accept() ) );
200  layout->addWidget( mEmbeddedRendererWidget, 0, 0 );
201  layout->addWidget( buttonBox, 1, 0 );
202  d->exec();
203  mEmbeddedRendererWidget->setParent( 0 );
204  delete d;
205  }
206 }
207 
208 void QgsPointDisplacementRendererWidget::on_mLabelFontButton_clicked()
209 {
210  if ( !mRenderer )
211  {
212  return;
213  }
214 
215  bool ok;
216 #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
217  // Native Mac dialog works only for QT Carbon
218  QFont newFont = QFontDialog::getFont( &ok, mRenderer->labelFont(), 0, tr( "Label Font" ), QFontDialog::DontUseNativeDialog );
219 #else
220  QFont newFont = QFontDialog::getFont( &ok, mRenderer->labelFont(), 0, tr( "Label Font" ) );
221 #endif
222  if ( ok )
223  {
224  mRenderer->setLabelFont( newFont );
225  }
226 }
227 
228 void QgsPointDisplacementRendererWidget::on_mCircleWidthSpinBox_valueChanged( double d )
229 {
230  if ( mRenderer )
231  {
232  mRenderer->setCircleWidth( d );
233  }
234 }
235 
236 void QgsPointDisplacementRendererWidget::on_mCircleColorButton_colorChanged( const QColor& newColor )
237 {
238  if ( !mRenderer )
239  {
240  return;
241  }
242 
243  mRenderer->setCircleColor( newColor );
244 }
245 
246 void QgsPointDisplacementRendererWidget::on_mLabelColorButton_colorChanged( const QColor& newColor )
247 {
248  if ( !mRenderer )
249  {
250  return;
251  }
252 
253  mRenderer->setLabelColor( newColor );
254 }
255 
256 void QgsPointDisplacementRendererWidget::on_mCircleModificationSpinBox_valueChanged( double d )
257 {
258  if ( !mRenderer )
259  {
260  return;
261  }
262 
263  mRenderer->setCircleRadiusAddition( d );
264 }
265 
266 void QgsPointDisplacementRendererWidget::on_mDistanceSpinBox_valueChanged( double d )
267 {
268  if ( mRenderer )
269  {
270  mRenderer->setTolerance( d );
271  }
272 }
273 
274 void QgsPointDisplacementRendererWidget::on_mScaleDependentLabelsCheckBox_stateChanged( int state )
275 {
276  if ( state == Qt::Unchecked )
277  {
278  mMaxScaleDenominatorEdit->setText( "-1" );
279  mMaxScaleDenominatorEdit->setEnabled( false );
280  }
281  else
282  {
283  mMaxScaleDenominatorEdit->setEnabled( true );
284  }
285 }
286 
287 void QgsPointDisplacementRendererWidget::on_mMaxScaleDenominatorEdit_textChanged( const QString & text )
288 {
289  if ( !mRenderer )
290  {
291  return;
292  }
293 
294  bool ok;
295  double scaleDenominator = text.toDouble( &ok );
296  if ( ok )
297  {
298  mRenderer->setMaxLabelScaleDenominator( scaleDenominator );
299  }
300 }
301 
302 void QgsPointDisplacementRendererWidget::blockAllSignals( bool block )
303 {
304  mLabelFieldComboBox->blockSignals( block );
305  mLabelFontButton->blockSignals( block );
306  mCircleWidthSpinBox->blockSignals( block );
307  mCircleColorButton->blockSignals( block );
308  mRendererComboBox->blockSignals( block );
309  mLabelColorButton->blockSignals( block );
310  mCircleModificationSpinBox->blockSignals( block );
311  mScaleDependentLabelsCheckBox->blockSignals( block );
312  mMaxScaleDenominatorEdit->blockSignals( block );
313  mCenterSymbolPushButton->blockSignals( block );
314  mDistanceSpinBox->blockSignals( block );
315 }
316 
317 void QgsPointDisplacementRendererWidget::on_mCenterSymbolPushButton_clicked()
318 {
319  if ( !mRenderer || !mRenderer->centerSymbol() )
320  {
321  return;
322  }
323  QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( mRenderer->centerSymbol()->clone() );
324  QgsSymbolV2SelectorDialog dlg( markerSymbol, QgsStyleV2::defaultStyle(), mLayer, this );
325  if ( dlg.exec() == QDialog::Rejected )
326  {
327  delete markerSymbol;
328  return;
329  }
330  mRenderer->setCenterSymbol( markerSymbol );
331  updateCenterIcon();
332 }
333 
334 void QgsPointDisplacementRendererWidget::updateCenterIcon()
335 {
336  QgsMarkerSymbolV2* symbol = mRenderer->centerSymbol();
337  if ( !symbol )
338  {
339  return;
340  }
341  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, mCenterSymbolPushButton->iconSize() );
342  mCenterSymbolPushButton->setIcon( icon );
343 }
344 
345 void QgsPointDisplacementRendererWidget::setupBlankUi( const QString& layerName )
346 {
347  QGridLayout* layout = new QGridLayout( this );
348  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 );
349  layout->addWidget( label );
350 }
static QgsRendererV2Registry * instance()
static unsigned index
QgsRendererV2AbstractMetadata * rendererMetadata(QString rendererName)
get metadata for particular renderer. Returns NULL if not found in registry.
void setLabelAttributeName(const QString &name)
void setCenterSymbol(QgsMarkerSymbolV2 *symbol)
Sets the center symbol (takes ownership)
Container of fields for a vector layer.
Definition: qgsfield.h:172
static QgsPointDisplacementRenderer * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsPointDisplacementRenderer from an existing renderer.
Stores metadata about one renderer class.
static QIcon icon(QString icon)
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
QString type() const
Definition: qgsrendererv2.h:81
const QString & name() const
Get the display name of the layer.
virtual QgsFeatureRendererV2 * clone() const =0
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
int count() const
Return number of items.
Definition: qgsfield.h:214
QgsPointDisplacementRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
QgsFeatureRendererV2 * embeddedRenderer() const
QgsFeatureRendererV2 * renderer()
return pointer to the renderer (no transfer of ownership)
A renderer that automatically displaces points with the same position.
virtual QgsSymbolV2 * clone() const
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)
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)
#define tr(sourceText)