QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
47 {
48 QObject::connect( &mModel, &QStandardItemModel::itemChanged, this, [ = ]( const QStandardItem * item )
49 {
50 Q_UNUSED( item )
52 } );
53 }
54
55 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override {return new QgsSettingsFlagsEditorWidgetWrapper<ENUM, FLAGS>( parent );}
56
57 virtual QString id() const override
58 {
59 return QStringLiteral( "%1-%2" ).arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<FLAGS>().name() );
60 }
61
62 QVariant variantValueFromWidget() const override
63 {
64 // enum/flags are stored as text
65 return this->mSetting->convertToVariant( valueFromWidget() );
66 };
67
68 bool setSettingFromWidget() const override
69 {
70 if ( this->mEditor )
71 {
72 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
73 return true;
74 }
75 else
76 {
77 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
78 }
79 return false;
80 }
81
82 FLAGS valueFromWidget() const override
83 {
84 if ( this->mEditor )
85 {
86 FLAGS value;
87 for ( int r = 0; r < mModel.rowCount(); r++ )
88 {
89 QStandardItem *item = mModel.item( r );
90 if ( item->data( Qt::CheckStateRole ) == Qt::Checked )
91 value |= item->data().value<ENUM>();
92 }
93 return value;
94 }
95 else
96 {
97 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
98 }
99 return FLAGS();
100 }
101
102 bool setWidgetValue( const FLAGS &value ) const override
103 {
104 if ( this->mEditor )
105 {
106 for ( int r = 0; r < mModel.rowCount(); r++ )
107 {
108 QStandardItem *item = mModel.item( r );
109 bool isChecked = value.testFlag( item->data().value<ENUM>() );
110 item->setData( isChecked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole );
111 }
112 return true;
113 }
114 else
115 {
116 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
117 }
118 return false;
119 }
120
121 protected:
122
124 {
125 mModel.clear();
126 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
127 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
128 {
129 QStandardItem *item = new QStandardItem( it.value() );
130 item->setData( QVariant::fromValue( it.key() ) );
131 item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
132 item->setData( Qt::Unchecked, Qt::CheckStateRole );
133 mModel.appendRow( item );
134 }
135 this->mEditor->setModel( &mModel );
136 }
137
138 QStandardItemModel mModel;
139};
140
147template <class ENUM>
148class QgsSettingsEnumEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>
149{
150 public:
152 QgsSettingsEnumEditorWidgetWrapper( QObject *parent = nullptr )
153 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>( parent )
154 {}
155
157 {
158 QObject::connect( this->mEditor, qOverload<int>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
159 {
160 Q_UNUSED( index );
161 ENUM value = this->mEditor->currentData().template value<ENUM>();
162 this->mSetting->setValue( value, this->mDynamicKeyPartList );
163 } );
164 }
165
166 virtual QString id() const override
167 {
168 return QStringLiteral( "%1-%2" ).arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<ENUM>().name() );
169 }
170
176 void setDisplayStrings( const QMap<ENUM, QString> &displayStrings ) { mDisplayStrings = displayStrings; }
177
178 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override {return new QgsSettingsEnumEditorWidgetWrapper<ENUM>( parent );}
179
180 QVariant variantValueFromWidget() const override
181 {
182 // enum/flags are stored as text
183 return this->mSetting->convertToVariant( valueFromWidget() );
184 };
185
186 bool setSettingFromWidget() const override
187 {
188 if ( this->mEditor )
189 {
190 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
191 return true;
192 }
193 else
194 {
195 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
196 }
197 return false;
198 }
199
200 ENUM valueFromWidget() const override
201 {
202 if ( this->mEditor )
203 {
204 return this->mEditor->currentData().template value<ENUM>();
205 }
206 else
207 {
208 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
209 }
210 return ENUM();
211 }
212
213 bool setWidgetValue( const ENUM &value ) const override
214 {
215 if ( this->mEditor )
216 {
217 int i = this->mEditor->findData( QVariant::fromValue( value ) );
218 this->mEditor->setCurrentIndex( i );
219 return i >= 0;
220 }
221 else
222 {
223 QgsDebugMsgLevel( QStringLiteral( "Settings editor not set for %1" ).arg( this->mSetting->definitionKey() ), 2 );
224 }
225 return false;
226 }
227
228 protected:
229
231 {
232 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
233 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
234 {
235 const QString displayString = mDisplayStrings.value( it.key(), it.value() );
236 this->mEditor->addItem( displayString, QVariant::fromValue( it.key() ) );
237 }
238 }
239
240 private:
241 QMap<ENUM, QString> mDisplayStrings;
242
243};
244
245#endif // QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
@ EnumFlag
Enum or Flag.
This class is a base factory of editor for settings.
virtual bool setSettingFromWidget() const override=0
Sets the setting value from the widget value The wrapper must be configured before calling this medth...
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.
void enableAutomaticUpdatePrivate() override
Enables automatic update, which causes the setting to be updated immediately when the widget value is...
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.
void setDisplayStrings(const QMap< ENUM, QString > &displayStrings)
This will set the display strings so they can be readable and translatable.
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...
void enableAutomaticUpdatePrivate() override
Enables automatic update, which causes the setting to be updated immediately when the widget value is...
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