QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
135 // Delegate to single diagram's syncToLayer
136 static_cast<QgsDiagramProperties *>( mWidget )->syncToLayer();
137 }
138 }
139 else // No Diagram
140 {
141 mDiagramTypeComboBox->setCurrentIndex( ModeNone );
142 }
143}
144
145void QgsDiagramWidget::mDiagramTypeComboBox_currentIndexChanged( int index )
146{
147 if ( mWidget )
148 mStackedWidget->removeWidget( mWidget );
149
150 delete mWidget;
151 mWidget = nullptr;
152
153 if ( index < 0 )
154 return;
155
156 const Mode mode = static_cast< Mode >( mDiagramTypeComboBox->currentData().toInt() );
157
158 switch ( mode )
159 {
160 case ModePie:
161 case ModeText:
162 case ModeHistogram:
163 case ModeStackedBar:
164 {
165 QgsDiagramProperties *singleWidget = new QgsDiagramProperties( mLayer, this, mMapCanvas );
166 singleWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
167 singleWidget->setDockMode( dockMode() );
168 singleWidget->syncToLayer();
169
170 if ( mode == ModePie )
172 else if ( mode == ModeText )
174 else if ( mode == ModeHistogram )
176 else if ( mode == ModeStackedBar )
178
179 connect( singleWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
182
183 mWidget = singleWidget;
184 mStackedWidget->addWidget( mWidget );
185 mStackedWidget->setCurrentWidget( mWidget );
186 break;
187 }
188 case ModeStacked:
189 {
190 QgsStackedDiagramProperties *stackedWidget = new QgsStackedDiagramProperties( mLayer, this, mCanvas );
191 stackedWidget->setDockMode( dockMode() );
192 connect( stackedWidget, &QgsPanelWidget::showPanel, this, &QgsPanelWidget::openPanel );
194
195 mWidget = stackedWidget;
196 mStackedWidget->addWidget( mWidget );
197 mStackedWidget->setCurrentWidget( mWidget );
198 break;
199 }
200 case ModeNone:
201 break;
202 }
203 emit widgetChanged();
204}
205
206void QgsDiagramWidget::showEngineConfigDialog()
207{
209 if ( panel && panel->dockMode() )
210 {
213 panel->openPanel( widget );
214 }
215 else
216 {
217 QgsLabelEngineConfigDialog dialog( mCanvas, this );
218 dialog.exec();
219 // reactivate button's window
220 activateWindow();
221 }
222}
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