QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslayoutshapewidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutshapewidget.cpp
3  --------------------------
4  begin : November 2009
5  copyright : (C) 2009 by Marco Hugentobler
6  email : [email protected]
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 
18 #include "qgslayoutshapewidget.h"
19 #include "qgsstyle.h"
20 #include "qgslayoutitemshape.h"
21 #include "qgslayout.h"
22 #include "qgslayoutundostack.h"
23 #include "qgsvectorlayer.h"
24 
26  : QgsLayoutItemBaseWidget( nullptr, shape )
27  , mShape( shape )
28 {
29  Q_ASSERT( mShape );
30 
31  setupUi( this );
32  connect( mShapeComboBox, &QComboBox::currentTextChanged, this, &QgsLayoutShapeWidget::mShapeComboBox_currentIndexChanged );
33  connect( mCornerRadiusSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutShapeWidget::mCornerRadiusSpinBox_valueChanged );
34  setPanelTitle( tr( "Shape Properties" ) );
35 
36  //add widget for general composer item properties
37  mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, shape );
38  //shapes don't use background or frame, since the symbol style is set through a QgsSymbolSelectorWidget
39  mItemPropertiesWidget->showBackgroundGroup( false );
40  mItemPropertiesWidget->showFrameGroup( false );
41  mainLayout->addWidget( mItemPropertiesWidget );
42 
43  blockAllSignals( true );
44 
45  //shape types
46  mShapeComboBox->addItem( tr( "Rectangle" ), QgsLayoutItemShape::Rectangle );
47  mShapeComboBox->addItem( tr( "Ellipse" ), QgsLayoutItemShape::Ellipse );
48  mShapeComboBox->addItem( tr( "Triangle" ), QgsLayoutItemShape::Triangle );
49 
50  mShapeStyleButton->setSymbolType( QgsSymbol::Fill );
51  mRadiusUnitsComboBox->linkToWidget( mCornerRadiusSpinBox );
52  mRadiusUnitsComboBox->setConverter( &mShape->layout()->renderContext().measurementConverter() );
53 
54  setGuiElementValues();
55 
56  blockAllSignals( false );
57 
58  connect( mShape, &QgsLayoutObject::changed, this, &QgsLayoutShapeWidget::setGuiElementValues );
59  mShapeStyleButton->registerExpressionContextGenerator( mShape );
60 
61  connect( mShapeStyleButton, &QgsSymbolButton::changed, this, &QgsLayoutShapeWidget::symbolChanged );
62  connect( mRadiusUnitsComboBox, &QgsLayoutUnitsComboBox::changed, this, &QgsLayoutShapeWidget::radiusUnitsChanged );
63 
64  mShapeStyleButton->setLayer( coverageLayer() );
65  if ( mShape->layout() )
66  {
67  connect( &mShape->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, mShapeStyleButton, &QgsSymbolButton::setLayer );
68  }
69 }
70 
72 {
73  if ( mItemPropertiesWidget )
74  mItemPropertiesWidget->setMasterLayout( masterLayout );
75 }
76 
78 {
80  return false;
81 
82  if ( mShape )
83  {
84  disconnect( mShape, &QgsLayoutObject::changed, this, &QgsLayoutShapeWidget::setGuiElementValues );
85  }
86 
87  mShape = qobject_cast< QgsLayoutItemShape * >( item );
88  mItemPropertiesWidget->setItem( mShape );
89 
90  if ( mShape )
91  {
92  connect( mShape, &QgsLayoutObject::changed, this, &QgsLayoutShapeWidget::setGuiElementValues );
93  mShapeStyleButton->registerExpressionContextGenerator( mShape );
94  }
95 
96  setGuiElementValues();
97 
98  return true;
99 }
100 
101 void QgsLayoutShapeWidget::blockAllSignals( bool block )
102 {
103  mShapeComboBox->blockSignals( block );
104  mCornerRadiusSpinBox->blockSignals( block );
105  mRadiusUnitsComboBox->blockSignals( block );
106  mShapeStyleButton->blockSignals( block );
107 }
108 
109 void QgsLayoutShapeWidget::setGuiElementValues()
110 {
111  if ( !mShape )
112  {
113  return;
114  }
115 
116  blockAllSignals( true );
117 
118  mShapeStyleButton->setSymbol( mShape->symbol()->clone() );
119 
120  mCornerRadiusSpinBox->setValue( mShape->cornerRadius().length() );
121  mRadiusUnitsComboBox->setUnit( mShape->cornerRadius().units() );
122 
123  mShapeComboBox->setCurrentIndex( mShapeComboBox->findData( mShape->shapeType() ) );
124  toggleRadiusSpin( mShape->shapeType() );
125 
126  blockAllSignals( false );
127 }
128 
129 void QgsLayoutShapeWidget::symbolChanged()
130 {
131  if ( !mShape )
132  return;
133 
134  mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Style" ), QgsLayoutItem::UndoShapeStyle );
135  mShape->setSymbol( mShapeStyleButton->clonedSymbol<QgsFillSymbol>() );
136  mShape->layout()->undoStack()->endCommand();
137 }
138 
139 void QgsLayoutShapeWidget::mCornerRadiusSpinBox_valueChanged( double val )
140 {
141  if ( !mShape )
142  return;
143 
144  mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Radius" ), QgsLayoutItem::UndoShapeCornerRadius );
145  mShape->setCornerRadius( QgsLayoutMeasurement( val, mRadiusUnitsComboBox->unit() ) );
146  mShape->layout()->undoStack()->endCommand();
147  mShape->update();
148 }
149 
150 void QgsLayoutShapeWidget::radiusUnitsChanged()
151 {
152  if ( !mShape )
153  return;
154 
155  mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Radius" ), QgsLayoutItem::UndoShapeCornerRadius );
156  mShape->setCornerRadius( QgsLayoutMeasurement( mCornerRadiusSpinBox->value(), mRadiusUnitsComboBox->unit() ) );
157  mShape->layout()->undoStack()->endCommand();
158  mShape->update();
159 }
160 
161 void QgsLayoutShapeWidget::mShapeComboBox_currentIndexChanged( const QString & )
162 {
163  if ( !mShape )
164  {
165  return;
166  }
167 
168  mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Type" ) );
169  QgsLayoutItemShape::Shape shape = static_cast< QgsLayoutItemShape::Shape >( mShapeComboBox->currentData().toInt() );
170  mShape->setShapeType( shape );
171  toggleRadiusSpin( shape );
172  mShape->update();
173  mShape->layout()->undoStack()->endCommand();
174 }
175 
176 void QgsLayoutShapeWidget::toggleRadiusSpin( QgsLayoutItemShape::Shape shape )
177 {
178  switch ( shape )
179  {
182  {
183  mCornerRadiusSpinBox->setEnabled( false );
184  break;
185  }
187  {
188  mCornerRadiusSpinBox->setEnabled( true );
189  break;
190  }
191  }
192 }
QgsLayoutItemShape
Layout item for basic filled shapes (e.g.
Definition: qgslayoutitemshape.h:33
QgsSymbolButton::changed
void changed()
Emitted when the symbol's settings are changed.
qgslayoutundostack.h
QgsLayoutUnitsComboBox::changed
void changed(QgsUnitTypes::LayoutUnit unit)
Emitted when the unit is changed.
QgsLayoutItem::UndoShapeStyle
@ UndoShapeStyle
Shape symbol style.
Definition: qgslayoutitem.h:226
QgsLayoutItemPropertiesWidget::showBackgroundGroup
void showBackgroundGroup(bool showGroup)
Determines if the background of the group box shall be shown.
Definition: qgslayoutitemwidget.cpp:352
QgsLayoutShapeWidget::setNewItem
bool setNewItem(QgsLayoutItem *item) override
Attempts to update the widget to show the properties for the specified item.
Definition: qgslayoutshapewidget.cpp:77
QgsLayoutObject::changed
void changed()
Emitted when the object's properties change.
QgsLayoutItemPropertiesWidget
A widget for controlling the common properties of layout items (e.g.
Definition: qgslayoutitemwidget.h:219
QgsLayoutItemShape::Ellipse
@ Ellipse
Ellipse shape.
Definition: qgslayoutitemshape.h:41
QgsLayoutItemBaseWidget::coverageLayer
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
Definition: qgslayoutitemwidget.cpp:217
QgsLayoutItemPropertiesWidget::showFrameGroup
void showFrameGroup(bool showGroup)
Determines if the frame of the group box shall be shown.
Definition: qgslayoutitemwidget.cpp:357
QgsLayoutItemPropertiesWidget::setMasterLayout
void setMasterLayout(QgsMasterLayoutInterface *masterLayout)
Sets the master layout associated with the item.
Definition: qgslayoutitemwidget.cpp:381
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
QgsSymbol::Fill
@ Fill
Fill symbol.
Definition: qgssymbol.h:89
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:113
QgsPanelWidget::setPanelTitle
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
Definition: qgspanelwidget.h:44
qgslayout.h
QgsLayoutItemShape::Rectangle
@ Rectangle
Rectangle shape.
Definition: qgslayoutitemshape.h:42
QgsLayoutShapeWidget::QgsLayoutShapeWidget
QgsLayoutShapeWidget(QgsLayoutItemShape *shape)
constructor
Definition: qgslayoutshapewidget.cpp:25
qgsstyle.h
QgsLayoutItemBaseWidget
A base class for property widgets for layout items.
Definition: qgslayoutitemwidget.h:123
qgsvectorlayer.h
QgsSymbolButton::setLayer
void setLayer(QgsVectorLayer *layer)
Sets a layer to associate with the widget.
Definition: qgssymbolbutton.cpp:180
QgsLayoutItem::UndoShapeCornerRadius
@ UndoShapeCornerRadius
Shape corner radius.
Definition: qgslayoutitem.h:227
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1234
qgslayoutitemshape.h
QgsLayoutItemShape::Shape
Shape
Shape type.
Definition: qgslayoutitemshape.h:40
QgsMasterLayoutInterface
Interface for master layout type objects, such as print layouts and reports.
Definition: qgsmasterlayoutinterface.h:43
QgsLayoutItemShape::Triangle
@ Triangle
Triangle shape.
Definition: qgslayoutitemshape.h:43
QgsLayoutItemRegistry::LayoutShape
@ LayoutShape
Shape item.
Definition: qgslayoutitemregistry.h:321
QgsLayoutItemPropertiesWidget::setItem
void setItem(QgsLayoutItem *item)
Sets the layout item.
Definition: qgslayoutitemwidget.cpp:362
QgsLayoutReportContext::layerChanged
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
qgslayoutshapewidget.h
QgsLayoutShapeWidget::setMasterLayout
void setMasterLayout(QgsMasterLayoutInterface *masterLayout) override
Sets the master layout associated with the item.
Definition: qgslayoutshapewidget.cpp:71
QgsLayoutMeasurement
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Definition: qgslayoutmeasurement.h:34