QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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  mCircleWidthSpinBox->setValue( mRenderer->circleWidth() );
97  mCircleColorButton->setColor( mRenderer->circleColor() );
98  mLabelColorButton->setColor( mRenderer->labelColor() );
99  mCircleModificationSpinBox->setValue( mRenderer->circleRadiusAddition() );
100  mDistanceSpinBox->setValue( mRenderer->tolerance() );
101 
102  //scale dependent labelling
103  mMaxScaleDenominatorEdit->setText( QString::number( mRenderer->maxLabelScaleDenominator() ) );
104  mMaxScaleDenominatorEdit->setValidator( new QDoubleValidator( mMaxScaleDenominatorEdit ) );
105  if ( mRenderer->maxLabelScaleDenominator() > 0 )
106  {
107  mScaleDependentLabelsCheckBox->setCheckState( Qt::Checked );
108  }
109  else
110  {
111  mScaleDependentLabelsCheckBox->setCheckState( Qt::Unchecked );
112  mMaxScaleDenominatorEdit->setEnabled( false );
113  }
114 
115 
116  blockAllSignals( false );
117 
118  //set the appropriate renderer dialog
120  {
121  QString rendererName = mRenderer->embeddedRenderer()->type();
122  int rendererIndex = mRendererComboBox->findData( rendererName );
123  if ( rendererIndex != -1 )
124  {
125  mRendererComboBox->setCurrentIndex( rendererIndex );
127  }
128  }
129 
131 }
132 
134 {
135  delete mRenderer;
137 }
138 
140 {
142  {
144  if ( embeddedRenderer )
145  {
146  mRenderer->setEmbeddedRenderer( embeddedRenderer->clone() );
147  }
148  }
149  return mRenderer;
150 }
151 
153 {
154  if ( mRenderer )
155  {
156  if ( text == tr( "None" ) )
157  {
159  }
160  else
161  {
163  }
164  }
165 }
166 
168 {
169  QString rendererId = mRendererComboBox->itemData( index ).toString();
171  if ( m )
172  {
175  }
176 }
177 
179 {
181  {
182  //create a dialog with the embedded widget
183 #ifdef Q_WS_MAC
184  QDialog* d = new QDialog( this->window() );
185  d->setWindowModality( Qt::WindowModal );
186 #else
187  QDialog* d = new QDialog();
188 #endif
189  QGridLayout* layout = new QGridLayout( d );
190  mEmbeddedRendererWidget->setParent( d );
191  QDialogButtonBox* buttonBox = new QDialogButtonBox( d );
192  buttonBox->addButton( QDialogButtonBox::Ok );
193  QObject::connect( buttonBox, SIGNAL( accepted() ), d, SLOT( accept() ) );
194  layout->addWidget( mEmbeddedRendererWidget, 0, 0 );
195  layout->addWidget( buttonBox, 1, 0 );
196  d->exec();
197  mEmbeddedRendererWidget->setParent( 0 );
198  delete d;
199  }
200 }
201 
203 {
204  if ( !mRenderer )
205  {
206  return;
207  }
208 
209  bool ok;
210 #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
211  // Native Mac dialog works only for QT Carbon
212  QFont newFont = QFontDialog::getFont( &ok, mRenderer->labelFont(), 0, tr( "Label Font" ), QFontDialog::DontUseNativeDialog );
213 #else
214  QFont newFont = QFontDialog::getFont( &ok, mRenderer->labelFont(), 0, tr( "Label Font" ) );
215 #endif
216  if ( ok )
217  {
218  mRenderer->setLabelFont( newFont );
219  }
220 }
221 
223 {
224  if ( mRenderer )
225  {
227  }
228 }
229 
231 {
232  if ( !mRenderer )
233  {
234  return;
235  }
236 
237  mRenderer->setCircleColor( newColor );
238 }
239 
241 {
242  if ( !mRenderer )
243  {
244  return;
245  }
246 
247  mRenderer->setLabelColor( newColor );
248 }
249 
251 {
252  if ( !mRenderer )
253  {
254  return;
255  }
256 
258 }
259 
261 {
262  if ( mRenderer )
263  {
264  mRenderer->setTolerance( d );
265  }
266 }
267 
269 {
270  if ( state == Qt::Unchecked )
271  {
272  mMaxScaleDenominatorEdit->setText( "-1" );
273  mMaxScaleDenominatorEdit->setEnabled( false );
274  }
275  else
276  {
277  mMaxScaleDenominatorEdit->setEnabled( true );
278  }
279 }
280 
282 {
283  if ( !mRenderer )
284  {
285  return;
286  }
287 
288  bool ok;
289  double scaleDenominator = text.toDouble( &ok );
290  if ( ok )
291  {
292  mRenderer->setMaxLabelScaleDenominator( scaleDenominator );
293  }
294 }
295 
297 {
298  mLabelFieldComboBox->blockSignals( block );
299  mLabelFontButton->blockSignals( block );
300  mCircleWidthSpinBox->blockSignals( block );
301  mCircleColorButton->blockSignals( block );
302  mRendererComboBox->blockSignals( block );
303  mLabelColorButton->blockSignals( block );
304  mCircleModificationSpinBox->blockSignals( block );
305  mScaleDependentLabelsCheckBox->blockSignals( block );
306  mMaxScaleDenominatorEdit->blockSignals( block );
307  mCenterSymbolPushButton->blockSignals( block );
308  mDistanceSpinBox->blockSignals( block );
309 }
310 
312 {
313  if ( !mRenderer || !mRenderer->centerSymbol() )
314  {
315  return;
316  }
317  QgsMarkerSymbolV2* markerSymbol = dynamic_cast<QgsMarkerSymbolV2*>( mRenderer->centerSymbol()->clone() );
318  QgsSymbolV2SelectorDialog dlg( markerSymbol, QgsStyleV2::defaultStyle(), mLayer, this );
319  if ( dlg.exec() == QDialog::Rejected )
320  {
321  delete markerSymbol;
322  return;
323  }
324  mRenderer->setCenterSymbol( markerSymbol );
326 }
327 
329 {
331  if ( !symbol )
332  {
333  return;
334  }
335  QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, mCenterSymbolPushButton->iconSize() );
336  mCenterSymbolPushButton->setIcon( icon );
337 }
338 
339 void QgsPointDisplacementRendererWidget::setupBlankUi( const QString& layerName )
340 {
341  QGridLayout* layout = new QGridLayout( this );
342  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 );
343  layout->addWidget( label );
344 }
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)