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