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 1 commit
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ nodz.gridSnapToggle = False
del : delete the selected nodes
f : zoom focus on selected items, all the items if nothing is selected
s : snap the selected node on the grid
tab : display the node creator helper

```

Expand All @@ -75,6 +76,7 @@ Nodes
def createNode(name, preset, position, alternate)
def deleteNode(node)
def editNode(node, newName)
def initNodeCreationHelper(nodeList, nodeCreatorFunction)
```
Attributes
```python
Expand Down
18 changes: 18 additions & 0 deletions nodz_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ def on_keyPressed(key):

nodz.signal_KeyPressed.connect(on_keyPressed)

######################################################################
# Test Node creation UI
######################################################################

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"]
nodz.initNodeCreationHelper(nodeList, demoNodeCreator)

######################################################################
# Test API
Expand Down
64 changes: 64 additions & 0 deletions nodz_extra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from Qt import QtGui, QtCore, QtWidgets
import nodz_main

class QtPopupLineEditWidget(QtWidgets.QLineEdit):

@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):
position = self.parentWidget().mapFromGlobal(QtGui.QCursor.pos())
self.move(position)
self.clear()
self.show()
self.setFocus()
self.setNodesList(self.nodeList)

def popdown(self):
self.hide()
self.clear()
self.parentWidget().setFocus()

def setNodesList(self, nodeList):
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)
20 changes: 19 additions & 1 deletion nodz_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from Qt import QtGui, QtCore, QtWidgets
import nodz_utils as utils

import nodz_extra


defautConfigPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'default_config.json')
Expand Down Expand Up @@ -66,6 +66,9 @@ def __init__(self, parent):
self.currentState = 'DEFAULT'
self.pressedKeys = list()

# Node creation helper
self.nodeCreationPopup = None

def wheelEvent(self, event):
"""
Zoom in the view with the mouse wheel.
Expand Down Expand Up @@ -298,6 +301,12 @@ def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_S:
self._nodeSnap = True

if event.key() == QtCore.Qt.Key_Tab:
self.nodeCreationPopup.popup()

if event.key() == QtCore.Qt.Key_Escape:
self.nodeCreationPopup.popdown()

# Emit signal.
self.signal_KeyPressed.emit(event.key())

Expand Down Expand Up @@ -472,6 +481,15 @@ def initialize(self):
# Connect signals.
self.scene().selectionChanged.connect(self._returnSelection)

def initNodeCreationHelper(self, completerNodeList=[], nodeCreatorCallback=None):
"""
Setup the node's creation helper that is available from the tab key

"""
self.nodeCreationPopup = nodz_extra.QtPopupLineEditWidget(self.scene().views()[0])
self.nodeCreationPopup.setNodesList(completerNodeList)
if nodeCreatorCallback is not None:
self.nodeCreationPopup.nodeCreator = nodeCreatorCallback

# NODES
def createNode(self, name='default', preset='node_default', position=None, alternate=True):
Expand Down