Skip to content

Commit

Permalink
minor clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchanvfx committed Aug 21, 2023
1 parent 2b3d8f6 commit 45d6948
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
53 changes: 33 additions & 20 deletions NodeGraphQt/base/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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):
"""
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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`
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
# --------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion NodeGraphQt/pkg_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
__version__ = '0.6.18'
__version__ = '0.6.19'
__status__ = 'Work in Progress'
__license__ = 'MIT'

Expand Down
14 changes: 7 additions & 7 deletions NodeGraphQt/widgets/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down
28 changes: 20 additions & 8 deletions NodeGraphQt/widgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45d6948

Please sign in to comment.