Skip to content

Commit 359902c

Browse files
committed
make node text item editable
1 parent 42f2718 commit 359902c

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

NodeGraphQt/base/node.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ def set_property(self, name, value):
325325
pass
326326

327327
if self.graph and name == 'name':
328+
if len(value) == 0:
329+
value = '_'
328330
value = self.graph.get_unique_name(value)
329331
self.NODE_NAME = value
330332

@@ -516,6 +518,7 @@ def __init__(self):
516518
self._inputs = []
517519
self._outputs = []
518520
self._has_draw = False
521+
self._view.text_item.editingFinished.connect(self.set_name)
519522

520523
def draw(self, force=True):
521524
if force:

NodeGraphQt/base/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,13 @@ def get_input_nodes(node):
339339
return list(nodes.values())
340340

341341

342-
def get_output_nodes(node):
342+
def get_output_nodes(node, cook=True):
343343
"""
344344
Get output nodes of node.
345345
346346
Args:
347347
node (NodeGraphQt.BaseNode).
348+
cook (bool): call this function for cook node.
348349
Returns:
349350
list[NodeGraphQt.BaseNode].
350351
"""
@@ -353,7 +354,7 @@ def get_output_nodes(node):
353354
for p in node.output_ports():
354355
for cp in p.connected_ports():
355356
n = cp.node()
356-
if n.has_property('graph_rect'):
357+
if cook and n.has_property('graph_rect'):
357358
n.mark_node_to_be_cooked(cp)
358359
nodes[n.id] = n
359360
return list(nodes.values())
@@ -609,7 +610,7 @@ def update_nodes_by_up(nodes):
609610

610611
def _update_node_rank_down(node, nodes_rank):
611612
rank = nodes_rank[node] + 1
612-
for n in get_output_nodes(node):
613+
for n in get_output_nodes(node, False):
613614
if n in nodes_rank:
614615
nodes_rank[n] = max(nodes_rank[n], rank)
615616
else:

NodeGraphQt/qgraphics/node_base.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ..errors import NodeWidgetError
1111
from .node_abstract import AbstractNodeItem
1212
from .port import PortItem
13+
from .text_item import text_item
1314

1415

1516
class XDisabledItem(QtWidgets.QGraphicsItem):
@@ -127,7 +128,7 @@ def __init__(self, name='node', parent=None):
127128
self._properties['icon'] = ICON_NODE_BASE
128129
self._icon_item = QtWidgets.QGraphicsPixmapItem(pixmap, self)
129130
self._icon_item.setTransformationMode(QtCore.Qt.SmoothTransformation)
130-
self._text_item = QtWidgets.QGraphicsTextItem(self.name, self)
131+
self.text_item = text_item(self.name, self)
131132
self._x_item = XDisabledItem(self, 'DISABLED')
132133
self._input_items = {}
133134
self._output_items = {}
@@ -262,7 +263,7 @@ def _set_text_color(self, color):
262263
text.setDefaultTextColor(text_color)
263264
for port, text in self._output_items.items():
264265
text.setDefaultTextColor(text_color)
265-
self._text_item.setDefaultTextColor(text_color)
266+
self.text_item.setDefaultTextColor(text_color)
266267

267268
def activate_pipes(self):
268269
"""
@@ -299,8 +300,8 @@ def calc_size(self, add_w=0.0, add_h=0.0):
299300
add_w (float): additional width.
300301
add_h (float): additional height.
301302
"""
302-
width = self._text_item.boundingRect().width()
303-
height = self._text_item.boundingRect().height()
303+
width = 0
304+
height = 0
304305

305306
if self._widgets:
306307
wid_width = max([
@@ -366,11 +367,11 @@ def arrange_label(self, h_offset=0.0, v_offset=0.0):
366367
v_offset (float): vertical offset.
367368
h_offset (float): horizontal offset.
368369
"""
369-
text_rect = self._text_item.boundingRect()
370+
text_rect = self.text_item.boundingRect()
370371
text_x = (self._width / 2) - (text_rect.width() / 2)
371372
text_x += h_offset
372-
text_y = 1.0 + v_offset
373-
self._text_item.setPos(text_x, text_y)
373+
text_y = v_offset - 25
374+
self.text_item.setPos(text_x, text_y)
374375

375376
def arrange_widgets(self, v_offset=0.0):
376377
"""
@@ -444,15 +445,15 @@ def offset_label(self, x=0.0, y=0.0):
444445
x (float): horizontal x offset
445446
y (float): vertical y offset
446447
"""
447-
icon_x = self._text_item.pos().x() + x
448-
icon_y = self._text_item.pos().y() + y
449-
self._text_item.setPos(icon_x, icon_y)
448+
icon_x = self.text_item.pos().x() + x
449+
icon_y = self.text_item.pos().y() + y
450+
self.text_item.setPos(icon_x, icon_y)
450451

451452
def draw_node(self):
452453
"""
453454
Draw the node item in the scene.
454455
"""
455-
height = self._text_item.boundingRect().height()
456+
height = self.text_item.boundingRect().height()
456457
# setup initial base size.
457458
self._set_base_size(add_w=0.0, add_h=height)
458459
# set text color when node is initialized.
@@ -520,7 +521,7 @@ def set_proxy_mode(self, mode):
520521
# for pipe in port.connected_pipes:
521522
# pipe.setVisible(visible)
522523

523-
self._text_item.setVisible(visible)
524+
self.text_item.setVisible(visible)
524525
self._icon_item.setVisible(visible)
525526

526527
@property
@@ -571,9 +572,9 @@ def selected(self, selected=False):
571572
@AbstractNodeItem.name.setter
572573
def name(self, name=''):
573574
AbstractNodeItem.name.fset(self, name)
574-
self._text_item.setPlainText(name)
575+
self.text_item.setPlainText(name)
575576
if self.scene():
576-
self.draw_node()
577+
self.arrange_label()
577578
self.update()
578579

579580
@AbstractNodeItem.color.setter

NodeGraphQt/qgraphics/text_item.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from PySide2 import QtWidgets, QtGui, QtCore
2+
3+
4+
class text_item(QtWidgets.QGraphicsTextItem):
5+
editingFinished = QtCore.Signal(str)
6+
7+
def __init__(self, text, parent=None):
8+
super(text_item, self).__init__(text, parent)
9+
self.setFlags(QtWidgets.QGraphicsItem.ItemIsFocusable)
10+
self.setTextInteractionFlags(QtGui.Qt.NoTextInteraction)
11+
self.isEditing = False
12+
13+
def _editingFinished(self):
14+
if self.isEditing:
15+
self.setTextInteractionFlags(QtGui.Qt.NoTextInteraction)
16+
self.isEditing = False
17+
self.editingFinished.emit(self.toPlainText())
18+
19+
def mousePressEvent(self, event):
20+
self.setTextInteractionFlags(QtGui.Qt.TextEditable)
21+
self.isEditing = True
22+
super(text_item, self).mousePressEvent(event)
23+
24+
def focusOutEvent(self, event):
25+
self._editingFinished()
26+
super(text_item, self).focusOutEvent(event)
27+
28+
def keyPressEvent(self, event):
29+
if event.key() is QtGui.Qt.Key_Return:
30+
self._editingFinished()
31+
super(text_item, self).keyPressEvent(event)

0 commit comments

Comments
 (0)