|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + qgssettingsenumflageditorwrapper.py |
| 6 | + --------------------- |
| 7 | + Date : October 2024 |
| 8 | + Copyright : (C) 2021 by Denis Rouzaud |
| 9 | + |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +from qgis.PyQt.QtWidgets import QWidget, QComboBox |
| 21 | + |
| 22 | +from qgis.core import QgsSettingsEntryBase |
| 23 | +from qgis.gui import QgsSettingsEditorWidgetWrapper |
| 24 | + |
| 25 | + |
| 26 | +class PyQgsSettingsEnumEditorWidgetWrapper(QgsSettingsEditorWidgetWrapper): |
| 27 | + """ |
| 28 | + A settings editor widget wrapper for enum settings as PyQgsSettingsEntryEnumFlag |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, parent=None, editor=None, setting=None, displayStrings: dict = None): |
| 32 | + self.setting = setting |
| 33 | + self.editor = editor |
| 34 | + self.displayStrings = {} |
| 35 | + super().__init__(parent) |
| 36 | + if editor is not None and setting is not None: |
| 37 | + if displayStrings: |
| 38 | + self.displayStrings = displayStrings |
| 39 | + self.configureEditor(editor, setting) |
| 40 | + |
| 41 | + def id(self): |
| 42 | + return 'py-enum' |
| 43 | + |
| 44 | + def createWrapper(self, parent=None): |
| 45 | + return PyQgsSettingsEnumEditorWidgetWrapper(parent) |
| 46 | + |
| 47 | + def setWidgetFromSetting(self): |
| 48 | + if self.setting: |
| 49 | + return self.setWidgetFromVariant(self.setting.value(self.dynamicKeyPartList())) |
| 50 | + return False |
| 51 | + |
| 52 | + def setSettingFromWidget(self): |
| 53 | + if self.editor: |
| 54 | + self.setting.setValue(self.variantValueFromWidget(), self.dynamicKeyPartList()) |
| 55 | + return True |
| 56 | + else: |
| 57 | + return False |
| 58 | + |
| 59 | + def variantValueFromWidget(self): |
| 60 | + if self.editor: |
| 61 | + return self.setting.defaultValue().__class__(self.editor.currentData()) |
| 62 | + return None |
| 63 | + |
| 64 | + def setWidgetFromVariant(self, value): |
| 65 | + if self.editor: |
| 66 | + idx = self.editor.findData(value) |
| 67 | + self.editor.setCurrentIndex(idx) |
| 68 | + return idx >= 0 |
| 69 | + return False |
| 70 | + |
| 71 | + def createEditorPrivate(self, parent=None): |
| 72 | + return QComboBox(parent) |
| 73 | + |
| 74 | + def configureEditorPrivate(self, editor: QWidget, setting: QgsSettingsEntryBase): |
| 75 | + self.setting = setting |
| 76 | + if isinstance(editor, QComboBox): |
| 77 | + self.editor = editor |
| 78 | + for i in range(self.setting.metaEnum().keyCount()): |
| 79 | + value = self.setting.metaEnum().value(i) |
| 80 | + key = self.setting.metaEnum().key(i) |
| 81 | + text = self.displayStrings.get(value, key) |
| 82 | + self.editor.addItem(text, value) |
| 83 | + return True |
| 84 | + else: |
| 85 | + return False |
| 86 | + |
| 87 | + def enableAutomaticUpdatePrivate(self): |
| 88 | + self.editor.currentIndexChanged.connect( |
| 89 | + lambda: self.setting.setValue(self.editor.currentData(), self.dynamicKeyPartList())) |
0 commit comments