QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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> class 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 { return u"%1-%2"_s.arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<FLAGS>().name() ); }
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( u"Settings editor not set for %1"_s.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( u"Settings editor not set for %1"_s.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> class QgsSettingsEnumEditorWidgetWrapper : public QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>
145{
146 public:
148 QgsSettingsEnumEditorWidgetWrapper( QObject *parent = nullptr )
149 : QgsSettingsEditorWidgetWrapperTemplate<QgsSettingsEntryEnumFlag<ENUM>, QComboBox, ENUM>( parent )
150 {}
151
153 {
154 QObject::connect( this->mEditor, qOverload<int>( &QComboBox::currentIndexChanged ), this, [this]( int index ) {
155 Q_UNUSED( index );
156 ENUM value = this->mEditor->currentData().template value<ENUM>();
157 this->mSetting->setValue( value, this->mDynamicKeyPartList );
158 } );
159 }
160
161 QString id() const override { return u"%1-%2"_s.arg( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( Qgis::SettingsType::EnumFlag ) ), QMetaEnum::fromType<ENUM>().name() ); }
162
168 void setDisplayStrings( const QMap<ENUM, QString> &displayStrings ) { mDisplayStrings = displayStrings; }
169
170 QgsSettingsEditorWidgetWrapper *createWrapper( QObject *parent = nullptr ) const override { return new QgsSettingsEnumEditorWidgetWrapper<ENUM>( parent ); }
171
172 QVariant variantValueFromWidget() const override
173 {
174 // enum/flags are stored as text
175 return this->mSetting->convertToVariant( valueFromWidget() );
176 };
177
178 bool setSettingFromWidget() const override
179 {
180 if ( this->mEditor )
181 {
182 this->mSetting->setValue( this->valueFromWidget(), this->mDynamicKeyPartList );
183 return true;
184 }
185 else
186 {
187 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
188 }
189 return false;
190 }
191
192 ENUM valueFromWidget() const override
193 {
194 if ( this->mEditor )
195 {
196 return this->mEditor->currentData().template value<ENUM>();
197 }
198 else
199 {
200 QgsDebugMsgLevel( QString( "editor is not set, returning a non-existing value" ), 2 );
201 }
202 return ENUM();
203 }
204
205 bool setWidgetValue( const ENUM &value ) const override
206 {
207 if ( this->mEditor )
208 {
209 int i = this->mEditor->findData( QVariant::fromValue( value ) );
210 this->mEditor->setCurrentIndex( i );
211 return i >= 0;
212 }
213 else
214 {
215 QgsDebugMsgLevel( u"Settings editor not set for %1"_s.arg( this->mSetting->definitionKey() ), 2 );
216 }
217 return false;
218 }
219
220 protected:
222 {
223 const QMap<ENUM, QString> enumMap = qgsEnumMap<ENUM>();
224 for ( auto it = enumMap.constBegin(); it != enumMap.constEnd(); ++it )
225 {
226 const QString displayString = mDisplayStrings.value( it.key(), it.value() );
227 this->mEditor->addItem( displayString, QVariant::fromValue( it.key() ) );
228 }
229 }
230
231 private:
232 QMap<ENUM, QString> mDisplayStrings;
233};
234
235#endif // QGSSETTINGSENUMFLAGEDITORWIDGETWRAPPER_H
@ EnumFlag
Enum or Flag.
Definition qgis.h:670
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:7140
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63