QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgslayoutmarkerwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutmarkerwidget.cpp
3 --------------------------
4 begin : April 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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 "qgslayoutitemmarker.h"
21#include "qgslayout.h"
22#include "qgslayoutundostack.h"
23#include "qgsvectorlayer.h"
24#include "qgslayoutitemmap.h"
25#include "qgsmarkersymbol.h"
27
29 : QgsLayoutItemBaseWidget( nullptr, marker )
30 , mMarker( marker )
31{
32 Q_ASSERT( mMarker );
33
34 setupUi( this );
35 setPanelTitle( tr( "Marker Properties" ) );
36
37 //add widget for general composer item properties
38 mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, marker );
39 mItemPropertiesWidget->showFrameGroup( false );
40 mainLayout->addWidget( mItemPropertiesWidget );
41
42 blockAllSignals( true );
43
44 mShapeStyleButton->setSymbolType( Qgis::SymbolType::Marker );
45
46 blockAllSignals( false );
47
48 connect( mMarker, &QgsLayoutObject::changed, this, &QgsLayoutMarkerWidget::setGuiElementValues );
49 mShapeStyleButton->registerExpressionContextGenerator( mMarker );
50
51 connect( mShapeStyleButton, &QgsSymbolButton::changed, this, &QgsLayoutMarkerWidget::symbolChanged );
52
53 connect( mRotationFromMapCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutMarkerWidget::rotationFromMapCheckBoxChanged );
54 connect( mNorthOffsetSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMarkerWidget::northOffsetSpinBoxChanged );
55 connect( mNorthTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutMarkerWidget::northTypeComboBoxChanged );
56
57 mNorthTypeComboBox->blockSignals( true );
58 mNorthTypeComboBox->addItem( tr( "Grid North" ), QgsLayoutNorthArrowHandler::GridNorth );
59 mNorthTypeComboBox->addItem( tr( "True North" ), QgsLayoutNorthArrowHandler::TrueNorth );
60 mNorthTypeComboBox->blockSignals( false );
61 mNorthOffsetSpinBox->setClearValue( 0.0 );
62
63 mShapeStyleButton->setLayer( coverageLayer() );
64 if ( mMarker->layout() )
65 {
66 connect( &mMarker->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, mShapeStyleButton, &QgsSymbolButton::setLayer );
67 mMapComboBox->setCurrentLayout( mMarker->layout() );
68 mMapComboBox->setItemType( QgsLayoutItemRegistry::LayoutMap );
69 connect( mMapComboBox, &QgsLayoutItemComboBox::itemChanged, this, &QgsLayoutMarkerWidget::mapChanged );
70 }
71
72 setGuiElementValues();
73}
74
76{
77 if ( mItemPropertiesWidget )
78 mItemPropertiesWidget->setMasterLayout( masterLayout );
79}
80
82{
84 return false;
85
86 if ( mMarker )
87 {
88 disconnect( mMarker, &QgsLayoutObject::changed, this, &QgsLayoutMarkerWidget::setGuiElementValues );
89 }
90
91 mMarker = qobject_cast< QgsLayoutItemMarker * >( item );
92 mItemPropertiesWidget->setItem( mMarker );
93
94 if ( mMarker )
95 {
96 connect( mMarker, &QgsLayoutObject::changed, this, &QgsLayoutMarkerWidget::setGuiElementValues );
97 mShapeStyleButton->registerExpressionContextGenerator( mMarker );
98 }
99
100 setGuiElementValues();
101
102 return true;
103}
104
105void QgsLayoutMarkerWidget::blockAllSignals( bool block )
106{
107 mShapeStyleButton->blockSignals( block );
108 mMapComboBox->blockSignals( block );
109 mRotationFromMapCheckBox->blockSignals( block );
110 mNorthTypeComboBox->blockSignals( block );
111 mNorthOffsetSpinBox->blockSignals( block );
112}
113
114void QgsLayoutMarkerWidget::setGuiElementValues()
115{
116 if ( !mMarker )
117 {
118 return;
119 }
120
121 blockAllSignals( true );
122
123 mShapeStyleButton->setSymbol( mMarker->symbol()->clone() );
124
125 mMapComboBox->setItem( mMarker->linkedMap() );
126 if ( mMarker->linkedMap() )
127 {
128 mRotationFromMapCheckBox->setCheckState( Qt::Checked );
129 mMapComboBox->setEnabled( true );
130 mNorthTypeComboBox->setEnabled( true );
131 mNorthOffsetSpinBox->setEnabled( true );
132 }
133 else
134 {
135 mRotationFromMapCheckBox->setCheckState( Qt::Unchecked );
136 mMapComboBox->setEnabled( false );
137 mNorthTypeComboBox->setEnabled( false );
138 mNorthOffsetSpinBox->setEnabled( false );
139 }
140 mNorthTypeComboBox->setCurrentIndex( mNorthTypeComboBox->findData( mMarker->northMode() ) );
141 mNorthOffsetSpinBox->setValue( mMarker->northOffset() );
142
143 blockAllSignals( false );
144}
145
146void QgsLayoutMarkerWidget::symbolChanged()
147{
148 if ( !mMarker )
149 return;
150
151 mMarker->layout()->undoStack()->beginCommand( mMarker, tr( "Change Marker Symbol" ), QgsLayoutItem::UndoShapeStyle );
152 mMarker->setSymbol( mShapeStyleButton->clonedSymbol<QgsMarkerSymbol>() );
153 mMarker->layout()->undoStack()->endCommand();
154}
155
156void QgsLayoutMarkerWidget::rotationFromMapCheckBoxChanged( int state )
157{
158 if ( !mMarker )
159 {
160 return;
161 }
162
163 mMarker->beginCommand( tr( "Toggle Rotation Sync" ) );
164 if ( state == Qt::Unchecked )
165 {
166 mMarker->setLinkedMap( nullptr );
167 mMapComboBox->setEnabled( false );
168 mNorthTypeComboBox->setEnabled( false );
169 mNorthOffsetSpinBox->setEnabled( false );
170 }
171 else
172 {
173 QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap * >( mMapComboBox->currentItem() );
174 mMarker->setLinkedMap( map );
175 mNorthTypeComboBox->setEnabled( true );
176 mNorthOffsetSpinBox->setEnabled( true );
177 mMapComboBox->setEnabled( true );
178 }
179 mMarker->endCommand();
180}
181
182void QgsLayoutMarkerWidget::mapChanged( QgsLayoutItem *item )
183{
184 if ( !mMarker )
185 {
186 return;
187 }
188
189 const QgsLayout *layout = mMarker->layout();
190 if ( !layout )
191 {
192 return;
193 }
194
195 QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap *>( item );
196 if ( !map )
197 {
198 return;
199 }
200
201 mMarker->beginCommand( tr( "Change Rotation Map" ) );
202 mMarker->setLinkedMap( map );
203 mMarker->update();
204 mMarker->endCommand();
205}
206
207void QgsLayoutMarkerWidget::northOffsetSpinBoxChanged( double d )
208{
209 mMarker->beginCommand( tr( "Change Marker North Offset" ), QgsLayoutItem::UndoPictureNorthOffset );
210 mMarker->setNorthOffset( d );
211 mMarker->endCommand();
212 mMarker->update();
213}
214
215void QgsLayoutMarkerWidget::northTypeComboBoxChanged( int index )
216{
217 mMarker->beginCommand( tr( "Change Marker North Mode" ) );
218 mMarker->setNorthMode( static_cast< QgsLayoutNorthArrowHandler::NorthMode >( mNorthTypeComboBox->itemData( index ).toInt() ) );
219 mMarker->endCommand();
220 mMarker->update();
221}
@ Marker
Marker symbol.
A base class for property widgets for layout items.
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
Layout graphical items for displaying a map.
A layout item for showing marker symbols.
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.
Base class for graphical items within a QgsLayout.
@ UndoPictureNorthOffset
Picture north offset.
@ UndoShapeStyle
Shape symbol style.
int type() const override
Returns a unique graphics item type identifier.
void setMasterLayout(QgsMasterLayoutInterface *masterLayout) override
Sets the master layout associated with the item.
bool setNewItem(QgsLayoutItem *item) override
Attempts to update the widget to show the properties for the specified item.
QgsLayoutMarkerWidget(QgsLayoutItemMarker *marker)
constructor
NorthMode
Method for syncing rotation to a map's North direction.
void changed()
Emitted when the object's properties change.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
A marker symbol type, for rendering Point and MultiPoint geometries.
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.