QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsdiagramwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdiagramwidget.h
3 Container widget for diagram layers
4 -------------------
5 begin : September 2024
6 copyright : (C) Germán Carrillo
7 email : german at opengis dot ch
8
9 ***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
23
24#include "qgsdiagramwidget.h"
25#include "moc_qgsdiagramwidget.cpp"
26#include "qgsvectorlayer.h"
27#include "qgsapplication.h"
28#include "qgsguiutils.h"
32
33
35 : QgsMapLayerConfigWidget( layer, canvas, parent )
36 , mLayer( layer )
37 , mCanvas( canvas )
38{
39 if ( !layer )
40 {
41 return;
42 }
43
44 setupUi( this );
45
46 // Initialize stacked diagram controls
47 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "diagramNone.svg" ) ), tr( "No Diagrams" ), ModeNone );
48 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "pie-chart.svg" ) ), tr( "Pie Chart" ), ModePie );
49 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "text.svg" ) ), tr( "Text Diagram" ), ModeText );
50 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "histogram.svg" ) ), tr( "Histogram" ), ModeHistogram );
51 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "stacked-bar.svg" ) ), tr( "Stacked Bars" ), ModeStackedBar );
52 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "stacked-diagram.svg" ) ), tr( "Stacked Diagram" ), ModeStacked );
53
54 connect( mEngineSettingsButton, &QAbstractButton::clicked, this, &QgsDiagramWidget::showEngineConfigDialog );
55
56 connect( mDiagramTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged );
57
58 const int iconSize16 = QgsGuiUtils::scaleIconSize( 16 );
59 mEngineSettingsButton->setIconSize( QSize( iconSize16, iconSize16 ) );
60}
61
63{
64 const Mode mode = static_cast< Mode >( mDiagramTypeComboBox->currentData().toInt() );
65
66 switch ( mode )
67 {
68 case ModeStacked:
69 {
70 // Delegate to stacked diagram's apply
71 static_cast<QgsStackedDiagramProperties *>( mWidget )->apply();
72 break;
73 }
74 case ModePie:
75 case ModeText:
76 case ModeHistogram:
77 case ModeStackedBar:
78 {
79 // Delegate to single diagram's apply
80 static_cast<QgsDiagramProperties *>( mWidget )->apply();
81 break;
82 }
83 case ModeNone:
84 {
85 mLayer->setDiagramRenderer( nullptr );
86
88 mLayer->setDiagramLayerSettings( dls );
89
90 // refresh
92 mLayer->triggerRepaint();
93 }
94 }
95}
96
98{
99 if ( !mLayer )
100 {
101 return;
102 }
103
104 whileBlocking( mDiagramTypeComboBox )->setCurrentIndex( -1 );
105
106 const QgsDiagramRenderer *dr = mLayer->diagramRenderer();
107
108 // pick the right mode from the layer
109 if ( dr && dr->diagram() )
110 {
112 {
113 mDiagramTypeComboBox->setCurrentIndex( ModeStacked );
114 }
115 else // Single diagram
116 {
117 const QString diagramName = dr->diagram()->diagramName();
118 if ( diagramName == QgsPieDiagram::DIAGRAM_NAME_PIE )
119 {
120 mDiagramTypeComboBox->setCurrentIndex( ModePie ) ;
121 }
122 else if ( diagramName == QgsTextDiagram::DIAGRAM_NAME_TEXT )
123 {
124 mDiagramTypeComboBox->setCurrentIndex( ModeText ) ;
125 }
126 else if ( diagramName == QgsStackedBarDiagram::DIAGRAM_NAME_STACKED_BAR )
127 {
128 mDiagramTypeComboBox->setCurrentIndex( ModeStackedBar ) ;
129 }
130 else // diagramName == QgsHistogramDiagram::DIAGRAM_NAME_HISTOGRAM
131 {
132 // Play safe and set to histogram by default if the diagram name is unknown
133 mDiagramTypeComboBox->setCurrentIndex( ModeHistogram );
134 }
135
136 // Delegate to single diagram's syncToLayer
137 static_cast<QgsDiagramProperties *>( mWidget )->syncToLayer();
138 }
139 }
140 else // No Diagram
141 {
142 mDiagramTypeComboBox->setCurrentIndex( ModeNone );
143 }
144}
145
146void QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged( int index )
147{
148 if ( mWidget )
149 mStackedWidget->removeWidget( mWidget );
150
151 delete mWidget;
152 mWidget = nullptr;
153
154 if ( index < 0 )
155 return;
156
157 const Mode mode = static_cast< Mode >( mDiagramTypeComboBox->currentData().toInt() );
158
159 switch ( mode )
160 {
161 case ModePie:
162 case ModeText:
163 case ModeHistogram:
164 case ModeStackedBar:
165 {
166 QgsDiagramProperties *singleWidget = new QgsDiagramProperties( mLayer, this, mMapCanvas );
167 singleWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
168 singleWidget->setDockMode( dockMode() );
169 singleWidget->syncToLayer();
170
171 if ( mode == ModePie )
173 else if ( mode == ModeText )
175 else if ( mode == ModeHistogram )
177 else if ( mode == ModeStackedBar )
179
180 connect( singleWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
183
184 mWidget = singleWidget;
185 mStackedWidget->addWidget( mWidget );
186 mStackedWidget->setCurrentWidget( mWidget );
187 break;
188 }
189 case ModeStacked:
190 {
191 QgsStackedDiagramProperties *stackedWidget = new QgsStackedDiagramProperties( mLayer, this, mCanvas );
192 stackedWidget->setDockMode( dockMode() );
193 connect( stackedWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
195
196 mWidget = stackedWidget;
197 mStackedWidget->addWidget( mWidget );
198 mStackedWidget->setCurrentWidget( mWidget );
199 break;
200 }
201 case ModeNone:
202 break;
203 }
204 emit widgetChanged();
205}
206
207void QgsDiagramWidget::showEngineConfigDialog()
208{
210 if ( panel && panel->dockMode() )
211 {
214 panel->openPanel( widget );
215 }
216 else
217 {
218 QgsLabelEngineConfigDialog dialog( mCanvas, this );
219 dialog.exec();
220 // reactivate button's window
221 activateWindow();
222 }
223}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Stores the settings for rendering of all diagrams for a layer.
void setDockMode(bool dockMode) override
Sets the widget in dock mode.
void syncToLayer()
Updates the widget to reflect the layer's current diagram settings.
void setDiagramType(const QString diagramType)
Defines the widget's diagram type and lets it know it should hide the type comboBox.
Evaluates and returns the diagram settings relating to a diagram for a specific feature.
virtual QString rendererName() const =0
QgsDiagram * diagram() const
void syncToOwnLayer()
Updates the widget to reflect the layer's current diagram settings.
void apply() override
Saves the labeling configuration and immediately updates the map canvas to reflect the changes.
QgsDiagramWidget(QgsVectorLayer *layer, QgsMapCanvas *canvas, QWidget *parent=nullptr)
constructor
void auxiliaryFieldCreated()
Emitted when an auxiliary field is created.
virtual QString diagramName() const =0
Gets a descriptive name for this diagram type.
static const QString DIAGRAM_NAME_HISTOGRAM
Dialog for configuring the labeling engine.
Widget for configuring the labeling engine.
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.
virtual void syncToLayer(QgsMapLayer *layer)
Reset to original (vector layer) values.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
Base class for any widget that can be shown as a inline panel.
void showPanel(QgsPanelWidget *panel)
Emit when you require a panel to be show in the interface.
void openPanel(QgsPanelWidget *panel)
Open a panel or dialog depending on dock mode setting If dock mode is true this method will emit the ...
void widgetChanged()
Emitted when the widget state changes.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
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 const QString DIAGRAM_NAME_PIE
static QgsProject * instance()
Returns the QgsProject singleton instance.
void setDirty(bool b=true)
Flag the project as dirty (modified).
static const QString DIAGRAM_NAME_STACKED_BAR
static const QString DIAGRAM_RENDERER_NAME_STACKED
static const QString DIAGRAM_NAME_TEXT
Represents a vector layer which manages a vector based data sets.
void setDiagramLayerSettings(const QgsDiagramLayerSettings &s)
void setDiagramRenderer(QgsDiagramRenderer *r)
Sets diagram rendering object (takes ownership)
const QgsDiagramRenderer * diagramRenderer() const
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5821