QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
21#include "qgslabelinggui.h"
22#include "qgsmeshlayer.h"
25#include "qgsproject.h"
26#include "qgsapplication.h"
27
29 : QgsMapLayerConfigWidget( layer, canvas, parent )
30 , mLayer( layer )
31 , mCanvas( canvas )
32 , mMessageBar( messageBar )
33
34{
35 setupUi( this );
36
37 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingNone.svg" ) ), tr( "No Labels" ), ModeNone );
38 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Labels on Vertices" ), ModeVertices );
39 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Labels on Faces" ), ModeFaces );
40
41 connect( mLabelModeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsMeshLabelingWidget::labelModeChanged );
42 setLayer( layer );
43}
44
46{
48
49 if ( QgsLabelingGui *l = labelingGui() )
50 l->setDockMode( dockMode );
51}
52
54{
55 return qobject_cast<QgsLabelingGui *>( mWidget );
56}
57
59{
60 if ( mOldSettings )
61 {
62 mLayer->setLabeling( mOldSettings.release() );
63 mLayer->setLabelsEnabled( mOldLabelsEnabled );
64 }
65 setLayer( mLayer );
66}
67
69{
70 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Mesh )
71 {
72 setEnabled( false );
73 return;
74 }
75 else
76 {
77 setEnabled( true );
78 }
79
80 QgsMeshLayer *layer = qobject_cast<QgsMeshLayer *>( mapLayer );
81 mLayer = layer;
82 if ( mLayer->labeling() )
83 {
84 mOldSettings.reset( mLayer->labeling()->clone() );
85 }
86 else
87 {
88 mOldSettings.reset();
89 }
90 mOldLabelsEnabled = mLayer->labelsEnabled();
91
93}
94
96{
97 if ( !mLayer )
98 return;
99
100 whileBlocking( mLabelModeComboBox )->setCurrentIndex( -1 );
101
102 // pick the right mode of the layer
103 Mode mode = ModeNone;
104 if ( mLayer->labelsEnabled() )
105 {
106 if ( QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() ) )
107 {
108 mode = labeling->provider( mLayer )->labelFaces() ? ModeFaces : ModeVertices;
109 }
110 }
111 mLabelModeComboBox->setCurrentIndex( mLabelModeComboBox->findData( mode ) );
112
113 if ( QgsLabelingGui *lg = qobject_cast<QgsLabelingGui *>( mWidget ) )
114 {
115 lg->updateUi();
116 }
117}
118
120{
121 const Mode mode = static_cast< Mode >( mLabelModeComboBox->currentData().toInt() );
122 switch ( mode )
123 {
124 case ModeVertices:
125 {
126 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), false ) );
127 mLayer->setLabelsEnabled( true );
128 break;
129 }
130
131 case ModeFaces:
132 {
133 mLayer->setLabeling( new QgsMeshLayerSimpleLabeling( qobject_cast<QgsLabelingGui *>( mWidget )->layerSettings(), true ) );
134 mLayer->setLabelsEnabled( true );
135 break;
136 }
137
138 case ModeNone:
139 {
140 mLayer->setLabelsEnabled( false );
141 break;
142 }
143 }
144}
145
147{
150 // trigger refresh
151 mLayer->triggerRepaint();
152}
153
154void QgsMeshLabelingWidget::labelModeChanged( int index )
155{
156 if ( mWidget )
157 mStackedWidget->removeWidget( mWidget );
158
159 delete mWidget;
160 mWidget = nullptr;
161
162 if ( index < 0 )
163 return;
164
165 const Mode mode = static_cast< Mode >( mLabelModeComboBox->currentData().toInt() );
166
167 switch ( mode )
168 {
169 case ModeVertices:
170 case ModeFaces:
171 {
172 QgsMeshLayerSimpleLabeling *labeling = dynamic_cast<QgsMeshLayerSimpleLabeling *>( mLayer->labeling() );
173 if ( labeling )
174 {
175 mSettings.reset( new QgsPalLayerSettings( labeling->settings() ) );
176 }
177 else
178 {
179 mSettings = std::make_unique< QgsPalLayerSettings >( QgsAbstractMeshLayerLabeling::defaultSettingsForLayer( mLayer ) );
180 }
181
183 context.setMapCanvas( mMapCanvas );
184 context.setMessageBar( mMessageBar );
185
187 QgsLabelingGui *labelingGui = new QgsLabelingGui( mLayer, mCanvas, *mSettings, this, geomType );
188 labelingGui->setLabelMode( QgsLabelingGui::Labels );
189 labelingGui->layout()->setContentsMargins( 0, 0, 0, 0 );
190 labelingGui->setContext( context );
191
192 labelingGui->setDockMode( dockMode() );
193 connect( labelingGui, &QgsLabelingGui::widgetChanged, this, &QgsMeshLabelingWidget::widgetChanged );
194 connect( labelingGui, &QgsLabelingGui::auxiliaryFieldCreated, this, &QgsMeshLabelingWidget::auxiliaryFieldCreated );
195
196 mWidget = labelingGui;
197
198
199 mStackedWidget->addWidget( mWidget );
200 mStackedWidget->setCurrentWidget( mWidget );
201 break;
202 }
203
204 case ModeNone:
205 break;
206 }
207 emit widgetChanged();
208}
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:5821