QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #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 
102 void QgsLayoutShapeWidget::blockAllSignals( bool block )
103 {
104  mShapeComboBox->blockSignals( block );
105  mCornerRadiusSpinBox->blockSignals( block );
106  mRadiusUnitsComboBox->blockSignals( block );
107  mShapeStyleButton->blockSignals( block );
108 }
109 
110 void 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 
130 void 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 
140 void 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 
151 void 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 
162 void 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 
177 void 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 }
QgsLayoutItemShape
Layout item for basic filled shapes (e.g. rectangles, ellipses).
Definition: qgslayoutitemshape.h:31
QgsSymbolButton::changed
void changed()
Emitted when the symbol's settings are changed.
qgslayoutundostack.h
Qgis::SymbolType::Fill
@ Fill
Fill symbol.
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:391
QgsLayoutShapeWidget::setNewItem
bool setNewItem(QgsLayoutItem *item) override
Attempts to update the widget to show the properties for the specified item.
Definition: qgslayoutshapewidget.cpp:78
QgsLayoutObject::changed
void changed()
Emitted when the object's properties change.
QgsLayoutItemPropertiesWidget
A widget for controlling the common properties of layout items (e.g. position and size,...
Definition: qgslayoutitemwidget.h:217
QgsLayoutItemShape::Ellipse
@ Ellipse
Ellipse shape.
Definition: qgslayoutitemshape.h:40
QgsLayoutItemBaseWidget::coverageLayer
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
Definition: qgslayoutitemwidget.cpp:256
QgsLayoutItemPropertiesWidget::showFrameGroup
void showFrameGroup(bool showGroup)
Determines if the frame of the group box shall be shown.
Definition: qgslayoutitemwidget.cpp:396
QgsLayoutItemPropertiesWidget::setMasterLayout
void setMasterLayout(QgsMasterLayoutInterface *masterLayout)
Sets the master layout associated with the item.
Definition: qgslayoutitemwidget.cpp:420
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
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:41
QgsLayoutShapeWidget::QgsLayoutShapeWidget
QgsLayoutShapeWidget(QgsLayoutItemShape *shape)
constructor
Definition: qgslayoutshapewidget.cpp:26
qgsstyle.h
QgsLayoutItemBaseWidget
A base class for property widgets for layout items. All layout item widgets should inherit from this ...
Definition: qgslayoutitemwidget.h:122
qgsvectorlayer.h
QgsSymbolButton::setLayer
void setLayer(QgsVectorLayer *layer)
Sets a layer to associate with the widget.
Definition: qgssymbolbutton.cpp:208
QgsLayoutItem::UndoShapeCornerRadius
@ UndoShapeCornerRadius
Shape corner radius.
Definition: qgslayoutitem.h:227
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:29
qgslayoutitemshape.h
QgsLayoutItemShape::Shape
Shape
Shape type.
Definition: qgslayoutitemshape.h:38
QgsMasterLayoutInterface
Interface for master layout type objects, such as print layouts and reports.
Definition: qgsmasterlayoutinterface.h:42
QgsLayoutItemShape::Triangle
@ Triangle
Triangle shape.
Definition: qgslayoutitemshape.h:42
QgsLayoutItemRegistry::LayoutShape
@ LayoutShape
Shape item.
Definition: qgslayoutitemregistry.h:351
QgsLayoutItemPropertiesWidget::setItem
void setItem(QgsLayoutItem *item)
Sets the layout item.
Definition: qgslayoutitemwidget.cpp:401
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:72
qgsfillsymbol.h
QgsLayoutMeasurement
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Definition: qgslayoutmeasurement.h:33