QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsplotregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsplotregistry.cpp
3 ------------------------
4 begin : June 2025
5 copyright : (C) 2025 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsconfig.h"
18#include "qgsplotregistry.h"
19
20#include "qgsbarchartplot.h"
21#include "qgslinechartplot.h"
22#include "qgspiechartplot.h"
23#include "qgsplot.h"
24
25#include <QString>
26
27#include "moc_qgsplotregistry.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QObject( parent )
33{}
34
36{
37 qDeleteAll( mMetadata );
38}
39
41{
42 if ( !mMetadata.isEmpty() )
43 return false;
44
45 addPlotType( new QgsPlotMetadata( "bar"_L1, QObject::tr( "Bar chart" ), QgsBarChartPlot::create, QgsBarChartPlot::createDataGatherer ) );
46 addPlotType( new QgsPlotMetadata( "line"_L1, QObject::tr( "Line chart" ), QgsLineChartPlot::create, QgsLineChartPlot::createDataGatherer ) );
47 addPlotType( new QgsPlotMetadata( "pie"_L1, QObject::tr( "Pie chart" ), QgsPieChartPlot::create, QgsPieChartPlot::createDataGatherer ) );
48
49 return true;
50}
51
53{
54 return mMetadata.value( type );
55}
56
58{
59 if ( !metadata || mMetadata.contains( metadata->type() ) )
60 return false;
61
62 mMetadata[metadata->type()] = metadata;
63 emit plotAdded( metadata->type(), metadata->visibleName() );
64 return true;
65}
66
67bool QgsPlotRegistry::removePlotType( const QString &type )
68{
69 if ( !mMetadata.contains( type ) )
70 return false;
71
72 emit plotAboutToBeRemoved( type );
73 delete mMetadata.take( type );
74 return true;
75}
76
77QgsPlot *QgsPlotRegistry::createPlot( const QString &type ) const
78{
79 if ( !mMetadata.contains( type ) )
80 return nullptr;
81
82 return mMetadata[type]->createPlot();
83}
84
85QMap<QString, QString> QgsPlotRegistry::plotTypes() const
86{
87 QMap<QString, QString> types;
88 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
89 {
90 types.insert( it.key(), it.value()->visibleName() );
91 }
92
93 return types;
94}
static QgsVectorLayerAbstractPlotDataGatherer * createDataGatherer(QgsPlot *plot)
Returns a new data gatherer for a given bar chart plot.
static QgsBarChartPlot * create()
Returns a new bar chart.
static QgsVectorLayerAbstractPlotDataGatherer * createDataGatherer(QgsPlot *plot)
Returns a new data gatherer for a given line chart plot.
static QgsLineChartPlot * create()
Returns a new line chart.
static QgsPieChartPlot * create()
Returns a new pie chart.
static QgsVectorLayerAbstractPlotDataGatherer * createDataGatherer(QgsPlot *plot)
Returns a new data gatherer for a given pie chart plot.
Stores metadata about a plot class.
QString visibleName() const
Returns a translated, user visible name for the plot class.
QString type() const
Returns the unique type code for the plot class.
Convenience metadata class that uses static functions to create plots.
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.
bool removePlotType(const QString &type)
Removes a new a plot type from the registry.
QgsPlotRegistry(QObject *parent=nullptr)
Creates a new empty plot registry.
bool addPlotType(QgsPlotAbstractMetadata *metadata)
Registers a new plot type.
void plotAdded(const QString &type, const QString &name)
Emitted whenever a new plot type is added to the registry, with the specified type and visible name.
bool populate()
Populates the registry with standard plot types.
~QgsPlotRegistry() override
QgsPlotAbstractMetadata * plotMetadata(const QString &type) const
Returns the metadata for the specified plot type.
void plotAboutToBeRemoved(const QString &type)
Emitted whenever a plot type is about to be remove from the registry, with the specified type and vis...
Base class for plot/chart/graphs.
Definition qgsplot.h:48