QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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#include <QString>
30
31using namespace Qt::StringLiterals;
32
39template<class ENUM, class FLAGS>
40class GUI_EXPORT QgsSettingsFlagsEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<FLAGS>, QComboBox, FLAGS>
41{
42 public:
44 QgsSettingsFlagsEditorWidgetWrapper( QObject *parent = nullptr )
45 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<FLAGS>, QComboBox, FLAGS>( parent )
46 {}
47
49 {
50 QObject::connect( &mModel, &QStandardItemModel::itemChanged, this, [this]( const QStandardItem *item ) {
51 Q_UNUSED( item )
53 } );
54 }
55
56 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override { return new QgsSettingsFlagsEditorWidgetWrapper<ENUM, FLAGS>( parent ); }
57
58 QString id() const override
59 {
60 return u"%1-%2"_s.arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<FLAGS>().name() );
61 }
62
63 QVariant variantValueFromWidget() const override
64 {
65 // enum/flags are stored as text
66 return this->mSetting->convertToVariant( valueFromWidget() );
67 };
68
69 bool setSettingFromWidget() const override
70 {
71 if ( this->mEditor )
72 {
73 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
74 return true;
75 }
76 else
77 {
78 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
79 }
80 return false;
81 }
82
83 FLAGS valueFromWidget() const override
84 {
85 if ( this->mEditor )
86 {
87 FLAGS value;
88 for ( int r = 0; r < mModel.rowCount(); r++ )
89 {
90 QStandardItem *item = mModel.item( r );
91 if ( item->data( Qt::CheckStateRole ) == Qt::Checked )
92 value |= item->data().value<ENUM>();
93 }
94 return value;
95 }
96 else
97 {
98 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
99 }
100 return FLAGS();
101 }
102
103 bool setWidgetValue( const FLAGS &value ) const override
104 {
105 if ( this->mEditor )
106 {
107 for ( int r = 0; r < mModel.rowCount(); r++ )
108 {
109 QStandardItem *item = mModel.item( r );
110 bool isChecked = value.testFlag( item->data().value<ENUM>() );
111 item->setData( isChecked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole );
112 }
113 return true;
114 }
115 else
116 {
117 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
118 }
119 return false;
120 }
121
122 protected:
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, [this]( int index ) {
159 Q_UNUSED( index );
160 ENUM value = this->mEditor->currentData().template value<ENUM>();
161 this->mSetting->setValue( value, this->mDynamicKeyPartList );
162 } );
163 }
164
165 QString id() const override
166 {
167 return u"%1-%2"_s.arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<ENUM>().name() );
168 }
169
175 void setDisplayStrings( const QMap<ENUM, QString> &displayStrings ) { mDisplayStrings = displayStrings; }
176
177 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override { return new QgsSettingsEnumEditorWidgetWrapper<ENUM>( parent ); }
178
179 QVariant variantValueFromWidget() const override
180 {
181 // enum/flags are stored as text
182 return this->mSetting->convertToVariant( valueFromWidget() );
183 };
184
185 bool setSettingFromWidget() const override
186 {
187 if ( this->mEditor )
188 {
189 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
190 return true;
191 }
192 else
193 {
194 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
195 }
196 return false;
197 }
198
199 ENUM valueFromWidget() const override
200 {
201 if ( this->mEditor )
202 {
203 return this->mEditor->currentData().template value<ENUM>();
204 }
205 else
206 {
207 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
208 }
209 return ENUM();
210 }
211
212 bool setWidgetValue( const ENUM &value ) const override
213 {
214 if ( this->mEditor )
215 {
216 int i = this->mEditor->findData( QVariant::fromValue( value ) );
217 this->mEditor->setCurrentIndex( i );
218 return i >= 0;
219 }
220 else
221 {
222 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
223 }
224 return false;
225 }
226
227 protected:
229 {
230 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
231 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
232 {
233 const QString displayString = mDisplayStrings.value( it.key(), it.value() );
234 this->mEditor->addItem( displayString, QVariant::fromValue( it.key() ) );
235 }
236 }
237
238 private:
239 QMap<ENUM, QString> mDisplayStrings;
240};
241
242#endif // QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
@ EnumFlag
Enum or Flag.
Definition qgis.h:663
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:7098
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63