Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a node creation helper, initialized by 'initNodeCreationHelper'… #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions nodz_addons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from Qt import QtGui, QtCore, QtWidgets
import nodz_main

class QtPopupLineEditWidget(QtWidgets.QLineEdit):
"""
An addon to Nodz that allows to use a line edit widget and type a node to create it
Usage:
nodeCreationPopup = nodz_addons.QtPopupLineEditWidget(nodz.scene().views()[0])

nodeList = ["NodeTypeA", "NodeTypeB", "NodeTypeC", "LongAndAnnoyingStringThatWillDisplayFarOfTheBounds"]
nodeCreationPopup.setNodesList(nodeList)

nodeCreator(nodzInst, nodeName, pos):
nodzInst.createNode(name=nodeName, position=pos)
nodeCreationPopup.nodeCreator = nodeCreator

# Pop up:
nodeCreationPopup.popup()
# Pop down:
nodeCreationPopup.popdown()

"""

@staticmethod
def defaultNodeCreator(nodzInst, nodeName, pos):
nodzInst.createNode(name=nodeName, position=pos)

def __init__(self, nodzInst, nodeList=[], nodeCreator=None):
"""
Initialize the graphics view.

"""
super(QtPopupLineEditWidget, self).__init__(nodzInst)
self.nodzInst = nodzInst
self.nodeList = nodeList
if nodeCreator is None:
self.nodeCreator = self.defaultNodeCreator
else:
self.nodeCreator = nodeCreator
self.returnPressed.connect(self.onReturnPressedSlot)

def popup(self):
"""
Pop-up the line edit widget at the mouse's position

"""
position = self.parentWidget().mapFromGlobal(QtGui.QCursor.pos())
self.move(position)
self.clear()
self.show()
self.setFocus()
self.setNodesList(self.nodeList)

def popdown(self):
"""
Pop-down the line edit widget

"""
self.hide()
self.clear()
self.parentWidget().setFocus()

def setNodesList(self, nodeList):
"""
Set the list of nodes to use for the auto-completion

"""
self.nodeList = nodeList
self.completer = QtGui.QCompleter(self.nodeList, self)
self.completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.setCompleter(self.completer)
self.completer.activated.connect(self.onCompleterActivatedSlot)

fontMetrics = QtGui.QFontMetrics(self.font())
maxSize = self.size()
for nodeName in self.nodeList:
boundingSize = fontMetrics.boundingRect(nodeName).size()
maxSize.setWidth(max(maxSize.width(), boundingSize.width()+30)) #30 is for margin
self.resize(maxSize.width(), self.size().height())

def focusOutEvent(self, QFocusEvent):
self.popdown()

def onCompleterActivatedSlot(self, text):
pos=QtCore.QPointF(self.nodzInst.mapToScene(self.pos()))
self.popdown()
self.nodeCreator(self.nodzInst, text, pos)

def onReturnPressedSlot(self):
name = self.text()
pos = QtCore.QPointF(self.nodzInst.mapToScene(self.pos()))
self.completer.activated.disconnect(self.onCompleterActivatedSlot)
self.popdown()
self.nodeCreator(self.nodzInst, name, pos)
29 changes: 28 additions & 1 deletion nodz_demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from Qt import QtCore, QtWidgets
import nodz_main
import nodz_addons

try:
app = QtWidgets.QApplication([])
Expand All @@ -12,6 +13,7 @@
nodz.initialize()
nodz.show()

nodeCreationPopup = None

######################################################################
# Test signals
Expand Down Expand Up @@ -78,6 +80,13 @@ def on_graphEvaluated():
def on_keyPressed(key):
print 'key pressed : ', key

if key == QtCore.Qt.Key_Tab and nodeCreationPopup is not None:
nodeCreationPopup.popup()

if key == QtCore.Qt.Key_Escape and nodeCreationPopup is not None:
nodeCreationPopup.popdown()


nodz.signal_NodeCreated.connect(on_nodeCreated)
nodz.signal_NodeDeleted.connect(on_nodeDeleted)
nodz.signal_NodeEdited.connect(on_nodeEdited)
Expand All @@ -99,7 +108,6 @@ def on_keyPressed(key):

nodz.signal_KeyPressed.connect(on_keyPressed)


######################################################################
# Test API
######################################################################
Expand Down Expand Up @@ -193,7 +201,26 @@ def on_keyPressed(key):

nodz.loadGraph(filePath='Enter your path')

######################################################################
# Test Add-on
######################################################################

def demoNodeCreator(nodzInst, nodeName, pos):
if nodeName in nodeList:
id=0
uniqueNodeName = '{}_{}'.format(nodeName, id)
while uniqueNodeName in nodzInst.scene().nodes.keys():
id+=1
uniqueNodeName = '{}_{}'.format(nodeName, id)

nodzInst.createNode(name=uniqueNodeName, position=pos)
else:
print "{} is node a recognized node type. Known types are: {}".format(nodeName, nodeList)

nodeList = ["NodeTypeA", "NodeTypeB", "NodeTypeC", "LongAndAnnoyingStringThatWillDisplayFarOfTheBounds"]
nodeCreationPopup = nodz_addons.QtPopupLineEditWidget(nodz.scene().views()[0])
nodeCreationPopup.setNodesList(nodeList)
nodeCreationPopup.nodeCreator = demoNodeCreator

if app:
# command line stand alone test... run our own event loop
Expand Down