QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgssettingsenumflageditorwidgetwrapper.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingsenumflageditorwidgetwrapper.h
3 --------------------------------------
4 Date : February 2023
5 Copyright : (C) 2023 by Denis Rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
17#define QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
18
19#define SIP_NO_FILE
20
21#include "qgis.h"
22#include "qgis_gui.h"
24#include "qgslogger.h"
25
27
28#include <QComboBox>
29#include <QStandardItemModel>
30
37template <class ENUM, class FLAGS>
38class GUI_EXPORT QgsSettingsFlagsEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<FLAGS>, QComboBox, FLAGS>
39{
40 public:
42 QgsSettingsFlagsEditorWidgetWrapper( QObject *parent = nullptr )
43 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<FLAGS>, QComboBox, FLAGS>( parent )
44 {}
45
46 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override {return new QgsSettingsFlagsEditorWidgetWrapper<ENUM, FLAGS>( parent );}
47
48 virtual QString id() const override
49 {
50 return QStringLiteral( "%1-%2" ).arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<FLAGS>().name() );
51 }
52
53 QVariant variantValueFromWidget() const override
54 {
55 // enum/flags are stored as text
56 return this->mSetting->convertToVariant( valueFromWidget() );
57 };
58
59 bool setSettingFromWidget() const override
60 {
61 if ( this->mEditor )
62 {
63 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
64 return true;
65 }
66 else
67 {
68 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
69 }
70 return false;
71 }
72
73 FLAGS valueFromWidget() const override
74 {
75 if ( this->mEditor )
76 {
77 FLAGS value;
78 for ( int r = 0; r < mModel.rowCount(); r++ )
79 {
80 QStandardItem *item = mModel.item( r );
81 if ( item->data( Qt::CheckStateRole ) == Qt::Checked )
82 value |= item->data().value<ENUM>();
83 }
84 return value;
85 }
86 else
87 {
88 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
89 }
90 return FLAGS();
91 }
92
93 bool setWidgetValue( const FLAGS &value ) const override
94 {
95 if ( this->mEditor )
96 {
97 for ( int r = 0; r < mModel.rowCount(); r++ )
98 {
99 QStandardItem *item = mModel.item( r );
100 bool isChecked = value.testFlag( item->data().value<ENUM>() );
101 item->setData( isChecked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole );
102 }
103 return true;
104 }
105 else
106 {
107 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
108 }
109 return false;
110 }
111
112 protected:
113
115 {
116 mModel.clear();
117 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
118 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
119 {
120 QStandardItem *item = new QStandardItem( it.value() );
121 item->setData( QVariant::fromValue( it.key() ) );
122 item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
123 item->setData( Qt::Unchecked, Qt::CheckStateRole );
124 mModel.appendRow( item );
125 }
126 this->mEditor->setModel( &mModel );
127 }
128
129 QStandardItemModel mModel;
130};
131
138template <class ENUM>
139class QgsSettingsEnumEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>
140{
141 public:
143 QgsSettingsEnumEditorWidgetWrapper( QObject *parent = nullptr )
144 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>( parent )
145 {}
146
147 virtual QString id() const override
148 {
149 return QStringLiteral( "%1-%2" ).arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<ENUM>().name() );
150 }
151
152 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override {return new QgsSettingsEnumEditorWidgetWrapper<ENUM>( parent );}
153
154 QVariant variantValueFromWidget() const override
155 {
156 // enum/flags are stored as text
157 return this->mSetting->convertToVariant( valueFromWidget() );
158 };
159
160 bool setSettingFromWidget() const override
161 {
162 if ( this->mEditor )
163 {
164 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
165 return true;
166 }
167 else
168 {
169 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
170 }
171 return false;
172 }
173
174 ENUM valueFromWidget() const override
175 {
176 if ( this->mEditor )
177 {
178 return this->mEditor->currentData().template value<ENUM>();
179 }
180 else
181 {
182 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
183 }
184 return ENUM();
185 }
186
187 bool setWidgetValue( const ENUM &value ) const override
188 {
189 if ( this->mEditor )
190 {
191 int i = this->mEditor->findData( QVariant::fromValue( value ) );
192 this->mEditor->setCurrentIndex( i );
193 return i >= 0;
194 }
195 else
196 {
197 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
198 }
199 return false;
200 }
201
202 protected:
203
205 {
206 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
207 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
208 {
209 this->mEditor->addItem( it.value(), QVariant::fromValue( it.key() ) );
210 }
211 }
212
213};
214
215#endif // QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
@ EnumFlag
Enum or Flag.
This class is a base factory of editor for settings.
virtual U valueFromWidget() const =0
Returns the widget value.
Base class for settings editor wrappers.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QString definitionKey() const
Returns settings entry defining key.
A template class for enum and flag settings entry.
QVariant convertToVariant(const T &value) const override
Converts the value to a variant.
This class is a factory of editor for enum settings.
QVariant variantValueFromWidget() const override
Returns the value from the widget as a variant The wrapper must be configured before calling this med...
bool setSettingFromWidget() const override
Sets the setting value from the widget value The wrapper must be configured before calling this medth...
QgsSettingsEditorWidgetWrapper * createWrapper(QObject *parent=nullptr) const override
Creates a new instance of the editor wrapper so it can be configured for a widget and a setting.
void configureEditorPrivateImplementation() override
To be re-implemented to implemeent type specific configuration (e.g. opacity for colors)
virtual QString id() const override
This id of the type of settings it handles.
QgsSettingsEnumEditorWidgetWrapper(QObject *parent=nullptr)
Constructor.
ENUM valueFromWidget() const override
Returns the widget value.
bool setWidgetValue(const ENUM &value) const override
Sets the widget value.
This class is a factory of editor for flags settings.
QVariant variantValueFromWidget() const override
Returns the value from the widget as a variant The wrapper must be configured before calling this med...
bool setSettingFromWidget() const override
Sets the setting value from the widget value The wrapper must be configured before calling this medth...
void configureEditorPrivateImplementation() override
To be re-implemented to implemeent type specific configuration (e.g. opacity for colors)
bool setWidgetValue(const FLAGS &value) const override
Sets the widget value.
virtual QString id() const override
This id of the type of settings it handles.
QgsSettingsFlagsEditorWidgetWrapper(QObject *parent=nullptr)
Constructor.
FLAGS valueFromWidget() const override
Returns the widget value.
QgsSettingsEditorWidgetWrapper * createWrapper(QObject *parent=nullptr) const override
Creates a new instance of the editor wrapper so it can be configured for a widget and a setting.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39