nmreval/src/gui_qt/io/filedialog.py

127 lines
4.4 KiB
Python
Raw Normal View History

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
class FileDialog(QtWidgets.QFileDialog):
last_path = None
2022-03-08 09:27:40 +00:00
def __init__(self, directory=None, caption=None, filters='', parent=None):
super().__init__(parent=parent)
self.setOption(QtWidgets.QFileDialog.DontUseNativeDialog, True)
self.setWindowTitle(caption)
if directory is not None:
self.setDirectory(str(directory))
2022-03-08 09:27:40 +00:00
self.setNameFilters(filters.split(';;'))
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())
def save_file(self) -> pathlib.Path | None:
outfile = self.selectedFiles()
if outfile:
return pathlib.Path(outfile[0])
return
2022-03-08 09:27:40 +00:00
class OpenFileDialog(FileDialog):
2022-03-08 09:27:40 +00:00
def __init__(self, directory=None, caption=None, filters='', parent=None):
super().__init__(directory=directory, caption=caption, filters=filters, parent=parent)
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)
class SaveDirectoryDialog(FileDialog):
2022-03-08 09:27:40 +00:00
def __init__(self, directory=None, filters='', parent=None):
super().__init__(directory=directory, filters=filters, parent=parent)
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>&lt;label&gt;</b> as placeholder in filename. (e.g. <i>t1_&lt;label&gt;.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)'])