QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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  QStringList names;
69  names << QStringLiteral( "circle" ) << QStringLiteral( "rectangle" ) << QStringLiteral( "diamond" ) << QStringLiteral( "cross" ) << QStringLiteral( "triangle" ) << QStringLiteral( "right_half_triangle" ) << QStringLiteral( "left_half_triangle" ) << QStringLiteral( "semi_circle" );
70 
71  int size = mShapeListWidget->iconSize().width();
72 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
73  size = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().width( 'X' ) * 3 ) ) );
74 #else
75  size = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance( 'X' ) * 3 ) ) );
76 #endif
77  mShapeListWidget->setGridSize( QSize( size * 1.2, size * 1.2 ) );
78  mShapeListWidget->setIconSize( QSize( size, size ) );
79 
80  double markerSize = size * 0.8;
81  const auto constNames = names;
82  for ( const QString &name : constNames )
83  {
87  lyr->setSymbolName( name );
88  lyr->setStrokeColor( QColor( 0, 0, 0 ) );
89  lyr->setFillColor( QColor( 200, 200, 200 ) );
90  lyr->setSymbolWidth( markerSize );
91  lyr->setSymbolHeight( markerSize * 0.75 );
92  QIcon icon = QgsSymbolLayerUtils::symbolLayerPreviewIcon( lyr, QgsUnitTypes::RenderPixels, QSize( size, size ) );
93  QListWidgetItem *item = new QListWidgetItem( icon, QString(), mShapeListWidget );
94  item->setToolTip( name );
95  item->setData( Qt::UserRole, name );
96  delete lyr;
97  }
98  // show at least 2 rows (only 1 row is required, but looks too cramped)
99  mShapeListWidget->setMinimumHeight( mShapeListWidget->gridSize().height() * 2.1 );
100 
101  connect( spinOffsetX, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
102  connect( spinOffsetY, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
103  connect( cboJoinStyle, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::penJoinStyleChanged );
104 }
105 
107 {
108  if ( !layer || layer->layerType() != QLatin1String( "EllipseMarker" ) )
109  {
110  return;
111  }
112 
113  mLayer = static_cast<QgsEllipseSymbolLayer *>( layer );
114  mWidthSpinBox->setValue( mLayer->symbolWidth() );
115  mHeightSpinBox->setValue( mLayer->symbolHeight() );
116  mRotationSpinBox->setValue( mLayer->angle() );
117  mStrokeStyleComboBox->setPenStyle( mLayer->strokeStyle() );
118  mStrokeWidthSpinBox->setValue( mLayer->strokeWidth() );
119  btnChangeColorStroke->setColor( mLayer->strokeColor() );
120  btnChangeColorFill->setColor( mLayer->fillColor() );
121 
122  QList<QListWidgetItem *> symbolItemList = mShapeListWidget->findItems( mLayer->symbolName(), Qt::MatchExactly );
123  if ( !symbolItemList.isEmpty() )
124  {
125  mShapeListWidget->setCurrentItem( symbolItemList.at( 0 ) );
126  }
127 
128  //set combo entries to current values
129  blockComboSignals( true );
130  mSymbolWidthUnitWidget->setUnit( mLayer->symbolWidthUnit() );
131  mSymbolWidthUnitWidget->setMapUnitScale( mLayer->symbolWidthMapUnitScale() );
132  mStrokeWidthUnitWidget->setUnit( mLayer->strokeWidthUnit() );
133  mStrokeWidthUnitWidget->setMapUnitScale( mLayer->strokeWidthMapUnitScale() );
134  mSymbolHeightUnitWidget->setUnit( mLayer->symbolHeightUnit() );
135  mSymbolHeightUnitWidget->setMapUnitScale( mLayer->symbolHeightMapUnitScale() );
136  mOffsetUnitWidget->setUnit( mLayer->offsetUnit() );
137  mOffsetUnitWidget->setMapUnitScale( mLayer->offsetMapUnitScale() );
138  QPointF offsetPt = mLayer->offset();
139  spinOffsetX->setValue( offsetPt.x() );
140  spinOffsetY->setValue( offsetPt.y() );
141  mHorizontalAnchorComboBox->setCurrentIndex( mLayer->horizontalAnchorPoint() );
142  mVerticalAnchorComboBox->setCurrentIndex( mLayer->verticalAnchorPoint() );
143  cboJoinStyle->setPenJoinStyle( mLayer->penJoinStyle() );
144  blockComboSignals( false );
145 
158 
159 }
160 
162 {
163  return mLayer;
164 }
165 
166 void QgsEllipseSymbolLayerWidget::mShapeListWidget_itemSelectionChanged()
167 {
168  if ( mLayer )
169  {
170  QListWidgetItem *item = mShapeListWidget->currentItem();
171  if ( item )
172  {
173  mLayer->setSymbolName( item->data( Qt::UserRole ).toString() );
174  emit changed();
175  }
176  }
177 }
178 
179 void QgsEllipseSymbolLayerWidget::mWidthSpinBox_valueChanged( double d )
180 {
181  if ( mLayer )
182  {
183  mLayer->setSymbolWidth( d );
184  emit changed();
185  }
186 }
187 
188 void QgsEllipseSymbolLayerWidget::mHeightSpinBox_valueChanged( double d )
189 {
190  if ( mLayer )
191  {
192  mLayer->setSymbolHeight( d );
193  emit changed();
194  }
195 }
196 
197 void QgsEllipseSymbolLayerWidget::mRotationSpinBox_valueChanged( double d )
198 {
199  if ( mLayer )
200  {
201  mLayer->setAngle( d );
202  emit changed();
203  }
204 }
205 
206 void QgsEllipseSymbolLayerWidget::mStrokeStyleComboBox_currentIndexChanged( int index )
207 {
208  Q_UNUSED( index )
209 
210  if ( mLayer )
211  {
212  mLayer->setStrokeStyle( mStrokeStyleComboBox->penStyle() );
213  emit changed();
214  }
215 }
216 
217 void QgsEllipseSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged( double d )
218 {
219  if ( mLayer )
220  {
221  mLayer->setStrokeWidth( d );
222  emit changed();
223  }
224 }
225 
226 void QgsEllipseSymbolLayerWidget::btnChangeColorStroke_colorChanged( const QColor &newColor )
227 {
228  if ( !mLayer )
229  {
230  return;
231  }
232 
233  mLayer->setStrokeColor( newColor );
234  emit changed();
235 }
236 
237 void QgsEllipseSymbolLayerWidget::btnChangeColorFill_colorChanged( const QColor &newColor )
238 {
239  if ( !mLayer )
240  {
241  return;
242  }
243 
244  mLayer->setFillColor( newColor );
245  emit changed();
246 }
247 
248 void QgsEllipseSymbolLayerWidget::mSymbolWidthUnitWidget_changed()
249 {
250  if ( mLayer )
251  {
252  mLayer->setSymbolWidthUnit( mSymbolWidthUnitWidget->unit() );
253  mLayer->setSymbolWidthMapUnitScale( mSymbolWidthUnitWidget->getMapUnitScale() );
254  emit changed();
255  }
256 }
257 
258 void QgsEllipseSymbolLayerWidget::mStrokeWidthUnitWidget_changed()
259 {
260  if ( mLayer )
261  {
262  mLayer->setStrokeWidthUnit( mStrokeWidthUnitWidget->unit() );
263  mLayer->setStrokeWidthMapUnitScale( mStrokeWidthUnitWidget->getMapUnitScale() );
264  emit changed();
265  }
266 }
267 
268 void QgsEllipseSymbolLayerWidget::mSymbolHeightUnitWidget_changed()
269 {
270  if ( mLayer )
271  {
272  mLayer->setSymbolHeightUnit( mSymbolHeightUnitWidget->unit() );
273  mLayer->setSymbolHeightMapUnitScale( mSymbolHeightUnitWidget->getMapUnitScale() );
274  emit changed();
275  }
276 }
277 
278 void QgsEllipseSymbolLayerWidget::mOffsetUnitWidget_changed()
279 {
280  if ( mLayer )
281  {
282  mLayer->setOffsetUnit( mOffsetUnitWidget->unit() );
283  mLayer->setOffsetMapUnitScale( mOffsetUnitWidget->getMapUnitScale() );
284  emit changed();
285  }
286 }
287 
288 void QgsEllipseSymbolLayerWidget::penJoinStyleChanged()
289 {
290  mLayer->setPenJoinStyle( cboJoinStyle->penJoinStyle() );
291  emit changed();
292 }
293 
294 void QgsEllipseSymbolLayerWidget::blockComboSignals( bool block )
295 {
296  mSymbolWidthUnitWidget->blockSignals( block );
297  mStrokeWidthUnitWidget->blockSignals( block );
298  mSymbolHeightUnitWidget->blockSignals( block );
299  mHorizontalAnchorComboBox->blockSignals( block );
300  mVerticalAnchorComboBox->blockSignals( block );
301  mSymbolWidthUnitWidget->blockSignals( block );
302  mStrokeWidthUnitWidget->blockSignals( block );
303  mSymbolHeightUnitWidget->blockSignals( block );
304  mOffsetUnitWidget->blockSignals( block );
305  cboJoinStyle->blockSignals( block );
306 }
307 
308 void QgsEllipseSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged( int index )
309 {
310  if ( mLayer )
311  {
313  emit changed();
314  }
315 }
316 
317 void QgsEllipseSymbolLayerWidget::mVerticalAnchorComboBox_currentIndexChanged( int index )
318 {
319  if ( mLayer )
320  {
322  emit changed();
323  }
324 }
325 
326 void QgsEllipseSymbolLayerWidget::setOffset()
327 {
328  mLayer->setOffset( QPointF( spinOffsetX->value(), spinOffsetY->value() ) );
329  emit changed();
330 }
331 
332 
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:183
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.
const QgsMapUnitScale & symbolHeightMapUnitScale() const
void setSymbolHeightMapUnitScale(const QgsMapUnitScale &scale)
void setSymbolWidthMapUnitScale(const QgsMapUnitScale &scale)
void setSymbolName(const QString &name)
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)
QgsUnitTypes::RenderUnit symbolHeightUnit() const
Returns the units for the symbol's height.
QColor fillColor() const override
Gets fill color.
void setFillColor(const QColor &c) override
Set fill color.
QgsUnitTypes::RenderUnit symbolWidthUnit() const
Returns the units for the symbol's width.
Qt::PenStyle strokeStyle() const
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.
void setStrokeWidthUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol's stroke width.
Qt::PenJoinStyle penJoinStyle() const
Gets stroke join style.
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())
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.
@ 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:239
@ RenderMetersInMapUnits
Meters value as Map units.
Definition: qgsunittypes.h:175
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:172
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:170
@ RenderInches
Inches.
Definition: qgsunittypes.h:173
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:169
Represents a vector layer which manages a vector based data sets.