From 45d694822cddeaf33db23651152dda4379b6ca68 Mon Sep 17 00:00:00 2001 From: Johnny Chan Date: Mon, 21 Aug 2023 23:36:45 +1200 Subject: [PATCH] minor clean up. --- NodeGraphQt/base/graph.py | 53 +++++++++++++++++++++------------- NodeGraphQt/pkg_info.py | 2 +- NodeGraphQt/widgets/dialogs.py | 14 ++++----- NodeGraphQt/widgets/viewer.py | 28 +++++++++++++----- 4 files changed, 61 insertions(+), 36 deletions(-) diff --git a/NodeGraphQt/base/graph.py b/NodeGraphQt/base/graph.py index bd548f3b..cc484245 100644 --- a/NodeGraphQt/base/graph.py +++ b/NodeGraphQt/base/graph.py @@ -1478,7 +1478,7 @@ def delete_nodes(self, nodes, push_undo=True): def extract_nodes(self, nodes, push_undo=True, prompt_warning=True): """ - Extract select nodes from it connections. + Extract select nodes from its connections. Args: nodes (list[NodeGraphQt.BaseNode]): list of node instances. @@ -1843,7 +1843,8 @@ def serialize_session(self): """ return self._serialize(self.all_nodes()) - def deserialize_session(self, layout_data): + def deserialize_session(self, layout_data, clear_session=True, + clear_undo_stack=True): """ Load node graph session from a dictionary object. @@ -1854,11 +1855,15 @@ def deserialize_session(self, layout_data): Args: layout_data (dict): dictionary object containing a node session. + clear_session (bool): clear current session. + clear_undo_stack (bool): clear the undo stack. """ - self.clear_session() + if clear_session: + self.clear_session() self._deserialize(layout_data) self.clear_selection() - self._undo_stack.clear() + if clear_undo_stack: + self._undo_stack.clear() def save_session(self, file_path): """ @@ -1872,7 +1877,7 @@ def save_session(self, file_path): Args: file_path (str): path to the saved node layout. """ - serialized_data = self._serialize(self.all_nodes()) + serialized_data = self.serialize_session() file_path = file_path.strip() def default(obj): @@ -1906,14 +1911,15 @@ def load_session(self, file_path): raise IOError('file does not exist: {}'.format(file_path)) self.clear_session() - self.import_session(file_path) + self.import_session(file_path, clear_undo_stack=True) - def import_session(self, file_path): + def import_session(self, file_path, clear_undo_stack=True): """ - Import node graph session layout file. + Import node graph into the current session. Args: file_path (str): path to the serialized layout file. + clear_undo_stack (bool): clear the undo stack after import. """ file_path = file_path.strip() if not os.path.isfile(file_path): @@ -1929,15 +1935,18 @@ def import_session(self, file_path): if not layout_data: return - self._deserialize(layout_data) - self._undo_stack.clear() + self.deserialize_session( + layout_data, + clear_session=False, + clear_undo_stack=clear_undo_stack + ) self._model.session = file_path self.session_changed.emit(file_path) def copy_nodes(self, nodes=None): """ - Copy nodes to the clipboard. + Copy nodes to the clipboard as a JSON formatted ``str``. See Also: :meth:`NodeGraph.cut_nodes` @@ -1959,7 +1968,7 @@ def copy_nodes(self, nodes=None): def cut_nodes(self, nodes=None): """ - Cut nodes to the clipboard. + Cut nodes to the clipboard as a JSON formatted ``str``. Note: This function doesn't trigger the @@ -2245,7 +2254,7 @@ def auto_layout_nodes(self, nodes=None, down_stream=True, start_nodes=None): # convenience dialog functions. # -------------------------------------------------------------------------- - def question_dialog(self, text, title='Node Graph'): + def question_dialog(self, text, title='Node Graph', parent=None): """ Prompts a question open dialog with ``"Yes"`` and ``"No"`` buttons in the node graph. @@ -2257,13 +2266,14 @@ def question_dialog(self, text, title='Node Graph'): Args: text (str): question text. title (str): dialog window title. + parent (QtWidgets.QObject): override dialog parent. (optional) Returns: bool: true if user clicked yes. """ - return self._viewer.question_dialog(text, title) + return self._viewer.question_dialog(text, title, parent) - def message_dialog(self, text, title='Node Graph'): + def message_dialog(self, text, title='Node Graph', parent=None): """ Prompts a file open dialog in the node graph. @@ -2274,10 +2284,11 @@ def message_dialog(self, text, title='Node Graph'): Args: text (str): message text. title (str): dialog window title. + parent (QtWidgets.QObject): override dialog parent. (optional) """ - self._viewer.message_dialog(text, title) + self._viewer.message_dialog(text, title, parent) - def load_dialog(self, current_dir=None, ext=None): + def load_dialog(self, current_dir=None, ext=None, parent=None): """ Prompts a file open dialog in the node graph. @@ -2288,13 +2299,14 @@ def load_dialog(self, current_dir=None, ext=None): Args: current_dir (str): path to a directory. ext (str): custom file type extension (default: ``"json"``) + parent (QtWidgets.QObject): override dialog parent. (optional) Returns: str: selected file path. """ - return self._viewer.load_dialog(current_dir, ext) + return self._viewer.load_dialog(current_dir, ext, parent) - def save_dialog(self, current_dir=None, ext=None): + def save_dialog(self, current_dir=None, ext=None, parent=None): """ Prompts a file save dialog in the node graph. @@ -2305,11 +2317,12 @@ def save_dialog(self, current_dir=None, ext=None): Args: current_dir (str): path to a directory. ext (str): custom file type extension (default: ``"json"``) + parent (QtWidgets.QObject): override dialog parent. (optional) Returns: str: selected file path. """ - return self._viewer.save_dialog(current_dir, ext) + return self._viewer.save_dialog(current_dir, ext, parent) # group node / sub graph. # -------------------------------------------------------------------------- diff --git a/NodeGraphQt/pkg_info.py b/NodeGraphQt/pkg_info.py index 274b2646..0a3476e5 100644 --- a/NodeGraphQt/pkg_info.py +++ b/NodeGraphQt/pkg_info.py @@ -1,6 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -__version__ = '0.6.18' +__version__ = '0.6.19' __status__ = 'Work in Progress' __license__ = 'MIT' diff --git a/NodeGraphQt/widgets/dialogs.py b/NodeGraphQt/widgets/dialogs.py index fd88d95a..121a66cf 100644 --- a/NodeGraphQt/widgets/dialogs.py +++ b/NodeGraphQt/widgets/dialogs.py @@ -5,7 +5,7 @@ _current_user_directory = os.path.expanduser('~') -def set_dir(file): +def _set_dir(file): global _current_user_directory if os.path.isdir(file): _current_user_directory = file @@ -24,7 +24,7 @@ def getSaveFileName(parent=None, title='Save File', file_dir=None, parent, title, file_dir, ext_filter) file = file_dlg[0] or None if file: - set_dir(file) + _set_dir(file) return file_dlg @staticmethod @@ -36,23 +36,23 @@ def getOpenFileName(parent=None, title='Open File', file_dir=None, parent, title, file_dir, ext_filter) file = file_dlg[0] or None if file: - set_dir(file) + _set_dir(file) return file_dlg class BaseDialog(object): @staticmethod - def message_dialog(text='', title='Message'): - dlg = QtWidgets.QMessageBox() + def message_dialog(parent=None, text='', title='Message'): + dlg = QtWidgets.QMessageBox(parent=parent) dlg.setWindowTitle(title) dlg.setInformativeText(text) dlg.setStandardButtons(QtWidgets.QMessageBox.Ok) dlg.exec_() @staticmethod - def question_dialog(text='', title='Are you sure?'): - dlg = QtWidgets.QMessageBox() + def question_dialog(parent=None, text='', title='Are you sure?'): + dlg = QtWidgets.QMessageBox(parent=parent) dlg.setWindowTitle(title) dlg.setInformativeText(text) dlg.setStandardButtons( diff --git a/NodeGraphQt/widgets/viewer.py b/NodeGraphQt/widgets/viewer.py index c08f5d87..654d1f6a 100644 --- a/NodeGraphQt/widgets/viewer.py +++ b/NodeGraphQt/widgets/viewer.py @@ -1224,70 +1224,82 @@ def context_menus(self): """ return {'graph': self._ctx_graph_menu, 'nodes': self._ctx_node_menu} - def question_dialog(self, text, title='Node Graph'): + def question_dialog(self, text, title='Node Graph', parent=None): """ Prompt node viewer question dialog widget with "yes", "no" buttons. Args: text (str): dialog text. title (str): dialog window title. + parent (QtWidgets.QObject): override dialog parent. (optional) Returns: bool: true if user click yes. """ + parent = parent or self + self.clear_key_state() - return BaseDialog.question_dialog(text, title) + return BaseDialog.question_dialog(parent, text, title) - def message_dialog(self, text, title='Node Graph'): + def message_dialog(self, text, title='Node Graph', parent=None): """ Prompt node viewer message dialog widget with "ok" button. Args: text (str): dialog text. title (str): dialog window title. + parent (QtWidgets.QObject): override dialog parent. (optional) """ + parent = parent or self + self.clear_key_state() - BaseDialog.message_dialog(text, title) + BaseDialog.message_dialog(parent, text, title) - def load_dialog(self, current_dir=None, ext=None): + def load_dialog(self, current_dir=None, ext=None, parent=None): """ Prompt node viewer file load dialog widget. Args: current_dir (str): directory path starting point. (optional) ext (str): custom file extension filter type. (optional) + parent (QtWidgets.QObject): override dialog parent. (optional) Returns: str: selected file path. """ + parent = parent or self + self.clear_key_state() ext = '*{} '.format(ext) if ext else '' ext_filter = ';;'.join([ 'Node Graph ({}*json)'.format(ext), 'All Files (*)' ]) file_dlg = FileDialog.getOpenFileName( - self, 'Open File', current_dir, ext_filter) + parent, 'Open File', current_dir, ext_filter) file = file_dlg[0] or None return file - def save_dialog(self, current_dir=None, ext=None): + def save_dialog(self, current_dir=None, ext=None, parent=None): """ Prompt node viewer file save dialog widget. Args: current_dir (str): directory path starting point. (optional) ext (str): custom file extension filter type. (optional) + parent (QtWidgets.QObject): override dialog parent. (optional) Returns: str: selected file path. """ + parent = parent or self + self.clear_key_state() ext_label = '*{} '.format(ext) if ext else '' ext_type = '.{}'.format(ext) if ext else '.json' ext_map = {'Node Graph ({}*json)'.format(ext_label): ext_type, 'All Files (*)': ''} file_dlg = FileDialog.getSaveFileName( - self, 'Save Session', current_dir, ';;'.join(ext_map.keys())) + parent, 'Save Session', current_dir, ';;'.join(ext_map.keys())) file_path = file_dlg[0] if not file_path: return