|
23 | 23 | from NodeGraphQt.widgets.viewer import NodeViewer |
24 | 24 |
|
25 | 25 |
|
| 26 | +class QWidgetDrops(QtWidgets.QWidget): |
| 27 | + def dragEnterEvent(self, event): |
| 28 | + if event.mimeData().hasUrls: |
| 29 | + event.accept() |
| 30 | + else: |
| 31 | + event.ignore() |
| 32 | + |
| 33 | + def dragMoveEvent(self, event): |
| 34 | + if event.mimeData().hasUrls: |
| 35 | + event.accept() |
| 36 | + else: |
| 37 | + event.ignore() |
| 38 | + |
| 39 | + def dropEvent(self, event): |
| 40 | + if event.mimeData().hasUrls: |
| 41 | + event.setDropAction(QtCore.Qt.CopyAction) |
| 42 | + event.accept() |
| 43 | + for url in event.mimeData().urls(): |
| 44 | + self.import_session(url.toLocalFile()) |
| 45 | + else: |
| 46 | + e.ignore() |
| 47 | + |
| 48 | + |
26 | 49 | class NodeGraph(QtCore.QObject): |
27 | 50 | """ |
28 | 51 | The ``NodeGraph`` class is the main controller for managing all nodes. |
@@ -110,6 +133,7 @@ def __init__(self, parent=None): |
110 | 133 | self._viewer.need_show_tab_search.connect(self._toggle_tab_search) |
111 | 134 |
|
112 | 135 | self._wire_signals() |
| 136 | + self.widget.setAcceptDrops(True) |
113 | 137 |
|
114 | 138 | def __repr__(self): |
115 | 139 | return '<{} object at {}>'.format(self.__class__.__name__, hex(id(self))) |
@@ -325,8 +349,10 @@ def widget(self): |
325 | 349 | PySide2.QtWidgets.QWidget: node graph widget. |
326 | 350 | """ |
327 | 351 | if self._widget is None: |
328 | | - self._widget = QtWidgets.QWidget() |
329 | | - layout = QtWidgets.QVBoxLayout(self._widget) |
| 352 | + self._widget = QWidgetDrops() |
| 353 | + self._widget.import_session = self.import_session |
| 354 | + |
| 355 | + layout = QtWidgets.QVBoxLayout(self._widget) |
330 | 356 | layout.setContentsMargins(0, 0, 0, 0) |
331 | 357 | layout.addWidget(self._viewer) |
332 | 358 | return self._widget |
@@ -1046,16 +1072,25 @@ def save_session(self, file_path): |
1046 | 1072 | def load_session(self, file_path): |
1047 | 1073 | """ |
1048 | 1074 | Load node graph session layout file. |
| 1075 | + |
| 1076 | + Args: |
| 1077 | + file_path (str): path to the serialized layout file. |
| 1078 | + """ |
| 1079 | + self.clear_session() |
| 1080 | + self.import_session(file_path) |
1049 | 1081 |
|
| 1082 | + def import_session(self, file_path): |
| 1083 | + """ |
| 1084 | + Import node graph session layout file. |
| 1085 | + |
1050 | 1086 | Args: |
1051 | 1087 | file_path (str): path to the serialized layout file. |
1052 | 1088 | """ |
| 1089 | + |
1053 | 1090 | file_path = file_path.strip() |
1054 | 1091 | if not os.path.isfile(file_path): |
1055 | 1092 | raise IOError('file does not exist.') |
1056 | 1093 |
|
1057 | | - self.clear_session() |
1058 | | - |
1059 | 1094 | try: |
1060 | 1095 | with open(file_path) as data_file: |
1061 | 1096 | layout_data = json.load(data_file) |
|
0 commit comments