2022-03-24 16:35:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
|
2022-03-08 09:27:40 +00:00
|
|
|
from ..Qt import QtWidgets, QtCore
|
|
|
|
|
|
|
|
|
2022-11-19 16:59:35 +00:00
|
|
|
class FileDialog(QtWidgets.QFileDialog):
|
|
|
|
last_path = None
|
|
|
|
|
2023-01-05 14:27:33 +00:00
|
|
|
def __init__(self, directory=None, caption=None, filter='', parent=None):
|
2022-03-08 09:27:40 +00:00
|
|
|
super().__init__(parent=parent)
|
|
|
|
|
|
|
|
self.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
|
|
|
|
|
|
|
|
self.setWindowTitle(caption)
|
2023-01-05 14:27:33 +00:00
|
|
|
if directory:
|
2022-11-19 16:59:35 +00:00
|
|
|
self.setDirectory(str(directory))
|
2023-01-05 14:27:33 +00:00
|
|
|
elif self.last_path is not None:
|
|
|
|
self.setDirectory(str(FileDialog.last_path))
|
|
|
|
|
|
|
|
self.setNameFilters(filter.split(';;'))
|
2022-03-08 09:27:40 +00:00
|
|
|
|
|
|
|
file_tree = self.findChild(QtWidgets.QTreeView, 'treeView')
|
|
|
|
file_tree.setSortingEnabled(True)
|
|
|
|
for i in range(file_tree.header().count()):
|
|
|
|
file_tree.header().setSectionResizeMode(i, 0)
|
|
|
|
file_tree.model().sort(0)
|
|
|
|
|
|
|
|
file_list = self.findChild(QtWidgets.QListView, 'listView')
|
|
|
|
file_list.model().sort(0)
|
|
|
|
|
|
|
|
line = QtWidgets.QFrame(self)
|
|
|
|
line.setFrameShape(line.HLine)
|
|
|
|
line.setFrameShadow(line.Sunken)
|
|
|
|
self.layout().addWidget(line, self.layout().rowCount(), 0, 1, self.layout().columnCount())
|
|
|
|
|
2022-11-19 16:59:35 +00:00
|
|
|
def save_file(self) -> pathlib.Path | None:
|
|
|
|
outfile = self.selectedFiles()
|
|
|
|
if outfile:
|
2023-01-05 14:27:33 +00:00
|
|
|
if self.is_valid(outfile[0]):
|
|
|
|
return pathlib.Path(outfile[0])
|
|
|
|
else:
|
|
|
|
_ = QtWidgets.QMessageBox.warning(self, 'Save file',
|
|
|
|
'Filename contains one or more invalid character: / * < > \\ | : "')
|
2022-11-19 16:59:35 +00:00
|
|
|
return
|
|
|
|
|
2023-01-05 14:27:33 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_valid(filename: str):
|
|
|
|
bad_character = r'/*<>\|:"'
|
|
|
|
for c in bad_character:
|
|
|
|
if c in filename:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
FileDialog.last_path = self.directory()
|
|
|
|
super().close()
|
|
|
|
|
2022-03-08 09:27:40 +00:00
|
|
|
|
2022-11-19 16:59:35 +00:00
|
|
|
class OpenFileDialog(FileDialog):
|
2023-01-05 14:27:33 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
2022-03-08 09:27:40 +00:00
|
|
|
|
|
|
|
self.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
|
|
|
|
|
|
|
|
self.checkBox = QtWidgets.QCheckBox(self)
|
|
|
|
self.checkBox.setChecked(False)
|
|
|
|
self.checkBox.setText('Use existing graph')
|
|
|
|
self.checkBox.stateChanged.connect(self.checkbox_state)
|
|
|
|
self.layout().addWidget(self.checkBox)
|
|
|
|
|
|
|
|
self.comboBox = QtWidgets.QComboBox(self)
|
|
|
|
self.comboBox.setEnabled(False)
|
|
|
|
self.layout().addWidget(self.comboBox)
|
|
|
|
|
|
|
|
self.add_to_graph = None
|
|
|
|
|
|
|
|
def set_graphs(self, graphs):
|
|
|
|
self.comboBox.blockSignals(True)
|
|
|
|
for gid, name in graphs:
|
|
|
|
self.comboBox.addItem(name, userData=gid)
|
|
|
|
self.comboBox.blockSignals(False)
|
|
|
|
|
|
|
|
if not self.comboBox.count():
|
|
|
|
self.comboBox.hide()
|
|
|
|
self.checkBox.hide()
|
|
|
|
|
|
|
|
def checkbox_state(self, checked: QtCore.Qt.CheckState):
|
|
|
|
if checked == QtCore.Qt.Checked:
|
|
|
|
self.comboBox.setEnabled(True)
|
|
|
|
self.add_to_graph = self.comboBox.currentData()
|
|
|
|
else:
|
|
|
|
self.comboBox.setEnabled(False)
|
|
|
|
self.add_to_graph = None
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot(int, name='on_comboBox_currentIndexChanged')
|
|
|
|
def graph_changed(self, idx: int):
|
|
|
|
self.add_to_graph = self.comboBox.itemData(idx)
|
|
|
|
|
|
|
|
|
2022-11-19 16:59:35 +00:00
|
|
|
class SaveDirectoryDialog(FileDialog):
|
2023-01-05 14:27:33 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
2022-03-08 09:27:40 +00:00
|
|
|
|
|
|
|
self.setOption(QtWidgets.QFileDialog.DontConfirmOverwrite, False)
|
|
|
|
self.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
|
|
|
|
|
2022-03-24 16:35:10 +00:00
|
|
|
lay = self.layout()
|
|
|
|
|
2022-03-08 09:27:40 +00:00
|
|
|
self.label = QtWidgets.QLabel(self)
|
|
|
|
self.label.setTextFormat(QtCore.Qt.RichText)
|
|
|
|
self.label.setText('Use <b><label></b> as placeholder in filename. (e.g. <i>t1_<label>.dat</i>)')
|
2022-03-24 16:35:10 +00:00
|
|
|
lay.addWidget(self.label, lay.rowCount(), 0, 1, lay.columnCount())
|
|
|
|
|
|
|
|
line = QtWidgets.QFrame(self)
|
|
|
|
line.setFrameShape(line.HLine)
|
|
|
|
line.setFrameShadow(line.Sunken)
|
|
|
|
lay.addWidget(line, lay.rowCount(), 0, 1, lay.columnCount())
|
|
|
|
|
|
|
|
h_layout = QtWidgets.QHBoxLayout()
|
|
|
|
h_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
h_layout.setSpacing(3)
|
2022-03-08 09:27:40 +00:00
|
|
|
|
|
|
|
self.checkBox = QtWidgets.QCheckBox(self)
|
|
|
|
self.checkBox.setChecked(True)
|
2022-03-24 16:35:10 +00:00
|
|
|
self.checkBox.setText('Replace spaces with _')
|
|
|
|
h_layout.addWidget(self.checkBox)
|
|
|
|
|
|
|
|
self.agr_cb = QtWidgets.QCheckBox(self)
|
|
|
|
self.agr_cb.setChecked(True)
|
|
|
|
self.agr_cb.setText('Save graph as Grace file')
|
|
|
|
h_layout.addWidget(self.agr_cb)
|
|
|
|
|
|
|
|
self.fit_cb = QtWidgets.QCheckBox(self)
|
|
|
|
self.fit_cb.setChecked(True)
|
|
|
|
self.fit_cb.setText('Save fit parameter')
|
|
|
|
h_layout.addWidget(self.fit_cb)
|
|
|
|
|
|
|
|
lay.addLayout(h_layout, lay.rowCount(), 0, 1, lay.columnCount())
|
2022-03-08 09:27:40 +00:00
|
|
|
|
|
|
|
self.setWindowTitle('Save')
|
2022-03-24 16:35:10 +00:00
|
|
|
self.setNameFilters(['All files (*.*)', 'Session file (*.nmr)', 'Text file (*.dat)',
|
|
|
|
'HDF file (*.h5)', 'Grace files (*.agr)'])
|