QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
18#include "qgsdiagramwidget.h"
19
25#include "qgsapplication.h"
27#include "qgsguiutils.h"
30#include "qgsvectorlayer.h"
31
32#include "moc_qgsdiagramwidget.cpp"
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 }
137 else // No Diagram
138 {
139 mDiagramTypeComboBox->setCurrentIndex( ModeNone );
140 }
141}
142
143void QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged( int index )
144{
145 if ( mWidget )
146 mStackedWidget->removeWidget( mWidget );
147
148 delete mWidget;
149 mWidget = nullptr;
150
151 if ( index < 0 )
152 return;
153
154 const Mode mode = static_cast<Mode>( mDiagramTypeComboBox->currentData().toInt() );
155
156 switch ( mode )
157 {
158 case ModePie:
159 case ModeText:
160 case ModeHistogram:
161 case ModeStackedBar:
162 {
163 QgsDiagramProperties *singleWidget = new QgsDiagramProperties( mLayer, this, mMapCanvas );
164 singleWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
165 singleWidget->setDockMode( dockMode() );
166 singleWidget->syncToLayer();
167
168 if ( mode == ModePie )
170 else if ( mode == ModeText )
172 else if ( mode == ModeHistogram )
174 else if ( mode == ModeStackedBar )
176
177 connect( singleWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
180
181 mWidget = singleWidget;
182 mStackedWidget->addWidget( mWidget );
183 mStackedWidget->setCurrentWidget( mWidget );
184 break;
185 }
186 case ModeStacked:
187 {
188 QgsStackedDiagramProperties *stackedWidget = new QgsStackedDiagramProperties( mLayer, this, mCanvas );
189 stackedWidget->setDockMode( dockMode() );
190 connect( stackedWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
192
193 mWidget = stackedWidget;
194 mStackedWidget->addWidget( mWidget );
195 mStackedWidget->setCurrentWidget( mWidget );
196 break;
197 }
198 case ModeNone:
199 break;
200 }
201 emit widgetChanged();
202}
203
204void QgsDiagramWidget::showEngineConfigDialog()
205{
207 if ( panel && panel->dockMode() )
208 {
209 QgsLabelEngineConfigWidget *widget = new QgsLabelEngineConfigWidget( mCanvas );
211 panel->openPanel( widget );
212 }
213 else
214 {
215 QgsLabelEngineConfigDialog dialog( mCanvas, this );
216 dialog.exec();
217 // reactivate button's window
218 activateWindow();
219 }
220}
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.
A widget which configures diagrams for a vector 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 diagram 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
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.
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 ...
bool dockMode() const
Returns the dock mode state.
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
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.
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
A widget for editing sub diagrams.
static const QString DIAGRAM_RENDERER_NAME_STACKED
static const QString DIAGRAM_NAME_TEXT
Represents a vector layer which manages a vector based dataset.
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:6511