|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ScriptSelector.py |
| 6 | + --------------------- |
| 7 | + Date : May 2016 |
| 8 | + Copyright : (C) 2016 by Victor Olaya |
| 9 | + Email : volayaf at gmail dot com |
| 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 | +__author__ = 'Victor Olaya' |
| 21 | +__date__ = 'May 2016' |
| 22 | +__copyright__ = '(C) 2016, Victor Olaya' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | +from PyQt4 import QtGui, uic, QtCore |
| 26 | +import os |
| 27 | +from collections import defaultdict |
| 28 | +from processing.core.alglist import algList |
| 29 | + |
| 30 | +WIDGET, BASE = uic.loadUiType( |
| 31 | + os.path.join(os.path.dirname(__file__), 'scriptselector.ui')) |
| 32 | + |
| 33 | +class ScriptSelector(BASE, WIDGET): |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + QtGui.QDialog.__init__(self) |
| 37 | + self.setupUi(self) |
| 38 | + |
| 39 | + self.scripts = None |
| 40 | + |
| 41 | + allScripts = defaultdict(list) |
| 42 | + alglist = algList.getProviderFromName("script").algs |
| 43 | + for script in alglist: |
| 44 | + allScripts[script.group].append(script) |
| 45 | + |
| 46 | + for group, groupScripts in allScripts.iteritems(): |
| 47 | + groupItem = QtGui.QTreeWidgetItem() |
| 48 | + groupItem.setText(0, group) |
| 49 | + groupItem.setFlags(groupItem.flags() | QtCore.Qt.ItemIsTristate); |
| 50 | + for script in groupScripts: |
| 51 | + scriptItem = QtGui.QTreeWidgetItem() |
| 52 | + scriptItem.setFlags(scriptItem.flags() | QtCore.Qt.ItemIsUserCheckable); |
| 53 | + scriptItem.setCheckState(0, QtCore.Qt.Checked); |
| 54 | + scriptItem.script = script |
| 55 | + scriptItem.setText(0, script.name) |
| 56 | + groupItem.addChild(scriptItem) |
| 57 | + self.scriptsTree.addTopLevelItem(groupItem) |
| 58 | + |
| 59 | + self.scriptsTree.expandAll() |
| 60 | + |
| 61 | + self.selectAllLabel.linkActivated.connect(lambda: self.checkScripts(True)) |
| 62 | + self.unselectAllLabel.linkActivated.connect(lambda: self.checkScripts(False)) |
| 63 | + |
| 64 | + self.folderButton.clicked.connect(self.selectFolder) |
| 65 | + |
| 66 | + self.buttonBox.accepted.connect(self.okPressed) |
| 67 | + self.buttonBox.rejected.connect(self.cancelPressed) |
| 68 | + |
| 69 | + def selectFolder(self): |
| 70 | + folder = QtGui.QFileDialog.getExistingDirectory(self, 'Select folder') |
| 71 | + if folder: |
| 72 | + self.folderBox.setText(folder) |
| 73 | + |
| 74 | + def checkScripts(self, b): |
| 75 | + state = QtCore.Qt.Checked if b else QtCore.Qt.Unchecked |
| 76 | + for i in xrange(self.scriptsTree.topLevelItemCount()): |
| 77 | + item = self.scriptsTree.topLevelItem(i) |
| 78 | + for j in xrange(item.childCount()): |
| 79 | + child = item.child(j) |
| 80 | + child.setCheckState(0, state) |
| 81 | + |
| 82 | + def cancelPressed(self): |
| 83 | + self.close() |
| 84 | + |
| 85 | + def _getValue(self, textBox): |
| 86 | + textBox.setStyleSheet("QLineEdit{background: white}") |
| 87 | + value = textBox.text() |
| 88 | + if value: |
| 89 | + return value |
| 90 | + textBox.setStyleSheet("QLineEdit{background: yellow}") |
| 91 | + raise Exception("wrong parameter value") |
| 92 | + |
| 93 | + def okPressed(self): |
| 94 | + self.scripts = [] |
| 95 | + for i in xrange(self.scriptsTree.topLevelItemCount()): |
| 96 | + groupItem = self.scriptsTree.topLevelItem(i) |
| 97 | + for j in xrange(groupItem.childCount()): |
| 98 | + scriptItem = groupItem.child(j) |
| 99 | + if scriptItem.checkState(0) == QtCore.Qt.Checked: |
| 100 | + self.scripts.append(scriptItem.script) |
| 101 | + self.folder = self._getValue(self.folderBox) |
| 102 | + try: |
| 103 | + self.name = self._getValue(self.nameBox) |
| 104 | + self.description = self._getValue(self.descriptionBox) |
| 105 | + self.author = self._getValue(self.authorBox) |
| 106 | + self.email = self._getValue(self.emailBox) |
| 107 | + except: |
| 108 | + return |
| 109 | + self.close() |
| 110 | + |
0 commit comments