QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
18 #include "qgslayoutmarkerwidget.h"
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 
27  : QgsLayoutItemBaseWidget( nullptr, marker )
28  , mMarker( marker )
29 {
30  Q_ASSERT( mMarker );
31 
32  setupUi( this );
33  setPanelTitle( tr( "Marker Properties" ) );
34 
35  //add widget for general composer item properties
36  mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, marker );
37  mItemPropertiesWidget->showFrameGroup( false );
38  mainLayout->addWidget( mItemPropertiesWidget );
39 
40  blockAllSignals( true );
41 
42  mShapeStyleButton->setSymbolType( QgsSymbol::Marker );
43 
44  blockAllSignals( false );
45 
46  connect( mMarker, &QgsLayoutObject::changed, this, &QgsLayoutMarkerWidget::setGuiElementValues );
47  mShapeStyleButton->registerExpressionContextGenerator( mMarker );
48 
49  connect( mShapeStyleButton, &QgsSymbolButton::changed, this, &QgsLayoutMarkerWidget::symbolChanged );
50 
51  connect( mRotationFromMapCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutMarkerWidget::rotationFromMapCheckBoxChanged );
52  connect( mNorthOffsetSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutMarkerWidget::northOffsetSpinBoxChanged );
53  connect( mNorthTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutMarkerWidget::northTypeComboBoxChanged );
54 
55  mNorthTypeComboBox->blockSignals( true );
56  mNorthTypeComboBox->addItem( tr( "Grid North" ), QgsLayoutNorthArrowHandler::GridNorth );
57  mNorthTypeComboBox->addItem( tr( "True North" ), QgsLayoutNorthArrowHandler::TrueNorth );
58  mNorthTypeComboBox->blockSignals( false );
59  mNorthOffsetSpinBox->setClearValue( 0.0 );
60 
61  mShapeStyleButton->setLayer( coverageLayer() );
62  if ( mMarker->layout() )
63  {
64  connect( &mMarker->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, mShapeStyleButton, &QgsSymbolButton::setLayer );
65  mMapComboBox->setCurrentLayout( mMarker->layout() );
66  mMapComboBox->setItemType( QgsLayoutItemRegistry::LayoutMap );
67  connect( mMapComboBox, &QgsLayoutItemComboBox::itemChanged, this, &QgsLayoutMarkerWidget::mapChanged );
68  }
69 
70  setGuiElementValues();
71 }
72 
74 {
75  if ( mItemPropertiesWidget )
76  mItemPropertiesWidget->setMasterLayout( masterLayout );
77 }
78 
80 {
82  return false;
83 
84  if ( mMarker )
85  {
86  disconnect( mMarker, &QgsLayoutObject::changed, this, &QgsLayoutMarkerWidget::setGuiElementValues );
87  }
88 
89  mMarker = qobject_cast< QgsLayoutItemMarker * >( item );
90  mItemPropertiesWidget->setItem( mMarker );
91 
92  if ( mMarker )
93  {
94  connect( mMarker, &QgsLayoutObject::changed, this, &QgsLayoutMarkerWidget::setGuiElementValues );
95  mShapeStyleButton->registerExpressionContextGenerator( mMarker );
96  }
97 
98  setGuiElementValues();
99 
100  return true;
101 }
102 
103 void QgsLayoutMarkerWidget::blockAllSignals( bool block )
104 {
105  mShapeStyleButton->blockSignals( block );
106  mMapComboBox->blockSignals( block );
107  mRotationFromMapCheckBox->blockSignals( block );
108  mNorthTypeComboBox->blockSignals( block );
109  mNorthOffsetSpinBox->blockSignals( block );
110 }
111 
112 void QgsLayoutMarkerWidget::setGuiElementValues()
113 {
114  if ( !mMarker )
115  {
116  return;
117  }
118 
119  blockAllSignals( true );
120 
121  mShapeStyleButton->setSymbol( mMarker->symbol()->clone() );
122 
123  mMapComboBox->setItem( mMarker->linkedMap() );
124  if ( mMarker->linkedMap() )
125  {
126  mRotationFromMapCheckBox->setCheckState( Qt::Checked );
127  mMapComboBox->setEnabled( true );
128  mNorthTypeComboBox->setEnabled( true );
129  mNorthOffsetSpinBox->setEnabled( true );
130  }
131  else
132  {
133  mRotationFromMapCheckBox->setCheckState( Qt::Unchecked );
134  mMapComboBox->setEnabled( false );
135  mNorthTypeComboBox->setEnabled( false );
136  mNorthOffsetSpinBox->setEnabled( false );
137  }
138  mNorthTypeComboBox->setCurrentIndex( mNorthTypeComboBox->findData( mMarker->northMode() ) );
139  mNorthOffsetSpinBox->setValue( mMarker->northOffset() );
140 
141  blockAllSignals( false );
142 }
143 
144 void QgsLayoutMarkerWidget::symbolChanged()
145 {
146  if ( !mMarker )
147  return;
148 
149  mMarker->layout()->undoStack()->beginCommand( mMarker, tr( "Change Marker Symbol" ), QgsLayoutItem::UndoShapeStyle );
150  mMarker->setSymbol( mShapeStyleButton->clonedSymbol<QgsMarkerSymbol>() );
151  mMarker->layout()->undoStack()->endCommand();
152 }
153 
154 void QgsLayoutMarkerWidget::rotationFromMapCheckBoxChanged( int state )
155 {
156  if ( !mMarker )
157  {
158  return;
159  }
160 
161  mMarker->beginCommand( tr( "Toggle Rotation Sync" ) );
162  if ( state == Qt::Unchecked )
163  {
164  mMarker->setLinkedMap( nullptr );
165  mMapComboBox->setEnabled( false );
166  mNorthTypeComboBox->setEnabled( false );
167  mNorthOffsetSpinBox->setEnabled( false );
168  }
169  else
170  {
171  QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap * >( mMapComboBox->currentItem() );
172  mMarker->setLinkedMap( map );
173  mNorthTypeComboBox->setEnabled( true );
174  mNorthOffsetSpinBox->setEnabled( true );
175  mMapComboBox->setEnabled( true );
176  }
177  mMarker->endCommand();
178 }
179 
180 void QgsLayoutMarkerWidget::mapChanged( QgsLayoutItem *item )
181 {
182  if ( !mMarker )
183  {
184  return;
185  }
186 
187  const QgsLayout *layout = mMarker->layout();
188  if ( !layout )
189  {
190  return;
191  }
192 
193  QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap *>( item );
194  if ( !map )
195  {
196  return;
197  }
198 
199  mMarker->beginCommand( tr( "Change Rotation Map" ) );
200  mMarker->setLinkedMap( map );
201  mMarker->update();
202  mMarker->endCommand();
203 }
204 
205 void QgsLayoutMarkerWidget::northOffsetSpinBoxChanged( double d )
206 {
207  mMarker->beginCommand( tr( "Change Marker North Offset" ), QgsLayoutItem::UndoPictureNorthOffset );
208  mMarker->setNorthOffset( d );
209  mMarker->endCommand();
210  mMarker->update();
211 }
212 
213 void QgsLayoutMarkerWidget::northTypeComboBoxChanged( int index )
214 {
215  mMarker->beginCommand( tr( "Change Marker North Mode" ) );
216  mMarker->setNorthMode( static_cast< QgsLayoutNorthArrowHandler::NorthMode >( mNorthTypeComboBox->itemData( index ).toInt() ) );
217  mMarker->endCommand();
218  mMarker->update();
219 }
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.
Definition: qgssymbol.h:1004
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.
@ Marker
Marker symbol.
Definition: qgssymbol.h:88