QGIS API Documentation 3.41.0-Master (57ec4277f5e)
Loading...
Searching...
No Matches
qgsrasterlabelingwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterlabelingwidget.cpp
3 ---------------------------
4 begin : December 2024
5 copyright : (C) 2024 by Nyall Dawson
6 email : nyall dot dawson 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#include "moc_qgsrasterlabelingwidget.cpp"
18
19#include "qgsrasterlayer.h"
20#include "qgsproject.h"
21#include "qgsapplication.h"
23#include "qgslabelingwidget.h"
24
26 : QgsMapLayerConfigWidget( layer, canvas, parent )
27 , mCanvas( canvas )
28 , mMessageBar( messageBar )
29
30{
31 setupUi( this );
32
33 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingNone.svg" ) ), tr( "No Labels" ), QStringLiteral( "none" ) );
34 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Label with Pixel Values" ), QStringLiteral( "simple" ) );
35
36 connect( mLabelModeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsRasterLabelingWidget::labelModeChanged );
37 setLayer( layer );
38
39 connect( mLabelRulesButton, &QAbstractButton::clicked, this, &QgsRasterLabelingWidget::showLabelingEngineRulesPrivate );
40 connect( mEngineSettingsButton, &QAbstractButton::clicked, this, &QgsRasterLabelingWidget::showEngineConfigDialogPrivate );
41
42 const int iconSize16 = QgsGuiUtils::scaleIconSize( 16 );
43 mEngineSettingsButton->setIconSize( QSize( iconSize16, iconSize16 ) );
44 mLabelRulesButton->setIconSize( QSize( iconSize16, iconSize16 ) );
45}
46
48{
50
51 if ( QgsRasterLabelSettingsWidget *l = qobject_cast<QgsRasterLabelSettingsWidget *>( mWidget ) )
52 {
53 l->setDockMode( dockMode );
54 }
55}
56
58{
59 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Raster )
60 {
61 setEnabled( false );
62 return;
63 }
64 else
65 {
66 setEnabled( true );
67 }
68
69 QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( mapLayer );
70 mLayer = layer;
71
73}
74
76{
77 if ( !mLayer )
78 return;
79
80 int index = -1;
81 QgsAbstractRasterLayerLabeling *labeling = mLayer->labeling();
82 if ( mLayer->labelsEnabled() && labeling )
83 {
84 index = mLabelModeComboBox->findData( labeling->type() );
85 if ( QgsRasterLabelSettingsWidget *settingsWidget = qobject_cast<QgsRasterLabelSettingsWidget *>( mWidget ) )
86 {
87 settingsWidget->setLayer( mLayer );
88 settingsWidget->setLabeling( labeling );
89 }
90 }
91 else
92 {
93 index = 0;
94 }
95
96 if ( index != mLabelModeComboBox->currentIndex() )
97 {
98 mLabelModeComboBox->setCurrentIndex( index );
99 }
100}
101
103{
104 const QString mode = mLabelModeComboBox->currentData().toString();
105 if ( mode == QLatin1String( "simple" ) )
106 {
107 std::unique_ptr<QgsRasterLayerSimpleLabeling> labeling = std::make_unique<QgsRasterLayerSimpleLabeling>();
108 if ( QgsRasterLabelSettingsWidget *settingsWidget = qobject_cast<QgsRasterLabelSettingsWidget *>( mWidget ) )
109 {
110 settingsWidget->updateLabeling( labeling.get() );
111 }
112 mLayer->setLabeling( labeling.release() );
113 mLayer->setLabelsEnabled( true );
114 }
115 else
116 {
117 mLayer->setLabelsEnabled( false );
118 }
119}
120
122{
125 // trigger refresh
126 mLayer->triggerRepaint();
127}
128
129void QgsRasterLabelingWidget::labelModeChanged( int index )
130{
131 if ( mWidget )
132 mStackedWidget->removeWidget( mWidget );
133
134 delete mWidget;
135 mWidget = nullptr;
136
137 if ( index < 0 )
138 return;
139
140 const QString mode = mLabelModeComboBox->currentData().toString();
141 if ( mode == QLatin1String( "simple" ) )
142 {
144 context.setMapCanvas( mMapCanvas );
145 context.setMessageBar( mMessageBar );
146
147 QgsRasterLabelSettingsWidget *settingsWidget = new QgsRasterLabelSettingsWidget( mLayer, mCanvas, this );
148 settingsWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
149 settingsWidget->setContext( context );
150
151 settingsWidget->setDockMode( dockMode() );
152 connect( settingsWidget, &QgsLabelingGui::widgetChanged, this, &QgsRasterLabelingWidget::widgetChanged );
153
154 mWidget = settingsWidget;
155 if ( !dynamic_cast<QgsRasterLayerSimpleLabeling *>( mLayer->labeling() ) )
156 {
157 std::unique_ptr<QgsRasterLayerSimpleLabeling> labeling = std::make_unique<QgsRasterLayerSimpleLabeling>();
158 settingsWidget->setLabeling( labeling.get() );
159 mLayer->setLabeling( labeling.release() );
160 }
161 else
162 {
163 settingsWidget->setLabeling( mLayer->labeling() );
164 }
165
166 mStackedWidget->addWidget( mWidget );
167 mStackedWidget->setCurrentWidget( mWidget );
168 }
169
170 emit widgetChanged();
171}
172
173void QgsRasterLabelingWidget::showLabelingEngineRulesPrivate()
174{
176}
177
178void QgsRasterLabelingWidget::showEngineConfigDialogPrivate()
179{
181}
@ Raster
Raster layer.
Abstract base class for labeling settings for raster layers.
virtual QString type() const =0
Unique type string of the labeling configuration implementation.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static void showLabelingEngineRules(QWidget *parent, QgsMapCanvas *canvas)
Shows the labeling engine rules.
static void showEngineConfiguration(QWidget *parent, QgsMapCanvas *canvas)
Shows the labeling engine configuration.
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
A bar for displaying non-blocking messages to the user.
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).
A widget for customizing settings for raster layer labeling.
void setLabeling(QgsAbstractRasterLayerLabeling *labeling)
Sets the labeling settings to show in the widget.
QgsRasterLabelingWidget(QgsRasterLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr, QgsMessageBar *messageBar=nullptr)
constructor
void setLayer(QgsMapLayer *layer)
Sets the layer to configure.
void writeSettingsToLayer()
Saves the labeling configuration to the destination layer.
void apply() override
Saves the labeling configuration and immediately updates the map canvas to reflect the changes.
void adaptToLayer()
Reload the settings shown in the dialog from the current layer.
void setDockMode(bool dockMode) override
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
Basic implementation of the labeling interface for raster layers.
Represents a raster layer.
const QgsAbstractRasterLayerLabeling * labeling() const
Access to const labeling configuration.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
void setLabeling(QgsAbstractRasterLayerLabeling *labeling)
Sets labeling configuration.
void setLabelsEnabled(bool enabled)
Sets whether labels should be enabled for the layer.
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.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...