2024-10-08 20:58:00 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
qgssettingsenumflageditorwrapper.py
|
|
|
|
---------------------
|
|
|
|
Date : October 2024
|
|
|
|
Copyright : (C) 2021 by Denis Rouzaud
|
|
|
|
Email : denis@opengis.ch
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
2024-10-14 23:03:24 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QComboBox
|
2024-10-08 20:58:00 +02:00
|
|
|
|
2024-10-09 07:19:55 +02:00
|
|
|
from qgis.core import QgsSettingsEntryBase
|
|
|
|
from qgis.gui import QgsSettingsEditorWidgetWrapper
|
2024-10-08 20:58:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PyQgsSettingsEnumEditorWidgetWrapper(QgsSettingsEditorWidgetWrapper):
|
|
|
|
"""
|
|
|
|
A settings editor widget wrapper for enum settings as PyQgsSettingsEntryEnumFlag
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, parent=None, editor=None, setting=None, displayStrings: dict = None):
|
2024-10-09 12:08:45 +02:00
|
|
|
self.setting = setting
|
|
|
|
self.editor = editor
|
2024-10-08 20:58:00 +02:00
|
|
|
self.displayStrings = {}
|
|
|
|
super().__init__(parent)
|
|
|
|
if editor is not None and setting is not None:
|
|
|
|
if displayStrings:
|
|
|
|
self.displayStrings = displayStrings
|
|
|
|
self.configureEditor(editor, setting)
|
|
|
|
|
|
|
|
def id(self):
|
|
|
|
return 'py-enum'
|
|
|
|
|
|
|
|
def createWrapper(self, parent=None):
|
|
|
|
return PyQgsSettingsEnumEditorWidgetWrapper(parent)
|
|
|
|
|
|
|
|
def setWidgetFromSetting(self):
|
|
|
|
if self.setting:
|
|
|
|
return self.setWidgetFromVariant(self.setting.value(self.dynamicKeyPartList()))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def setSettingFromWidget(self):
|
|
|
|
if self.editor:
|
|
|
|
self.setting.setValue(self.variantValueFromWidget(), self.dynamicKeyPartList())
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def variantValueFromWidget(self):
|
|
|
|
if self.editor:
|
|
|
|
return self.setting.defaultValue().__class__(self.editor.currentData())
|
|
|
|
return None
|
|
|
|
|
|
|
|
def setWidgetFromVariant(self, value):
|
2024-10-14 23:03:24 +02:00
|
|
|
if self.editor and value is not None:
|
|
|
|
ok = True
|
|
|
|
if isinstance(value, str):
|
|
|
|
value, ok = self.setting.metaEnum().keyToValue(value)
|
|
|
|
if ok:
|
|
|
|
idx = self.editor.findData(int(value))
|
|
|
|
self.editor.setCurrentIndex(idx)
|
2024-10-08 20:58:00 +02:00
|
|
|
return idx >= 0
|
|
|
|
return False
|
|
|
|
|
|
|
|
def createEditorPrivate(self, parent=None):
|
|
|
|
return QComboBox(parent)
|
|
|
|
|
2024-10-14 23:03:24 +02:00
|
|
|
def configureEditorPrivate(self, editor: QComboBox, setting: QgsSettingsEntryBase):
|
2024-10-08 20:58:00 +02:00
|
|
|
self.setting = setting
|
2024-10-09 12:11:44 +02:00
|
|
|
if isinstance(editor, QComboBox):
|
|
|
|
self.editor = editor
|
2024-10-08 20:58:00 +02:00
|
|
|
for i in range(self.setting.metaEnum().keyCount()):
|
|
|
|
value = self.setting.metaEnum().value(i)
|
|
|
|
key = self.setting.metaEnum().key(i)
|
|
|
|
text = self.displayStrings.get(value, key)
|
|
|
|
self.editor.addItem(text, value)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def enableAutomaticUpdatePrivate(self):
|
|
|
|
self.editor.currentIndexChanged.connect(
|
|
|
|
lambda: self.setting.setValue(self.editor.currentData(), self.dynamicKeyPartList()))
|