From aa900effb0045b7b9ed309c81d5e2cd60e90b316 Mon Sep 17 00:00:00 2001 From: Simon Renblad Date: Mon, 8 Jan 2024 14:26:38 +0800 Subject: [PATCH] gui.tools: add get_open_file_name --- artiq/gui/tools.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/artiq/gui/tools.py b/artiq/gui/tools.py index 441da9b81..2171ab915 100644 --- a/artiq/gui/tools.py +++ b/artiq/gui/tools.py @@ -69,6 +69,23 @@ async def get_open_file_name(parent, caption, dir, filter): return await fut +async def get_save_file_name(parent, caption, dir, filter, suffix=None): + """like QtWidgets.QFileDialog.getSaveFileName(), but a coroutine""" + dialog = QtWidgets.QFileDialog(parent, caption, dir, filter) + dialog.setFileMode(dialog.AnyFile) + dialog.setAcceptMode(dialog.AcceptSave) + if suffix is not None: + dialog.setDefaultSuffix(suffix) + fut = asyncio.Future() + + def on_accept(): + fut.set_result(dialog.selectedFiles()[0]) + dialog.accepted.connect(on_accept) + dialog.rejected.connect(fut.cancel) + dialog.open() + return await fut + + # Based on: # http://stackoverflow.com/questions/250890/using-qsortfilterproxymodel-with-a-tree-model class QRecursiveFilterProxyModel(QtCore.QSortFilterProxyModel):