28#include "moc_qgslayoutchartwidget.cpp"
32 , mChartItem( chartItem )
38 mainLayout->addWidget( mItemPropertiesWidget );
41 for (
auto plotTypesIterator = plotTypes.keyValueBegin(); plotTypesIterator != plotTypes.keyValueEnd(); ++plotTypesIterator )
43 mChartTypeComboBox->addItem( plotTypesIterator->second, plotTypesIterator->first );
48 connect( mChartTypeComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsLayoutChartWidget::mChartTypeComboBox_currentIndexChanged );
49 connect( mChartPropertiesButton, &QPushButton::clicked,
this, &QgsLayoutChartWidget::mChartPropertiesButton_clicked );
50 connect( mFlipAxesCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutChartWidget::mFlipAxesCheckBox_stateChanged );
54 connect( mSortCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutChartWidget::mSortCheckBox_stateChanged );
56 connect( mSortDirectionButton, &QToolButton::clicked,
this, &QgsLayoutChartWidget::mSortDirectionButton_clicked );
58 connect( mSeriesListWidget, &QListWidget::currentItemChanged,
this, &QgsLayoutChartWidget::mSeriesListWidget_currentItemChanged );
59 connect( mSeriesListWidget, &QListWidget::itemChanged,
this, &QgsLayoutChartWidget::mSeriesListWidget_itemChanged );
60 connect( mAddSeriesPushButton, &QPushButton::clicked,
this, &QgsLayoutChartWidget::mAddSeriesPushButton_clicked );
61 connect( mRemoveSeriesPushButton, &QPushButton::clicked,
this, &QgsLayoutChartWidget::mRemoveSeriesPushButton_clicked );
62 connect( mSeriesPropertiesButton, &QPushButton::clicked,
this, &QgsLayoutChartWidget::mSeriesPropertiesButton_clicked );
64 mLinkedMapComboBox->setCurrentLayout( mChartItem->layout() );
68 connect( mFilterOnlyVisibleFeaturesCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutChartWidget::mFilterOnlyVisibleFeaturesCheckBox_stateChanged );
69 connect( mIntersectAtlasCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutChartWidget::mIntersectAtlasCheckBox_stateChanged );
71 setGuiElementValues();
79 if ( mItemPropertiesWidget )
80 mItemPropertiesWidget->setMasterLayout( masterLayout );
93 mChartItem = qobject_cast<QgsLayoutItemChart *>( item );
94 mItemPropertiesWidget->setItem( mChartItem );
101 setGuiElementValues();
106void QgsLayoutChartWidget::setGuiElementValues()
110 whileBlocking( mChartTypeComboBox )->setCurrentIndex( mChartTypeComboBox->findData( mChartItem->plot()->type() ) );
111 whileBlocking( mLayerComboBox )->setLayer( mChartItem->sourceLayer() );
116 mFlipAxesCheckBox->setEnabled(
true );
121 mFlipAxesCheckBox->setEnabled(
false );
125 whileBlocking( mSortCheckBox )->setCheckState( mChartItem->sortFeatures() ? Qt::Checked : Qt::Unchecked );
127 whileBlocking( mSortDirectionButton )->setEnabled( mChartItem->sortFeatures() );
128 whileBlocking( mSortDirectionButton )->setArrowType( mChartItem->sortAscending() ? Qt::UpArrow : Qt::DownArrow );
130 whileBlocking( mSortExpressionWidget )->setEnabled( mChartItem->sortFeatures() );
131 whileBlocking( mSortExpressionWidget )->setLayer( mChartItem->sourceLayer() );
132 whileBlocking( mSortExpressionWidget )->setField( mChartItem->sortExpression() );
134 mSeriesListWidget->clear();
135 const QList<QgsLayoutItemChart::SeriesDetails> seriesList = mChartItem->seriesList();
136 for (
const QgsLayoutItemChart::SeriesDetails &series : seriesList )
138 addSeriesListItem( series.name() );
141 if ( !seriesList.isEmpty() )
143 mSeriesListWidget->setCurrentRow( 0 );
147 mSeriesPropertiesButton->setEnabled(
false );
150 whileBlocking( mFilterOnlyVisibleFeaturesCheckBox )->setChecked( mChartItem->filterOnlyVisibleFeatures() );
151 mLinkedMapLabel->setEnabled( mFilterOnlyVisibleFeaturesCheckBox->isChecked() );
152 mLinkedMapComboBox->setEnabled( mFilterOnlyVisibleFeaturesCheckBox->isChecked() );
153 whileBlocking( mLinkedMapComboBox )->setItem( mChartItem->map() );
155 whileBlocking( mIntersectAtlasCheckBox )->setChecked( mChartItem->filterToAtlasFeature() );
159 mSeriesListWidget->clear();
163void QgsLayoutChartWidget::mChartTypeComboBox_currentIndexChanged(
int )
170 const QString plotType = mChartTypeComboBox->currentData().toString();
171 if ( mChartItem->plot()->type() == plotType )
177 Qgs2DXyPlot *newPlot2DXy =
dynamic_cast<Qgs2DXyPlot *
>( newPlot );
180 Qgs2DXyPlot *oldPlot2DXy =
dynamic_cast<Qgs2DXyPlot *
>( mChartItem->plot() );
198 mFlipAxesCheckBox->setEnabled(
true );
199 mFlipAxesCheckBox->setChecked( newPlot2DXy->
flipAxes() );
203 mFlipAxesCheckBox->setEnabled(
false );
204 mFlipAxesCheckBox->setChecked(
false );
207 mChartItem->beginCommand( tr(
"Change Chart Type" ) );
208 mChartItem->setPlot( newPlot );
209 mChartItem->update();
210 mChartItem->endCommand();
213void QgsLayoutChartWidget::mChartPropertiesButton_clicked()
222 const QString plotType = mChartTypeComboBox->currentData().toString();
224 if ( !abstractMetadata )
236 widget->
setPlot( mChartItem->plot() );
244 mChartItem->beginCommand( tr(
"Modify Chart" ) );
246 mChartItem->endCommand();
247 mChartItem->update();
253void QgsLayoutChartWidget::mFlipAxesCheckBox_stateChanged(
int state )
260 Qgs2DXyPlot *plot2DXy =
dynamic_cast<Qgs2DXyPlot *
>( mChartItem->plot() );
266 mChartItem->beginCommand( tr(
"Modify Chart" ) );
268 mChartItem->endCommand();
269 mChartItem->update();
272void QgsLayoutChartWidget::changeLayer(
QgsMapLayer *layer )
279 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
285 mChartItem->beginCommand( tr(
"Change Chart Source Layer" ) );
286 mChartItem->setSourceLayer( vl );
287 mChartItem->update();
288 mChartItem->endCommand();
289 updateButtonsState();
292void QgsLayoutChartWidget::changeSortExpression(
const QString &expression,
bool )
299 mChartItem->beginCommand( tr(
"Change Chart Source Sort Expression" ) );
300 mChartItem->setSortExpression( expression );
301 mChartItem->update();
302 mChartItem->endCommand();
305void QgsLayoutChartWidget::mSortCheckBox_stateChanged(
int state )
310 if ( state == Qt::Checked )
312 mSortDirectionButton->setEnabled(
true );
313 mSortExpressionWidget->setEnabled(
true );
317 mSortDirectionButton->setEnabled(
false );
318 mSortExpressionWidget->setEnabled(
false );
321 mChartItem->beginCommand( tr(
"Toggle Atlas Sorting" ) );
322 mChartItem->setSortFeatures( state == Qt::Checked );
323 mChartItem->update();
324 mChartItem->endCommand();
327void QgsLayoutChartWidget::mSortDirectionButton_clicked()
334 Qt::ArrowType at = mSortDirectionButton->arrowType();
335 at = ( at == Qt::UpArrow ) ? Qt::DownArrow : Qt::UpArrow;
336 mSortDirectionButton->setArrowType( at );
338 mChartItem->beginCommand( tr(
"Change Chart Source Sort Direction" ) );
339 mChartItem->setSortAscending( at == Qt::UpArrow );
340 mChartItem->update();
341 mChartItem->endCommand();
344QListWidgetItem *QgsLayoutChartWidget::addSeriesListItem(
const QString &name )
346 QListWidgetItem *item =
new QListWidgetItem( name,
nullptr );
347 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
348 mSeriesListWidget->addItem( item );
352void QgsLayoutChartWidget::mSeriesListWidget_currentItemChanged( QListWidgetItem *current, QListWidgetItem * )
354 mSeriesPropertiesButton->setEnabled(
static_cast<bool>( current ) );
357void QgsLayoutChartWidget::mSeriesListWidget_itemChanged( QListWidgetItem *item )
364 QList<QgsLayoutItemChart::SeriesDetails> seriesList = mChartItem->seriesList();
365 const int idx = mSeriesListWidget->row( item );
366 if ( idx >= seriesList.size() )
371 mChartItem->beginCommand( tr(
"Rename Chart Series" ) );
372 seriesList[idx].setName( item->text() );
373 mChartItem->setSeriesList( seriesList );
374 mChartItem->endCommand();
377void QgsLayoutChartWidget::mAddSeriesPushButton_clicked()
384 QList<QgsLayoutItemChart::SeriesDetails> seriesList = mChartItem->seriesList();
385 const QString itemName = tr(
"Series %1" ).arg( seriesList.size() + 1 );
386 addSeriesListItem( itemName );
388 mChartItem->beginCommand( tr(
"Add Chart Series" ) );
389 seriesList << QgsLayoutItemChart::SeriesDetails( itemName );
390 mChartItem->setSeriesList( seriesList );
391 mChartItem->endCommand();
392 mChartItem->update();
394 mSeriesListWidget->setCurrentRow( mSeriesListWidget->count() - 1 );
395 mSeriesListWidget_currentItemChanged( mSeriesListWidget->currentItem(),
nullptr );
396 updateButtonsState();
399void QgsLayoutChartWidget::mRemoveSeriesPushButton_clicked()
401 QListWidgetItem *item = mSeriesListWidget->currentItem();
402 if ( !item || !mChartItem )
407 QList<QgsLayoutItemChart::SeriesDetails> seriesList = mChartItem->seriesList();
408 const int idx = mSeriesListWidget->row( item );
409 if ( idx >= seriesList.size() )
414 QListWidgetItem *deletedItem = mSeriesListWidget->takeItem( mSeriesListWidget->row( item ) );
417 mChartItem->beginCommand( tr(
"Remove Chart Series" ) );
418 seriesList.removeAt( idx );
419 mChartItem->setSeriesList( seriesList );
420 mChartItem->endCommand();
421 mChartItem->update();
422 updateButtonsState();
425void QgsLayoutChartWidget::mSeriesPropertiesButton_clicked()
427 QListWidgetItem *item = mSeriesListWidget->currentItem();
428 if ( !item || !mChartItem )
433 QList<QgsLayoutItemChart::SeriesDetails> seriesList = mChartItem->seriesList();
434 const int idx = mSeriesListWidget->row( item );
435 if ( idx >= seriesList.size() )
440 QgsLayoutChartSeriesDetailsWidget *widget =
new QgsLayoutChartSeriesDetailsWidget( mChartItem->sourceLayer(), idx, seriesList[idx],
this );
449 QList<QgsLayoutItemChart::SeriesDetails> seriesList = mChartItem->seriesList();
450 const int idx = widget->
index();
451 if ( idx >= seriesList.size() )
456 mChartItem->beginCommand( tr(
"Modify Chart Series" ) );
457 seriesList[idx].setXExpression( widget->
xExpression() );
458 seriesList[idx].setYExpression( widget->
yExpression() );
460 mChartItem->setSeriesList( seriesList );
461 mChartItem->endCommand();
462 mChartItem->update();
468void QgsLayoutChartWidget::mLinkedMapComboBox_itemChanged(
QgsLayoutItem *item )
475 mChartItem->beginCommand( tr(
"Change Chart Map Item" ) );
476 mChartItem->setMap( qobject_cast<QgsLayoutItemMap *>( item ) );
477 mChartItem->endCommand();
478 mChartItem->update();
481void QgsLayoutChartWidget::mFilterOnlyVisibleFeaturesCheckBox_stateChanged(
int state )
488 mChartItem->beginCommand( tr(
"Toggle Visible Features Only Filter" ) );
489 const bool useOnlyVisibleFeatures = ( state == Qt::Checked );
490 mChartItem->setFilterOnlyVisibleFeatures( useOnlyVisibleFeatures );
491 mChartItem->endCommand();
492 mChartItem->update();
495 mLinkedMapComboBox->setEnabled( state == Qt::Checked );
496 mLinkedMapLabel->setEnabled( state == Qt::Checked );
499void QgsLayoutChartWidget::mIntersectAtlasCheckBox_stateChanged(
int state )
506 mChartItem->beginCommand( tr(
"Toggle Chart Atlas Filter" ) );
507 const bool filterToAtlas = ( state == Qt::Checked );
508 mChartItem->setFilterToAtlasFeature( filterToAtlas );
509 mChartItem->endCommand();
510 mChartItem->update();
513void QgsLayoutChartWidget::updateButtonsState()
520 const bool enable = qobject_cast<QgsVectorLayer *>( mLayerComboBox->currentLayer() ) !=
nullptr;
521 mSortCheckBox->setEnabled( enable );
522 mAddSeriesPushButton->setEnabled( enable );
523 mRemoveSeriesPushButton->setEnabled( mSeriesListWidget->count() > 0 );
Base class for 2-dimensional plot/chart/graphs with an X and Y axes.
void setXMaximum(double maximum)
Sets the maximum value of the x axis.
double yMaximum() const
Returns the maximum value of the y axis.
void setYMinimum(double minimum)
Sets the minimum value of the y axis.
double xMinimum() const
Returns the minimum value of the x axis.
void setFlipAxes(bool flipAxes)
Sets whether the X and Y axes are flipped.
QgsPlotAxis & yAxis()
Returns a reference to the plot's y axis.
bool flipAxes() const
Returns whether the X and Y axes are flipped.
void setXMinimum(double minimum)
Sets the minimum value of the x axis.
QgsPlotAxis & xAxis()
Returns a reference to the plot's x axis.
double yMinimum() const
Returns the minimum value of the y axis.
void setYMaximum(double maximum)
Sets the maximum value of the y axis.
double xMaximum() const
Returns the maximum value of the x axis.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
static QgsPlotRegistry * plotRegistry()
Returns the application's plot registry, used for plot types.
static void initPlotWidgets()
Initializes plot widgets.
A layout item subclass that renders chart plots.
void itemChanged(QgsLayoutItem *item)
Emitted whenever the currently selected item changes.
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
void changed()
Emitted when the object's properties change.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
Base class for all map layer types.
Interface for master layout type objects, such as print layouts and reports.
double gridIntervalMinor() const
Returns the interval of minor grid lines for the axis.
double gridIntervalMajor() const
Returns the interval of major grid lines for the axis.
void setType(Qgis::PlotAxisType type)
Sets the axis type.
void setGridIntervalMajor(double interval)
Sets the interval of major grid lines for the axis.
void setGridIntervalMinor(double interval)
Sets the interval of minor grid lines for the axis.
void setLabelInterval(double interval)
Sets the interval of labels for the axis.
Qgis::PlotAxisType type() const
Returns the axis type.
double labelInterval() const
Returns the interval of labels for the axis.
QgsPlot * createPlot(const QString &type) const
Creates a new instance of a plot given the type.
QMap< QString, QString > plotTypes() const
Returns a map of available plot types to translated name.
QgsPlotAbstractMetadata * plotMetadata(const QString &type) const
Returns the metadata for the specified plot type.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.