QGIS API Documentation 3.99.0-Master (a5475b57e34)
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
20#include "qgis.h"
21#include "qgis_gui.h"
22#include "qgslogger.h"
25
26#include <QComboBox>
27#include <QStandardItemModel>
28#include <QString>
29
30#define SIP_NO_FILE
31
32using namespace Qt::StringLiterals;
33
40template<class ENUM, class FLAGS>
41class GUI_EXPORT QgsSettingsFlagsEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<FLAGS>, QComboBox, FLAGS>
42{
43 public:
45 QgsSettingsFlagsEditorWidgetWrapper( QObject *parent = nullptr )
46 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<FLAGS>, QComboBox, FLAGS>( parent )
47 {}
48
50 {
51 QObject::connect( &mModel, &QStandardItemModel::itemChanged, this, [this]( const QStandardItem *item ) {
52 Q_UNUSED( item )
54 } );
55 }
56
57 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override { return new QgsSettingsFlagsEditorWidgetWrapper<ENUM, FLAGS>( parent ); }
58
59 QString id() const override
60 {
61 return u"%1-%2"_s.arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<FLAGS>().name() );
62 }
63
64 QVariant variantValueFromWidget() const override
65 {
66 // enum/flags are stored as text
67 return this->mSetting->convertToVariant( valueFromWidget() );
68 };
69
70 bool setSettingFromWidget() const override
71 {
72 if ( this->mEditor )
73 {
74 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
75 return true;
76 }
77 else
78 {
79 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
80 }
81 return false;
82 }
83
84 FLAGS valueFromWidget() const override
85 {
86 if ( this->mEditor )
87 {
88 FLAGS value;
89 for ( int r = 0; r < mModel.rowCount(); r++ )
90 {
91 QStandardItem *item = mModel.item( r );
92 if ( item->data( Qt::CheckStateRole ) == Qt::Checked )
93 value |= item->data().value<ENUM>();
94 }
95 return value;
96 }
97 else
98 {
99 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
100 }
101 return FLAGS();
102 }
103
104 bool setWidgetValue( const FLAGS &value ) const override
105 {
106 if ( this->mEditor )
107 {
108 for ( int r = 0; r < mModel.rowCount(); r++ )
109 {
110 QStandardItem *item = mModel.item( r );
111 bool isChecked = value.testFlag( item->data().value<ENUM>() );
112 item->setData( isChecked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole );
113 }
114 return true;
115 }
116 else
117 {
118 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
119 }
120 return false;
121 }
122
123 protected:
125 {
126 mModel.clear();
127 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
128 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
129 {
130 QStandardItem *item = new QStandardItem( it.value() );
131 item->setData( QVariant::fromValue( it.key() ) );
132 item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
133 item->setData( Qt::Unchecked, Qt::CheckStateRole );
134 mModel.appendRow( item );
135 }
136 this->mEditor->setModel( &mModel );
137 }
138
139 QStandardItemModel mModel;
140};
141
148template<class ENUM>
149class QgsSettingsEnumEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>
150{
151 public:
153 QgsSettingsEnumEditorWidgetWrapper( QObject *parent = nullptr )
154 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>( parent )
155 {}
156
158 {
159 QObject::connect( this->mEditor, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int index ) {
160 Q_UNUSED( index );
161 ENUM value = this->mEditor->currentData().template value<ENUM>();
162 this->mSetting->setValue( value, this->mDynamicKeyPartList );
163 } );
164 }
165
166 QString id() const override
167 {
168 return u"%1-%2"_s.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( u"Settings editor not set for %1"_s.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( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
224 }
225 return false;
226 }
227
228 protected:
230 {
231 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
232 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
233 {
234 const QString displayString = mDisplayStrings.value( it.key(), it.value() );
235 this->mEditor->addItem( displayString, QVariant::fromValue( it.key() ) );
236 }
237 }
238
239 private:
240 QMap<ENUM, QString> mDisplayStrings;
241};
242
243#endif // QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
@ EnumFlag
Enum or Flag.
Definition qgis.h:665
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:7124
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63