File: widgetPluginBase.py

package info (click to toggle)
qgis 2.18.28%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,007,948 kB
  • sloc: cpp: 671,774; python: 158,539; xml: 35,690; ansic: 8,346; sh: 1,766; perl: 1,669; sql: 999; yacc: 836; lex: 461; makefile: 292
file content (209 lines) | stat: -rw-r--r-- 7,390 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-

"""
***************************************************************************
    widgetPluginBase.py
    ---------------------
    Date                 : June 2010
    Copyright            : (C) 2010 by Giuseppe Sucameli
    Email                : brush dot tyler at gmail dot com
***************************************************************************
*                                                                         *
*   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__ = 'Giuseppe Sucameli'
__date__ = 'June 2010'
__copyright__ = '(C) 2010, Giuseppe Sucameli'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QFileInfo
from qgis.PyQt.QtWidgets import QWidget, QMessageBox, QAbstractButton, QGroupBox

from .dialogBase import GdalToolsBaseDialog as BaseDialog
from . import GdalTools_utils as Utils


class GdalToolsBasePluginWidget:

    def __init__(self, iface, commandName):
        self.iface = iface
        self.initialized = False
        self.base = BaseDialog(iface.mainWindow(), iface, self, self.windowTitle(), commandName)

        self.base.processError.connect(self.onError)
        self.base.processFinished.connect(self.onFinished)

        self.base.okClicked.connect(self.onRun)
        self.base.closeClicked.connect(self.onClosing)
        self.base.helpClicked.connect(self.onHelp)

        self.base.finished.connect(self.finished)
        self.base.refreshArgs.connect(self.someValueChanged)

    def someValueChanged(self):
        if self.initialized:
            self.base.valuesChanged.emit(self.getArguments())

    def onLayersChanged(self):
        pass

    def initialize(self):
        if not self.initialized:
            Utils.LayerRegistry.instance().layersChanged.connect(self.onLayersChanged)
            self.onLayersChanged()
            self.initialized = True
            self.someValueChanged()

    def exec_(self):
        self.initialize()
        return self.base.exec_()

    def show_(self):
        self.initialize()
        return self.base.show()

    def setCommandViewerEnabled(self, enable):
        self.base.setCommandViewerEnabled(enable)
        self.someValueChanged()

    def onRun(self):
        self.base.onRun()

    def onClosing(self):
        Utils.LayerRegistry.instance().layersChanged.disconnect(self.onLayersChanged)
        self.base.onClosing()
        self.initialized = False

    def onHelp(self):
        self.base.onHelp()

    def onFinished(self, exitCode, status):
        self.base.onFinished(exitCode, status)

    def onError(self, error):
        self.base.onError(error)

    def getArguments(self):
        pass

    def getInputFileName(self):
        pass

    def getOutputFileName(self):
        pass

    def addLayerIntoCanvas(self, fileInfo):
        pass

    def finished(self, load):
        outFn = self.getOutputFileName()
        if outFn is None:
            return

        if outFn == '':
            QMessageBox.warning(self, self.tr("Warning"), self.tr("No output file created."))
            return

        fileInfo = QFileInfo(outFn)
        if fileInfo.exists():
            if load:
                self.addLayerIntoCanvas(fileInfo)
            QMessageBox.information(self, self.tr("Finished"), self.tr("Processing completed."))
        else:
            #QMessageBox.warning(self, self.tr( "Warning" ), self.tr( "%1 not created." ).arg( outFn ) )
            QMessageBox.warning(self, self.tr("Warning"), self.tr("%s not created.") % outFn)

    # This method is useful to set up options for the command. It sets for each passed widget:
    # 1. its passed signals to connect to the BasePluginWidget.someValueChanged() slot,
    # 2. its enabler checkbox or enabled status,
    # 3. its status as visible (hide) if the installed gdal version is greater or equal (lesser) then the passed version
    #
    # wdgts_sgnls_chk_ver_list: list of wdgts_sgnls_chk_ver
    #     wdgts_sgnls_chk_ver: tuple containing widgets, signals, enabler checkbox or enabled status, required version
    def setParamsStatus(self, wdgts_sgnls_chk_ver_list):
        if isinstance(wdgts_sgnls_chk_ver_list, list):
            for wdgts_sgnls_chk_ver in wdgts_sgnls_chk_ver_list:
                self.setParamsStatus(wdgts_sgnls_chk_ver)
            return

        wdgts_sgnls_chk_ver = wdgts_sgnls_chk_ver_list
        if not isinstance(wdgts_sgnls_chk_ver, tuple):
            return

        if len(wdgts_sgnls_chk_ver) > 0:
            wdgts = wdgts_sgnls_chk_ver[0]
        else:
            wdgts = None

        if len(wdgts_sgnls_chk_ver) > 1:
            sgnls = wdgts_sgnls_chk_ver[1]
        else:
            sgnls = None

        if len(wdgts_sgnls_chk_ver) > 2:
            chk = wdgts_sgnls_chk_ver[2]
        else:
            chk = None

        if len(wdgts_sgnls_chk_ver) > 3:
            ver = wdgts_sgnls_chk_ver[3]
        else:
            ver = None

        if isinstance(wdgts, list):
            for wdgt in wdgts:
                self.setParamsStatus((wdgt, sgnls, chk, ver))
            return

        wdgt = wdgts
        if not isinstance(wdgt, QWidget):
            return

        # if check version fails, disable the widget then hide both it and its enabler checkbox
        # new check for gdal 1.10, must update all widgets for this and then remove previous check
        if ver is not None and isinstance(ver, int):
            gdalVerNum = Utils.GdalConfig.versionNum()
            if ver > gdalVerNum:
                wdgt.setVisible(False)
                if isinstance(chk, QWidget):
                    chk.setVisible(False)
                    chk.setChecked(False)
                sgnls = None
                chk = False

        elif ver is not None:
            if not isinstance(ver, Utils.Version):
                ver = Utils.Version(ver)
            gdalVer = Utils.GdalConfig.version()
            if ver < Utils.Version("0") or (gdalVer is not None and ver > gdalVer):
                wdgt.setVisible(False)
                if isinstance(chk, QWidget):
                    chk.setVisible(False)
                    chk.setChecked(False)
                sgnls = None
                chk = False

        # connects the passed signals to the BasePluginWidget.someValueChanged slot
        if isinstance(sgnls, list):
            for sgnl in sgnls:
                self.setParamsStatus((wdgt, sgnl, chk))
            return

        sgnl = sgnls
        if sgnl is not None:
            getattr(wdgt, sgnl).connect(self.someValueChanged)

        # set the passed checkbox as widget enabler
        if isinstance(chk, bool):
            wdgt.setEnabled(chk)
        if (isinstance(chk, QAbstractButton) or isinstance(chk, QGroupBox)) and chk.isCheckable():
            wdgt.setEnabled(chk.isChecked())
            chk.toggled.connect(wdgt.setEnabled)
            chk.toggled.connect(self.someValueChanged)