QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
16#include <QDialogButtonBox>
17#include <QDomElement>
18
20#include "moc_qgsmeshlabelingwidget.cpp"
21
22#include "qgslabelinggui.h"
23#include "qgsmeshlayer.h"
26#include "qgsproject.h"
27#include "qgsapplication.h"
28
30 : QgsMapLayerConfigWidget( layer, canvas, parent )
31 , mLayer( layer )
32 , mCanvas( canvas )
33 , mMessageBar( messageBar )
34
35{
36 setupUi( this );
37
38 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingNone.svg" ) ), tr( "No Labels" ), ModeNone );
39 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Labels on Vertices" ), ModeVertices );
40 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Labels on Faces" ), ModeFaces );
41
42 connect( mLabelModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMeshLabelingWidget::labelModeChanged );
43 setLayer( layer );
44}
45
47{
49
50 if ( QgsLabelingGui *l = labelingGui() )
51 l->setDockMode( dockMode );
52}
53
55{
56 return qobject_cast<QgsLabelingGui *>( mWidget );
57}
58
60{
61 if ( mOldSettings )
62 {
63 mLayer->setLabeling( mOldSettings.release() );
64 mLayer->setLabelsEnabled( mOldLabelsEnabled );
65 }
66 setLayer( mLayer );
67}
68
70{
71 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Mesh )
72 {
73 setEnabled( false );
74 return;
75 }
76 else
77 {
78 setEnabled( true );
79 }
80
81 QgsMeshLayer *layer = qobject_cast<QgsMeshLayer *>( mapLayer );
82 mLayer = layer;
83 if ( mLayer->labeling() )
84 {
85 mOldSettings.reset( mLayer->labeling()->clone() );
86 }
87 else
88 {
89 mOldSettings.reset();
90 }
91 mOldLabelsEnabled = mLayer->labelsEnabled();
92
94}
95
97{
98 if ( !mLayer )
99 return;
100
101 whileBlocking( mLabelModeComboBox )->setCurrentIndex( -1 );
102
103 // pick the right mode of the layer
104 Mode mode = ModeNone;
105 if ( mLayer->labelsEnabled() )
106 {
107 if ( QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() ) )
108 {
109 mode = labeling->provider( mLayer )->labelFaces() ? ModeFaces : ModeVertices;
110 }
111 }
112 mLabelModeComboBox->setCurrentIndex( mLabelModeComboBox->findData( mode ) );
113
114 if ( QgsLabelingGui *lg = qobject_cast<QgsLabelingGui *>( mWidget ) )
115 {
116 lg->updateUi();
117 }
118}
119
121{
122 const Mode mode = static_cast< Mode >( mLabelModeComboBox->currentData().toInt() );
123 switch ( mode )
124 {
125 case ModeVertices:
126 {
127 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), false ) );
128 mLayer->setLabelsEnabled( true );
129 break;
130 }
131
132 case ModeFaces:
133 {
134 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), true ) );
135 mLayer->setLabelsEnabled( true );
136 break;
137 }
138
139 case ModeNone:
140 {
141 mLayer->setLabelsEnabled( false );
142 break;
143 }
144 }
145}
146
148{
151 // trigger refresh
152 mLayer->triggerRepaint();
153}
154
155void QgsMeshLabelingWidget::labelModeChanged( int index )
156{
157 if ( mWidget )
158 mStackedWidget->removeWidget( mWidget );
159
160 delete mWidget;
161 mWidget = nullptr;
162
163 if ( index < 0 )
164 return;
165
166 const Mode mode = static_cast< Mode >( mLabelModeComboBox->currentData().toInt() );
167
168 switch ( mode )
169 {
170 case ModeVertices:
171 case ModeFaces:
172 {
173 QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() );
174 if ( labeling )
175 {
176 mSettings.reset( new QgsPalLayerSettings( labeling->settings() ) );
177 }
178 else
179 {
180 mSettings = std::make_unique< QgsPalLayerSettings >( QgsAbstractMeshLayerLabeling::defaultSettingsForLayer( mLayer ) );
181 }
182
184 context.setMapCanvas( mMapCanvas );
185 context.setMessageBar( mMessageBar );
186
188 QgsLabelingGui *labelingGui = new QgsLabelingGui( mLayer, mCanvas, *mSettings, this, geomType );
189 labelingGui->setLabelMode( QgsLabelingGui::Labels );
190 labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
191 labelingGui->setContext( context );
192
193 labelingGui->setDockMode( dockMode() );
194 connect( labelingGui, &QgsLabelingGui::widgetChanged, this, &QgsMeshLabelingWidget::widgetChanged );
195 connect( labelingGui, &QgsLabelingGui::auxiliaryFieldCreated, this, &QgsMeshLabelingWidget::auxiliaryFieldCreated );
196
197 mWidget = labelingGui;
198
199
200 mStackedWidget->addWidget( mWidget );
201 mStackedWidget->setCurrentWidget( mWidget );
202 break;
203 }
204
205 case ModeNone:
206 break;
207 }
208 emit widgetChanged();
209}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Polygon
Polygons.
@ Mesh
Mesh layer. Added in QGIS 3.2.
static QgsPalLayerSettings defaultSettingsForLayer(const QgsMeshLayer *layer)
Returns the default layer settings to use for the specified mesh layer.
virtual QgsAbstractMeshLayerLabeling * clone() const =0
Returns a new copy of the object.
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.
A panel widget that can be shown in the map style dock.
Base class for all map layer types.
Definition qgsmaplayer.h:76
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
Qgis::LayerType type
Definition qgsmaplayer.h:86
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 layer.
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.
const QgsAbstractMeshLayerLabeling * labeling() const
Access to const labeling configuration.
void setLabeling(QgsAbstractMeshLayerLabeling *labeling)
Sets labeling configuration.
void setLabelsEnabled(bool enabled)
Sets whether labels should be enabled for the layer.
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.
Contains settings for how a map layer will be labeled.
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.
bool dockMode()
Returns the dock mode state.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setDirty(bool b=true)
Flag the project as dirty (modified).
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
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:5862