Skip to content
Permalink
Browse files
[processing] add new output type - directory
  • Loading branch information
alexbruy committed May 16, 2014
1 parent 4d5b7d3 commit 1507f0db88ee8cc26ce30afbca5281a5e49861b4
@@ -33,7 +33,7 @@
class NumberInputPanel(QWidget, Ui_widgetNumberInput):

def __init__(self, number, minimum, maximum, isInteger):
QDialog.__init__(self)
QWidget.__init__(self)
self.setupUi(self)

self.isInteger = isInteger
@@ -27,99 +27,114 @@

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)

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)

popupmenu.exec_(QCursor.pos())
self.text.setPlaceholderText(self.SAVE_TO_TEMP_FILE)

self.btnBrowse.clicked.connect(self.selectOutput)

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('')

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
@@ -58,6 +58,7 @@
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 @@ def setupUi(self):
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()
@@ -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
@@ -32,6 +32,7 @@
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 @@ def getFromString(s):
OutputNumber,
OutputFile,
OutputString,
OutputDirectory
]
for clazz in classes:
if s.startswith(clazz().outputTypeName()):
@@ -7,48 +7,45 @@
<x>0</x>
<y>0</y>
<width>205</width>
<height>49</height>
<height>24</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>0</number>
</property>

<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
<widget class="QDoubleSpinBox" name="spnValue">
<property name="decimals">
<number>6</number>
</property>
<property name="minimum">
<double>-99999999.999999001622200</double>
</property>
<property name="maximum">
<double>99999999.999999001622200</double>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnCalc">
<property name="toolTip">
<string>Open number input dialog</string>
</property>
<property name="text">
<string>...</string>
</property>
<item>
<widget class="QDoubleSpinBox" name="spnValue">
<property name="decimals">
<number>6</number>
</property>
<property name="minimum">
<double>-99999999.999999001622200</double>
</property>
<property name="maximum">
<double>99999999.999999001622200</double>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="btnCalc">
<property name="toolTip">
<string>Open number input dialog</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>widgetOutputSelect</class>
<widget class="QWidget" name="widgetOutputSelect">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>24</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="text"/>
</item>
<item>
<widget class="QToolButton" name="btnBrowse">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit 1507f0d

Please sign in to comment.