QGIS API Documentation  3.23.0-Master (dd0cd13a00)
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 "qgssymbollayerutils.h"
19 #include <QColorDialog>
20 
22  : QgsSymbolLayerWidget( parent, vl )
23 
24 {
25  setupUi( this );
26  connect( mShapeListWidget, &QListWidget::itemSelectionChanged, this, &QgsEllipseSymbolLayerWidget::mShapeListWidget_itemSelectionChanged );
27  connect( mWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mWidthSpinBox_valueChanged );
28  connect( mHeightSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mHeightSpinBox_valueChanged );
29  connect( mRotationSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mRotationSpinBox_valueChanged );
30  connect( mStrokeStyleComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::mStrokeStyleComboBox_currentIndexChanged );
31  connect( mStrokeWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged );
32  connect( btnChangeColorStroke, &QgsColorButton::colorChanged, this, &QgsEllipseSymbolLayerWidget::btnChangeColorStroke_colorChanged );
33  connect( btnChangeColorFill, &QgsColorButton::colorChanged, this, &QgsEllipseSymbolLayerWidget::btnChangeColorFill_colorChanged );
34  connect( mSymbolWidthUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mSymbolWidthUnitWidget_changed );
35  connect( mStrokeWidthUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mStrokeWidthUnitWidget_changed );
36  connect( mSymbolHeightUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mSymbolHeightUnitWidget_changed );
37  connect( mOffsetUnitWidget, &QgsUnitSelectionWidget::changed, this, &QgsEllipseSymbolLayerWidget::mOffsetUnitWidget_changed );
38  connect( mHorizontalAnchorComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged );
39  connect( mVerticalAnchorComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::mVerticalAnchorComboBox_currentIndexChanged );
40 
49 
50  btnChangeColorFill->setAllowOpacity( true );
51  btnChangeColorFill->setColorDialogTitle( tr( "Select Fill Color" ) );
52  btnChangeColorFill->setContext( QStringLiteral( "symbology" ) );
53  btnChangeColorFill->setShowNoColor( true );
54  btnChangeColorFill->setNoColorString( tr( "Transparent Fill" ) );
55  btnChangeColorStroke->setAllowOpacity( true );
56  btnChangeColorStroke->setColorDialogTitle( tr( "Select Stroke Color" ) );
57  btnChangeColorStroke->setContext( QStringLiteral( "symbology" ) );
58  btnChangeColorStroke->setShowNoColor( true );
59  btnChangeColorStroke->setNoColorString( tr( "Transparent Stroke" ) );
60 
61  mFillColorDDBtn->registerLinkedWidget( btnChangeColorFill );
62  mStrokeColorDDBtn->registerLinkedWidget( btnChangeColorStroke );
63 
64  spinOffsetX->setClearValue( 0.0 );
65  spinOffsetY->setClearValue( 0.0 );
66  mRotationSpinBox->setClearValue( 0.0 );
67 
68  int size = mShapeListWidget->iconSize().width();
69  size = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 3 ) ) );
70  mShapeListWidget->setGridSize( QSize( size * 1.2, size * 1.2 ) );
71  mShapeListWidget->setIconSize( QSize( size, size ) );
72 
73  const double markerSize = size * 0.8;
74  const auto shapes = QgsEllipseSymbolLayer::availableShapes();
75  for ( const QgsEllipseSymbolLayer::Shape shape : shapes )
76  {
80  lyr->setShape( shape );
81  lyr->setStrokeColor( QColor( 0, 0, 0 ) );
82  lyr->setFillColor( QColor( 200, 200, 200 ) );
83  lyr->setSymbolWidth( markerSize );
84  lyr->setSymbolHeight( markerSize * 0.75 );
85  const QIcon icon = QgsSymbolLayerUtils::symbolLayerPreviewIcon( lyr, QgsUnitTypes::RenderPixels, QSize( size, size ) );
86  QListWidgetItem *item = new QListWidgetItem( icon, QString(), mShapeListWidget );
87  item->setData( Qt::UserRole, static_cast< int >( shape ) );
88  item->setToolTip( QgsEllipseSymbolLayer::encodeShape( shape ) );
89  delete lyr;
90  }
91  // show at least 2 rows (only 1 row is required, but looks too cramped)
92  mShapeListWidget->setMinimumHeight( mShapeListWidget->gridSize().height() * 2.1 );
93 
94  connect( spinOffsetX, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
95  connect( spinOffsetY, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
96  connect( cboJoinStyle, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::penJoinStyleChanged );
97  connect( cboCapStyle, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::penCapStyleChanged );
98 }
99 
101 {
102  if ( !layer || layer->layerType() != QLatin1String( "EllipseMarker" ) )
103  {
104  return;
105  }
106 
107  mLayer = static_cast<QgsEllipseSymbolLayer *>( layer );
108  mWidthSpinBox->setValue( mLayer->symbolWidth() );
109  mHeightSpinBox->setValue( mLayer->symbolHeight() );
110  mRotationSpinBox->setValue( mLayer->angle() );
111  mStrokeStyleComboBox->setPenStyle( mLayer->strokeStyle() );
112  mStrokeWidthSpinBox->setValue( mLayer->strokeWidth() );
113  btnChangeColorStroke->setColor( mLayer->strokeColor() );
114  btnChangeColorFill->setColor( mLayer->fillColor() );
115 
116  const QgsEllipseSymbolLayer::Shape shape = mLayer->shape();
117  for ( int i = 0; i < mShapeListWidget->count(); ++i )
118  {
119  if ( static_cast< QgsEllipseSymbolLayer::Shape >( mShapeListWidget->item( i )->data( Qt::UserRole ).toInt() ) == shape )
120  {
121  mShapeListWidget->setCurrentRow( i );
122  break;
123  }
124  }
125  btnChangeColorFill->setEnabled( QgsEllipseSymbolLayer::shapeIsFilled( mLayer->shape() ) );
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  const 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  cboCapStyle->setPenCapStyle( mLayer->penCapStyle() );
144  blockComboSignals( false );
145 
159 
160 }
161 
163 {
164  return mLayer;
165 }
166 
167 void QgsEllipseSymbolLayerWidget::mShapeListWidget_itemSelectionChanged()
168 {
169  if ( mLayer )
170  {
171  mLayer->setShape( static_cast< QgsEllipseSymbolLayer::Shape>( mShapeListWidget->currentItem()->data( Qt::UserRole ).toInt() ) );
172  btnChangeColorFill->setEnabled( QgsEllipseSymbolLayer::shapeIsFilled( mLayer->shape() ) );
173  emit changed();
174  }
175 }
176 
177 void QgsEllipseSymbolLayerWidget::mWidthSpinBox_valueChanged( double d )
178 {
179  if ( mLayer )
180  {
181  mLayer->setSymbolWidth( d );
182  emit changed();
183  }
184 }
185 
186 void QgsEllipseSymbolLayerWidget::mHeightSpinBox_valueChanged( double d )
187 {
188  if ( mLayer )
189  {
190  mLayer->setSymbolHeight( d );
191  emit changed();
192  }
193 }
194 
195 void QgsEllipseSymbolLayerWidget::mRotationSpinBox_valueChanged( double d )
196 {
197  if ( mLayer )
198  {
199  mLayer->setAngle( d );
200  emit changed();
201  }
202 }
203 
204 void QgsEllipseSymbolLayerWidget::mStrokeStyleComboBox_currentIndexChanged( int index )
205 {
206  Q_UNUSED( index )
207 
208  if ( mLayer )
209  {
210  mLayer->setStrokeStyle( mStrokeStyleComboBox->penStyle() );
211  emit changed();
212  }
213 }
214 
215 void QgsEllipseSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged( double d )
216 {
217  if ( mLayer )
218  {
219  mLayer->setStrokeWidth( d );
220  emit changed();
221  }
222 }
223 
224 void QgsEllipseSymbolLayerWidget::btnChangeColorStroke_colorChanged( const QColor &newColor )
225 {
226  if ( !mLayer )
227  {
228  return;
229  }
230 
231  mLayer->setStrokeColor( newColor );
232  emit changed();
233 }
234 
235 void QgsEllipseSymbolLayerWidget::btnChangeColorFill_colorChanged( const QColor &newColor )
236 {
237  if ( !mLayer )
238  {
239  return;
240  }
241 
242  mLayer->setFillColor( newColor );
243  emit changed();
244 }
245 
246 void QgsEllipseSymbolLayerWidget::mSymbolWidthUnitWidget_changed()
247 {
248  if ( mLayer )
249  {
250  mLayer->setSymbolWidthUnit( mSymbolWidthUnitWidget->unit() );
251  mLayer->setSymbolWidthMapUnitScale( mSymbolWidthUnitWidget->getMapUnitScale() );
252  emit changed();
253  }
254 }
255 
256 void QgsEllipseSymbolLayerWidget::mStrokeWidthUnitWidget_changed()
257 {
258  if ( mLayer )
259  {
260  mLayer->setStrokeWidthUnit( mStrokeWidthUnitWidget->unit() );
261  mLayer->setStrokeWidthMapUnitScale( mStrokeWidthUnitWidget->getMapUnitScale() );
262  emit changed();
263  }
264 }
265 
266 void QgsEllipseSymbolLayerWidget::mSymbolHeightUnitWidget_changed()
267 {
268  if ( mLayer )
269  {
270  mLayer->setSymbolHeightUnit( mSymbolHeightUnitWidget->unit() );
271  mLayer->setSymbolHeightMapUnitScale( mSymbolHeightUnitWidget->getMapUnitScale() );
272  emit changed();
273  }
274 }
275 
276 void QgsEllipseSymbolLayerWidget::mOffsetUnitWidget_changed()
277 {
278  if ( mLayer )
279  {
280  mLayer->setOffsetUnit( mOffsetUnitWidget->unit() );
281  mLayer->setOffsetMapUnitScale( mOffsetUnitWidget->getMapUnitScale() );
282  emit changed();
283  }
284 }
285 
286 void QgsEllipseSymbolLayerWidget::penJoinStyleChanged()
287 {
288  mLayer->setPenJoinStyle( cboJoinStyle->penJoinStyle() );
289  emit changed();
290 }
291 
292 void QgsEllipseSymbolLayerWidget::penCapStyleChanged()
293 {
294  mLayer->setPenCapStyle( cboCapStyle->penCapStyle() );
295  emit changed();
296 }
297 
298 void QgsEllipseSymbolLayerWidget::blockComboSignals( bool block )
299 {
300  mSymbolWidthUnitWidget->blockSignals( block );
301  mStrokeWidthUnitWidget->blockSignals( block );
302  mSymbolHeightUnitWidget->blockSignals( block );
303  mHorizontalAnchorComboBox->blockSignals( block );
304  mVerticalAnchorComboBox->blockSignals( block );
305  mSymbolWidthUnitWidget->blockSignals( block );
306  mStrokeWidthUnitWidget->blockSignals( block );
307  mSymbolHeightUnitWidget->blockSignals( block );
308  mOffsetUnitWidget->blockSignals( block );
309  cboJoinStyle->blockSignals( block );
310  cboCapStyle->blockSignals( block );
311 }
312 
313 void QgsEllipseSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged( int index )
314 {
315  if ( mLayer )
316  {
318  emit changed();
319  }
320 }
321 
322 void QgsEllipseSymbolLayerWidget::mVerticalAnchorComboBox_currentIndexChanged( int index )
323 {
324  if ( mLayer )
325  {
327  emit changed();
328  }
329 }
330 
331 void QgsEllipseSymbolLayerWidget::setOffset()
332 {
333  mLayer->setOffset( QPointF( spinOffsetX->value(), spinOffsetY->value() ) );
334  emit changed();
335 }
336 
337 
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:1307
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
QgsEllipseSymbolLayerWidget(QgsVectorLayer *vl, QWidget *parent=nullptr)
Constructor for QgsEllipseSymbolLayerWidget.
void setSymbolLayer(QgsSymbolLayer *layer) override
QgsSymbolLayer * symbolLayer() override
A symbol layer for rendering objects with major and minor axis (e.g.
void setPenCapStyle(Qt::PenCapStyle style)
Sets the marker's stroke cap style (e.g., flat, round, etc).
const QgsMapUnitScale & symbolHeightMapUnitScale() const
void setSymbolHeightMapUnitScale(const QgsMapUnitScale &scale)
void setSymbolWidthMapUnitScale(const QgsMapUnitScale &scale)
const QgsMapUnitScale & strokeWidthMapUnitScale() const
QgsUnitTypes::RenderUnit strokeWidthUnit() const
Returns the units for the symbol's stroke width.
void setStrokeColor(const QColor &c) override
Set stroke color.
void setStrokeStyle(Qt::PenStyle strokeStyle)
Shape
Marker symbol shapes.
QgsUnitTypes::RenderUnit symbolHeightUnit() const
Returns the units for the symbol's height.
QColor fillColor() const override
Gets fill color.
QgsEllipseSymbolLayer::Shape shape() const
Returns the shape for the rendered ellipse marker symbol.
void setFillColor(const QColor &c) override
Set fill color.
void setShape(QgsEllipseSymbolLayer::Shape shape)
Sets the rendered ellipse marker shape.
static QList< QgsEllipseSymbolLayer::Shape > availableShapes()
Returns a list of all available shape types.
QgsUnitTypes::RenderUnit symbolWidthUnit() const
Returns the units for the symbol's width.
Qt::PenStyle strokeStyle() const
static QString encodeShape(QgsEllipseSymbolLayer::Shape shape)
Encodes a shape to its string representation.
void setStrokeWidthMapUnitScale(const QgsMapUnitScale &scale)
QColor strokeColor() const override
Gets stroke color.
void setSymbolWidthUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol's width.
void setPenJoinStyle(Qt::PenJoinStyle style)
Set stroke join style.
static bool shapeIsFilled(const QgsEllipseSymbolLayer::Shape &shape)
Returns true if a shape has a fill.
void setStrokeWidthUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol's stroke width.
Qt::PenJoinStyle penJoinStyle() const
Gets stroke join style.
Qt::PenCapStyle penCapStyle() const
Returns the marker's stroke cap style (e.g., flat, round, etc).
void setSymbolHeightUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol's height.
const QgsMapUnitScale & symbolWidthMapUnitScale() const
QPointF offset() const
Returns the marker's offset, which is the horizontal and vertical displacement which the rendered mar...
HorizontalAnchorPoint
Symbol horizontal anchor points.
void setAngle(double angle)
Sets the rotation angle for the marker.
void setVerticalAnchorPoint(VerticalAnchorPoint v)
Sets the vertical anchor point for positioning the symbol.
void setHorizontalAnchorPoint(HorizontalAnchorPoint h)
Sets the horizontal anchor point for positioning the symbol.
void setOffset(QPointF offset)
Sets the marker's offset, which is the horizontal and vertical displacement which the rendered marker...
const QgsMapUnitScale & offsetMapUnitScale() const
Returns the map unit scale for the symbol's offset.
QgsUnitTypes::RenderUnit offsetUnit() const
Returns the units for the symbol's offset.
VerticalAnchorPoint verticalAnchorPoint() const
Returns the vertical anchor point for positioning the symbol.
HorizontalAnchorPoint horizontalAnchorPoint() const
Returns the horizontal anchor point for positioning the symbol.
VerticalAnchorPoint
Symbol vertical anchor points.
void setOffsetUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol's offset.
void setOffsetMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the symbol's offset.
double angle() const
Returns the rotation angle for the marker, in degrees clockwise from north.
static QIcon symbolLayerPreviewIcon(const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::SymbolType parentSymbolType=Qgis::SymbolType::Hybrid)
Draws a symbol layer preview to an icon.
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsSymbolLayer::Property key)
Registers a data defined override button.
void changed()
Should be emitted whenever configuration changes happened on this symbol layer configuration.
@ PropertyStrokeStyle
Stroke style (eg solid, dashed)
@ PropertyHorizontalAnchor
Horizontal anchor point.
@ PropertyCapStyle
Line cap style.
@ PropertyAngle
Symbol angle.
@ PropertyVerticalAnchor
Vertical anchor point.
@ PropertyJoinStyle
Line join style.
@ PropertyOffset
Symbol offset.
@ PropertyStrokeWidth
Stroke width.
@ PropertyFillColor
Fill color.
@ PropertyHeight
Symbol height.
@ PropertyName
Name, eg shape name for simple markers.
@ PropertyStrokeColor
Stroke color.
@ PropertyWidth
Symbol width.
virtual QString layerType() const =0
Returns a string that represents this layer type.
virtual void setColor(const QColor &color)
The fill color.
QList< QgsUnitTypes::RenderUnit > RenderUnitList
List of render units.
Definition: qgsunittypes.h:240
@ RenderMetersInMapUnits
Meters value as Map units.
Definition: qgsunittypes.h:176
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:171
@ RenderInches
Inches.
Definition: qgsunittypes.h:174
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:170
Represents a vector layer which manages a vector based data sets.