QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
30#include "moc_qgsmeshlabelingwidget.cpp"
31
33 : QgsMapLayerConfigWidget( layer, canvas, parent )
34 , mLayer( layer )
35 , mCanvas( canvas )
36 , mMessageBar( messageBar )
37
38{
39 setupUi( this );
40
41 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingNone.svg" ) ), tr( "No Labels" ), ModeNone );
42 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Labels on Vertices" ), ModeVertices );
43 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Labels on Faces" ), ModeFaces );
44
45 connect( mLabelModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMeshLabelingWidget::labelModeChanged );
46 setLayer( layer );
47}
48
50{
52
53 if ( QgsLabelingGui *l = labelingGui() )
54 l->setDockMode( dockMode );
55}
56
58{
59 return qobject_cast<QgsLabelingGui *>( mWidget );
60}
61
63{
64 if ( mOldSettings )
65 {
66 mLayer->setLabeling( mOldSettings.release() );
67 mLayer->setLabelsEnabled( mOldLabelsEnabled );
68 }
69 setLayer( mLayer );
70}
71
73{
74 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Mesh )
75 {
76 setEnabled( false );
77 return;
78 }
79 else
80 {
81 setEnabled( true );
82 }
83
84 QgsMeshLayer *layer = qobject_cast<QgsMeshLayer *>( mapLayer );
85 mLayer = layer;
86 if ( mLayer->labeling() )
87 {
88 mOldSettings.reset( mLayer->labeling()->clone() );
89 }
90 else
91 {
92 mOldSettings.reset();
93 }
94 mOldLabelsEnabled = mLayer->labelsEnabled();
95
97}
98
100{
101 if ( !mLayer )
102 return;
103
104 whileBlocking( mLabelModeComboBox )->setCurrentIndex( -1 );
105
106 // pick the right mode of the layer
107 Mode mode = ModeNone;
108 if ( mLayer->labelsEnabled() )
109 {
110 if ( QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() ) )
111 {
112 mode = labeling->provider( mLayer )->labelFaces() ? ModeFaces : ModeVertices;
113 }
114 }
115 mLabelModeComboBox->setCurrentIndex( mLabelModeComboBox->findData( mode ) );
116
117 if ( QgsLabelingGui *lg = qobject_cast<QgsLabelingGui *>( mWidget ) )
118 {
119 lg->updateUi();
120 }
121}
122
124{
125 const Mode mode = static_cast<Mode>( mLabelModeComboBox->currentData().toInt() );
126 switch ( mode )
127 {
128 case ModeVertices:
129 {
130 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), false ) );
131 mLayer->setLabelsEnabled( true );
132 break;
133 }
134
135 case ModeFaces:
136 {
137 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), true ) );
138 mLayer->setLabelsEnabled( true );
139 break;
140 }
141
142 case ModeNone:
143 {
144 mLayer->setLabelsEnabled( false );
145 break;
146 }
147 }
148}
149
151{
154 // trigger refresh
155 mLayer->triggerRepaint();
156}
157
158void QgsMeshLabelingWidget::labelModeChanged( int index )
159{
160 if ( mWidget )
161 mStackedWidget->removeWidget( mWidget );
162
163 delete mWidget;
164 mWidget = nullptr;
165
166 if ( index < 0 )
167 return;
168
169 const Mode mode = static_cast<Mode>( mLabelModeComboBox->currentData().toInt() );
170
171 switch ( mode )
172 {
173 case ModeVertices:
174 case ModeFaces:
175 {
176 QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() );
177 if ( labeling )
178 {
179 mSettings = std::make_unique<QgsPalLayerSettings>( labeling->settings() );
180 }
181 else
182 {
183 mSettings = std::make_unique<QgsPalLayerSettings>( QgsAbstractMeshLayerLabeling::defaultSettingsForLayer( mLayer ) );
184 }
185
186 QgsSymbolWidgetContext context;
187 context.setMapCanvas( mMapCanvas );
188 context.setMessageBar( mMessageBar );
189
191 QgsLabelingGui *labelingGui = new QgsLabelingGui( mLayer, mCanvas, *mSettings, this, geomType );
192 labelingGui->setLabelMode( QgsLabelingGui::Labels );
193 labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
194 labelingGui->setContext( context );
195
196 labelingGui->setDockMode( dockMode() );
197 connect( labelingGui, &QgsLabelingGui::widgetChanged, this, &QgsMeshLabelingWidget::widgetChanged );
198 connect( labelingGui, &QgsLabelingGui::auxiliaryFieldCreated, this, &QgsMeshLabelingWidget::auxiliaryFieldCreated );
199
200 mWidget = labelingGui;
201
202
203 mStackedWidget->addWidget( mWidget );
204 mStackedWidget->setCurrentWidget( mWidget );
205 break;
206 }
207
208 case ModeNone:
209 break;
210 }
211 emit widgetChanged();
212}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
@ Point
Points.
Definition qgis.h:359
@ Polygon
Polygons.
Definition qgis.h:361
@ Mesh
Mesh layer. Added in QGIS 3.2.
Definition qgis.h:194
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:80
Qgis::LayerType type
Definition qgsmaplayer.h:90
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:6511