QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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  if ( renderer && renderer->type() == "pointDisplacement" )
53  {
54  mRenderer = dynamic_cast<QgsPointDisplacementRenderer*>( renderer->clone() );
55  }
56  else
57  {
59  }
60 
61  blockAllSignals( true );
62 
63  //insert attributes into combo box
64  if ( layer )
65  {
66  const QgsFields& layerAttributes = layer->pendingFields();
67  for ( int idx = 0; idx < layerAttributes.count(); ++idx )
68  {
69  mLabelFieldComboBox->addItem( layerAttributes[idx].name() );
70  }
71  mLabelFieldComboBox->addItem( tr( "None" ) );
72 
73  QString currentLabelAttribute = mRenderer->labelAttributeName();
74  if ( !currentLabelAttribute.isEmpty() )
75  {
76  mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( currentLabelAttribute ) );
77  }
78  else
79  {
80  mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( tr( "None" ) ) );
81  }
82  }
83 
84  //insert possible renderer types
85  QStringList rendererList = QgsRendererV2Registry::instance()->renderersList();
86  QStringList::const_iterator it = rendererList.constBegin();
87  for ( ; it != rendererList.constEnd(); ++it )
88  {
89  if ( *it != "pointDisplacement" )
90  {
92  mRendererComboBox->addItem( m->icon(), m->visibleName(), *it );
93  }
94  }
95 
96  mCircleColorButton->setColorDialogTitle( tr( "Select color" ) );
97  mCircleColorButton->setContext( "symbology" );
98  mLabelColorButton->setContext( "symbology" );
99  mLabelColorButton->setColorDialogTitle( tr( "Select color" ) );
100 
101  mCircleWidthSpinBox->setValue( mRenderer->circleWidth() );
102  mCircleColorButton->setColor( mRenderer->circleColor() );
103  mLabelColorButton->setColor( mRenderer->labelColor() );
104  mCircleModificationSpinBox->setValue( mRenderer->circleRadiusAddition() );
105  mDistanceSpinBox->setValue( mRenderer->tolerance() );
106 
107  //scale dependent labelling
108  mMaxScaleDenominatorEdit->setText( QString::number( mRenderer->maxLabelScaleDenominator() ) );
109  mMaxScaleDenominatorEdit->setValidator( new QDoubleValidator( mMaxScaleDenominatorEdit ) );
110  if ( mRenderer->maxLabelScaleDenominator() > 0 )
111  {
112  mScaleDependentLabelsCheckBox->setCheckState( Qt::Checked );
113  }
114  else
115  {
116  mScaleDependentLabelsCheckBox->setCheckState( Qt::Unchecked );
117  mMaxScaleDenominatorEdit->setEnabled( false );
118  }
119 
120 
121  blockAllSignals( false );
122 
123  //set the appropriate renderer dialog
125  {
126  QString rendererName = mRenderer->embeddedRenderer()->type();
127  int rendererIndex = mRendererComboBox->findData( rendererName );
128  if ( rendererIndex != -1 )
129  {
130  mRendererComboBox->setCurrentIndex( rendererIndex );
132  }
133  }
134 
136 }
137 
139 {
140  delete mRenderer;
142 }
143 
145 {
147  {
149  if ( embeddedRenderer )
150  {
151  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
152  }
153  }
154  return mRenderer;
155 }
156 
158 {
159  if ( mRenderer )
160  {
161  if ( text == tr( "None" ) )
162  {
164  }
165  else
166  {
168  }
169  }
170 }
171 
173 {
174  QString rendererId = mRendererComboBox->itemData( index ).toString();
176  if ( m )
177  {
180  }
181 }
182 
184 {
186  {
187  //create a dialog with the embedded widget
188 #ifdef Q_WS_MAC
189  QDialog* d = new QDialog( this->window() );
190  d->setWindowModality( Qt::WindowModal );
191 #else
192  QDialog* d = new QDialog();
193 #endif
194  QGridLayout* layout = new QGridLayout( d );
195  mEmbeddedRendererWidget->setParent( d );
196  QDialogButtonBox* buttonBox = new QDialogButtonBox( d );
197  buttonBox->addButton( QDialogButtonBox::Ok );
198  QObject::connect( buttonBox, SIGNAL( accepted() ), d, SLOT( accept() ) );
199  layout->addWidget( mEmbeddedRendererWidget, 0, 0 );
200  layout->addWidget( buttonBox, 1, 0 );
201  d->exec();
202  mEmbeddedRendererWidget->setParent( 0 );
203  delete d;
204  }
205 }
206 
208 {
209  if ( !mRenderer )
210  {
211  return;
212  }
213 
214  bool ok;
215 #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
216  // Native Mac dialog works only for QT Carbon
217  QFont newFont = QFontDialog::getFont( &ok, mRenderer->labelFont(), 0, tr( "Label Font" ), QFontDialog::DontUseNativeDialog );
218 #else
219  QFont newFont = QFontDialog::getFont( &ok, mRenderer->labelFont(), 0, tr( "Label Font" ) );
220 #endif
221  if ( ok )
222  {
223  mRenderer->setLabelFont( newFont );
224  }
225 }
226 
228 {
229  if ( mRenderer )
230  {
232  }
233 }
234 
236 {
237  if ( !mRenderer )
238  {
239  return;
240  }
241 
242  mRenderer->setCircleColor( newColor );
243 }
244 
246 {
247  if ( !mRenderer )
248  {
249  return;
250  }
251 
252  mRenderer->setLabelColor( newColor );
253 }
254 
256 {
257  if ( !mRenderer )
258  {
259  return;
260  }
261 
263 }
264 
266 {
267  if ( mRenderer )
268  {
269  mRenderer->setTolerance( d );
270  }
271 }
272 
274 {
275  if ( state == Qt::Unchecked )
276  {
277  mMaxScaleDenominatorEdit->setText( "-1" );
278  mMaxScaleDenominatorEdit->setEnabled( false );
279  }
280  else
281  {
282  mMaxScaleDenominatorEdit->setEnabled( true );
283  }
284 }
285 
287 {
288  if ( !mRenderer )
289  {
290  return;
291  }
292 
293  bool ok;
294  double scaleDenominator = text.toDouble( &ok );
295  if ( ok )
296  {
297  mRenderer->setMaxLabelScaleDenominator( scaleDenominator );
298  }
299 }
300 
302 {
303  mLabelFieldComboBox->blockSignals( block );
304  mLabelFontButton->blockSignals( block );
305  mCircleWidthSpinBox->blockSignals( block );
306  mCircleColorButton->blockSignals( block );
307  mRendererComboBox->blockSignals( block );
308  mLabelColorButton->blockSignals( block );
309  mCircleModificationSpinBox->blockSignals( block );
310  mScaleDependentLabelsCheckBox->blockSignals( block );
311  mMaxScaleDenominatorEdit->blockSignals( block );
312  mCenterSymbolPushButton->blockSignals( block );
313  mDistanceSpinBox->blockSignals( block );
314 }
315 
317 {
318  if ( !mRenderer || !mRenderer->centerSymbol() )
319  {
320  return;
321  }
322  QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( mRenderer->centerSymbol()->clone() );
323  QgsSymbolV2SelectorDialog dlg( markerSymbol, QgsStyleV2::defaultStyle(), mLayer, this );
324  if ( dlg.exec() == QDialog::Rejected )
325  {
326  delete markerSymbol;
327  return;
328  }
329  mRenderer->setCenterSymbol( markerSymbol );
331 }
332 
334 {
336  if ( !symbol )
337  {
338  return;
339  }
340  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, mCenterSymbolPushButton->iconSize() );
341  mCenterSymbolPushButton->setIcon( icon );
342 }
343 
344 void QgsPointDisplacementRendererWidget::setupBlankUi( const QString& layerName )
345 {
346  QGridLayout* layout = new QGridLayout( this );
347  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 );
348  layout->addWidget( label );
349 }
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)
void on_mLabelFieldComboBox_currentIndexChanged(const QString &text)
Container of fields for a vector layer.
Definition: qgsfield.h:163
Stores metadata about one renderer class.
static QIcon icon(QString icon)
QGis::WkbType wkbType() const
Returns the WKBType or WKBUnknown in case of error.
void on_mCircleColorButton_colorChanged(const QColor &newColor)
QString type() const
Definition: qgsrendererv2.h:77
const QString & name() const
Get the display name of the layer.
void on_mLabelColorButton_colorChanged(const QColor &newColor)
static QIcon symbolPreviewIcon(QgsSymbolV2 *symbol, QSize size)
virtual QgsFeatureRendererV2 * clone()=0
QgsFeatureRendererV2 * embeddedRenderer()
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:200
QgsPointDisplacementRendererWidget(QgsVectorLayer *layer, QgsStyleV2 *style, QgsFeatureRendererV2 *renderer)
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)