Fix dialog interaction by moving it to main thread

This commit is contained in:
Ben 2024-02-04 14:53:06 +01:00
parent 6c0f1dcbce
commit bd46237f69
Signed by: ben
GPG key ID: 0F54A7ED232D3319

View file

@ -12,7 +12,7 @@ from NodeGraphQt import NodeGraph, Port
from NodeGraphQt.constants import PortTypeEnum, NodePropWidgetEnum, ViewerEnum from NodeGraphQt.constants import PortTypeEnum, NodePropWidgetEnum, ViewerEnum
from NodeGraphQt.widgets.node_widgets import _NodeGroupBox, NodeLineEdit, NodeCheckBox from NodeGraphQt.widgets.node_widgets import _NodeGroupBox, NodeLineEdit, NodeCheckBox
from PySide2 import QtGui from PySide2 import QtGui
from PySide2.QtCore import Qt, QObject, QThread from PySide2.QtCore import Qt, QObject, QThread, Slot, Signal
from PySide2.QtWidgets import QSlider, QLineEdit, QCheckBox, QGraphicsItem, QInputDialog, QDialog, QLabel, QSizePolicy from PySide2.QtWidgets import QSlider, QLineEdit, QCheckBox, QGraphicsItem, QInputDialog, QDialog, QLabel, QSizePolicy
from Qt import QtWidgets from Qt import QtWidgets
from sqlalchemy import create_engine from sqlalchemy import create_engine
@ -141,8 +141,9 @@ class AsyncResourceFinder(QThread):
resource = data_provider.update_resource_recipes(session=session, resource=resource) resource = data_provider.update_resource_recipes(session=session, resource=resource)
if self.debug: if self.debug:
print("AsyncResourceFinder updated resource", resource) print("AsyncResourceFinder updated resource", resource)
self.result.set_result(resource)
session.commit() session.commit()
session.refresh(resource)
self.result.set_result(resource)
except: except:
self.result.set_exception(sys.exception()) self.result.set_exception(sys.exception())
@ -444,6 +445,10 @@ class Machine(BaseNode):
class GraphController(QObject): class GraphController(QObject):
prompt_dialog_show = Signal(str, list, str)
loading_dialog_show = Signal()
loading_dialog_hide = Signal()
def __init__(self, debug: bool, parent=None): def __init__(self, debug: bool, parent=None):
super().__init__(parent=parent) super().__init__(parent=parent)
self.debug = debug self.debug = debug
@ -461,19 +466,39 @@ class GraphController(QObject):
self.graph.port_connected.connect(self.on_port_connected) self.graph.port_connected.connect(self.on_port_connected)
self.prompt_dialog = QInputDialog(parent=self.graph.widget) self._prompt_dialog = QInputDialog(parent=self.graph.widget)
self.prompt_dialog.setStyleSheet(generate_fgbg_stylesheet()) self._prompt_dialog.setStyleSheet(generate_fgbg_stylesheet())
self.prompt_dialog.setModal(True) self._prompt_dialog.setModal(True)
self._dialog_future = Future() self._dialog_future = Future()
self.prompt_dialog.rejected.connect(lambda: self._dialog_future.set_result(None)) self._prompt_dialog.rejected.connect(lambda: self._dialog_future.set_result(None))
self.prompt_dialog.accepted.connect(lambda: self._dialog_future.set_result(self.prompt_dialog.textValue())) self._prompt_dialog.accepted.connect(lambda: self._dialog_future.set_result(self._prompt_dialog.textValue()))
self.prompt_dialog_show.connect(self.show_prompt_dialog)
self.loading_dialog = QDialog(parent=self.graph.widget, f=(Qt.Dialog | Qt.ToolTip)) self._loading_dialog = QDialog(parent=self.graph.widget, f=(Qt.Dialog | Qt.ToolTip))
self.loading_dialog.setStyleSheet(generate_fgbg_stylesheet(0.5)) self._loading_dialog.setStyleSheet(generate_fgbg_stylesheet(0.5))
self.loading_dialog.setModal(True) self._loading_dialog.setModal(True)
self.loading_dialog.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._loading_dialog.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
label = QLabel("Loading…", self.loading_dialog) label = QLabel("Loading…", self._loading_dialog)
label.setAlignment(Qt.AlignCenter) label.setAlignment(Qt.AlignCenter)
self.loading_dialog_show.connect(self.show_loading_dialog)
self.loading_dialog_hide.connect(self.hide_loading_dialog)
@Slot(str, list, str)
def show_prompt_dialog(self, text: str, options: list, default: str = ""):
assert self._prompt_dialog.isHidden(), "Prompt dialog already visible"
self._prompt_dialog.setLabelText(text)
self._prompt_dialog.setComboBoxItems(options)
if default:
self._prompt_dialog.setTextValue(default)
self._prompt_dialog.show()
@Slot()
def show_loading_dialog(self):
self._loading_dialog.show()
@Slot()
def hide_loading_dialog(self):
self._loading_dialog.hide()
def add_machine_from_search(self, search: str): def add_machine_from_search(self, search: str):
recipe_selected_future = Future() recipe_selected_future = Future()
@ -642,8 +667,8 @@ class GraphController(QObject):
debug=self.debug, debug=self.debug,
) )
fetch_future = Future() fetch_future = Future()
self.loading_dialog.show() self.loading_dialog_show.emit()
fetch_future.add_done_callback(lambda fut: self.loading_dialog.hide()) fetch_future.add_done_callback(lambda fut: self.loading_dialog_hide.emit())
fetcher_thread.finished.connect(lambda: fetch_future.set_result(None)) fetcher_thread.finished.connect(lambda: fetch_future.set_result(None))
fetcher_thread.start() fetcher_thread.start()
return fetch_future return fetch_future
@ -665,22 +690,16 @@ class GraphController(QObject):
prompt=show_prompt, prompt=show_prompt,
debug=self.debug, debug=self.debug,
) )
self.loading_dialog.show() self.loading_dialog_show.emit()
loading_future.add_done_callback(lambda fut: self.loading_dialog.hide()) loading_future.add_done_callback(lambda fut: self.loading_dialog_hide.emit())
fetcher_thread.start() fetcher_thread.start()
return fetcher_thread.result return fetcher_thread.result
def dialog_prompt(self, options: dict[int, str], text: str, default: int) -> Future[int | None]: def dialog_prompt(self, options: dict[int, str], text: str, default: int) -> Future[int | None]:
assert self.prompt_dialog.isHidden(), "Prompt dialog already visible"
if self.debug: if self.debug:
print("Displaying QInputDialog with options:", ", ".join(options.values())) print("Displaying QInputDialog with options:", ", ".join(options.values()))
self.prompt_dialog.setLabelText(text)
if default in options:
self.prompt_dialog.setTextValue(options[default])
reversed_options: dict[str, int] = {value: key for key, value in options.items()} reversed_options: dict[str, int] = {value: key for key, value in options.items()}
self.prompt_dialog.setComboBoxItems(reversed_options.keys())
ret = Future() ret = Future()
self._dialog_future = Future() self._dialog_future = Future()
@ -692,7 +711,7 @@ class GraphController(QObject):
ret.set_result(reversed_options[result]) ret.set_result(reversed_options[result])
self._dialog_future.add_done_callback(map_result) self._dialog_future.add_done_callback(map_result)
self.prompt_dialog.show() self.prompt_dialog_show.emit(text, list(reversed_options.keys()), options.get(default, ""))
return ret return ret