|
27 | 27 |
|
28 | 28 | import os.path |
29 | 29 | import re |
| 30 | + |
30 | 31 | from PyQt4.QtCore import * |
31 | 32 | from PyQt4.QtGui import * |
32 | | -from qgis.gui import * |
| 33 | + |
33 | 34 | from processing.core.ProcessingConfig import ProcessingConfig |
34 | 35 | from processing.outputs.OutputVector import OutputVector |
| 36 | +from processing.outputs.OutputDirectory import OutputDirectory |
35 | 37 |
|
| 38 | +from processing.ui.ui_widgetOutputSelect import Ui_widgetOutputSelect |
36 | 39 |
|
37 | | -class OutputSelectionPanel(QWidget): |
| 40 | +class OutputSelectionPanel(QWidget, Ui_widgetOutputSelect): |
38 | 41 |
|
39 | | - lastOutputFolder = None |
40 | | - SAVE_TO_TEMP_FILE = '[Save to temporary file]' |
| 42 | + SAVE_TO_TEMP_FILE = QCoreApplication.translate( |
| 43 | + 'OutputSelectionPanel', '[Save to temporary file]') |
41 | 44 |
|
42 | 45 | def __init__(self, output, alg): |
| 46 | + QWidget.__init__(self) |
| 47 | + self.setupUi(self) |
| 48 | + |
43 | 49 | self.output = output |
44 | 50 | self.alg = alg |
45 | | - super(OutputSelectionPanel, self).__init__(None) |
46 | | - self.horizontalLayout = QHBoxLayout(self) |
47 | | - self.horizontalLayout.setSpacing(2) |
48 | | - self.horizontalLayout.setMargin(0) |
49 | | - self.text = QLineEdit() |
| 51 | + |
50 | 52 | if hasattr(self.text, 'setPlaceholderText'): |
51 | | - self.text.setPlaceholderText( |
52 | | - OutputSelectionPanel.SAVE_TO_TEMP_FILE) |
53 | | - self.text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) |
54 | | - self.horizontalLayout.addWidget(self.text) |
55 | | - self.pushButton = QPushButton() |
56 | | - self.pushButton.setText('...') |
57 | | - self.pushButton.clicked.connect(self.buttonPushed) |
58 | | - self.horizontalLayout.addWidget(self.pushButton) |
59 | | - self.setLayout(self.horizontalLayout) |
60 | | - |
61 | | - def buttonPushed(self): |
62 | | - popupmenu = QMenu() |
63 | | - saveToTemporaryFileAction = QAction('Save to a temporary file', |
64 | | - self.pushButton) |
65 | | - saveToTemporaryFileAction.triggered.connect(self.saveToTemporaryFile) |
66 | | - popupmenu.addAction(saveToTemporaryFileAction) |
67 | | - if isinstance(self.output, OutputVector) \ |
68 | | - and self.alg.provider.supportsNonFileBasedOutput(): |
69 | | - saveToMemoryAction = QAction('Save to a memory layer', |
70 | | - self.pushButton) |
71 | | - saveToMemoryAction.triggered.connect(self.saveToMemory) |
72 | | - popupmenu.addAction(saveToMemoryAction) |
73 | | - saveToFileAction = QAction('Save to file...', self.pushButton) |
74 | | - saveToFileAction.triggered.connect(self.saveToFile) |
75 | | - popupmenu.addAction(saveToFileAction) |
76 | | - |
77 | | - popupmenu.exec_(QCursor.pos()) |
| 53 | + self.text.setPlaceholderText(self.SAVE_TO_TEMP_FILE) |
| 54 | + |
| 55 | + self.btnBrowse.clicked.connect(self.selectOutput) |
| 56 | + |
| 57 | + def selectOutput(self): |
| 58 | + if isinstance(self.output, OutputDirectory): |
| 59 | + self.selectDirectory() |
| 60 | + else: |
| 61 | + popupMenu = QMenu() |
| 62 | + |
| 63 | + actionSaveToTempFile = QAction( |
| 64 | + self.tr('Save to a temporary file'), self.btnBrowse) |
| 65 | + actionSaveToTempFile.triggered.connect(self.saveToTemporaryFile) |
| 66 | + popupMenu.addAction(actionSaveToTempFile) |
| 67 | + |
| 68 | + actionSaveToFile = QAction( |
| 69 | + self.tr('Save to file...'), self.btnBrowse) |
| 70 | + actionSaveToFile.triggered.connect(self.selectFile) |
| 71 | + popupMenu.addAction(actionSaveToFile) |
| 72 | + |
| 73 | + if isinstance(self.output, OutputVector) \ |
| 74 | + and self.alg.provider.supportsNonFileBasedOutput(): |
| 75 | + actionSaveToMemory = QAction( |
| 76 | + self.tr('Save to memory layer'), self.btnBrowse) |
| 77 | + actionSaveToMemory.triggered.connect(self.saveToMemory) |
| 78 | + popupMenu.addAction(actionSaveToMemory) |
| 79 | + |
| 80 | + popupMenu.exec_(QCursor.pos()) |
78 | 81 |
|
79 | 82 | def saveToTemporaryFile(self): |
80 | 83 | self.text.setText('') |
81 | 84 |
|
82 | 85 | def saveToMemory(self): |
83 | 86 | self.text.setText('memory:') |
84 | 87 |
|
85 | | - def saveToFile(self): |
86 | | - filefilter = self.output.getFileFilter(self.alg) |
| 88 | + def selectFile(self): |
| 89 | + fileFilter = self.output.getFileFilter(self.alg) |
| 90 | + |
87 | 91 | settings = QSettings() |
88 | 92 | if settings.contains('/Processing/LastOutputPath'): |
89 | 93 | path = settings.value('/Processing/LastOutputPath') |
90 | 94 | else: |
91 | 95 | path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER) |
92 | | - lastEncoding = settings.value('/Processing/encoding', 'System') |
93 | | - fileDialog = QgsEncodingFileDialog(self, 'Save file', path, |
94 | | - filefilter, lastEncoding) |
| 96 | + |
| 97 | + encoding = settings.value('/Processing/encoding', 'System') |
| 98 | + fileDialog = QgsEncodingFileDialog( |
| 99 | + self, self.tr('Save file'), path, fileFilter, encoding) |
95 | 100 | fileDialog.setFileMode(QFileDialog.AnyFile) |
96 | 101 | fileDialog.setAcceptMode(QFileDialog.AcceptSave) |
97 | 102 | fileDialog.setConfirmOverwrite(True) |
| 103 | + |
98 | 104 | if fileDialog.exec_() == QDialog.Accepted: |
99 | 105 | files = fileDialog.selectedFiles() |
100 | 106 | encoding = unicode(fileDialog.encoding()) |
101 | 107 | self.output.encoding = encoding |
102 | | - filename = unicode(files[0]) |
103 | | - selectedFilefilter = unicode(fileDialog.selectedNameFilter()) |
104 | | - if not filename.lower().endswith( |
105 | | - tuple(re.findall("\*(\.[a-z]{1,5})", filefilter))): |
| 108 | + fileName = unicode(files[0]) |
| 109 | + selectedFileFilter = unicode(fileDialog.selectedNameFilter()) |
| 110 | + if not fileName.lower().endswith( |
| 111 | + tuple(re.findall("\*(\.[a-z]{1,5})", fileFilter))): |
106 | 112 | ext = re.search("\*(\.[a-z]{1,5})", selectedFilefilter) |
107 | 113 | if ext: |
108 | | - filename = filename + ext.group(1) |
109 | | - self.text.setText(filename) |
| 114 | + fileName += ext.group(1) |
| 115 | + self.text.setText(fileName) |
110 | 116 | settings.setValue('/Processing/LastOutputPath', |
111 | | - os.path.dirname(filename)) |
| 117 | + os.path.dirname(fileName)) |
112 | 118 | settings.setValue('/Processing/encoding', encoding) |
113 | 119 |
|
| 120 | + def selectDirectory(self): |
| 121 | + lastDir = '' |
| 122 | + |
| 123 | + dirName = QFileDialog.getExistingDirectory(self, |
| 124 | + self.tr('Select directory'), lastDir, QFileDialog.ShowDirsOnly) |
| 125 | + |
| 126 | + self.text.setText(dirName) |
| 127 | + |
114 | 128 | def getValue(self): |
115 | | - filename = unicode(self.text.text()) |
116 | | - if filename.strip() == '' or filename \ |
117 | | - == OutputSelectionPanel.SAVE_TO_TEMP_FILE: |
118 | | - return None |
119 | | - if filename.startswith('memory:'): |
120 | | - return filename |
| 129 | + fileName = unicode(self.text.text()) |
| 130 | + if fileName.strip() in ['', self.SAVE_TO_TEMP_FILE]: |
| 131 | + value = None |
| 132 | + elif fileName.startswith('memory:'): |
| 133 | + value = fileName |
| 134 | + elif not os.path.isabs(fileName): |
| 135 | + value = ProcessingConfig.getSetting( |
| 136 | + ProcessingConfig.OUTPUT_FOLDER) + os.sep + fileName |
121 | 137 | else: |
122 | | - if not os.path.isabs(filename): |
123 | | - filename = ProcessingConfig.getSetting( |
124 | | - ProcessingConfig.OUTPUT_FOLDER) + os.sep + filename |
125 | | - return filename |
| 138 | + value = fileName |
| 139 | + |
| 140 | + return value |
0 commit comments