Skip to content

Commit

Permalink
add registry of settings editors wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Apr 23, 2023
1 parent effbfc2 commit 77cc02f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/gui/qgsgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
#include "qgssensorguiregistry.h"
#include "qgshistoryentry.h"

#include "qgssettingseditorwidgetregistry.h"


#include <QPushButton>
#include <QToolButton>

Expand Down Expand Up @@ -182,6 +185,11 @@ QgsHistoryProviderRegistry *QgsGui::historyProviderRegistry()
return instance()->mHistoryProviderRegistry;
}

QgsSettingsEditorWidgetRegistry *QgsGui::settingsEditorWidgetRegistry()
{
return instance()->mSettingsEditorRegistry;
}

void QgsGui::enableAutoGeometryRestore( QWidget *widget, const QString &key )
{
if ( widget->objectName().isEmpty() )
Expand Down Expand Up @@ -238,6 +246,7 @@ QgsGui::~QgsGui()
delete mRelationEditorRegistry;
delete mSettingsRegistryGui;
delete mSensorGuiRegistry;
delete mSettingsEditorRegistry;
}

QColor QgsGui::sampleColor( QPoint point )
Expand Down Expand Up @@ -288,6 +297,8 @@ QgsGui::QgsGui()

mSettingsRegistryGui = new QgsSettingsRegistryGui();

mSettingsEditorRegistry = new QgsSettingsEditorWidgetRegistry();

mCodeEditorColorSchemeRegistry = new QgsCodeEditorColorSchemeRegistry();

// provider gui registry initialize QgsProviderRegistry too
Expand Down
9 changes: 8 additions & 1 deletion src/gui/qgsgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class QgsRelationWidgetRegistry;
class QgsMapToolShapeRegistry;
class QgsHistoryProviderRegistry;
class QgsSensorGuiRegistry;

class QgsSettingsEditorWidgetRegistry;

/**
* \ingroup gui
Expand Down Expand Up @@ -213,6 +213,12 @@ class GUI_EXPORT QgsGui : public QObject
*/
static QgsHistoryProviderRegistry *historyProviderRegistry() SIP_KEEPREFERENCE;

/**
* Returns the registry of settings editors.
* \since QGIS 3.32
*/
static QgsSettingsEditorWidgetRegistry *settingsEditorWidgetRegistry() SIP_KEEPREFERENCE;

/**
* Register the widget to allow its position to be automatically saved and restored when open and closed.
* Use this to avoid needing to call saveGeometry() and restoreGeometry() on your widget.
Expand Down Expand Up @@ -322,6 +328,7 @@ class GUI_EXPORT QgsGui : public QObject
QgsMapToolShapeRegistry *mShapeMapToolRegistry = nullptr;
QgsHistoryProviderRegistry *mHistoryProviderRegistry = nullptr;
QgsSensorGuiRegistry *mSensorGuiRegistry = nullptr;
QgsSettingsEditorWidgetRegistry *mSettingsEditorRegistry = nullptr;
std::unique_ptr< QgsWindowManagerInterface > mWindowManager;

#ifdef SIP_RUN
Expand Down
72 changes: 72 additions & 0 deletions src/gui/settings/qgssettingseditorwidgetregistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/***************************************************************************
qgssettingsfactoryfactoryregistry.cpp
---------------------
begin : April 2023
copyright : (C) 2023 by Denis Rouzaud
email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgssettingseditorwidgetregistry.h"

#include "qgslogger.h"
#include "qgssettingseditorwidgetwrapper.h"
#include "qgssettingseditorwidgetwrapperimpl.h"
#include "qgssettingsentry.h"

QgsSettingsEditorWidgetRegistry::QgsSettingsEditorWidgetRegistry()
{
addWrapper( new QgsSettingsStringEditorWidgetWrapper() );
addWrapper( new QgsSettingsBoolEditorWidgetWrapper() );
addWrapper( new QgsSettingsIntegerEditorWidgetWrapper() );
addWrapper( new QgsSettingsDoubleEditorWidgetWrapper() );
addWrapper( new QgsSettingsColorEditorWidgetWrapper() );

}

QgsSettingsEditorWidgetRegistry::~QgsSettingsEditorWidgetRegistry()
{
qDeleteAll( mWrappers );
}

bool QgsSettingsEditorWidgetRegistry::addWrapper( QgsSettingsEditorWidgetWrapper *wrapper )
{
if ( mWrappers.contains( wrapper->id() ) )
return false;

mWrappers.insert( wrapper->id(), wrapper );
return true;
}

QgsSettingsEditorWidgetWrapper *QgsSettingsEditorWidgetRegistry::createWrapper( const QString &id, QObject *parent ) const
{
QgsSettingsEditorWidgetWrapper *wrapper = mWrappers.value( id );
if ( wrapper )
{
return wrapper->createWrapper( parent );
}
else
{
QgsDebugMsg( QStringLiteral( "Setting factory was not found for '%1', returning the default string factory" ).arg( id ) );
return nullptr;
}
}

QWidget *QgsSettingsEditorWidgetRegistry::createEditor( const QgsSettingsEntryBase *setting, const QStringList &dynamicKeyPartList, QWidget *parent ) const
{
QgsSettingsEditorWidgetWrapper *eww = createWrapper( setting->typeId(), parent );
if ( eww )
return eww->createEditor( setting, dynamicKeyPartList, parent );
else
return nullptr;
}




62 changes: 62 additions & 0 deletions src/gui/settings/qgssettingseditorwidgetregistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/***************************************************************************
qgssettingseditorwidgetregistry.h
---------------------
begin : April 2023
copyright : (C) 2023 by Denis Rouzaud
email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSSETTINGSEDITORREGISTRY_H
#define QGSSETTINGSEDITORREGISTRY_H

#include <QObject>
#include <QMap>

#include "qgis_gui.h"
#include "qgis_sip.h"

class QWidget;
class QgsSettingsEntryBase;
class QgsSettingsEditorWidgetWrapper;

/**
* \ingroup gui
* \brief This class manages editors for settings
*
* \since QGIS 3.32
*/
class GUI_EXPORT QgsSettingsEditorWidgetRegistry
{
public:
//! Constructor
QgsSettingsEditorWidgetRegistry();
~QgsSettingsEditorWidgetRegistry();

/**
* Adds a editor to the registry
* Returns FALSE if a editor with same id already exists.
*/
bool addWrapper( QgsSettingsEditorWidgetWrapper *wrapper SIP_TRANSFER );

//! Returns a new instance of the editor for the given id
QgsSettingsEditorWidgetWrapper *createWrapper( const QString &id, QObject *parent ) const;

//! Creates the editor for the given settings using the corresponding registered wrapper
QWidget *createEditor( const QgsSettingsEntryBase *setting, const QStringList &dynamicKeyPartList, QWidget *parent = nullptr ) const SIP_FACTORY;

//! Returns a map <name, id> of all registered editors.
QMap<QString, QString> editorNames() const;


private:
QMap<QString, QgsSettingsEditorWidgetWrapper *> mWrappers;
};

#endif // QGSSETTINGSEDITORREGISTRY_H

0 comments on commit 77cc02f

Please sign in to comment.