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