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