File: qgssettingsenumflageditorwrapper.py

package info (click to toggle)
qgis 3.40.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,183,672 kB
  • sloc: cpp: 1,595,771; python: 372,544; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,257; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 161
file content (98 lines) | stat: -rw-r--r-- 3,563 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
***************************************************************************
    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.                                   *
*                                                                         *
***************************************************************************
"""

from qgis.PyQt.QtWidgets import QComboBox

from qgis.core import QgsSettingsEntryBase
from qgis.gui import QgsSettingsEditorWidgetWrapper


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
    ):
        self.setting = setting
        self.editor = editor
        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.valueAsVariant(self.dynamicKeyPartList())
            )
        return False

    def setSettingFromWidget(self):
        if self.editor:
            self.setting.setVariantValue(
                self.variantValueFromWidget(), self.dynamicKeyPartList()
            )
            return True
        else:
            return False

    def variantValueFromWidget(self):
        if self.editor:
            return self.editor.currentData()
        return None

    def setWidgetFromVariant(self, value):
        if self.editor and value is not None:
            if isinstance(value, int):
                value = self.setting.metaEnum().valueToKey(value)
            idx = self.editor.findData(value)
            self.editor.setCurrentIndex(idx)
            return idx >= 0
        return False

    def createEditorPrivate(self, parent=None):
        return QComboBox(parent)

    def configureEditorPrivate(self, editor: QComboBox, setting: QgsSettingsEntryBase):
        self.setting = setting
        if isinstance(editor, QComboBox):
            self.editor = editor
            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, key)
            return True
        else:
            return False

    def enableAutomaticUpdatePrivate(self):
        self.editor.currentIndexChanged.connect(
            lambda: self.setting.setValue(
                self.editor.currentData(), self.dynamicKeyPartList()
            )
        )