QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "qgsapplication.h"
19#include "qgslabelingwidget.h"
20#include "qgsproject.h"
22#include "qgsrasterlayer.h"
23
24#include "moc_qgsrasterlabelingwidget.cpp"
25
27 : QgsMapLayerConfigWidget( layer, canvas, parent )
28 , mCanvas( canvas )
29 , mMessageBar( messageBar )
30
31{
32 setupUi( this );
33
34 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingNone.svg" ) ), tr( "No Labels" ), QStringLiteral( "none" ) );
35 mLabelModeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "labelingSingle.svg" ) ), tr( "Label with Pixel Values" ), QStringLiteral( "simple" ) );
36
37 connect( mLabelModeComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsRasterLabelingWidget::labelModeChanged );
38 setLayer( layer );
39
40 connect( mLabelRulesButton, &QAbstractButton::clicked, this, &QgsRasterLabelingWidget::showLabelingEngineRulesPrivate );
41 connect( mEngineSettingsButton, &QAbstractButton::clicked, this, &QgsRasterLabelingWidget::showEngineConfigDialogPrivate );
42
43 const int iconSize16 = QgsGuiUtils::scaleIconSize( 16 );
44 mEngineSettingsButton->setIconSize( QSize( iconSize16, iconSize16 ) );
45 mLabelRulesButton->setIconSize( QSize( iconSize16, iconSize16 ) );
46}
47
49{
51
52 if ( QgsRasterLabelSettingsWidget *l = qobject_cast<QgsRasterLabelSettingsWidget *>( mWidget ) )
53 {
54 l->setDockMode( dockMode );
55 }
56}
57
59{
60 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Raster )
61 {
62 setEnabled( false );
63 return;
64 }
65 else
66 {
67 setEnabled( true );
68 }
69
70 QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( mapLayer );
71 mLayer = layer;
72
74}
75
77{
78 if ( !mLayer )
79 return;
80
81 int index = -1;
82 QgsAbstractRasterLayerLabeling *labeling = mLayer->labeling();
83 if ( mLayer->labelsEnabled() && labeling )
84 {
85 index = mLabelModeComboBox->findData( labeling->type() );
86 if ( QgsRasterLabelSettingsWidget *settingsWidget = qobject_cast<QgsRasterLabelSettingsWidget *>( mWidget ) )
87 {
88 settingsWidget->setLayer( mLayer );
89 settingsWidget->setLabeling( labeling );
90 }
91 }
92 else
93 {
94 index = 0;
95 }
96
97 if ( index != mLabelModeComboBox->currentIndex() )
98 {
99 mLabelModeComboBox->setCurrentIndex( index );
100 }
101}
102
104{
105 const QString mode = mLabelModeComboBox->currentData().toString();
106 if ( mode == QLatin1String( "simple" ) )
107 {
108 auto labeling = std::make_unique<QgsRasterLayerSimpleLabeling>();
109 if ( QgsRasterLabelSettingsWidget *settingsWidget = qobject_cast<QgsRasterLabelSettingsWidget *>( mWidget ) )
110 {
111 settingsWidget->updateLabeling( labeling.get() );
112 }
113 mLayer->setLabeling( labeling.release() );
114 mLayer->setLabelsEnabled( true );
115 }
116 else
117 {
118 mLayer->setLabelsEnabled( false );
119 }
120}
121
123{
126 // trigger refresh
127 mLayer->triggerRepaint();
128}
129
130void QgsRasterLabelingWidget::labelModeChanged( int index )
131{
132 if ( mWidget )
133 mStackedWidget->removeWidget( mWidget );
134
135 delete mWidget;
136 mWidget = nullptr;
137
138 if ( index < 0 )
139 return;
140
141 const QString mode = mLabelModeComboBox->currentData().toString();
142 if ( mode == QLatin1String( "simple" ) )
143 {
145 context.setMapCanvas( mMapCanvas );
146 context.setMessageBar( mMessageBar );
147
148 QgsRasterLabelSettingsWidget *settingsWidget = new QgsRasterLabelSettingsWidget( mLayer, mCanvas, this );
149 settingsWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
150 settingsWidget->setContext( context );
151
152 settingsWidget->setDockMode( dockMode() );
153 connect( settingsWidget, &QgsLabelingGui::widgetChanged, this, &QgsRasterLabelingWidget::widgetChanged );
154
155 mWidget = settingsWidget;
156 if ( !dynamic_cast<QgsRasterLayerSimpleLabeling *>( mLayer->labeling() ) )
157 {
158 auto labeling = std::make_unique<QgsRasterLayerSimpleLabeling>();
159 settingsWidget->setLabeling( labeling.get() );
160 mLayer->setLabeling( labeling.release() );
161 }
162 else
163 {
164 settingsWidget->setLabeling( mLayer->labeling() );
165 }
166
167 mStackedWidget->addWidget( mWidget );
168 mStackedWidget->setCurrentWidget( mWidget );
169 }
170
171 emit widgetChanged();
172}
173
174void QgsRasterLabelingWidget::showLabelingEngineRulesPrivate()
175{
177}
178
179void QgsRasterLabelingWidget::showEngineConfigDialogPrivate()
180{
182}
@ Raster
Raster layer.
Definition qgis.h:192
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.
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
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).
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.
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,...