QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  spinOffsetX->setClearValue( 0.0 );
61  spinOffsetY->setClearValue( 0.0 );
62  mRotationSpinBox->setClearValue( 0.0 );
63 
64  QStringList names;
65  names << QStringLiteral( "circle" ) << QStringLiteral( "rectangle" ) << QStringLiteral( "diamond" ) << QStringLiteral( "cross" ) << QStringLiteral( "triangle" ) << QStringLiteral( "right_half_triangle" ) << QStringLiteral( "left_half_triangle" ) << QStringLiteral( "semi_circle" );
66 
67  int size = mShapeListWidget->iconSize().width();
68  size = std::max( 30, static_cast< int >( std::round( Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "XXX" ) ) ) ) );
69  mShapeListWidget->setGridSize( QSize( size * 1.2, size * 1.2 ) );
70  mShapeListWidget->setIconSize( QSize( size, size ) );
71 
72  double markerSize = size * 0.8;
73  Q_FOREACH ( const QString &name, names )
74  {
78  lyr->setSymbolName( name );
79  lyr->setStrokeColor( QColor( 0, 0, 0 ) );
80  lyr->setFillColor( QColor( 200, 200, 200 ) );
81  lyr->setSymbolWidth( markerSize );
82  lyr->setSymbolHeight( markerSize * 0.75 );
83  QIcon icon = QgsSymbolLayerUtils::symbolLayerPreviewIcon( lyr, QgsUnitTypes::RenderPixels, QSize( size, size ) );
84  QListWidgetItem *item = new QListWidgetItem( icon, QString(), mShapeListWidget );
85  item->setToolTip( name );
86  item->setData( Qt::UserRole, name );
87  delete lyr;
88  }
89  // show at least 2 rows (only 1 row is required, but looks too cramped)
90  mShapeListWidget->setMinimumHeight( mShapeListWidget->gridSize().height() * 2.1 );
91 
92  connect( spinOffsetX, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
93  connect( spinOffsetY, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsEllipseSymbolLayerWidget::setOffset );
94  connect( cboJoinStyle, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEllipseSymbolLayerWidget::penJoinStyleChanged );
95 }
96 
98 {
99  if ( !layer || layer->layerType() != QLatin1String( "EllipseMarker" ) )
100  {
101  return;
102  }
103 
104  mLayer = static_cast<QgsEllipseSymbolLayer *>( layer );
105  mWidthSpinBox->setValue( mLayer->symbolWidth() );
106  mHeightSpinBox->setValue( mLayer->symbolHeight() );
107  mRotationSpinBox->setValue( mLayer->angle() );
108  mStrokeStyleComboBox->setPenStyle( mLayer->strokeStyle() );
109  mStrokeWidthSpinBox->setValue( mLayer->strokeWidth() );
110  btnChangeColorStroke->setColor( mLayer->strokeColor() );
111  btnChangeColorFill->setColor( mLayer->fillColor() );
112 
113  QList<QListWidgetItem *> symbolItemList = mShapeListWidget->findItems( mLayer->symbolName(), Qt::MatchExactly );
114  if ( !symbolItemList.isEmpty() )
115  {
116  mShapeListWidget->setCurrentItem( symbolItemList.at( 0 ) );
117  }
118 
119  //set combo entries to current values
120  blockComboSignals( true );
121  mSymbolWidthUnitWidget->setUnit( mLayer->symbolWidthUnit() );
122  mSymbolWidthUnitWidget->setMapUnitScale( mLayer->symbolWidthMapUnitScale() );
123  mStrokeWidthUnitWidget->setUnit( mLayer->strokeWidthUnit() );
124  mStrokeWidthUnitWidget->setMapUnitScale( mLayer->strokeWidthMapUnitScale() );
125  mSymbolHeightUnitWidget->setUnit( mLayer->symbolHeightUnit() );
126  mSymbolHeightUnitWidget->setMapUnitScale( mLayer->symbolHeightMapUnitScale() );
127  mOffsetUnitWidget->setUnit( mLayer->offsetUnit() );
128  mOffsetUnitWidget->setMapUnitScale( mLayer->offsetMapUnitScale() );
129  QPointF offsetPt = mLayer->offset();
130  spinOffsetX->setValue( offsetPt.x() );
131  spinOffsetY->setValue( offsetPt.y() );
132  mHorizontalAnchorComboBox->setCurrentIndex( mLayer->horizontalAnchorPoint() );
133  mVerticalAnchorComboBox->setCurrentIndex( mLayer->verticalAnchorPoint() );
134  cboJoinStyle->setPenJoinStyle( mLayer->penJoinStyle() );
135  blockComboSignals( false );
136 
149 
150 }
151 
153 {
154  return mLayer;
155 }
156 
157 void QgsEllipseSymbolLayerWidget::mShapeListWidget_itemSelectionChanged()
158 {
159  if ( mLayer )
160  {
161  QListWidgetItem *item = mShapeListWidget->currentItem();
162  if ( item )
163  {
164  mLayer->setSymbolName( item->data( Qt::UserRole ).toString() );
165  emit changed();
166  }
167  }
168 }
169 
170 void QgsEllipseSymbolLayerWidget::mWidthSpinBox_valueChanged( double d )
171 {
172  if ( mLayer )
173  {
174  mLayer->setSymbolWidth( d );
175  emit changed();
176  }
177 }
178 
179 void QgsEllipseSymbolLayerWidget::mHeightSpinBox_valueChanged( double d )
180 {
181  if ( mLayer )
182  {
183  mLayer->setSymbolHeight( d );
184  emit changed();
185  }
186 }
187 
188 void QgsEllipseSymbolLayerWidget::mRotationSpinBox_valueChanged( double d )
189 {
190  if ( mLayer )
191  {
192  mLayer->setAngle( d );
193  emit changed();
194  }
195 }
196 
197 void QgsEllipseSymbolLayerWidget::mStrokeStyleComboBox_currentIndexChanged( int index )
198 {
199  Q_UNUSED( index );
200 
201  if ( mLayer )
202  {
203  mLayer->setStrokeStyle( mStrokeStyleComboBox->penStyle() );
204  emit changed();
205  }
206 }
207 
208 void QgsEllipseSymbolLayerWidget::mStrokeWidthSpinBox_valueChanged( double d )
209 {
210  if ( mLayer )
211  {
212  mLayer->setStrokeWidth( d );
213  emit changed();
214  }
215 }
216 
217 void QgsEllipseSymbolLayerWidget::btnChangeColorStroke_colorChanged( const QColor &newColor )
218 {
219  if ( !mLayer )
220  {
221  return;
222  }
223 
224  mLayer->setStrokeColor( newColor );
225  emit changed();
226 }
227 
228 void QgsEllipseSymbolLayerWidget::btnChangeColorFill_colorChanged( const QColor &newColor )
229 {
230  if ( !mLayer )
231  {
232  return;
233  }
234 
235  mLayer->setFillColor( newColor );
236  emit changed();
237 }
238 
239 void QgsEllipseSymbolLayerWidget::mSymbolWidthUnitWidget_changed()
240 {
241  if ( mLayer )
242  {
243  mLayer->setSymbolWidthUnit( mSymbolWidthUnitWidget->unit() );
244  mLayer->setSymbolWidthMapUnitScale( mSymbolWidthUnitWidget->getMapUnitScale() );
245  emit changed();
246  }
247 }
248 
249 void QgsEllipseSymbolLayerWidget::mStrokeWidthUnitWidget_changed()
250 {
251  if ( mLayer )
252  {
253  mLayer->setStrokeWidthUnit( mStrokeWidthUnitWidget->unit() );
254  mLayer->setStrokeWidthMapUnitScale( mStrokeWidthUnitWidget->getMapUnitScale() );
255  emit changed();
256  }
257 }
258 
259 void QgsEllipseSymbolLayerWidget::mSymbolHeightUnitWidget_changed()
260 {
261  if ( mLayer )
262  {
263  mLayer->setSymbolHeightUnit( mSymbolHeightUnitWidget->unit() );
264  mLayer->setSymbolHeightMapUnitScale( mSymbolHeightUnitWidget->getMapUnitScale() );
265  emit changed();
266  }
267 }
268 
269 void QgsEllipseSymbolLayerWidget::mOffsetUnitWidget_changed()
270 {
271  if ( mLayer )
272  {
273  mLayer->setOffsetUnit( mOffsetUnitWidget->unit() );
274  mLayer->setOffsetMapUnitScale( mOffsetUnitWidget->getMapUnitScale() );
275  emit changed();
276  }
277 }
278 
279 void QgsEllipseSymbolLayerWidget::penJoinStyleChanged()
280 {
281  mLayer->setPenJoinStyle( cboJoinStyle->penJoinStyle() );
282  emit changed();
283 }
284 
285 void QgsEllipseSymbolLayerWidget::blockComboSignals( bool block )
286 {
287  mSymbolWidthUnitWidget->blockSignals( block );
288  mStrokeWidthUnitWidget->blockSignals( block );
289  mSymbolHeightUnitWidget->blockSignals( block );
290  mHorizontalAnchorComboBox->blockSignals( block );
291  mVerticalAnchorComboBox->blockSignals( block );
292  mSymbolWidthUnitWidget->blockSignals( block );
293  mStrokeWidthUnitWidget->blockSignals( block );
294  mSymbolHeightUnitWidget->blockSignals( block );
295  mOffsetUnitWidget->blockSignals( block );
296  cboJoinStyle->blockSignals( block );
297 }
298 
299 void QgsEllipseSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged( int index )
300 {
301  if ( mLayer )
302  {
304  emit changed();
305  }
306 }
307 
308 void QgsEllipseSymbolLayerWidget::mVerticalAnchorComboBox_currentIndexChanged( int index )
309 {
310  if ( mLayer )
311  {
313  emit changed();
314  }
315 }
316 
317 void QgsEllipseSymbolLayerWidget::setOffset()
318 {
319  mLayer->setOffset( QPointF( spinOffsetX->value(), spinOffsetY->value() ) );
320  emit changed();
321 }
322 
323 
void setOffset(QPointF offset)
Sets the marker&#39;s offset, which is the horizontal and vertical displacement which the rendered marker...
const QgsMapUnitScale & strokeWidthMapUnitScale() const
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:119
void setOffsetUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the symbol&#39;s offset.
HorizontalAnchorPoint horizontalAnchorPoint() const
Returns the horizontal anchor point for positioning the symbol.
void setStrokeWidthMapUnitScale(const QgsMapUnitScale &scale)
Qt::PenJoinStyle penJoinStyle() const
Gets stroke join style.
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:152
QgsEllipseSymbolLayerWidget(QgsVectorLayer *vl, QWidget *parent=nullptr)
Constructor for QgsEllipseSymbolLayerWidget.
void setFillColor(const QColor &c) override
Set fill color.
QgsUnitTypes::RenderUnit symbolWidthUnit() const
Returns the units for the symbol&#39;s width.
QPointF offset() const
Returns the marker&#39;s offset, which is the horizontal and vertical displacement which the rendered mar...
void setOffsetMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the symbol&#39;s offset.
void setPenJoinStyle(Qt::PenJoinStyle style)
Set stroke join style.
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:183
Name, eg shape name for simple markers.
static QIcon symbolLayerPreviewIcon(QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale=QgsMapUnitScale())
Draws a symbol layer preview to an icon.
virtual void setColor(const QColor &color)
The fill color.
double angle() const
Returns the rotation angle for the marker, in degrees clockwise from north.
void changed()
Should be emitted whenever configuration changes happened on this symbol layer configuration.
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.
points (e.g., for font sizes)
Definition: qgsunittypes.h:117
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.
QgsUnitTypes::RenderUnit offsetUnit() const
Returns the units for the symbol&#39;s offset.
void colorChanged(const QColor &color)
Is emitted whenever a new color is set for the button.
const QgsMapUnitScale & symbolHeightMapUnitScale() const
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.
QgsUnitTypes::RenderUnit strokeWidthUnit() const
Returns the units for the symbol&#39;s stroke width.
const QgsMapUnitScale & offsetMapUnitScale() const
Returns the map unit scale for the symbol&#39;s offset.
Qt::PenStyle strokeStyle() const
const QgsMapUnitScale & symbolWidthMapUnitScale() const
void setAngle(double angle)
Sets the rotation angle for the marker.
QColor fillColor() const override
Gets fill color.
QgsUnitTypes::RenderUnit symbolHeightUnit() const
Returns the units for the symbol&#39;s height.
VerticalAnchorPoint verticalAnchorPoint() const
Returns the vertical anchor point for positioning the symbol.
Represents a vector layer which manages a vector based data sets.
void setSymbolWidthMapUnitScale(const QgsMapUnitScale &scale)
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.