|
17 | 17 | from .model import NodeGraphModel |
18 | 18 | from .node import NodeObject, BaseNode, BackdropNode |
19 | 19 | from .port import Port |
20 | | -from ..constants import (DRAG_DROP_ID, |
| 20 | +from ..constants import (URI_SCHEME, URN_SCHEME, |
21 | 21 | PIPE_LAYOUT_CURVED, |
22 | 22 | PIPE_LAYOUT_STRAIGHT, |
23 | 23 | PIPE_LAYOUT_ANGLE, |
|
27 | 27 | from ..widgets.viewer import NodeViewer |
28 | 28 |
|
29 | 29 |
|
30 | | -class QWidgetDrops(QtWidgets.QWidget): |
31 | | - |
32 | | - def __init__(self): |
33 | | - super(QWidgetDrops, self).__init__() |
34 | | - self.setAcceptDrops(True) |
35 | | - self.setWindowTitle("NodeGraphQt") |
36 | | - self.setStyleSheet(''' |
37 | | - QWidget { |
38 | | - background-color: rgb(55,55,55); |
39 | | - color: rgb(200,200,200); |
40 | | - border-width: 0px; |
41 | | - }''') |
42 | | - |
43 | | - def dragEnterEvent(self, event): |
44 | | - if event.mimeData().hasUrls: |
45 | | - event.accept() |
46 | | - else: |
47 | | - event.ignore() |
48 | | - |
49 | | - def dragMoveEvent(self, event): |
50 | | - if event.mimeData().hasUrls: |
51 | | - event.accept() |
52 | | - else: |
53 | | - event.ignore() |
54 | | - |
55 | | - def dropEvent(self, event): |
56 | | - if event.mimeData().hasUrls: |
57 | | - event.setDropAction(QtCore.Qt.CopyAction) |
58 | | - event.accept() |
59 | | - for url in event.mimeData().urls(): |
60 | | - self.import_session(url.toLocalFile()) |
61 | | - else: |
62 | | - event.ignore() |
63 | | - |
64 | | - |
65 | 30 | class NodeGraph(QtCore.QObject): |
66 | 31 | """ |
67 | 32 | The ``NodeGraph`` class is the main controller for managing all nodes |
@@ -315,23 +280,41 @@ def _on_node_data_dropped(self, data, pos): |
315 | 280 | """ |
316 | 281 | called when data has been dropped on the viewer. |
317 | 282 |
|
| 283 | + Example Identifiers: |
| 284 | + URI = ngqt://path/to/node/session.graph |
| 285 | + URN = ngqt::node:com.nodes.MyNode1;node:com.nodes.MyNode2 |
| 286 | +
|
318 | 287 | Args: |
319 | 288 | data (QtCore.QMimeData): mime data. |
320 | 289 | pos (QtCore.QPoint): scene position relative to the drop. |
321 | 290 | """ |
322 | | - |
323 | | - # don't emit signal for internal widget drops. |
324 | | - if data.hasFormat('text/plain'): |
325 | | - if data.text().startswith('<${}>:'.format(DRAG_DROP_ID)): |
326 | | - node_ids = data.text()[len('<${}>:'.format(DRAG_DROP_ID)):] |
327 | | - x, y = pos.x(), pos.y() |
328 | | - for node_id in node_ids.split(','): |
329 | | - self.create_node(node_id, pos=[x, y]) |
330 | | - x += 20 |
331 | | - y += 20 |
332 | | - return |
333 | | - |
334 | | - self.data_dropped.emit(data, pos) |
| 291 | + uri_regex = re.compile('{}(?:/*)([\w/]+)(\\.\w+)'.format(URI_SCHEME)) |
| 292 | + urn_regex = re.compile('{}([\w\\.:;]+)'.format(URN_SCHEME)) |
| 293 | + if data.hasFormat('text/uri-list'): |
| 294 | + for url in data.urls(): |
| 295 | + local_file = url.toLocalFile() |
| 296 | + if local_file: |
| 297 | + try: |
| 298 | + self.import_session(local_file) |
| 299 | + continue |
| 300 | + except Exception as e: |
| 301 | + pass |
| 302 | + |
| 303 | + url_str = url.toString() |
| 304 | + uri_search = uri_regex.search(url_str) |
| 305 | + urn_search = urn_regex.search(url_str) |
| 306 | + if uri_search: |
| 307 | + path = uri_search.group(1) |
| 308 | + ext = uri_search.group(2) |
| 309 | + self.import_session('{}{}'.format(path, ext)) |
| 310 | + elif urn_search: |
| 311 | + search_str = urn_search.group(1) |
| 312 | + node_ids = sorted(re.findall('node:([\w\\.]+)', search_str)) |
| 313 | + for node_id in node_ids: |
| 314 | + x, y = pos.x(), pos.y() |
| 315 | + self.create_node(node_id, pos=[x, y]) |
| 316 | + x += 20 |
| 317 | + y += 20 |
335 | 318 |
|
336 | 319 | def _on_nodes_moved(self, node_data): |
337 | 320 | """ |
@@ -450,9 +433,7 @@ def widget(self): |
450 | 433 | PySide2.QtWidgets.QWidget: node graph widget. |
451 | 434 | """ |
452 | 435 | if self._widget is None: |
453 | | - self._widget = QWidgetDrops() |
454 | | - self._widget.import_session = self.import_session |
455 | | - |
| 436 | + self._widget = QtWidgets.QWidget() |
456 | 437 | layout = QtWidgets.QVBoxLayout(self._widget) |
457 | 438 | layout.setContentsMargins(0, 0, 0, 0) |
458 | 439 | layout.setSpacing(0) |
@@ -1435,7 +1416,7 @@ def import_session(self, file_path): |
1435 | 1416 |
|
1436 | 1417 | file_path = file_path.strip() |
1437 | 1418 | if not os.path.isfile(file_path): |
1438 | | - raise IOError('file does not exist.') |
| 1419 | + raise IOError('file does not exist: {}'.format(file_path)) |
1439 | 1420 |
|
1440 | 1421 | try: |
1441 | 1422 | with open(file_path) as data_file: |
|
0 commit comments