2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
OutputSelectionPanel.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
import os.path
|
2013-07-15 23:35:06 +02:00
|
|
|
import re
|
2014-05-05 15:08:05 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
2014-05-05 15:08:05 +03:00
|
|
|
|
2014-05-22 12:41:04 +02:00
|
|
|
from qgis.gui import *
|
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
from processing.core.outputs import OutputDirectory
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-11-06 11:32:44 +02:00
|
|
|
from processing.ui.ui_widgetBaseSelector import Ui_Form
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2014-11-06 11:32:44 +02:00
|
|
|
class OutputSelectionPanel(QWidget, Ui_Form):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-05-05 15:08:05 +03:00
|
|
|
SAVE_TO_TEMP_FILE = QCoreApplication.translate(
|
|
|
|
'OutputSelectionPanel', '[Save to temporary file]')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def __init__(self, output, alg):
|
2014-05-05 15:08:05 +03:00
|
|
|
QWidget.__init__(self)
|
|
|
|
self.setupUi(self)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
self.output = output
|
|
|
|
self.alg = alg
|
2014-05-05 15:08:05 +03:00
|
|
|
|
2014-11-06 11:32:44 +02:00
|
|
|
if hasattr(self.leText, 'setPlaceholderText'):
|
|
|
|
self.leText.setPlaceholderText(self.SAVE_TO_TEMP_FILE)
|
2014-05-05 15:08:05 +03:00
|
|
|
|
2014-11-06 11:32:44 +02:00
|
|
|
self.btnSelect.clicked.connect(self.selectOutput)
|
2014-05-05 15:08:05 +03:00
|
|
|
|
|
|
|
def selectOutput(self):
|
|
|
|
if isinstance(self.output, OutputDirectory):
|
|
|
|
self.selectDirectory()
|
|
|
|
else:
|
|
|
|
popupMenu = QMenu()
|
|
|
|
|
|
|
|
actionSaveToTempFile = QAction(
|
2014-11-06 11:32:44 +02:00
|
|
|
self.tr('Save to a temporary file'), self.btnSelect)
|
2014-05-05 15:08:05 +03:00
|
|
|
actionSaveToTempFile.triggered.connect(self.saveToTemporaryFile)
|
|
|
|
popupMenu.addAction(actionSaveToTempFile)
|
|
|
|
|
|
|
|
actionSaveToFile = QAction(
|
2014-11-06 11:32:44 +02:00
|
|
|
self.tr('Save to file...'), self.btnSelect)
|
2014-05-05 15:08:05 +03:00
|
|
|
actionSaveToFile.triggered.connect(self.selectFile)
|
|
|
|
popupMenu.addAction(actionSaveToFile)
|
|
|
|
|
|
|
|
if isinstance(self.output, OutputVector) \
|
|
|
|
and self.alg.provider.supportsNonFileBasedOutput():
|
|
|
|
actionSaveToMemory = QAction(
|
2014-11-06 11:32:44 +02:00
|
|
|
self.tr('Save to memory layer'), self.btnSelect)
|
2014-05-05 15:08:05 +03:00
|
|
|
actionSaveToMemory.triggered.connect(self.saveToMemory)
|
|
|
|
popupMenu.addAction(actionSaveToMemory)
|
|
|
|
|
|
|
|
popupMenu.exec_(QCursor.pos())
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def saveToTemporaryFile(self):
|
2014-11-06 11:32:44 +02:00
|
|
|
self.leText.setText('')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def saveToMemory(self):
|
2014-11-06 11:32:44 +02:00
|
|
|
self.leText.setText('memory:')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-05-05 15:08:05 +03:00
|
|
|
def selectFile(self):
|
|
|
|
fileFilter = self.output.getFileFilter(self.alg)
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
settings = QSettings()
|
2013-10-01 20:52:22 +03:00
|
|
|
if settings.contains('/Processing/LastOutputPath'):
|
|
|
|
path = settings.value('/Processing/LastOutputPath')
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2013-08-12 20:44:27 +02:00
|
|
|
path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)
|
2014-05-05 15:08:05 +03:00
|
|
|
|
|
|
|
encoding = settings.value('/Processing/encoding', 'System')
|
|
|
|
fileDialog = QgsEncodingFileDialog(
|
|
|
|
self, self.tr('Save file'), path, fileFilter, encoding)
|
2012-09-15 18:25:25 +03:00
|
|
|
fileDialog.setFileMode(QFileDialog.AnyFile)
|
|
|
|
fileDialog.setAcceptMode(QFileDialog.AcceptSave)
|
|
|
|
fileDialog.setConfirmOverwrite(True)
|
2014-05-05 15:08:05 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
if fileDialog.exec_() == QDialog.Accepted:
|
2012-12-02 19:13:32 +01:00
|
|
|
files = fileDialog.selectedFiles()
|
2012-12-10 00:12:07 +01:00
|
|
|
encoding = unicode(fileDialog.encoding())
|
2012-09-15 18:25:25 +03:00
|
|
|
self.output.encoding = encoding
|
2014-05-05 15:08:05 +03:00
|
|
|
fileName = unicode(files[0])
|
|
|
|
selectedFileFilter = unicode(fileDialog.selectedNameFilter())
|
|
|
|
if not fileName.lower().endswith(
|
|
|
|
tuple(re.findall("\*(\.[a-z]{1,5})", fileFilter))):
|
2014-05-27 00:16:17 +02:00
|
|
|
ext = re.search("\*(\.[a-z]{1,5})", selectedFileFilter)
|
2013-07-15 23:35:06 +02:00
|
|
|
if ext:
|
2014-05-05 15:08:05 +03:00
|
|
|
fileName += ext.group(1)
|
2014-11-06 11:32:44 +02:00
|
|
|
self.leText.setText(fileName)
|
2013-10-01 20:52:22 +03:00
|
|
|
settings.setValue('/Processing/LastOutputPath',
|
2014-05-05 15:08:05 +03:00
|
|
|
os.path.dirname(fileName))
|
2013-10-01 20:52:22 +03:00
|
|
|
settings.setValue('/Processing/encoding', encoding)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-05-05 15:08:05 +03:00
|
|
|
def selectDirectory(self):
|
|
|
|
lastDir = ''
|
|
|
|
|
|
|
|
dirName = QFileDialog.getExistingDirectory(self,
|
|
|
|
self.tr('Select directory'), lastDir, QFileDialog.ShowDirsOnly)
|
|
|
|
|
2014-11-06 11:32:44 +02:00
|
|
|
self.leText.setText(dirName)
|
2014-05-05 15:08:05 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def getValue(self):
|
2014-11-06 11:32:44 +02:00
|
|
|
fileName = unicode(self.leText.text())
|
2014-05-05 15:08:05 +03:00
|
|
|
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
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2014-05-05 15:08:05 +03:00
|
|
|
value = fileName
|
|
|
|
|
|
|
|
return value
|