annotate applications/lab/diagrameditor.py @ 44:cbf199e007c2

Added some demo applications
author windel
date Sat, 18 Feb 2012 16:42:23 +0100
parents
children 8a52263d67c4
rev   line source
44
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
1 #!/usr/bin/python
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
2
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
3 from PyQt4 import QtGui, QtCore
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
4 from PyQt4.QtGui import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
5 from PyQt4.QtCore import *
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
6 import sys
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
7
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
8 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
9 This script implements a basic diagram editor.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
10 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
11
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
12 class Connection:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
13 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
14 - fromPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
15 - list of line items in between
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
16 - toPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
17 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
18 def __init__(self, fromPort, toPort):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
19 self.fromPort = fromPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
20 self.pos1 = None
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
21 self.pos2 = None
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
22 self.p1dir = None
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
23 self.p2dir = None
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
24 self.setFromPort(fromPort)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
25 self.toPort = toPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
26 # Create arrow item:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
27 self.linePieces = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
28 def setFromPort(self, fromPort):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
29 self.fromPort = fromPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
30 if self.fromPort:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
31 self.pos1 = fromPort.scenePos()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
32 self.fromPort.posCallbacks.append(self.setBeginPos)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
33 def setToPort(self, toPort):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
34 self.toPort = toPort
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
35 if self.toPort:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
36 self.pos2 = toPort.scenePos()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
37 self.toPort.posCallbacks.append(self.setEndPos)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
38 def setBeginPos(self, pos1):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
39 self.pos1 = pos1
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
40 if self.pos1 and self.pos2:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
41 self.updateLineStukken()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
42 def setEndPos(self, endpos):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
43 self.pos2 = endpos
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
44 if self.pos1 and self.pos2:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
45 self.updateLineStukken()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
46 def updateLineStukken(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
47 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
48 This algorithm determines the optimal routing of all signals.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
49 TODO: implement nice automatic line router
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
50 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
51 # TODO: create pieces of lines.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
52
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
53 # Determine the current amount of linestukken:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
54 x1, y1 = self.pos1.x(), self.pos1.y()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
55 x2, y2 = self.pos2.x(), self.pos2.y()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
56
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
57 ds = editor.diagramScene
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
58
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
59 if y1 == y2 or x1 == x2:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
60 pass
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
61 else:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
62 # We require two lijnstukken to make one corner!
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
63 while len(self.linePieces) < 2:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
64 lp = LinePieceItem()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
65 ds.addItem(lp)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
66 self.linePieces.append(lp)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
67 lp1 = self.linePieces[0]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
68 lp2 = self.linePieces[1]
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
69 lp1.setLine(QLineF(x1, y1, x2, y1))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
70 lp2.setLine(QLineF(x2, y1, x2, y2))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
71
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
72 def delete(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
73 editor.diagramScene.removeItem(self.arrow)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
74 # Remove position update callbacks:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
75
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
76 class ParameterDialog(QDialog):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
77 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
78 super(ParameterDialog, self).__init__(parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
79 self.button = QPushButton('Ok', self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
80 l = QVBoxLayout(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
81 l.addWidget(self.button)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
82 self.button.clicked.connect(self.OK)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
83 def OK(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
84 self.close()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
85
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
86 class PortItem(QGraphicsEllipseItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
87 """ Represents a port to a subsystem """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
88 def __init__(self, name, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
89 QGraphicsEllipseItem.__init__(self, QRectF(-6,-6,12.0,12.0), parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
90 self.setCursor(QCursor(QtCore.Qt.CrossCursor))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
91 # Properties:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
92 self.setBrush(QBrush(Qt.red))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
93 # Name:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
94 self.name = name
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
95 self.posCallbacks = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
96 self.setFlag(self.ItemSendsScenePositionChanges, True)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
97 def itemChange(self, change, value):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
98 if change == self.ItemScenePositionHasChanged:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
99 value = value.toPointF() # Required!
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
100 for cb in self.posCallbacks:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
101 cb(value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
102 return value
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
103 return super(PortItem, self).itemChange(change, value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
104 def mousePressEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
105 editor.startConnection(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
106
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
107 # Block part:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
108 class HandleItem(QGraphicsEllipseItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
109 """ A handle that can be moved by the mouse """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
110 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
111 super(HandleItem, self).__init__(QRectF(-4.0,-4.0,8.0,8.0), parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
112 self.posChangeCallbacks = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
113 self.setBrush(QtGui.QBrush(Qt.white))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
114 self.setFlag(self.ItemIsMovable, True)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
115 self.setFlag(self.ItemSendsScenePositionChanges, True)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
116 self.setCursor(QtGui.QCursor(Qt.SizeFDiagCursor))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
117
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
118 def itemChange(self, change, value):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
119 if change == self.ItemPositionChange:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
120 value = value.toPointF()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
121 x, y = value.x(), value.y()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
122 # TODO: make this a signal?
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
123 # This cannot be a signal because this is not a QObject
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
124 for cb in self.posChangeCallbacks:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
125 res = cb(x, y)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
126 if res:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
127 x, y = res
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
128 value = QPointF(x, y)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
129 return value
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
130 # Call superclass method:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
131 return super(HandleItem, self).itemChange(change, value)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
132
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
133 class BlockItem(QGraphicsRectItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
134 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
135 Represents a block in the diagram
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
136 Has an x and y and width and height
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
137 width and height can only be adjusted with a tip in the lower right corner.
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
138
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
139 - in and output ports
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
140 - parameters
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
141 - description
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
142 """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
143 def __init__(self, name='Untitled', parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
144 super(BlockItem, self).__init__(parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
145 w = 60.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
146 h = 40.0
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
147 # Properties of the rectangle:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
148 self.setPen(QtGui.QPen(QtCore.Qt.blue, 2))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
149 self.setBrush(QtGui.QBrush(QtCore.Qt.lightGray))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
150 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
151 self.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
152 # Label:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
153 self.label = QGraphicsTextItem(name, self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
154 # Create corner for resize:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
155 self.sizer = HandleItem(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
156 self.sizer.setPos(w, h)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
157 self.sizer.posChangeCallbacks.append(self.changeSize) # Connect the callback
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
158 #self.sizer.setVisible(False)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
159 self.sizer.setFlag(self.sizer.ItemIsSelectable, True)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
160
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
161 # Inputs and outputs of the block:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
162 self.inputs = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
163 self.inputs.append( PortItem('a', self) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
164 self.inputs.append( PortItem('b', self) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
165 self.inputs.append( PortItem('c', self) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
166 self.outputs = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
167 self.outputs.append( PortItem('y', self) )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
168 # Update size:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
169 self.changeSize(w, h)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
170 def editParameters(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
171 pd = ParameterDialog(self.window())
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
172 pd.exec_()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
173
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
174 def contextMenuEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
175 menu = QMenu()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
176 menu.addAction('Delete')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
177 pa = menu.addAction('Parameters')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
178 pa.triggered.connect(self.editParameters)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
179 menu.exec_(event.screenPos())
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
180
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
181 def changeSize(self, w, h):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
182 """ Resize block function """
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
183 # Limit the block size:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
184 if h < 20:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
185 h = 20
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
186 if w < 40:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
187 w = 40
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
188 self.setRect(0.0, 0.0, w, h)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
189 # center label:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
190 rect = self.label.boundingRect()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
191 lw, lh = rect.width(), rect.height()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
192 lx = (w - lw) / 2
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
193 ly = (h - lh) / 2
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
194 self.label.setPos(lx, ly)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
195 # Update port positions:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
196 if len(self.inputs) == 1:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
197 self.inputs[0].setPos(-4, h / 2)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
198 elif len(self.inputs) > 1:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
199 y = 5
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
200 dy = (h - 10) / (len(self.inputs) - 1)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
201 for inp in self.inputs:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
202 inp.setPos(-4, y)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
203 y += dy
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
204 if len(self.outputs) == 1:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
205 self.outputs[0].setPos(w+4, h / 2)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
206 elif len(self.outputs) > 1:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
207 y = 5
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
208 dy = (h - 10) / (len(self.outputs) + 0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
209 for outp in self.outputs:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
210 outp.setPos(w+4, y)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
211 y += dy
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
212 return w, h
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
213
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
214 class LinePieceItem(QGraphicsLineItem):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
215 def __init__(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
216 super(LinePieceItem, self).__init__(None)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
217 self.setPen(QtGui.QPen(QtCore.Qt.red,2))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
218 self.setFlag(self.ItemIsSelectable, True)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
219 def x(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
220 pass
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
221
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
222 class EditorGraphicsView(QGraphicsView):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
223 def __init__(self, scene, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
224 QGraphicsView.__init__(self, scene, parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
225 def dragEnterEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
226 if event.mimeData().hasFormat('component/name'):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
227 event.accept()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
228 def dragMoveEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
229 if event.mimeData().hasFormat('component/name'):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
230 event.accept()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
231 def dropEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
232 if event.mimeData().hasFormat('component/name'):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
233 name = str(event.mimeData().data('component/name'))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
234 b1 = BlockItem(name)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
235 b1.setPos(self.mapToScene(event.pos()))
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
236 self.scene().addItem(b1)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
237
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
238 class LibraryModel(QStandardItemModel):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
239 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
240 QStandardItemModel.__init__(self, parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
241 def mimeTypes(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
242 return ['component/name']
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
243 def mimeData(self, idxs):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
244 mimedata = QMimeData()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
245 for idx in idxs:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
246 if idx.isValid():
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
247 txt = self.data(idx, Qt.DisplayRole).toByteArray()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
248 mimedata.setData('component/name', txt)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
249 return mimedata
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
250
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
251 class DiagramScene(QGraphicsScene):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
252 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
253 super(DiagramScene, self).__init__(parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
254 def mouseMoveEvent(self, mouseEvent):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
255 editor.sceneMouseMoveEvent(mouseEvent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
256 super(DiagramScene, self).mouseMoveEvent(mouseEvent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
257 def mouseReleaseEvent(self, mouseEvent):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
258 editor.sceneMouseReleaseEvent(mouseEvent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
259 super(DiagramScene, self).mouseReleaseEvent(mouseEvent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
260
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
261 class DiagramEditor(QWidget):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
262 def __init__(self, parent=None):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
263 QtGui.QWidget.__init__(self, parent)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
264 self.setWindowTitle("Diagram editor")
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
265
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
266 # Widget layout and child widgets:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
267 self.horizontalLayout = QtGui.QHBoxLayout(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
268 self.libraryBrowserView = QtGui.QListView(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
269 self.libraryModel = LibraryModel(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
270 self.libraryModel.setColumnCount(1)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
271 # Create an icon with an icon:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
272 pixmap = QPixmap(60, 60)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
273 pixmap.fill()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
274 painter = QPainter(pixmap)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
275 painter.fillRect(10, 10, 40, 40, Qt.blue)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
276 painter.setBrush(Qt.red)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
277 painter.drawEllipse(36, 2, 20, 20)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
278 painter.setBrush(Qt.yellow)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
279 painter.drawEllipse(20, 20, 20, 20)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
280 painter.end()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
281
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
282 self.libItems = []
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
283 self.libItems.append( QtGui.QStandardItem(QIcon(pixmap), 'Block') )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
284 self.libItems.append( QtGui.QStandardItem(QIcon(pixmap), 'Uber Unit') )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
285 self.libItems.append( QtGui.QStandardItem(QIcon(pixmap), 'Device') )
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
286 for i in self.libItems:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
287 self.libraryModel.appendRow(i)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
288 self.libraryBrowserView.setModel(self.libraryModel)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
289 self.libraryBrowserView.setViewMode(self.libraryBrowserView.IconMode)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
290 self.libraryBrowserView.setDragDropMode(self.libraryBrowserView.DragOnly)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
291
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
292 self.diagramScene = DiagramScene(self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
293 self.diagramView = EditorGraphicsView(self.diagramScene, self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
294 self.horizontalLayout.addWidget(self.libraryBrowserView)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
295 self.horizontalLayout.addWidget(self.diagramView)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
296
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
297 # Populate the diagram scene:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
298 b1 = BlockItem('SubSystem1')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
299 b1.setPos(50,100)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
300 self.diagramScene.addItem(b1)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
301 b2 = BlockItem('Unit2')
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
302 b2.setPos(-250,0)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
303 self.diagramScene.addItem(b2)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
304
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
305 self.startedConnection = None
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
306 fullScreenShortcut = QShortcut(QKeySequence("F11"), self)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
307 fullScreenShortcut.activated.connect(self.toggleFullScreen)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
308 def toggleFullScreen(self):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
309 self.setWindowState(self.windowState() ^ Qt.WindowFullScreen)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
310 def startConnection(self, port):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
311 self.startedConnection = Connection(port, None)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
312 def sceneMouseMoveEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
313 if self.startedConnection:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
314 pos = event.scenePos()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
315 self.startedConnection.setEndPos(pos)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
316 def sceneMouseReleaseEvent(self, event):
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
317 # Clear the actual connection:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
318 if self.startedConnection:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
319 pos = event.scenePos()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
320 items = self.diagramScene.items(pos)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
321 for item in items:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
322 if type(item) is PortItem:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
323 self.startedConnection.setToPort(item)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
324 if self.startedConnection.toPort == None:
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
325 self.startedConnection.delete()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
326 self.startedConnection = None
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
327
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
328 if __name__ == '__main__':
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
329 app = QtGui.QApplication(sys.argv)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
330 global editor
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
331 editor = DiagramEditor()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
332 editor.show()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
333 editor.resize(700, 800)
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
334 app.exec_()
cbf199e007c2 Added some demo applications
windel
parents:
diff changeset
335