Mercurial > lcfOS
changeset 53:6dd7d6a1737c
Added in and output port icons
author | windel |
---|---|
date | Sun, 08 Apr 2012 11:28:46 +0200 |
parents | 67056de5da0f |
children | d8163d2c3779 |
files | applications/lab/bouncing_cube.py applications/lab/diagrameditor.py |
diffstat | 2 files changed, 71 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/applications/lab/bouncing_cube.py Sun Apr 08 09:43:48 2012 +0200 +++ b/applications/lab/bouncing_cube.py Sun Apr 08 11:28:46 2012 +0200 @@ -11,6 +11,7 @@ import numpy as np import time import scipy.integrate +#import pyopencl """ Test script that lets a dice bounce.
--- a/applications/lab/diagrameditor.py Sun Apr 08 09:43:48 2012 +0200 +++ b/applications/lab/diagrameditor.py Sun Apr 08 11:28:46 2012 +0200 @@ -5,24 +5,25 @@ import sys import xml.dom.minidom as md -""" - This script implements a basic diagram editor. +""" + Author: Windel Bouwman + Year: 2012 + Description: This script implements a diagram editor. + run with python 3.x as: + $ python [thisfile.py] """ class Connection(QGraphicsPathItem): - """ - - fromPort - - list of line items in between - - toPort - """ + """ Implementation of a connection between blocks """ def __init__(self, fromPort, toPort): super(Connection, self).__init__() self.pos1 = None self.pos2 = None + self.fromPort = None + self.toPort = None #self.setFlags(self.ItemIsSelectable | self.ItemIsMovable) self.setFlags(self.ItemIsSelectable) - pen = QPen(Qt.blue) - pen.setWidth(2) + pen = QPen(Qt.blue, 2) pen.setCapStyle(Qt.RoundCap) self.setPen(pen) arrowPath = QPainterPath(QPointF(0.0, 0.0)) @@ -34,18 +35,25 @@ self.arrowhead.setBrush(QBrush(pen.color())) self.setFromPort(fromPort) self.setToPort(toPort) - def shape(self): - return self.path() + #def shape(self): + # return self.path() def setFromPort(self, fromPort): + if self.fromPort: + self.fromPort.posCallbacks.remove(self.setBeginPos) self.fromPort = fromPort if self.fromPort: self.setBeginPos(fromPort.scenePos()) self.fromPort.posCallbacks.append(self.setBeginPos) def setToPort(self, toPort): + if self.toPort: + self.toPort.posCallbacks.remove(self.setEndPos) self.toPort = toPort if self.toPort: self.setEndPos(toPort.scenePos()) self.toPort.posCallbacks.append(self.setEndPos) + def releasePorts(self): + self.setFromPort(None) + self.setToPort(None) def setBeginPos(self, pos1): self.pos1 = pos1 self.updateLineStukken() @@ -157,21 +165,46 @@ def OK(self): name = self.nameEdit.text() - self.block.addInput(PortItem(name, self.block)) + self.block.addOutput(PortItem(name, self.block, 'output')) self.close() -class PortItem(QGraphicsEllipseItem): +class PortItem(QGraphicsPathItem): """ Represents a port to a subsystem """ - def __init__(self, name, block): - QGraphicsEllipseItem.__init__(self, QRectF(-6,-6,12.0,12.0), block) + def __init__(self, name, block, direction): + super(PortItem, self).__init__(block) + #QRectF(-6,-6,12.0,12.0) + path = QPainterPath() + if direction == 'input': + d = 5.0 + path.moveTo(-d, -d) + path.lineTo(0.0, 0.0) + path.lineTo(-d, d) + else: + d = 5.0 + path.moveTo(0.0, -d) + path.lineTo(d, 0.0) + path.lineTo(0.0, d) + self.setPath(path) + self.direction = direction self.block = block self.setCursor(QCursor(Qt.CrossCursor)) - self.setBrush(QBrush(Qt.red)) + #self.setBrush(QBrush(Qt.red)) + self.setPen(QPen(Qt.blue, 2)) self.name = name self.textItem = QGraphicsTextItem(name, self) - self.textItem.setPos(10, 0) # TODO + self.setName(name) self.posCallbacks = [] self.setFlag(self.ItemSendsScenePositionChanges, True) + def setName(self, name): + self.name = name + self.textItem.setPlainText(name) + rect = self.textItem.boundingRect() + lw, lh = rect.width(), rect.height() + if self.direction == 'input': + lx = 3 + else: + lx = -3 - lw + self.textItem.setPos(lx, -lh / 2) # TODO def itemChange(self, change, value): if change == self.ItemScenePositionHasChanged: for cb in self.posCallbacks: @@ -181,6 +214,10 @@ def mousePressEvent(self, event): editor.startConnection(self) +class OutputPort(PortItem): + # TODO: create a subclass OR make a member porttype + pass + # Block part: class HandleItem(QGraphicsEllipseItem): """ A handle that can be moved by the mouse """ @@ -270,20 +307,20 @@ rect = self.rect() h, w = rect.height(), rect.width() if len(self.inputs) == 1: - self.inputs[0].setPos(-4, h / 2) + self.inputs[0].setPos(0.0, h / 2) elif len(self.inputs) > 1: - y = 8 - dy = (h - 16) / (len(self.inputs) - 1) + y = 15 + dy = (h - 30) / (len(self.inputs) - 1) for inp in self.inputs: - inp.setPos(-4, y) + inp.setPos(0.0, y) y += dy if len(self.outputs) == 1: - self.outputs[0].setPos(w+4, h / 2) + self.outputs[0].setPos(w, h / 2) elif len(self.outputs) > 1: - y = 8 - dy = (h - 16) / (len(self.outputs) + 0) + y = 15 + dy = (h - 30) / (len(self.outputs) - 1) for outp in self.outputs: - outp.setPos(w+4, y) + outp.setPos(w, y) y += dy def changeSize(self, w, h): @@ -329,7 +366,6 @@ mimedata = QMimeData() for idx in idxs: if idx.isValid(): - #txt = self.data(idx, Qt.DisplayRole).toByteArray() # python2 txt = self.data(idx, Qt.DisplayRole) # python 3 mimedata.setData('component/name', txt) return mimedata @@ -391,11 +427,8 @@ testShortcut.activated.connect(self.test) zoomShortcut = QShortcut(QKeySequence("F8"), self) zoomShortcut.activated.connect(self.zoomAll) - delAction = QAction('Delete', self) - delAction.setShortcut(QKeySequence.Delete) delShort = QShortcut(QKeySequence.Delete, self) delShort.activated.connect(self.deleteItem) - #delAction.triggered.connect(self.deleteItem) def test(self): self.diagramView.rotate(11) @@ -467,12 +500,12 @@ portElements = blockElement.getElementsByTagName('input') for portElement in portElements: name = portElement.getAttribute('name') - inp = PortItem(name, block) + inp = PortItem(name, block, 'input') block.addInput(inp) portElements = blockElement.getElementsByTagName('output') for portElement in portElements: name = portElement.getAttribute('name') - outp = PortItem(name, block) + outp = PortItem(name, block, 'output') block.addOutput(outp) connectionElements = sysElement.getElementsByTagName('connection') for connectionElement in connectionElements: @@ -505,11 +538,14 @@ rect = self.diagramScene.itemsBoundingRect() self.diagramView.fitInView(rect, Qt.KeepAspectRatio) def deleteItem(self): - print('del') items = self.diagramScene.selectedItems() for item in items: - #if type(item) is Connection: - print(item) + if type(item) is Connection: + item.releasePorts() + item.delete() + if type(item) is Block: + pass + def sceneMouseMoveEvent(self, event): if self.startedConnection: pos = event.scenePos()