QGIS/python/plugins/processing/gui/OutputSelectionPanel.py

113 lines
4.9 KiB
Python
Raw Normal View History

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. *
* *
***************************************************************************
"""
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
import os.path
import re
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.gui import *
2013-08-12 20:44:27 +02:00
from processing.core.ProcessingConfig import ProcessingConfig
from processing.outputs.OutputVector import OutputVector
2012-09-15 18:25:25 +03:00
class OutputSelectionPanel(QWidget):
lastOutputFolder = None
SAVE_TO_TEMP_FILE = "[Save to temporary file]"
def __init__(self, output, alg):
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)
2012-09-15 18:25:25 +03:00
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())
def saveToTemporaryFile(self):
self.text.setText("")
def saveToMemory(self):
self.text.setText("memory:")
def saveToFile(self):
filefilter = self.output.getFileFilter(self.alg)
settings = QSettings()
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)
lastEncoding = settings.value("/Processing/encoding", "System")
fileDialog = QgsEncodingFileDialog(self, "Save file", path, filefilter, lastEncoding)
2012-09-15 18:25:25 +03:00
fileDialog.setFileMode(QFileDialog.AnyFile)
fileDialog.setAcceptMode(QFileDialog.AcceptSave)
fileDialog.setConfirmOverwrite(True)
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
2013-06-11 17:11:53 +10:00
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)
2012-11-30 06:13:54 +01:00
self.text.setText(filename)
settings.setValue("/Processing/LastOutputPath", os.path.dirname(filename))
settings.setValue("/Processing/encoding", encoding)
2012-09-15 18:25:25 +03:00
def getValue(self):
2013-06-03 21:25:22 +02:00
filename = unicode(self.text.text())
2012-09-15 18:25:25 +03:00
if filename.strip() == "" or filename == OutputSelectionPanel.SAVE_TO_TEMP_FILE:
return None
if filename.startswith("memory:"):
return filename
else:
if not os.path.isabs(filename):
2013-08-12 20:44:27 +02:00
filename = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER) + os.sep + filename
2012-09-15 18:25:25 +03:00
return filename