QGIS API Documentation 3.39.0-Master (0c9320c8adc)
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 "qgsvectorlayer.h"
26#include "qgsapplication.h"
27#include "qgsguiutils.h"
31
32
34 : QgsMapLayerConfigWidget( layer, canvas, parent )
35 , mLayer( layer )
36 , mCanvas( canvas )
37{
38 if ( !layer )
39 {
40 return;
41 }
42
43 setupUi( this );
44
45 // Initialize stacked diagram controls
46 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "diagramNone.svg" ) ), tr( "No Diagrams" ), ModeNone );
47 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "pie-chart.svg" ) ), tr( "Pie Chart" ), ModePie );
48 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "text.svg" ) ), tr( "Text Diagram" ), ModeText );
49 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "histogram.svg" ) ), tr( "Histogram" ), ModeHistogram );
50 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "stacked-bar.svg" ) ), tr( "Stacked Bars" ), ModeStackedBar );
51 mDiagramTypeComboBox->addItem( QgsApplication::getThemeIcon( QStringLiteral( "stacked-diagram.svg" ) ), tr( "Stacked Diagram" ), ModeStacked );
52
53 connect( mEngineSettingsButton, &QAbstractButton::clicked, this, &QgsDiagramWidget::showEngineConfigDialog );
54
55 connect( mDiagramTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged );
56
57 const int iconSize16 = QgsGuiUtils::scaleIconSize( 16 );
58 mEngineSettingsButton->setIconSize( QSize( iconSize16, iconSize16 ) );
59}
60
62{
63 const Mode mode = static_cast< Mode >( mDiagramTypeComboBox->currentData().toInt() );
64
65 switch ( mode )
66 {
67 case ModeStacked:
68 {
69 // Delegate to stacked diagram's apply
70 static_cast<QgsStackedDiagramProperties *>( mWidget )->apply();
71 break;
72 }
73 case ModePie:
74 case ModeText:
75 case ModeHistogram:
76 case ModeStackedBar:
77 {
78 // Delegate to single diagram's apply
79 static_cast<QgsDiagramProperties *>( mWidget )->apply();
80 break;
81 }
82 case ModeNone:
83 {
84 mLayer->setDiagramRenderer( nullptr );
85
87 mLayer->setDiagramLayerSettings( dls );
88
89 // refresh
91 mLayer->triggerRepaint();
92 }
93 }
94}
95
97{
98 if ( !mLayer )
99 {
100 return;
101 }
102
103 whileBlocking( mDiagramTypeComboBox )->setCurrentIndex( -1 );
104
105 const QgsDiagramRenderer *dr = mLayer->diagramRenderer();
106
107 // pick the right mode from the layer
108 if ( dr && dr->diagram() )
109 {
111 {
112 mDiagramTypeComboBox->setCurrentIndex( ModeStacked );
113 }
114 else // Single diagram
115 {
116 const QString diagramName = dr->diagram()->diagramName();
117 if ( diagramName == QgsPieDiagram::DIAGRAM_NAME_PIE )
118 {
119 mDiagramTypeComboBox->setCurrentIndex( ModePie ) ;
120 }
121 else if ( diagramName == QgsTextDiagram::DIAGRAM_NAME_TEXT )
122 {
123 mDiagramTypeComboBox->setCurrentIndex( ModeText ) ;
124 }
125 else if ( diagramName == QgsStackedBarDiagram::DIAGRAM_NAME_STACKED_BAR )
126 {
127 mDiagramTypeComboBox->setCurrentIndex( ModeStackedBar ) ;
128 }
129 else // diagramName == QgsHistogramDiagram::DIAGRAM_NAME_HISTOGRAM
130 {
131 // Play safe and set to histogram by default if the diagram name is unknown
132 mDiagramTypeComboBox->setCurrentIndex( ModeHistogram );
133 }
134 // TODO: if we get a stacked diagram, take the first subdiagram,
135 // Set its diagram type and sync to its settings
136
137 // Delegate to single diagram's syncToLayer
138 static_cast<QgsDiagramProperties *>( mWidget )->syncToLayer();
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 {
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.
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