QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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 "moc_qgslayoutshapewidget.cpp"
20#include "qgslayoutitemshape.h"
21#include "qgslayout.h"
22#include "qgslayoutundostack.h"
23#include "qgsvectorlayer.h"
24#include "qgsfillsymbol.h"
27
29 : QgsLayoutItemBaseWidget( nullptr, shape )
30 , mShape( shape )
31{
32 Q_ASSERT( mShape );
33
34 setupUi( this );
35 connect( mShapeComboBox, &QComboBox::currentTextChanged, this, &QgsLayoutShapeWidget::mShapeComboBox_currentIndexChanged );
36 connect( mCornerRadiusSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutShapeWidget::mCornerRadiusSpinBox_valueChanged );
37 setPanelTitle( tr( "Shape Properties" ) );
38
39 //add widget for general composer item properties
40 mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, shape );
41 //shapes don't use background or frame, since the symbol style is set through a QgsSymbolSelectorWidget
42 mItemPropertiesWidget->showBackgroundGroup( false );
43 mItemPropertiesWidget->showFrameGroup( false );
44 mainLayout->addWidget( mItemPropertiesWidget );
45
46 blockAllSignals( true );
47
48 //shape types
49 mShapeComboBox->addItem( tr( "Rectangle" ), QgsLayoutItemShape::Rectangle );
50 mShapeComboBox->addItem( tr( "Ellipse" ), QgsLayoutItemShape::Ellipse );
51 mShapeComboBox->addItem( tr( "Triangle" ), QgsLayoutItemShape::Triangle );
52
53 mShapeStyleButton->setSymbolType( Qgis::SymbolType::Fill );
54 mRadiusUnitsComboBox->linkToWidget( mCornerRadiusSpinBox );
55 mRadiusUnitsComboBox->setConverter( &mShape->layout()->renderContext().measurementConverter() );
56
57 setGuiElementValues();
58
59 blockAllSignals( false );
60
61 connect( mShape, &QgsLayoutObject::changed, this, &QgsLayoutShapeWidget::setGuiElementValues );
62 mShapeStyleButton->registerExpressionContextGenerator( mShape );
63
64 connect( mShapeStyleButton, &QgsSymbolButton::changed, this, &QgsLayoutShapeWidget::symbolChanged );
65 connect( mRadiusUnitsComboBox, &QgsLayoutUnitsComboBox::unitChanged, this, &QgsLayoutShapeWidget::radiusUnitsChanged );
66
67 mShapeStyleButton->setLayer( coverageLayer() );
68 if ( mShape->layout() )
69 {
70 connect( &mShape->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, mShapeStyleButton, &QgsSymbolButton::setLayer );
71 }
72}
73
75{
76 if ( mItemPropertiesWidget )
77 mItemPropertiesWidget->setMasterLayout( masterLayout );
78}
79
81{
83 return false;
84
85 if ( mShape )
86 {
87 disconnect( mShape, &QgsLayoutObject::changed, this, &QgsLayoutShapeWidget::setGuiElementValues );
88 }
89
90 mShape = qobject_cast<QgsLayoutItemShape *>( item );
91 mItemPropertiesWidget->setItem( mShape );
92
93 if ( mShape )
94 {
95 connect( mShape, &QgsLayoutObject::changed, this, &QgsLayoutShapeWidget::setGuiElementValues );
96 mShapeStyleButton->registerExpressionContextGenerator( mShape );
97 }
98
99 setGuiElementValues();
100
101 return true;
102}
103
104void QgsLayoutShapeWidget::blockAllSignals( bool block )
105{
106 mShapeComboBox->blockSignals( block );
107 mCornerRadiusSpinBox->blockSignals( block );
108 mRadiusUnitsComboBox->blockSignals( block );
109 mShapeStyleButton->blockSignals( block );
110}
111
112void QgsLayoutShapeWidget::setGuiElementValues()
113{
114 if ( !mShape )
115 {
116 return;
117 }
118
119 blockAllSignals( true );
120
121 mShapeStyleButton->setSymbol( mShape->symbol()->clone() );
122
123 mCornerRadiusSpinBox->setValue( mShape->cornerRadius().length() );
124 mRadiusUnitsComboBox->setUnit( mShape->cornerRadius().units() );
125
126 mShapeComboBox->setCurrentIndex( mShapeComboBox->findData( mShape->shapeType() ) );
127 toggleRadiusSpin( mShape->shapeType() );
128
129 blockAllSignals( false );
130}
131
132void QgsLayoutShapeWidget::symbolChanged()
133{
134 if ( !mShape )
135 return;
136
137 mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Style" ), QgsLayoutItem::UndoShapeStyle );
138 mShape->setSymbol( mShapeStyleButton->clonedSymbol<QgsFillSymbol>() );
139 mShape->layout()->undoStack()->endCommand();
140}
141
142void QgsLayoutShapeWidget::mCornerRadiusSpinBox_valueChanged( double val )
143{
144 if ( !mShape )
145 return;
146
147 mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Radius" ), QgsLayoutItem::UndoShapeCornerRadius );
148 mShape->setCornerRadius( QgsLayoutMeasurement( val, mRadiusUnitsComboBox->unit() ) );
149 mShape->layout()->undoStack()->endCommand();
150 mShape->update();
151}
152
153void QgsLayoutShapeWidget::radiusUnitsChanged()
154{
155 if ( !mShape )
156 return;
157
158 mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Radius" ), QgsLayoutItem::UndoShapeCornerRadius );
159 mShape->setCornerRadius( QgsLayoutMeasurement( mCornerRadiusSpinBox->value(), mRadiusUnitsComboBox->unit() ) );
160 mShape->layout()->undoStack()->endCommand();
161 mShape->update();
162}
163
164void QgsLayoutShapeWidget::mShapeComboBox_currentIndexChanged( const QString & )
165{
166 if ( !mShape )
167 {
168 return;
169 }
170
171 mShape->layout()->undoStack()->beginCommand( mShape, tr( "Change Shape Type" ) );
172 const QgsLayoutItemShape::Shape shape = static_cast<QgsLayoutItemShape::Shape>( mShapeComboBox->currentData().toInt() );
173 mShape->setShapeType( shape );
174 toggleRadiusSpin( shape );
175 mShape->update();
176 mShape->layout()->undoStack()->endCommand();
177}
178
179void QgsLayoutShapeWidget::toggleRadiusSpin( QgsLayoutItemShape::Shape shape )
180{
181 switch ( shape )
182 {
185 {
186 mCornerRadiusSpinBox->setEnabled( false );
187 break;
188 }
190 {
191 mCornerRadiusSpinBox->setEnabled( true );
192 break;
193 }
194 }
195}
@ Fill
Fill symbol.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
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 unitChanged(Qgis::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.