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