QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsmeshlabelingwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshlabelingwidget.cpp
3 ---------------------
4 begin : November 2023
5 copyright : (C) 2023 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <memory>
19
20#include "qgsapplication.h"
21#include "qgslabelinggui.h"
22#include "qgsmeshlayer.h"
25#include "qgsproject.h"
26
27#include <QDialogButtonBox>
28#include <QDomElement>
29#include <QString>
30
31#include "moc_qgsmeshlabelingwidget.cpp"
32
33using namespace Qt::StringLiterals;
34
36 : QgsMapLayerConfigWidget( layer, canvas, parent )
37 , mLayer( layer )
38 , mCanvas( canvas )
39 , mMessageBar( messageBar )
40
41{
42 setupUi( this );
43
44 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( u"labelingNone.svg"_s ), tr( "No Labels" ), ModeNone );
45 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( u"labelingSingle.svg"_s ), tr( "Labels on Vertices" ), ModeVertices );
46 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( u"labelingSingle.svg"_s ), tr( "Labels on Faces" ), ModeFaces );
47
48 connect( mLabelModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMeshLabelingWidget::labelModeChanged );
49 setLayer( layer );
50}
51
53{
55
56 if ( QgsLabelingGui *l = labelingGui() )
57 l->setDockMode( dockMode );
58}
59
61{
62 return qobject_cast<QgsLabelingGui *>( mWidget );
63}
64
66{
67 if ( mOldSettings )
68 {
69 mLayer->setLabeling( mOldSettings.release() );
70 mLayer->setLabelsEnabled( mOldLabelsEnabled );
71 }
72 setLayer( mLayer );
73}
74
76{
77 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Mesh )
78 {
79 setEnabled( false );
80 return;
81 }
82 else
83 {
84 setEnabled( true );
85 }
86
87 QgsMeshLayer *layer = qobject_cast<QgsMeshLayer *>( mapLayer );
88 mLayer = layer;
89 if ( mLayer->labeling() )
90 {
91 mOldSettings.reset( mLayer->labeling()->clone() );
92 }
93 else
94 {
95 mOldSettings.reset();
96 }
97 mOldLabelsEnabled = mLayer->labelsEnabled();
98
100}
101
103{
104 if ( !mLayer )
105 return;
106
107 whileBlocking( mLabelModeComboBox )->setCurrentIndex( -1 );
108
109 // pick the right mode of the layer
110 Mode mode = ModeNone;
111 if ( mLayer->labelsEnabled() )
112 {
113 if ( QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() ) )
114 {
115 mode = labeling->provider( mLayer )->labelFaces() ? ModeFaces : ModeVertices;
116 }
117 }
118 mLabelModeComboBox->setCurrentIndex( mLabelModeComboBox->findData( mode ) );
119
120 if ( QgsLabelingGui *lg = qobject_cast<QgsLabelingGui *>( mWidget ) )
121 {
122 lg->updateUi();
123 }
124}
125
127{
128 const Mode mode = static_cast<Mode>( mLabelModeComboBox->currentData().toInt() );
129 switch ( mode )
130 {
131 case ModeVertices:
132 {
133 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), false ) );
134 mLayer->setLabelsEnabled( true );
135 break;
136 }
137
138 case ModeFaces:
139 {
140 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), true ) );
141 mLayer->setLabelsEnabled( true );
142 break;
143 }
144
145 case ModeNone:
146 {
147 mLayer->setLabelsEnabled( false );
148 break;
149 }
150 }
151}
152
154{
157 // trigger refresh
158 mLayer->triggerRepaint();
159}
160
161void QgsMeshLabelingWidget::labelModeChanged( int index )
162{
163 if ( mWidget )
164 mStackedWidget->removeWidget( mWidget );
165
166 delete mWidget;
167 mWidget = nullptr;
168
169 if ( index < 0 )
170 return;
171
172 const Mode mode = static_cast<Mode>( mLabelModeComboBox->currentData().toInt() );
173
174 switch ( mode )
175 {
176 case ModeVertices:
177 case ModeFaces:
178 {
179 QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() );
180 if ( labeling )
181 {
182 mSettings = std::make_unique<QgsPalLayerSettings>( labeling->settings() );
183 }
184 else
185 {
186 mSettings = std::make_unique<QgsPalLayerSettings>( QgsAbstractMeshLayerLabeling::defaultSettingsForLayer( mLayer ) );
187 }
188
189 QgsSymbolWidgetContext context;
190 context.setMapCanvas( mMapCanvas );
191 context.setMessageBar( mMessageBar );
192
194 QgsLabelingGui *labelingGui = new QgsLabelingGui( mLayer, mCanvas, *mSettings, this, geomType );
195 labelingGui->setLabelMode( QgsLabelingGui::Labels );
196 labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
197 labelingGui->setContext( context );
198
199 labelingGui->setDockMode( dockMode() );
200 connect( labelingGui, &QgsLabelingGui::widgetChanged, this, &QgsMeshLabelingWidget::widgetChanged );
201 connect( labelingGui, &QgsLabelingGui::auxiliaryFieldCreated, this, &QgsMeshLabelingWidget::auxiliaryFieldCreated );
202
203 mWidget = labelingGui;
204
205
206 mStackedWidget->addWidget( mWidget );
207 mStackedWidget->setCurrentWidget( mWidget );
208 break;
209 }
210
211 case ModeNone:
212 break;
213 }
214 emit widgetChanged();
215}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:365
@ Point
Points.
Definition qgis.h:366
@ Polygon
Polygons.
Definition qgis.h:368
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:197
static QgsPalLayerSettings defaultSettingsForLayer(const QgsMeshLayer *layer)
Returns the default layer settings to use for the specified mesh layer.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Map canvas is a class for displaying all GIS data types on a canvas.
QgsMapLayerConfigWidget(QgsMapLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr)
A panel widget that can be shown in the map style dock.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Qgis::LayerType type
Definition qgsmaplayer.h:93
void apply() override
Saves the labeling configuration and immediately updates the map canvas to reflect the changes.
void writeSettingsToLayer()
save config to layer
QgsLabelingGui * labelingGui()
Returns the labeling gui widget or nullptr if none.
void auxiliaryFieldCreated()
Emitted when an auxiliary field is created.
void setLayer(QgsMapLayer *layer)
Sets the layer to configure.
QgsMeshLabelingWidget(QgsMeshLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr, QgsMessageBar *messageBar=nullptr)
constructor
void resetSettings()
Reset the settings.
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
void adaptToLayer()
reload the settings shown in the dialog from the current layer
Basic implementation of the labeling interface for mesh layers.
QgsPalLayerSettings settings(const QString &providerId=QString()) const override
Gets associated label settings.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
A bar for displaying non-blocking messages to the user.
bool dockMode() const
Returns the dock mode state.
void widgetChanged()
Emitted when the widget state changes.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setDirty(bool b=true)
Flag the project as dirty (modified).
void setMapCanvas(QgsMapCanvas *canvas)
Sets the map canvas associated with the widget.
void setMessageBar(QgsMessageBar *bar)
Sets the message bar associated with the widget.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6828