Mercurial > lcfOS
changeset 72:b01311fb3be7
Refinements to clean up
author | windel |
---|---|
date | Sat, 10 Nov 2012 11:26:50 +0100 |
parents | 5351594349b0 |
children | 191d70a0ed52 |
files | python/apps/diagrameditor.py python/libs/compiler/core/__init__.py |
diffstat | 2 files changed, 62 insertions(+), 80 deletions(-) [+] |
line wrap: on
line diff
--- a/python/apps/diagrameditor.py Fri Nov 02 14:05:00 2012 +0100 +++ b/python/apps/diagrameditor.py Sat Nov 10 11:26:50 2012 +0100 @@ -2,9 +2,8 @@ from PyQt4.QtGui import * from PyQt4.QtCore import * -import sys +import sys, xml import xml.dom.minidom as md -import xml """ Author: Windel Bouwman @@ -25,7 +24,6 @@ pen = QPen(Qt.blue, 2) self.setPen(pen) self.setBrush(QBrush(pen.color())) - self.myshape = QPainterPath() class Connection(QGraphicsPathItem): """ Implementation of a connection between blocks """ @@ -71,6 +69,12 @@ hi.posChangeCallbacks.append(callback) hi.setPos(pos) self.updateLineStukken() + def myDelete(self): + scene = self.scene() + if scene: + self.setFromPort(None) + self.setToPort(None) + scene.removeItem(self) def setFromPort(self, fromPort): if self.fromPort: @@ -90,9 +94,6 @@ self.setEndPos(toPort.scenePos()) self.toPort.connection = self self.toPort.posCallbacks.append(self.setEndPos) - def releasePorts(self): - self.setFromPort(None) - self.setToPort(None) def setBeginPos(self, pos1): self.pos1 = pos1 self.updateLineStukken() @@ -305,16 +306,27 @@ menu.exec_(event.screenPos()) def itemChange(self, change, value): if change == self.ItemSelectedHasChanged: - self.sizer.setVisible(value) - self.buttonItemAddInput.setVisible(value) - self.buttonItemAddOutput.setVisible(value) + for child in [self.sizer, self.buttonItemAddInput, self.buttonItemAddOutput]: + child.setVisible(value) return super(BlockItem, self).itemChange(change, value) + def myDelete(self): + # TODO: move the below to blockitem: + scene = self.scene() + if scene: + for p in self.inputs + self.outputs: + if p.connection: + p.connection.myDelete() + scene.removeItem(self) def updateSize(self): rect = self.rect() h, w = rect.height(), rect.width() self.buttonItemAddInput.setPos(0, h + 4) self.buttonItemAddOutput.setPos(w+10, h+4) + for inp in self.inputs: + inp.setX(0.0) + for inp in self.outputs: + inp.setX(w) if len(self.inputs) == 1: self.inputs[0].setPos(0.0, h / 2) elif len(self.inputs) > 1: @@ -336,10 +348,8 @@ """ Resize block function """ w, h = p.x(), p.y() # Limit the block size: - if h < 20: - h = 20 - if w < 40: - w = 40 + h = 20 if h < 20 else h + w = 40 if w < 40 else w self.setRect(0.0, 0.0, w, h) # center label: rect = self.label.boundingRect() @@ -352,9 +362,30 @@ return QPointF(w, h) class EditorGraphicsView(QGraphicsView): - def __init__(self, scene, parent=None): - QGraphicsView.__init__(self, scene, parent) + def __init__(self, parent=None): + QGraphicsView.__init__(self, parent) + scene = DiagramScene(self) + self.diagramScene = scene + self.setScene(scene) self.setDragMode(QGraphicsView.RubberBandDrag) + testShortcut = QShortcut(QKeySequence("F12"), self) + testShortcut.activated.connect(self.test) + delShort = QShortcut(QKeySequence.Delete, self) + delShort.activated.connect(scene.deleteItems) + self.loadDiagram = scene.loadDiagram + def save(self): + self.diagramScene.saveDiagram('diagram2.usd') + def load(self): + filename = QFileDialog.getOpenFileName(self) + self.diagramScene.loadDiagram(filename) + def zoomAll(self): + """ zoom to fit all items """ + rect = self.diagramScene.itemsBoundingRect() + self.fitInView(rect, Qt.KeepAspectRatio) + def test(self): + self.rotate(30) + self.zoomAll() + def wheelEvent(self, event): pos = event.pos() posbefore = self.mapToScene(pos) @@ -523,7 +554,7 @@ self.startedConnection.setToPort(item) self.startedConnection = None return - self.deleteItem(self.startedConnection) + self.startedConnection.myDelete() self.startedConnection = None super(DiagramScene, self).mouseReleaseEvent(event) def startConnection(self, port): @@ -531,52 +562,10 @@ pos = port.scenePos() self.startedConnection.setEndPos(pos) self.addItem(self.startedConnection) - def deleteItem(self, item=None): - if item: - if type(item) is BlockItem: - for p in item.inputs + item.outputs: - if p.connection: - self.deleteItem(p.connection) - self.removeItem(item) - elif type(item) is Connection: - item.releasePorts() - self.removeItem(item) - else: - # No item was supplied, try to delete all currently selected items: - items = self.selectedItems() - connections = [item for item in items if type(item) is Connection] - blocks = [item for item in items if type(item) is BlockItem] - for item in connections + blocks: - self.deleteItem(item) - -class DiagramEditor(QWidget): - def __init__(self, parent=None): - QWidget.__init__(self, parent) - - # Widget layout and child widgets: - self.horizontalLayout = QHBoxLayout(self) - self.diagramScene = DiagramScene(self) - self.loadDiagram = self.diagramScene.loadDiagram - self.diagramView = EditorGraphicsView(self.diagramScene, self) - self.horizontalLayout.addWidget(self.diagramView) - - testShortcut = QShortcut(QKeySequence("F12"), self) - testShortcut.activated.connect(self.test) - delShort = QShortcut(QKeySequence.Delete, self) - delShort.activated.connect(self.diagramScene.deleteItem) - - def test(self): - self.diagramView.rotate(30) - self.zoomAll() - def save(self): - self.diagramScene.saveDiagram('diagram2.usd') - def load(self): - filename = QFileDialog.getOpenFileName(self) - self.diagramScene.loadDiagram(filename) - def zoomAll(self): - """ zoom to fit all items """ - rect = self.diagramScene.itemsBoundingRect() - self.diagramView.fitInView(rect, Qt.KeepAspectRatio) + def deleteItems(self): + # Try to delete all currently selected items: + for item in list(self.selectedItems()): + item.myDelete() class LibraryWidget(QListView): def __init__(self): @@ -606,27 +595,19 @@ class Main(QMainWindow): def __init__(self): super(Main, self).__init__(None) - self.editor = DiagramEditor() + self.editor = EditorGraphicsView() self.setCentralWidget(self.editor) self.setWindowTitle("Diagram editor") toolbar = self.addToolBar('Tools') - - saveAction = QAction('Save', self) - saveAction.setShortcuts(QKeySequence.Save) - saveAction.triggered.connect(self.editor.save) - toolbar.addAction(saveAction) - openAction = QAction('Open', self) - openAction.setShortcuts(QKeySequence.Open) - openAction.triggered.connect(self.editor.load) - toolbar.addAction(openAction) - fullScreenAction = QAction('Full screen', self) - fullScreenAction.setShortcuts(QKeySequence("F11")) - fullScreenAction.triggered.connect(self.toggleFullScreen) - toolbar.addAction(fullScreenAction) - zoomAction = QAction('Fit in view', self) - zoomAction.setShortcuts(QKeySequence('F8')) - zoomAction.triggered.connect(self.editor.zoomAll) - toolbar.addAction(zoomAction) + def act(name, shortcut, callback): + a = QAction(name, self) + a.setShortcuts(shortcut) + a.triggered.connect(callback) + toolbar.addAction(a) + act('Save', QKeySequence.Save, self.editor.save) + act('Load', QKeySequence.Open, self.editor.load) + act('Full screen', QKeySequence("F11"), self.toggleFullScreen) + act('Fit in view', QKeySequence("F8"), self.editor.zoomAll) self.library = LibraryWidget() libraryDock = QDockWidget('Library', self)