QGIS API Documentation 3.99.0-Master (357b655ed83)
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}
35
37{
38 qDeleteAll( mMetadata );
39}
40
42{
43 if ( !mMetadata.isEmpty() )
44 return false;
45
46 addPlotType( new QgsPlotMetadata( "bar"_L1, QObject::tr( "Bar chart" ), QgsBarChartPlot::create, QgsBarChartPlot::createDataGatherer ) );
47 addPlotType( new QgsPlotMetadata( "line"_L1, QObject::tr( "Line chart" ), QgsLineChartPlot::create, QgsLineChartPlot::createDataGatherer ) );
48 addPlotType( new QgsPlotMetadata( "pie"_L1, QObject::tr( "Pie chart" ), QgsPieChartPlot::create, QgsPieChartPlot::createDataGatherer ) );
49
50 return true;
51}
52
54{
55 return mMetadata.value( type );
56}
57
59{
60 if ( !metadata || mMetadata.contains( metadata->type() ) )
61 return false;
62
63 mMetadata[metadata->type()] = metadata;
64 emit plotAdded( metadata->type(), metadata->visibleName() );
65 return true;
66}
67
68bool QgsPlotRegistry::removePlotType( const QString &type )
69{
70 if ( !mMetadata.contains( type ) )
71 return false;
72
73 emit plotAboutToBeRemoved( type );
74 delete mMetadata.take( type );
75 return true;
76}
77
78QgsPlot *QgsPlotRegistry::createPlot( const QString &type ) const
79{
80 if ( !mMetadata.contains( type ) )
81 return nullptr;
82
83 return mMetadata[type]->createPlot();
84}
85
86QMap<QString, QString> QgsPlotRegistry::plotTypes() const
87{
88 QMap<QString, QString> types;
89 for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
90 {
91 types.insert( it.key(), it.value()->visibleName() );
92 }
93
94 return types;
95}
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