2016-04-06 10:50:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2016-04-06 14:56:55 +02:00
|
|
|
PreconfiguredAlgorithmDialog.py
|
2016-04-06 10:50:54 +02:00
|
|
|
---------------------
|
2016-04-06 14:56:55 +02:00
|
|
|
Date : April 2016
|
2016-04-06 10:50:54 +02:00
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf 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__ = 'Victor Olaya'
|
2016-04-06 14:56:55 +02:00
|
|
|
__date__ = 'April 2016'
|
|
|
|
__copyright__ = '(C) 2016, Victor Olaya'
|
2016-04-06 10:50:54 +02:00
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import json
|
2016-05-19 12:25:04 +02:00
|
|
|
|
2016-04-06 10:50:54 +02:00
|
|
|
from processing.preconfigured.PreconfiguredUtils import algAsDict
|
|
|
|
from processing.preconfigured.PreconfiguredUtils import preconfiguredAlgorithmsFolder
|
|
|
|
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
|
|
|
|
from processing.gui.AlgorithmDialog import AlgorithmDialog
|
2016-05-19 12:25:04 +02:00
|
|
|
|
2016-09-21 18:24:26 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QMessageBox, QVBoxLayout, QLabel, QLineEdit, QWidget
|
|
|
|
from qgis.PyQt.QtGui import QPalette, QColor
|
2016-04-06 10:50:54 +02:00
|
|
|
|
2017-04-04 11:28:28 +10:00
|
|
|
from qgis.core import QgsApplication
|
2016-09-16 14:32:30 +02:00
|
|
|
from qgis.gui import QgsMessageBar
|
|
|
|
|
2016-04-06 10:50:54 +02:00
|
|
|
|
|
|
|
class PreconfiguredAlgorithmDialog(AlgorithmDialog):
|
|
|
|
|
|
|
|
def __init__(self, alg, toolbox):
|
|
|
|
AlgorithmDialog.__init__(self, alg)
|
|
|
|
self.toolbox = toolbox
|
|
|
|
self.cornerWidget.setVisible(False)
|
|
|
|
self.btnRun.setText(self.tr("OK"))
|
|
|
|
self.tabWidget.removeTab(1)
|
|
|
|
self.settingsPanel = SettingsPanel()
|
|
|
|
self.tabWidget.addTab(self.settingsPanel, "Description")
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
try:
|
2017-05-15 16:19:46 +10:00
|
|
|
parameters = self.getParamValues()
|
|
|
|
self.setOutputValues()
|
2016-04-06 10:50:54 +02:00
|
|
|
msg = self.alg._checkParameterValuesBeforeExecuting()
|
|
|
|
if msg:
|
|
|
|
QMessageBox.warning(
|
|
|
|
self, self.tr('Unable to execute algorithm'), msg)
|
|
|
|
return
|
|
|
|
description = algAsDict(self.alg)
|
|
|
|
description["name"] = self.settingsPanel.txtName.text().strip()
|
|
|
|
description["group"] = self.settingsPanel.txtGroup.text().strip()
|
|
|
|
if not (description["name"] and description["group"]):
|
2016-05-18 15:37:43 +02:00
|
|
|
self.tabWidget.setCurrentIndex(self.tabWidget.count() - 1)
|
2016-04-06 10:50:54 +02:00
|
|
|
return
|
|
|
|
validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:'
|
|
|
|
filename = ''.join(c for c in description["name"] if c in validChars).lower() + ".json"
|
|
|
|
filepath = os.path.join(preconfiguredAlgorithmsFolder(), filename)
|
|
|
|
with open(filepath, "w") as f:
|
|
|
|
json.dump(description, f)
|
2017-04-04 11:28:28 +10:00
|
|
|
QgsApplication.processingRegistry().providerById('preconfigured').refreshAlgorithms()
|
2016-04-06 10:50:54 +02:00
|
|
|
except AlgorithmDialogBase.InvalidParameterValue as e:
|
|
|
|
try:
|
2017-03-04 16:23:36 +01:00
|
|
|
self.buttonBox.accepted.connect(lambda: e.widget.setPalette(QPalette()))
|
2016-04-06 10:50:54 +02:00
|
|
|
palette = e.widget.palette()
|
|
|
|
palette.setColor(QPalette.Base, QColor(255, 255, 0))
|
|
|
|
e.widget.setPalette(palette)
|
2017-03-04 16:23:36 +01:00
|
|
|
self.parent.bar.pushMessage("", self.tr('Missing parameter value: {0}').format(
|
|
|
|
e.parameter.description),
|
2016-09-19 11:52:31 +03:00
|
|
|
level=QgsMessageBar.WARNING, duration=5)
|
2016-04-06 10:50:54 +02:00
|
|
|
return
|
|
|
|
except:
|
|
|
|
QMessageBox.critical(self,
|
|
|
|
self.tr('Unable to execute algorithm'),
|
|
|
|
self.tr('Wrong or missing parameter values'))
|
|
|
|
self.close()
|
|
|
|
|
2016-04-07 14:10:25 +02:00
|
|
|
|
2016-04-06 10:50:54 +02:00
|
|
|
class SettingsPanel(QWidget):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
QWidget.__init__(self)
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
labelName = QLabel("Name")
|
|
|
|
labelGroup = QLabel("Group")
|
|
|
|
self.txtName = QLineEdit()
|
|
|
|
self.txtGroup = QLineEdit()
|
|
|
|
layout.addWidget(labelName)
|
|
|
|
layout.addWidget(self.txtName)
|
|
|
|
layout.addWidget(labelGroup)
|
|
|
|
layout.addWidget(self.txtGroup)
|
|
|
|
layout.addStretch()
|
|
|
|
self.setLayout(layout)
|