QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsrelationwidgetregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrelationwidgetregistry.h
3  ----------------------
4  begin : October 2020
5  copyright : (C) 2020 by Ivan Ivanov
6  email : [email protected]
7  ***************************************************************************/
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 
20 
22 {
24  addRelationWidget( factory );
25  setDefaultWidgetType( factory->type() );
26 }
27 
29 {
30  qDeleteAll( mRelationWidgetFactories );
31  mRelationWidgetFactories.clear();
32 }
33 
35 {
36  if ( !widgetFactory )
37  return;
38 
39  if ( mRelationWidgetFactories.contains( widgetFactory->type() ) )
40  return;
41 
42  mRelationWidgetFactories.insert( widgetFactory->type(), widgetFactory );
43 }
44 
45 void QgsRelationWidgetRegistry::removeRelationWidget( const QString &widgetType )
46 {
47  // protect the default relation editor widget from removing, so the user has at least one relation widget type
48  if ( widgetType == mDefaultWidgetType )
49  return;
50 
51  mRelationWidgetFactories.remove( widgetType );
52 }
53 
55 {
56  return mRelationWidgetFactories.keys();
57 }
58 
59 void QgsRelationWidgetRegistry::setDefaultWidgetType( const QString &defaultWidgetType )
60 {
61  if ( !mRelationWidgetFactories.contains( defaultWidgetType ) )
62  return;
63 
64  mDefaultWidgetType = defaultWidgetType;
65 }
66 
68 {
69  return mDefaultWidgetType;
70 }
71 
72 QMap<QString, QgsAbstractRelationEditorWidgetFactory *> QgsRelationWidgetRegistry::factories() const
73 {
74  return mRelationWidgetFactories;
75 }
76 
77 QgsAbstractRelationEditorWidget *QgsRelationWidgetRegistry::create( const QString &widgetType, const QVariantMap &config, QWidget *parent ) const
78 {
79  if ( ! mRelationWidgetFactories.contains( widgetType ) )
80  return nullptr;
81 
82  return mRelationWidgetFactories.value( widgetType )->create( config, parent );
83 }
84 
85 QgsAbstractRelationEditorConfigWidget *QgsRelationWidgetRegistry::createConfigWidget( const QString &widgetType, const QgsRelation &relation, QWidget *parent ) const
86 {
87  if ( ! mRelationWidgetFactories.contains( widgetType ) )
88  return nullptr;
89 
90  return mRelationWidgetFactories.value( widgetType )->configWidget( relation, parent );
91 }
This class should be subclassed for every configurable relation widget type.
Factory class for creating relation widgets and their corresponding config widgets.
virtual QString type() const =0
Returns the machine readable identifier name of this widget type.
Base class to build new relation widgets.
Factory class for creating a relation editor widget and the respective config widget.
QString type() const override
Returns the machine readable identifier name of this widget type.
void setDefaultWidgetType(const QString &widgetType)
Sets the default editor widget type.
QMap< QString, QgsAbstractRelationEditorWidgetFactory * > factories() const
Gets access to all registered factories.
QStringList relationWidgetNames()
Returns a list of names of registered relation widgets.
void addRelationWidget(QgsAbstractRelationEditorWidgetFactory *widgetFactory)
Adds a new registered relation widgetFactory.
QgsAbstractRelationEditorConfigWidget * createConfigWidget(const QString &widgetType, const QgsRelation &relation, QWidget *parent=nullptr) const
Creates a configuration widget.
void removeRelationWidget(const QString &widgetType)
Removes a registered relation widget with given widgetType.
QgsAbstractRelationEditorWidget * create(const QString &widgetType, const QVariantMap &config, QWidget *parent=nullptr) const
Create a relation widget of a given type for a given field.
QString defaultWidgetType() const
Returns the default editor widget type.