1717from .model import NodeGraphModel
1818from .node import NodeObject , BaseNode , BackdropNode
1919from .port import Port
20- from ..constants import (
21- DRAG_DROP_ID ,
22- NODE_LAYOUT_DIRECTION , NODE_LAYOUT_VERTICAL , NODE_LAYOUT_HORIZONTAL ,
23- PIPE_LAYOUT_CURVED , PIPE_LAYOUT_STRAIGHT , PIPE_LAYOUT_ANGLE ,
24- IN_PORT , OUT_PORT ,
25- VIEWER_GRID_LINES
26- )
20+ from ..constants import (URI_SCHEME , URN_SCHEME ,
21+ PIPE_LAYOUT_CURVED ,
22+ PIPE_LAYOUT_STRAIGHT ,
23+ PIPE_LAYOUT_ANGLE ,
24+ IN_PORT , OUT_PORT ,
25+ VIEWER_GRID_LINES )
2726from ..widgets .node_space_bar import node_space_bar
2827from ..widgets .viewer import NodeViewer
2928
3029
31- class QWidgetDrops (QtWidgets .QWidget ):
32-
33- def __init__ (self ):
34- super (QWidgetDrops , self ).__init__ ()
35- self .setAcceptDrops (True )
36- self .setWindowTitle ("NodeGraphQt" )
37- self .setStyleSheet ('''
38- QWidget {
39- background-color: rgb(55,55,55);
40- color: rgb(200,200,200);
41- border-width: 0px;
42- }''' )
43-
44- def dragEnterEvent (self , event ):
45- if event .mimeData ().hasUrls :
46- event .accept ()
47- else :
48- event .ignore ()
49-
50- def dragMoveEvent (self , event ):
51- if event .mimeData ().hasUrls :
52- event .accept ()
53- else :
54- event .ignore ()
55-
56- def dropEvent (self , event ):
57- if event .mimeData ().hasUrls :
58- event .setDropAction (QtCore .Qt .CopyAction )
59- event .accept ()
60- for url in event .mimeData ().urls ():
61- self .import_session (url .toLocalFile ())
62- else :
63- event .ignore ()
64-
65-
6630class NodeGraph (QtCore .QObject ):
6731 """
6832 The ``NodeGraph`` class is the main controller for managing all nodes
@@ -316,23 +280,41 @@ def _on_node_data_dropped(self, data, pos):
316280 """
317281 called when data has been dropped on the viewer.
318282
283+ Example Identifiers:
284+ URI = ngqt://path/to/node/session.graph
285+ URN = ngqt::node:com.nodes.MyNode1;node:com.nodes.MyNode2
286+
319287 Args:
320288 data (QtCore.QMimeData): mime data.
321289 pos (QtCore.QPoint): scene position relative to the drop.
322290 """
323-
324- # don't emit signal for internal widget drops.
325- if data .hasFormat ('text/plain' ):
326- if data .text ().startswith ('<${}>:' .format (DRAG_DROP_ID )):
327- node_ids = data .text ()[len ('<${}>:' .format (DRAG_DROP_ID )):]
328- x , y = pos .x (), pos .y ()
329- for node_id in node_ids .split (',' ):
330- self .create_node (node_id , pos = [x , y ])
331- x += 20
332- y += 20
333- return
334-
335- 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
336318
337319 def _on_nodes_moved (self , node_data ):
338320 """
@@ -451,9 +433,7 @@ def widget(self):
451433 PySide2.QtWidgets.QWidget: node graph widget.
452434 """
453435 if self ._widget is None :
454- self ._widget = QWidgetDrops ()
455- self ._widget .import_session = self .import_session
456-
436+ self ._widget = QtWidgets .QWidget ()
457437 layout = QtWidgets .QVBoxLayout (self ._widget )
458438 layout .setContentsMargins (0 , 0 , 0 , 0 )
459439 layout .setSpacing (0 )
@@ -848,7 +828,7 @@ def registered_nodes(self):
848828
849829 def register_node (self , node , alias = None ):
850830 """
851- Register the node to the node graph vendor.
831+ Register the node to the :meth:`NodeGraph.node_factory
852832
853833 Args:
854834 node (NodeGraphQt.NodeObject): node.
@@ -857,6 +837,16 @@ def register_node(self, node, alias=None):
857837 self ._node_factory .register_node (node , alias )
858838 self ._viewer .rebuild_tab_search ()
859839
840+ def register_nodes (self , nodes ):
841+ """
842+ Register the nodes to the :meth:`NodeGraph.node_factory
843+
844+ Args:
845+ nodes (list[NodeGraphQt.NodeObject]): list of nodes.
846+ """
847+ [self ._node_factory .register_node (n ) for n in nodes ]
848+ self ._viewer .rebuild_tab_search ()
849+
860850 def create_node (self , node_type , name = None , selected = True , color = None ,
861851 text_color = None , pos = None ):
862852 """
@@ -1436,7 +1426,7 @@ def import_session(self, file_path):
14361426
14371427 file_path = file_path .strip ()
14381428 if not os .path .isfile (file_path ):
1439- raise IOError ('file does not exist.' )
1429+ raise IOError ('file does not exist: {}' . format ( file_path ) )
14401430
14411431 try :
14421432 with open (file_path ) as data_file :
0 commit comments