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