From 1507f0db88ee8cc26ce30afbca5281a5e49861b4 Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Mon, 5 May 2014 15:08:05 +0300 Subject: [PATCH] [processing] add new output type - directory --- .../processing/gui/NumberInputPanel.py | 2 +- .../processing/gui/OutputSelectionPanel.py | 127 ++++++++++-------- .../modeler/ModelerParametersDialog.py | 3 +- .../processing/outputs/OutputDirectory.py | 32 +++++ .../processing/outputs/OutputFactory.py | 2 + .../processing/ui/widgetNumberInput.ui | 55 ++++---- .../processing/ui/widgetOutputSelect.ui | 37 +++++ 7 files changed, 171 insertions(+), 87 deletions(-) create mode 100644 python/plugins/processing/outputs/OutputDirectory.py create mode 100644 python/plugins/processing/ui/widgetOutputSelect.ui diff --git a/python/plugins/processing/gui/NumberInputPanel.py b/python/plugins/processing/gui/NumberInputPanel.py index 0765d21c773..e8b677650a9 100644 --- a/python/plugins/processing/gui/NumberInputPanel.py +++ b/python/plugins/processing/gui/NumberInputPanel.py @@ -33,7 +33,7 @@ from processing.ui.ui_widgetNumberInput import Ui_widgetNumberInput class NumberInputPanel(QWidget, Ui_widgetNumberInput): def __init__(self, number, minimum, maximum, isInteger): - QDialog.__init__(self) + QWidget.__init__(self) self.setupUi(self) self.isInteger = isInteger diff --git a/python/plugins/processing/gui/OutputSelectionPanel.py b/python/plugins/processing/gui/OutputSelectionPanel.py index 401f6de186a..405d4cd2495 100644 --- a/python/plugins/processing/gui/OutputSelectionPanel.py +++ b/python/plugins/processing/gui/OutputSelectionPanel.py @@ -27,54 +27,57 @@ __revision__ = '$Format:%H$' import os.path import re + from PyQt4.QtCore import * from PyQt4.QtGui import * -from qgis.gui import * + from processing.core.ProcessingConfig import ProcessingConfig from processing.outputs.OutputVector import OutputVector +from processing.outputs.OutputDirectory import OutputDirectory +from processing.ui.ui_widgetOutputSelect import Ui_widgetOutputSelect -class OutputSelectionPanel(QWidget): +class OutputSelectionPanel(QWidget, Ui_widgetOutputSelect): - lastOutputFolder = None - SAVE_TO_TEMP_FILE = '[Save to temporary file]' + SAVE_TO_TEMP_FILE = QCoreApplication.translate( + 'OutputSelectionPanel', '[Save to temporary file]') def __init__(self, output, alg): + QWidget.__init__(self) + self.setupUi(self) + self.output = output self.alg = alg - super(OutputSelectionPanel, self).__init__(None) - self.horizontalLayout = QHBoxLayout(self) - self.horizontalLayout.setSpacing(2) - self.horizontalLayout.setMargin(0) - self.text = QLineEdit() + if hasattr(self.text, 'setPlaceholderText'): - self.text.setPlaceholderText( - OutputSelectionPanel.SAVE_TO_TEMP_FILE) - self.text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) - self.horizontalLayout.addWidget(self.text) - self.pushButton = QPushButton() - self.pushButton.setText('...') - self.pushButton.clicked.connect(self.buttonPushed) - self.horizontalLayout.addWidget(self.pushButton) - self.setLayout(self.horizontalLayout) + self.text.setPlaceholderText(self.SAVE_TO_TEMP_FILE) - def buttonPushed(self): - popupmenu = QMenu() - saveToTemporaryFileAction = QAction('Save to a temporary file', - self.pushButton) - saveToTemporaryFileAction.triggered.connect(self.saveToTemporaryFile) - popupmenu.addAction(saveToTemporaryFileAction) - if isinstance(self.output, OutputVector) \ - and self.alg.provider.supportsNonFileBasedOutput(): - saveToMemoryAction = QAction('Save to a memory layer', - self.pushButton) - saveToMemoryAction.triggered.connect(self.saveToMemory) - popupmenu.addAction(saveToMemoryAction) - saveToFileAction = QAction('Save to file...', self.pushButton) - saveToFileAction.triggered.connect(self.saveToFile) - popupmenu.addAction(saveToFileAction) + self.btnBrowse.clicked.connect(self.selectOutput) - popupmenu.exec_(QCursor.pos()) + def selectOutput(self): + if isinstance(self.output, OutputDirectory): + self.selectDirectory() + else: + popupMenu = QMenu() + + actionSaveToTempFile = QAction( + self.tr('Save to a temporary file'), self.btnBrowse) + actionSaveToTempFile.triggered.connect(self.saveToTemporaryFile) + popupMenu.addAction(actionSaveToTempFile) + + actionSaveToFile = QAction( + self.tr('Save to file...'), self.btnBrowse) + actionSaveToFile.triggered.connect(self.selectFile) + popupMenu.addAction(actionSaveToFile) + + if isinstance(self.output, OutputVector) \ + and self.alg.provider.supportsNonFileBasedOutput(): + actionSaveToMemory = QAction( + self.tr('Save to memory layer'), self.btnBrowse) + actionSaveToMemory.triggered.connect(self.saveToMemory) + popupMenu.addAction(actionSaveToMemory) + + popupMenu.exec_(QCursor.pos()) def saveToTemporaryFile(self): self.text.setText('') @@ -82,44 +85,56 @@ class OutputSelectionPanel(QWidget): def saveToMemory(self): self.text.setText('memory:') - def saveToFile(self): - filefilter = self.output.getFileFilter(self.alg) + def selectFile(self): + fileFilter = self.output.getFileFilter(self.alg) + settings = QSettings() if settings.contains('/Processing/LastOutputPath'): path = settings.value('/Processing/LastOutputPath') else: path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER) - lastEncoding = settings.value('/Processing/encoding', 'System') - fileDialog = QgsEncodingFileDialog(self, 'Save file', path, - filefilter, lastEncoding) + + encoding = settings.value('/Processing/encoding', 'System') + fileDialog = QgsEncodingFileDialog( + self, self.tr('Save file'), path, fileFilter, encoding) fileDialog.setFileMode(QFileDialog.AnyFile) fileDialog.setAcceptMode(QFileDialog.AcceptSave) fileDialog.setConfirmOverwrite(True) + if fileDialog.exec_() == QDialog.Accepted: files = fileDialog.selectedFiles() encoding = unicode(fileDialog.encoding()) self.output.encoding = encoding - filename = unicode(files[0]) - selectedFilefilter = unicode(fileDialog.selectedNameFilter()) - if not filename.lower().endswith( - tuple(re.findall("\*(\.[a-z]{1,5})", filefilter))): + fileName = unicode(files[0]) + selectedFileFilter = unicode(fileDialog.selectedNameFilter()) + if not fileName.lower().endswith( + tuple(re.findall("\*(\.[a-z]{1,5})", fileFilter))): ext = re.search("\*(\.[a-z]{1,5})", selectedFilefilter) if ext: - filename = filename + ext.group(1) - self.text.setText(filename) + fileName += ext.group(1) + self.text.setText(fileName) settings.setValue('/Processing/LastOutputPath', - os.path.dirname(filename)) + os.path.dirname(fileName)) settings.setValue('/Processing/encoding', encoding) + def selectDirectory(self): + lastDir = '' + + dirName = QFileDialog.getExistingDirectory(self, + self.tr('Select directory'), lastDir, QFileDialog.ShowDirsOnly) + + self.text.setText(dirName) + def getValue(self): - filename = unicode(self.text.text()) - if filename.strip() == '' or filename \ - == OutputSelectionPanel.SAVE_TO_TEMP_FILE: - return None - if filename.startswith('memory:'): - return filename + fileName = unicode(self.text.text()) + if fileName.strip() in ['', self.SAVE_TO_TEMP_FILE]: + value = None + elif fileName.startswith('memory:'): + value = fileName + elif not os.path.isabs(fileName): + value = ProcessingConfig.getSetting( + ProcessingConfig.OUTPUT_FOLDER) + os.sep + fileName else: - if not os.path.isabs(filename): - filename = ProcessingConfig.getSetting( - ProcessingConfig.OUTPUT_FOLDER) + os.sep + filename - return filename + value = fileName + + return value diff --git a/python/plugins/processing/modeler/ModelerParametersDialog.py b/python/plugins/processing/modeler/ModelerParametersDialog.py index be5aafa983b..87c1a433d43 100644 --- a/python/plugins/processing/modeler/ModelerParametersDialog.py +++ b/python/plugins/processing/modeler/ModelerParametersDialog.py @@ -58,6 +58,7 @@ from processing.outputs.OutputString import OutputString from processing.outputs.OutputNumber import OutputNumber from processing.outputs.OutputHTML import OutputHTML from processing.outputs.OutputFile import OutputFile +from processing.outputs.OutputDirectory import OutputDirectory class ModelerParametersDialog(QtGui.QDialog): @@ -130,7 +131,7 @@ class ModelerParametersDialog(QtGui.QDialog): if output.hidden: continue if isinstance(output, (OutputRaster, OutputVector, OutputTable, - OutputHTML, OutputFile)): + OutputHTML, OutputFile, OutputDirectory)): label = QtGui.QLabel(output.description + '<' + output.__module__.split('.')[-1] + '>') item = QLineEdit() diff --git a/python/plugins/processing/outputs/OutputDirectory.py b/python/plugins/processing/outputs/OutputDirectory.py new file mode 100644 index 00000000000..475a6b5e3ba --- /dev/null +++ b/python/plugins/processing/outputs/OutputDirectory.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + OutputDirectory.py + --------------------- + Date : May 2014 + Copyright : (C) 2014 by Alexander Bruy + Email : alexander dot bruy 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__ = 'Alexander Bruy' +__date__ = 'May 2014' +__copyright__ = '(C) 2014, Alexander Bruy' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + +from processing.outputs.Output import Output + + +class OutputDirectory(Output): + pass diff --git a/python/plugins/processing/outputs/OutputFactory.py b/python/plugins/processing/outputs/OutputFactory.py index fc761a47aa5..0ab2d70b3bd 100644 --- a/python/plugins/processing/outputs/OutputFactory.py +++ b/python/plugins/processing/outputs/OutputFactory.py @@ -32,6 +32,7 @@ from processing.outputs.OutputVector import OutputVector from processing.outputs.OutputNumber import OutputNumber from processing.outputs.OutputFile import OutputFile from processing.outputs.OutputString import OutputString +from processing.outputs.OutputDirectory import OutputDirectory class OutputFactory: @@ -46,6 +47,7 @@ class OutputFactory: OutputNumber, OutputFile, OutputString, + OutputDirectory ] for clazz in classes: if s.startswith(clazz().outputTypeName()): diff --git a/python/plugins/processing/ui/widgetNumberInput.ui b/python/plugins/processing/ui/widgetNumberInput.ui index 39c1a94ff67..68365ebb53d 100644 --- a/python/plugins/processing/ui/widgetNumberInput.ui +++ b/python/plugins/processing/ui/widgetNumberInput.ui @@ -7,48 +7,45 @@ 0 0 205 - 49 + 24 Form - 0 + + + + 2 + 0 - - - 2 + + + 6 - - - - 6 - - - -99999999.999999001622200 - - - 99999999.999999001622200 - - - - - - - Open number input dialog - - - ... - - - - + + -99999999.999999001622200 + + + 99999999.999999001622200 + + + + + + + Open number input dialog + + + ... + + diff --git a/python/plugins/processing/ui/widgetOutputSelect.ui b/python/plugins/processing/ui/widgetOutputSelect.ui new file mode 100644 index 00000000000..f2137cf56a6 --- /dev/null +++ b/python/plugins/processing/ui/widgetOutputSelect.ui @@ -0,0 +1,37 @@ + + + widgetOutputSelect + + + + 0 + 0 + 400 + 24 + + + + Form + + + + 2 + + + 0 + + + + + + + + ... + + + + + + + +