QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
33
34#include "moc_qgsdiagramwidget.cpp"
35
36using namespace Qt::StringLiterals;
37
39 : QgsMapLayerConfigWidget( layer, canvas, parent )
40 , mLayer( layer )
41 , mCanvas( canvas )
42{
43 if ( !layer )
44 {
45 return;
46 }
47
48 setupUi( this );
49
50 // Initialize stacked diagram controls
51 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( u"diagramNone.svg"_s ), tr( "No Diagrams" ), ModeNone );
52 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( u"pie-chart.svg"_s ), tr( "Pie Chart" ), ModePie );
53 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( u"text.svg"_s ), tr( "Text Diagram" ), ModeText );
54 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( u"histogram.svg"_s ), tr( "Histogram" ), ModeHistogram );
55 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( u"stacked-bar.svg"_s ), tr( "Stacked Bars" ), ModeStackedBar );
56 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( u"stacked-diagram.svg"_s ), tr( "Stacked Diagram" ), ModeStacked );
57
58 connect( mEngineSettingsButton, &QAbstractButton::clicked, this, &QgsDiagramWidget::showEngineConfigDialog );
59
60 connect( mDiagramTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged );
61
62 const int iconSize16 = QgsGuiUtils::scaleIconSize( 16 );
63 mEngineSettingsButton->setIconSize( QSize( iconSize16, iconSize16 ) );
64}
65
67{
68 const Mode mode = static_cast<Mode>( mDiagramTypeComboBox->currentData().toInt() );
69
70 switch ( mode )
71 {
72 case ModeStacked:
73 {
74 // Delegate to stacked diagram's apply
75 static_cast<QgsStackedDiagramProperties *>( mWidget )->apply();
76 break;
77 }
78 case ModePie:
79 case ModeText:
80 case ModeHistogram:
81 case ModeStackedBar:
82 {
83 // Delegate to single diagram's apply
84 static_cast<QgsDiagramProperties *>( mWidget )->apply();
85 break;
86 }
87 case ModeNone:
88 {
89 mLayer->setDiagramRenderer( nullptr );
90
92 mLayer->setDiagramLayerSettings( dls );
93
94 // refresh
96 mLayer->triggerRepaint();
97 }
98 }
99}
100
102{
103 if ( !mLayer )
104 {
105 return;
106 }
107
108 whileBlocking( mDiagramTypeComboBox )->setCurrentIndex( -1 );
109
110 const QgsDiagramRenderer *dr = mLayer->diagramRenderer();
111
112 // pick the right mode from the layer
113 if ( dr && dr->diagram() )
114 {
116 {
117 mDiagramTypeComboBox->setCurrentIndex( ModeStacked );
118 }
119 else // Single diagram
120 {
121 const QString diagramName = dr->diagram()->diagramName();
122 if ( diagramName == QgsPieDiagram::DIAGRAM_NAME_PIE )
123 {
124 mDiagramTypeComboBox->setCurrentIndex( ModePie );
125 }
126 else if ( diagramName == QgsTextDiagram::DIAGRAM_NAME_TEXT )
127 {
128 mDiagramTypeComboBox->setCurrentIndex( ModeText );
129 }
130 else if ( diagramName == QgsStackedBarDiagram::DIAGRAM_NAME_STACKED_BAR )
131 {
132 mDiagramTypeComboBox->setCurrentIndex( ModeStackedBar );
133 }
134 else // diagramName == QgsHistogramDiagram::DIAGRAM_NAME_HISTOGRAM
135 {
136 // Play safe and set to histogram by default if the diagram name is unknown
137 mDiagramTypeComboBox->setCurrentIndex( ModeHistogram );
138 }
139 }
140 }
141 else // No Diagram
142 {
143 mDiagramTypeComboBox->setCurrentIndex( ModeNone );
144 }
145}
146
147void QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged( int index )
148{
149 if ( mWidget )
150 mStackedWidget->removeWidget( mWidget );
151
152 delete mWidget;
153 mWidget = nullptr;
154
155 if ( index < 0 )
156 return;
157
158 const Mode mode = static_cast<Mode>( mDiagramTypeComboBox->currentData().toInt() );
159
160 switch ( mode )
161 {
162 case ModePie:
163 case ModeText:
164 case ModeHistogram:
165 case ModeStackedBar:
166 {
167 QgsDiagramProperties *singleWidget = new QgsDiagramProperties( mLayer, this, mMapCanvas );
168 singleWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
169 singleWidget->setDockMode( dockMode() );
170 singleWidget->syncToLayer();
171
172 if ( mode == ModePie )
174 else if ( mode == ModeText )
176 else if ( mode == ModeHistogram )
178 else if ( mode == ModeStackedBar )
180
181 connect( singleWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
184
185 mWidget = singleWidget;
186 mStackedWidget->addWidget( mWidget );
187 mStackedWidget->setCurrentWidget( mWidget );
188 break;
189 }
190 case ModeStacked:
191 {
192 QgsStackedDiagramProperties *stackedWidget = new QgsStackedDiagramProperties( mLayer, this, mCanvas );
193 stackedWidget->setDockMode( dockMode() );
194 connect( stackedWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
196
197 mWidget = stackedWidget;
198 mStackedWidget->addWidget( mWidget );
199 mStackedWidget->setCurrentWidget( mWidget );
200 break;
201 }
202 case ModeNone:
203 break;
204 }
205 emit widgetChanged();
206}
207
208void QgsDiagramWidget::showEngineConfigDialog()
209{
211 if ( panel && panel->dockMode() )
212 {
213 QgsLabelEngineConfigWidget *widget = new QgsLabelEngineConfigWidget( mCanvas );
215 panel->openPanel( widget );
216 }
217 else
218 {
219 QgsLabelEngineConfigDialog dialog( mCanvas, this );
220 dialog.exec();
221 // reactivate button's window
222 activateWindow();
223 }
224}
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:6804