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

154 lines
6.8 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
BatchOutputSelectionPanel.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. *
* *
***************************************************************************
"""
from __future__ import print_function
2016-09-21 18:24:26 +02:00
from builtins import str
from builtins import range
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
2012-10-05 23:28:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-05 23:28:47 +02:00
__revision__ = '$Format:%H$'
import os
2015-10-01 08:13:09 +02:00
import re
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtWidgets import QWidget, QPushButton, QLineEdit, QHBoxLayout, QSizePolicy, QFileDialog
from qgis.PyQt.QtCore import QSettings
from processing.gui.AutofillDialog import AutofillDialog
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterFixedTable
from processing.core.outputs import OutputDirectory
2012-09-15 18:25:25 +03:00
class BatchOutputSelectionPanel(QWidget):
2012-09-15 18:25:25 +03:00
def __init__(self, output, alg, row, col, panel):
2012-09-15 18:25:25 +03:00
super(BatchOutputSelectionPanel, self).__init__(None)
self.alg = alg
self.row = row
self.col = col
self.output = output
self.panel = panel
self.table = self.panel.tblParameters
self.horizontalLayout = QHBoxLayout(self)
2012-09-15 18:25:25 +03:00
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setMargin(0)
self.text = QLineEdit()
self.text.setText('')
self.text.setMinimumWidth(300)
self.text.setSizePolicy(QSizePolicy.Expanding,
QSizePolicy.Expanding)
2012-09-15 18:25:25 +03:00
self.horizontalLayout.addWidget(self.text)
self.pushButton = QPushButton()
self.pushButton.setText('...')
2012-09-15 18:25:25 +03:00
self.pushButton.clicked.connect(self.showSelectionDialog)
self.horizontalLayout.addWidget(self.pushButton)
self.setLayout(self.horizontalLayout)
def showSelectionDialog(self):
if isinstance(self.output, OutputDirectory):
self.selectDirectory()
return
2012-09-15 18:25:25 +03:00
filefilter = self.output.getFileFilter(self.alg)
settings = QSettings()
if settings.contains('/Processing/LastBatchOutputPath'):
2016-09-21 18:24:26 +02:00
path = str(settings.value('/Processing/LastBatchOutputPath'))
else:
path = ''
filename, selectedFileFilter = QFileDialog.getSaveFileName(self,
self.tr('Save file'), path, filefilter)
2012-09-15 18:25:25 +03:00
if filename:
if not filename.lower().endswith(
tuple(re.findall("\*(\.[a-z]{1,10})", filefilter))):
ext = re.search("\*(\.[a-z]{1,10})", selectedFileFilter)
if ext:
filename += ext.group(1)
settings.setValue('/Processing/LastBatchOutputPath', os.path.dirname(filename))
2012-09-15 18:25:25 +03:00
dlg = AutofillDialog(self.alg)
dlg.exec_()
if dlg.mode is not None:
2012-09-15 18:25:25 +03:00
try:
if dlg.mode == AutofillDialog.DO_NOT_AUTOFILL:
self.table.cellWidget(self.row,
self.col).setValue(filename)
2012-09-15 18:25:25 +03:00
elif dlg.mode == AutofillDialog.FILL_WITH_NUMBERS:
n = self.table.rowCount() - self.row
for i in range(n):
name = filename[:filename.rfind('.')] \
2016-09-21 18:24:26 +02:00
+ str(i + 1) + filename[filename.rfind('.'):]
self.table.cellWidget(i + self.row,
self.col).setValue(name)
2012-09-15 18:25:25 +03:00
elif dlg.mode == AutofillDialog.FILL_WITH_PARAMETER:
n = self.table.rowCount() - self.row
for i in range(n):
widget = self.table.cellWidget(i + self.row,
dlg.param)
2012-09-15 18:25:25 +03:00
param = self.alg.parameters[dlg.param]
if isinstance(param, (ParameterRaster,
ParameterVector, ParameterTable,
ParameterMultipleInput)):
2016-09-21 18:24:26 +02:00
s = str(widget.getText())
2012-09-15 18:25:25 +03:00
s = os.path.basename(s)
s = os.path.splitext(s)[0]
2012-09-15 18:25:25 +03:00
elif isinstance(param, ParameterBoolean):
2016-09-21 18:24:26 +02:00
s = str(widget.currentIndex() == 0)
2012-09-15 18:25:25 +03:00
elif isinstance(param, ParameterSelection):
2016-09-21 18:24:26 +02:00
s = str(widget.currentText())
2012-09-15 18:25:25 +03:00
elif isinstance(param, ParameterFixedTable):
2016-09-21 18:24:26 +02:00
s = str(widget.table)
2012-09-15 18:25:25 +03:00
else:
2016-09-21 18:24:26 +02:00
s = str(widget.text())
name = filename[:filename.rfind('.')] + s \
+ filename[filename.rfind('.'):]
self.table.cellWidget(i + self.row,
self.col).setValue(name)
2012-09-15 18:25:25 +03:00
except:
pass
def selectDirectory(self):
settings = QSettings()
if settings.contains('/Processing/LastBatchOutputPath'):
2016-09-21 18:24:26 +02:00
lastDir = str(settings.value('/Processing/LastBatchOutputPath'))
else:
lastDir = ''
dirName = QFileDialog.getExistingDirectory(self,
self.tr('Select directory'), lastDir, QFileDialog.ShowDirsOnly)
if dirName:
self.table.cellWidget(self.row, self.col).setValue(dirName)
settings.setValue('/Processing/LastBatchOutputPath', dirName)
2012-09-15 18:25:25 +03:00
def setValue(self, text):
return self.text.setText(text)
def getValue(self):
2016-09-21 18:24:26 +02:00
return str(self.text.text())