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