OtbChoiceWidget.py 6.76 KB
Newer Older
Rashad Kanavath's avatar
Rashad Kanavath committed
1
# -*- coding: utf-8 -*-
Rashad Kanavath's avatar
Rashad Kanavath committed
2
3
"""
***************************************************************************
4
    OtbChoiceWidget.py
Rashad Kanavath's avatar
Rashad Kanavath committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    ------------------
    Date                 : June 2017
    Copyright            : (C) 2017 by CS Systemes d'Information (CS SI)
    Email                : rashad dot kanavath at c-s fr

***************************************************************************
*                                                                         *
*   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.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = 'Rashad Kanavath'
__date__ = 'June 2017'
__copyright__ = "(C) 2017,2018 by CS Systemes d'information (CS SI)"

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

28
from qgis.PyQt.QtWidgets import QComboBox
Rashad Kanavath's avatar
Rashad Kanavath committed
29
30
from qgis.core import (QgsProcessingParameterString,
                       QgsProcessingOutputString)
31
from processing.gui.wrappers import (WidgetWrapper,
32
33
34
35
                                     DIALOG_STANDARD,
                                     DIALOG_BATCH,
                                     DIALOG_MODELER)

Rashad Kanavath's avatar
Rashad Kanavath committed
36

37
class OtbChoiceWidgetWrapper(WidgetWrapper):
Rashad Kanavath's avatar
Rashad Kanavath committed
38
39
40
    def createWidget(self):
        widget = QComboBox()
        widget.addItems(self.param.options)
41
        if self.dialogType in(DIALOG_MODELER, DIALOG_STANDARD):
Rashad Kanavath's avatar
Rashad Kanavath committed
42
43
44
            widget.currentIndexChanged.connect(self.valueChanged)
        return widget

Rashad Kanavath's avatar
Rashad Kanavath committed
45
46
47
48
49
50
    def get_algorithm(self):
        if self.dialogType == DIALOG_MODELER:
            return self.dialog._alg
        else:
            return self.dialog.algorithm()

51
52
53
    def setWrapperVisible(self, name, visible):
        if self.dialogType == DIALOG_STANDARD:
            if self.dialog.mainWidget() is None:
Rashad Kanavath's avatar
Rashad Kanavath committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
                return
            # For compatibility with 3.x API, we need to check whether the wrapper is
            # the deprecated WidgetWrapper class. If not, it's the newer
            # QgsAbstractProcessingParameterWidgetWrapper class
            # TODO QGIS 4.0 - remove is_python_wrapper logic
            is_python_wrapper = issubclass(self.dialog.mainWidget().wrappers[name].__class__, WidgetWrapper)
            if is_python_wrapper:
                self.dialog.mainWidget().wrappers[name].widget.setVisible(visible)
                if self.dialog.mainWidget().wrappers[name].label:
                    self.dialog.mainWidget().wrappers[name].label.setVisible(visible)
            else:
                self.dialog.mainWidget().wrappers[name].wrappedWidget().setVisible(visible)
                if self.dialog.mainWidget().wrappers[name].wrappedLabel():
                    self.dialog.mainWidget().wrappers[name].wrappedLabel().setVisible(visible)
68
        else:
Rashad Kanavath's avatar
Rashad Kanavath committed
69
70
71
72
            # For compatibility with 3.x API, we need to check whether the wrapper is
            # the deprecated WidgetWrapper class. If not, it's the newer
            # QgsAbstractProcessingParameterWidgetWrapper class
            # TODO QGIS 4.0 - remove is_python_wrapper logic
73
            if name in self.dialog.wrappers:
Rashad Kanavath's avatar
Rashad Kanavath committed
74
75
76
77
78
79
80
81
82
                is_python_wrapper = issubclass(self.dialog.wrappers[name].__class__, WidgetWrapper)
                if is_python_wrapper:
                    self.dialog.wrappers[name].widget.setVisible(visible)
                    if self.dialog.wrappers[name].label:
                        self.dialog.wrappers[name].label.setVisible(visible)
                else:
                    self.dialog.wrappers[name].setVisible(visible)
                    if name in self.dialog.widget_labels:
                        self.dialog.widget_labels[name].setVisible(visible)
Rashad Kanavath's avatar
Rashad Kanavath committed
83

84
    def valueChanged(self, value):
Rashad Kanavath's avatar
Rashad Kanavath committed
85
        for parameter in self.get_algorithm().parameterDefinitions():
86
            if not 'group_key' in parameter.metadata() or parameter.metadata()['group_key'] != self.param.name():
87
                continue
Rashad Kanavath's avatar
Rashad Kanavath committed
88
            name = parameter.name()
89
            v = self.value() == parameter.metadata()['group_value']
90
            self.setWrapperVisible(name, v)
Rashad Kanavath's avatar
Rashad Kanavath committed
91
92
93
94
95
96
97
98
99
100

    def setValue(self, value):
        if value in self.param.options:
            index = self.param.options.index(value)
        else:
            index = int(value)
        self.widget.setCurrentIndex(index)

    def value(self):
        return self.widget.currentText()
Rashad Kanavath's avatar
Rashad Kanavath committed
101

102
103
104
105
    def postInitialize(self, wrappers):
        if self.dialogType == DIALOG_BATCH:
            return

Rashad Kanavath's avatar
Rashad Kanavath committed
106
        for parameter in self.get_algorithm().parameterDefinitions():
107
108
109
110
111
            if not 'group_key' in parameter.metadata() or parameter.metadata()['group_key'] != self.param.name():
                continue
            name = parameter.name()
            v = self.value() == parameter.metadata()['group_value']
            for wrapper in wrappers:
Rashad Kanavath's avatar
Rashad Kanavath committed
112
113
114
115
116
                # For compatibility with 3.x API, we need to check whether the wrapper is
                # the deprecated WidgetWrapper class. If not, it's the newer
                # QgsAbstractProcessingParameterWidgetWrapper class
                # TODO QGIS 4.0 - remove is_python_wrapper logic
                is_python_wrapper = issubclass(wrapper.__class__, WidgetWrapper)
117
                if wrapper.param.name() == name:
Rashad Kanavath's avatar
Rashad Kanavath committed
118
119
120
121
122
123
124
125
                    if is_python_wrapper:
                        wrapper.widget.setVisible(v)
                        if wrapper.label:
                            wrapper.label.setVisible(v)
                    else:
                        wrapper.wrappedWidget().setVisible(v)
                        if wrapper.wrappedLabel():
                            wrapper.wrappedLabel().setVisible(v)
Rashad Kanavath's avatar
Rashad Kanavath committed
126

127

Rashad Kanavath's avatar
Rashad Kanavath committed
128
129
from qgis.core import QgsProcessingParameterDefinition

130

131
class OtbParameterChoice(QgsProcessingParameterDefinition):
Rashad Kanavath's avatar
Rashad Kanavath committed
132
133
134
135
136
137
138
139

    def __init__(self, name='', description='', options=[], default=None, isSource=False,
                 multiple=False, optional=False):

        QgsProcessingParameterDefinition.__init__(self, name, description, default, optional)

        self.setMetadata({
            'widget_wrapper': {
140
                'class': 'otb.OtbChoiceWidget.OtbChoiceWidgetWrapper'}})
Rashad Kanavath's avatar
Rashad Kanavath committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        self.options = options

        if default is not None:
            try:
                self.default = int(default)
            except:
                self.default = 0
            self.value = self.default

    def setValue(self, value):
        if value is None:
            self.value = 0
        else:
            self.value = int(value)
            return True
156

Rashad Kanavath's avatar
Rashad Kanavath committed
157
    def type(self):
158
        #This value is written by otbQgisDescriptor.
Rashad Kanavath's avatar
Rashad Kanavath committed
159
        return 'OTBParameterChoice'