QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgsellipsesymbollayerwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsellipsesymbollayerwidget.cpp
3  ---------------------
4  begin : June 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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  ***************************************************************************/
16 #include "qgsellipsesymbollayer.h"
17 #include "qgsvectorlayer.h"
18 #include <QColorDialog>
19 
21  : QgsSymbolLayerWidget( parent, vl )
22 
23 {
24  setupUi( this );
25  connect( mShapeListWidget, &QListWidget::itemSelectionChanged, this, &QgsEllipseSymbolLayerWidget::mShapeListWidget_itemSelectionChanged );
26  connect( mWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mWidthSpinBox_valueChanged );
27  connect( mHeightSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mHeightSpinBox_valueChanged );
28  connect( mRotationSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mRotationSpinBox_valueChanged );
29  connect( mStrokeStyleComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::mStrokeStyleComboBox_currentIndexChanged );
30  connect( mStrokeWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged );
31  connect( btnChangeColorStroke, &QgsColorButton::colorChanged, this, &QgsEllipseSymbolLayerWidget::btnChangeColorStroke_colorChanged );
32  connect( btnChangeColorFill, &QgsColorButton::colorChanged, this, &QgsEllipseSymbolLayerWidget::btnChangeColorFill_colorChanged );
33  connect( mSymbolWidthUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mSymbolWidthUnitWidget_changed );
34  connect( mStrokeWidthUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mStrokeWidthUnitWidget_changed );
35  connect( mSymbolHeightUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mSymbolHeightUnitWidget_changed );
36  connect( mOffsetUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mOffsetUnitWidget_changed );
37  connect( mHorizontalAnchorComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged );
38  connect( mVerticalAnchorComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::mVerticalAnchorComboBox_currentIndexChanged );
39 
48 
49  btnChangeColorFill->setAllowOpacity( true );
50  btnChangeColorFill->setColorDialogTitle( tr( "Select Fill Color" ) );
51  btnChangeColorFill->setContext( QStringLiteral( "symbology" ) );
52  btnChangeColorFill->setShowNoColor( true );
53  btnChangeColorFill->setNoColorString( tr( "Transparent Fill" ) );
54  btnChangeColorStroke->setAllowOpacity( true );
55  btnChangeColorStroke->setColorDialogTitle( tr( "Select Stroke Color" ) );
56  btnChangeColorStroke->setContext( QStringLiteral( "symbology" ) );
57  btnChangeColorStroke->setShowNoColor( true );
58  btnChangeColorStroke->setNoColorString( tr( "Transparent Stroke" ) );
59 
60  mFillColorDDBtn->registerLinkedWidget( btnChangeColorFill );
61  mStrokeColorDDBtn->registerLinkedWidget( btnChangeColorStroke );
62 
63  spinOffsetX->setClearValue( 0.0 );
64  spinOffsetY->setClearValue( 0.0 );
65  mRotationSpinBox->setClearValue( 0.0 );
66 
67  QStringList names;
68  names << QStringLiteral( "circle" ) << QStringLiteral( "rectangle" ) << QStringLiteral( "diamond" ) << QStringLiteral( "cross" ) << QStringLiteral( "triangle" ) << QStringLiteral( "right_half_triangle" ) << QStringLiteral( "left_half_triangle" ) << QStringLiteral( "semi_circle" );
69 
70  int size = mShapeListWidget->iconSize().width();
71 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
72  size = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 3 ) ) );
73 #else
74  size = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 3 ) ) );
75 #endif
76  mShapeListWidget->setGridSize( QSize( size * 1.2, size * 1.2 ) );
77  mShapeListWidget->setIconSize( QSize( size, size ) );
78 
79  double markerSize = size * 0.8;
80  const auto constNames = names;
81  for ( const QString &name : constNames )
82  {
86  lyr->setSymbolName( name );
87  lyr->setStrokeColor( QColor( 0, 0, 0 ) );
88  lyr->setFillColor( QColor( 200, 200, 200 ) );
89  lyr->setSymbolWidth( markerSize );
90  lyr->setSymbolHeight( markerSize * 0.75 );
91  QIcon icon = QgsSymbolLayerUtils::symbolLayerPreviewIcon( lyr, QgsUnitTypes::RenderPixels, QSize( size, size ) );
92  QListWidgetItem *item = new QListWidgetItem( icon, QString(), mShapeListWidget );
93  item->setToolTip( name );
94  item->setData( Qt::UserRole, name );
95  delete lyr;
96  }
97  // show at least 2 rows (only 1 row is required, but looks too cramped)
98  mShapeListWidget->setMinimumHeight( mShapeListWidget->gridSize().height() * 2.1 );
99 
100  connect( spinOffsetX, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
101  connect( spinOffsetY, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
102  connect( cboJoinStyle, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::penJoinStyleChanged );
103 }
104 
106 {
107  if ( !layer || layer->layerType() != QLatin1String( "EllipseMarker" ) )
108  {
109  return;
110  }
111 
112  mLayer = static_cast<QgsEllipseSymbolLayer *>( layer );
113  mWidthSpinBox->setValue( mLayer->symbolWidth() );
114  mHeightSpinBox->setValue( mLayer->symbolHeight() );
115  mRotationSpinBox->setValue( mLayer->angle() );
116  mStrokeStyleComboBox->setPenStyle( mLayer->strokeStyle() );
117  mStrokeWidthSpinBox->setValue( mLayer->strokeWidth() );
118  btnChangeColorStroke->setColor( mLayer->strokeColor() );
119  btnChangeColorFill->setColor( mLayer->fillColor() );
120 
121  QList<QListWidgetItem *> symbolItemList = mShapeListWidget->findItems( mLayer->symbolName(), Qt::MatchExactly );
122  if ( !symbolItemList.isEmpty() )
123  {
124  mShapeListWidget->setCurrentItem( symbolItemList.at( 0 ) );
125  }
126 
127  //set combo entries to current values
128  blockComboSignals( true );
129  mSymbolWidthUnitWidget->setUnit( mLayer->symbolWidthUnit() );
130  mSymbolWidthUnitWidget->setMapUnitScale( mLayer->symbolWidthMapUnitScale() );
131  mStrokeWidthUnitWidget->setUnit( mLayer->strokeWidthUnit() );
132  mStrokeWidthUnitWidget->setMapUnitScale( mLayer->strokeWidthMapUnitScale() );
133  mSymbolHeightUnitWidget->setUnit( mLayer->symbolHeightUnit() );
134  mSymbolHeightUnitWidget->setMapUnitScale( mLayer->symbolHeightMapUnitScale() );
135  mOffsetUnitWidget->setUnit( mLayer->offsetUnit() );
136  mOffsetUnitWidget->setMapUnitScale( mLayer->offsetMapUnitScale() );
137  QPointF offsetPt = mLayer->offset();
138  spinOffsetX->setValue( offsetPt.x() );
139  spinOffsetY->setValue( offsetPt.y() );
140  mHorizontalAnchorComboBox->setCurrentIndex( mLayer->horizontalAnchorPoint() );
141  mVerticalAnchorComboBox->setCurrentIndex( mLayer->verticalAnchorPoint() );
142  cboJoinStyle->setPenJoinStyle( mLayer->penJoinStyle() );
143  blockComboSignals( false );
144 
157 
158 }
159 
161 {
162  return mLayer;
163 }
164 
165 void QgsEllipseSymbolLayerWidget::mShapeListWidget_itemSelectionChanged()
166 {
167  if ( mLayer )
168  {
169  QListWidgetItem *item = mShapeListWidget->currentItem();
170  if ( item )
171  {
172  mLayer->setSymbolName( item->data( Qt::UserRole ).toString() );
173  emit changed();
174  }
175  }
176 }
177 
178 void QgsEllipseSymbolLayerWidget::mWidthSpinBox_valueChanged( double d )
179 {
180  if ( mLayer )
181  {
182  mLayer->setSymbolWidth( d );
183  emit changed();
184  }
185 }
186 
187 void QgsEllipseSymbolLayerWidget::mHeightSpinBox_valueChanged( double d )
188 {
189  if ( mLayer )
190  {
191  mLayer->setSymbolHeight( d );
192  emit changed();
193  }
194 }
195 
196 void QgsEllipseSymbolLayerWidget::mRotationSpinBox_valueChanged( double d )
197 {
198  if ( mLayer )
199  {
200  mLayer->setAngle( d );
201  emit changed();
202  }
203 }
204 
205 void QgsEllipseSymbolLayerWidget::mStrokeStyleComboBox_currentIndexChanged( int index )
206 {
207  Q_UNUSED( index )
208 
209  if ( mLayer )
210  {
211  mLayer->setStrokeStyle( mStrokeStyleComboBox->penStyle() );
212  emit changed();
213  }
214 }
215 
216 void QgsEllipseSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged( double d )
217 {
218  if ( mLayer )
219  {
220  mLayer->setStrokeWidth( d );
221  emit changed();
222  }
223 }
224 
225 void QgsEllipseSymbolLayerWidget::btnChangeColorStroke_colorChanged( const QColor &newColor )
226 {
227  if ( !mLayer )
228  {
229  return;
230  }
231 
232  mLayer->setStrokeColor( newColor );
233  emit changed();
234 }
235 
236 void QgsEllipseSymbolLayerWidget::btnChangeColorFill_colorChanged( const QColor &newColor )
237 {
238  if ( !mLayer )
239  {
240  return;
241  }
242 
243  mLayer->setFillColor( newColor );
244  emit changed();
245 }
246 
247 void QgsEllipseSymbolLayerWidget::mSymbolWidthUnitWidget_changed()
248 {
249  if ( mLayer )
250  {
251  mLayer->setSymbolWidthUnit( mSymbolWidthUnitWidget->unit() );
252  mLayer->setSymbolWidthMapUnitScale( mSymbolWidthUnitWidget->getMapUnitScale() );
253  emit changed();
254  }
255 }
256 
257 void QgsEllipseSymbolLayerWidget::mStrokeWidthUnitWidget_changed()
258 {
259  if ( mLayer )
260  {
261  mLayer->setStrokeWidthUnit( mStrokeWidthUnitWidget->unit() );
262  mLayer->setStrokeWidthMapUnitScale( mStrokeWidthUnitWidget->getMapUnitScale() );
263  emit changed();
264  }
265 }
266 
267 void QgsEllipseSymbolLayerWidget::mSymbolHeightUnitWidget_changed()
268 {
269  if ( mLayer )
270  {
271  mLayer->setSymbolHeightUnit( mSymbolHeightUnitWidget->unit() );
272  mLayer->setSymbolHeightMapUnitScale( mSymbolHeightUnitWidget->getMapUnitScale() );
273  emit changed();
274  }
275 }
276 
277 void QgsEllipseSymbolLayerWidget::mOffsetUnitWidget_changed()
278 {
279  if ( mLayer )
280  {
281  mLayer->setOffsetUnit( mOffsetUnitWidget->unit() );
282  mLayer->setOffsetMapUnitScale( mOffsetUnitWidget->getMapUnitScale() );
283  emit changed();
284  }
285 }
286 
287 void QgsEllipseSymbolLayerWidget::penJoinStyleChanged()
288 {
289  mLayer->setPenJoinStyle( cboJoinStyle->penJoinStyle() );
290  emit changed();
291 }
292 
293 void QgsEllipseSymbolLayerWidget::blockComboSignals( bool block )
294 {
295  mSymbolWidthUnitWidget->blockSignals( block );
296  mStrokeWidthUnitWidget->blockSignals( block );
297  mSymbolHeightUnitWidget->blockSignals( block );
298  mHorizontalAnchorComboBox->blockSignals( block );
299  mVerticalAnchorComboBox->blockSignals( block );
300  mSymbolWidthUnitWidget->blockSignals( block );
301  mStrokeWidthUnitWidget->blockSignals( block );
302  mSymbolHeightUnitWidget->blockSignals( block );
303  mOffsetUnitWidget->blockSignals( block );
304  cboJoinStyle->blockSignals( block );
305 }
306 
307 void QgsEllipseSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged( int index )
308 {
309  if ( mLayer )
310  {
312  emit changed();
313  }
314 }
315 
316 void QgsEllipseSymbolLayerWidget::mVerticalAnchorComboBox_currentIndexChanged( int index )
317 {
318  if ( mLayer )
319  {
321  emit changed();
322  }
323 }
324 
325 void QgsEllipseSymbolLayerWidget::setOffset()
326 {
327  mLayer->setOffset( QPointF( spinOffsetX->value(), spinOffsetY->value() ) );
328  emit changed();
329 }
330 
331 
void setOffset(QPointF offset)
Sets the marker&#39;s offset, which is the horizontal and vertical displacement which the rendered marker...
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
Meters value as Map units.
Definition: qgsunittypes.h:154
static QIcon symbolLayerPreviewIcon(const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
Draws a symbol layer preview to an icon.
void setOffsetUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol&#39;s offset.
void setStrokeWidthMapUnitScale(const QgsMapUnitScale &scale)
const QgsMapUnitScale & strokeWidthMapUnitScale() const
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:182
QgsEllipseSymbolLayerWidget(QgsVectorLayer *vl, QWidget *parent=nullptr)
Constructor for QgsEllipseSymbolLayerWidget.
void setFillColor(const QColor &c) override
Set fill color.
void setOffsetMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the symbol&#39;s offset.
VerticalAnchorPoint verticalAnchorPoint() const
Returns the vertical anchor point for positioning the symbol.
void setPenJoinStyle(Qt::PenJoinStyle style)
Set stroke join style.
QgsUnitTypes::RenderUnit offsetUnit() const
Returns the units for the symbol&#39;s offset.
void setSymbolWidthUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol&#39;s width.
void setSymbolLayer(QgsSymbolLayer *layer) override
QColor strokeColor() const override
Gets stroke color.
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:218
Name, eg shape name for simple markers.
const QgsMapUnitScale & symbolHeightMapUnitScale() const
virtual void setColor(const QColor &color)
The fill color.
QgsUnitTypes::RenderUnit symbolHeightUnit() const
Returns the units for the symbol&#39;s height.
HorizontalAnchorPoint horizontalAnchorPoint() const
Returns the horizontal anchor point for positioning the symbol.
Qt::PenJoinStyle penJoinStyle() const
Gets stroke join style.
void changed()
Should be emitted whenever configuration changes happened on this symbol layer configuration.
Qt::PenStyle strokeStyle() const
double angle() const
Returns the rotation angle for the marker, in degrees clockwise from north.
A symbol layer for rendering objects with major and minor axis (e.g.
void setSymbolName(const QString &name)
void setHorizontalAnchorPoint(HorizontalAnchorPoint h)
Sets the horizontal anchor point for positioning the symbol.
void setStrokeWidthUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol&#39;s stroke width.
QgsSymbolLayer * symbolLayer() override
void setSymbolHeightMapUnitScale(const QgsMapUnitScale &scale)
HorizontalAnchorPoint
Symbol horizontal anchor points.
VerticalAnchorPoint
Symbol vertical anchor points.
void setSymbolHeightUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol&#39;s height.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsSymbolLayer::Property key)
Registers a data defined override button.
Stroke style (eg solid, dashed)
void setStrokeStyle(Qt::PenStyle strokeStyle)
void setStrokeColor(const QColor &c) override
Set stroke color.
Points (e.g., for font sizes)
Definition: qgsunittypes.h:151
const QgsMapUnitScale & symbolWidthMapUnitScale() const
void setAngle(double angle)
Sets the rotation angle for the marker.
QColor fillColor() const override
Gets fill color.
QPointF offset() const
Returns the marker&#39;s offset, which is the horizontal and vertical displacement which the rendered mar...
Represents a vector layer which manages a vector based data sets.
const QgsMapUnitScale & offsetMapUnitScale() const
Returns the map unit scale for the symbol&#39;s offset.
void setSymbolWidthMapUnitScale(const QgsMapUnitScale &scale)
QgsUnitTypes::RenderUnit symbolWidthUnit() const
Returns the units for the symbol&#39;s width.
QgsUnitTypes::RenderUnit strokeWidthUnit() const
Returns the units for the symbol&#39;s stroke width.
void setVerticalAnchorPoint(VerticalAnchorPoint v)
Sets the vertical anchor point for positioning the symbol.
virtual QString layerType() const =0
Returns a string that represents this layer type.