comparison python/apps/diagrameditor.py @ 75:ba11d99b1d4a

Added qformlayout..
author windel
date Sun, 11 Nov 2012 11:28:28 +0100
parents f506f6b74697
children 1084b433c278
comparison
equal deleted inserted replaced
74:f506f6b74697 75:ba11d99b1d4a
24 if n == 1: 24 if n == 1:
25 return [l / 2] 25 return [l / 2]
26 elif n > 1: 26 elif n > 1:
27 return [offset + (l - offset*2)/(n - 1)*i for i in range(n)] 27 return [offset + (l - offset*2)/(n - 1)*i for i in range(n)]
28 return [] 28 return []
29
30 def uniqify(name, names):
31 newname = name
32 i = 1
33 while newname in names:
34 newname = name + str(i)
35 i += 1
36 return newname
29 37
30 class Connection(QGraphicsPathItem): 38 class Connection(QGraphicsPathItem):
31 """ Implementation of a connection between blocks """ 39 """ Implementation of a connection between blocks """
32 def __init__(self, fromPort, toPort): 40 def __init__(self, fromPort, toPort):
33 super(Connection, self).__init__() 41 super(Connection, self).__init__()
134 class ParameterDialog(QDialog): 142 class ParameterDialog(QDialog):
135 def __init__(self, block, parent = None): 143 def __init__(self, block, parent = None):
136 super(ParameterDialog, self).__init__(parent) 144 super(ParameterDialog, self).__init__(parent)
137 self.block = block 145 self.block = block
138 self.button = QPushButton('Ok', self) 146 self.button = QPushButton('Ok', self)
139 l = QGridLayout(self)
140 l.addWidget(QLabel('Name:', self), 0, 0)
141 self.nameEdit = QLineEdit(self.block.name) 147 self.nameEdit = QLineEdit(self.block.name)
142 l.addWidget(self.nameEdit, 0, 1)
143 l.addWidget(QLabel('Code:', self), 1, 0)
144 self.codeEdit = QTextEdit(self) 148 self.codeEdit = QTextEdit(self)
145 self.codeEdit.setPlainText(self.block.code) 149 self.codeEdit.setPlainText(self.block.code)
146 l.addWidget(self.codeEdit, 1, 1) 150 l = QFormLayout(self)
147 l.addWidget(self.button, 2, 0, 1, 2) 151 l.addRow('Name:', self.nameEdit)
152 l.addRow('Code:', self.codeEdit)
153 l.addWidget(self.button)
148 self.button.clicked.connect(self.OK) 154 self.button.clicked.connect(self.OK)
149 def OK(self): 155 def OK(self):
150 self.block.setName(self.nameEdit.text()) 156 self.block.name = self.nameEdit.text()
151 self.block.code = self.codeEdit.toPlainText() 157 self.block.code = self.codeEdit.toPlainText()
152 self.close() 158 self.close()
153 159
154 class PortItem(QGraphicsPathItem): 160 class PortItem(QGraphicsPathItem):
155 """ Represents a port to a subsystem """ 161 """ Represents a port to a subsystem """
156 def __init__(self, name, block, direction): 162 def __init__(self, name, block, direction):
157 super(PortItem, self).__init__(block) 163 super(PortItem, self).__init__(block)
158 self.connection = None 164 self.connection = None
159 path = QPainterPath()
160 d = 10.0 165 d = 10.0
161 if direction == 'input': 166 if direction == 'input':
162 path.moveTo(-d, -d) 167 path = buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])
163 path.lineTo(0.0, 0.0)
164 path.lineTo(-d, d)
165 else: 168 else:
166 path.moveTo(0.0, -d) 169 path = buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])
167 path.lineTo(d, 0.0)
168 path.lineTo(0.0, d)
169 self.setPath(path) 170 self.setPath(path)
170 self.direction = direction 171 self.direction = direction
171 self.block = block 172 self.block = block
172 self.setCursor(QCursor(Qt.CrossCursor)) 173 self.setCursor(QCursor(Qt.CrossCursor))
173 pen = QPen(Qt.blue, 2) 174 pen = QPen(Qt.blue, 2)
174 pen.setCapStyle(Qt.RoundCap) 175 pen.setCapStyle(Qt.RoundCap)
175 self.setPen(pen) 176 self.setPen(pen)
176 self.textItem = QGraphicsTextItem(name, self) 177 self.textItem = QGraphicsTextItem(self)
177 self.setName(name) 178 self.name = name
178 self.posCallbacks = [] 179 self.posCallbacks = []
179 self.setFlag(self.ItemSendsScenePositionChanges, True) 180 self.setFlag(self.ItemSendsScenePositionChanges, True)
181 def getName(self): return self.textItem.toPlainText()
180 def setName(self, name): 182 def setName(self, name):
181 self.name = name
182 self.textItem.setPlainText(name) 183 self.textItem.setPlainText(name)
183 rect = self.textItem.boundingRect() 184 rect = self.textItem.boundingRect()
184 lw, lh = rect.width(), rect.height() 185 lw, lh = rect.width(), rect.height()
185 if self.direction == 'input': 186 if self.direction == 'input':
186 lx = 3 187 lx = 3
187 else: 188 else:
188 lx = -3 - lw 189 lx = -3 - lw
189 self.textItem.setPos(lx, -lh / 2) 190 self.textItem.setPos(lx, -lh / 2)
191 name = property(getName, setName)
190 def itemChange(self, change, value): 192 def itemChange(self, change, value):
191 if change == self.ItemScenePositionHasChanged: 193 if change == self.ItemScenePositionHasChanged:
192 for cb in self.posCallbacks: 194 for cb in self.posCallbacks:
193 cb(value) 195 cb(value)
194 return value 196 return value
225 value = res 227 value = res
226 return value 228 return value
227 # Call superclass method: 229 # Call superclass method:
228 return super(HandleItem, self).itemChange(change, value) 230 return super(HandleItem, self).itemChange(change, value)
229 231
230 def uniqify(name, names):
231 newname = name
232 i = 1
233 while newname in names:
234 newname = name + str(i)
235 i += 1
236 return newname
237
238 class BlockItem(QGraphicsRectItem): 232 class BlockItem(QGraphicsRectItem):
239 """ 233 """
240 Represents a block in the diagram 234 Represents a block in the diagram
241 Has an x and y and width and height 235 Has an x and y and width and height
242 width and height can only be adjusted with a tip in the lower right corner. 236 width and height can only be adjusted with a tip in the lower right corner.
248 def __init__(self, name='Untitled', parent=None): 242 def __init__(self, name='Untitled', parent=None):
249 super(BlockItem, self).__init__(parent) 243 super(BlockItem, self).__init__(parent)
250 # Properties of the rectangle: 244 # Properties of the rectangle:
251 self.setPen(QPen(Qt.blue, 2)) 245 self.setPen(QPen(Qt.blue, 2))
252 self.setBrush(QBrush(Qt.lightGray)) 246 self.setBrush(QBrush(Qt.lightGray))
253 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable) 247 self.setFlags(self.ItemIsSelectable | self.ItemIsMovable | self.ItemSendsScenePositionChanges)
254 self.setFlag(self.ItemSendsScenePositionChanges, True)
255 self.setCursor(QCursor(Qt.PointingHandCursor)) 248 self.setCursor(QCursor(Qt.PointingHandCursor))
256 self.label = QGraphicsTextItem(name, self) 249 self.label = QGraphicsTextItem(name, self)
257 self.name = name 250 self.name = name
258 self.code = '' 251 self.code = ''
259 # Create corner for resize: 252 # Create corner for resize:
283 names = [i.name for i in self.inputs + self.outputs] 276 names = [i.name for i in self.inputs + self.outputs]
284 self.addInput(PortItem(uniqify('in', names), self, 'input')) 277 self.addInput(PortItem(uniqify('in', names), self, 'input'))
285 def newOutputPort(self): 278 def newOutputPort(self):
286 names = [i.name for i in self.inputs + self.outputs] 279 names = [i.name for i in self.inputs + self.outputs]
287 self.addOutput(PortItem(uniqify('out', names), self, 'output')) 280 self.addOutput(PortItem(uniqify('out', names), self, 'output'))
288 def setName(self, name): 281 def setName(self, name): self.label.setPlainText(name)
289 self.name = name 282 def getName(self): return self.label.toPlainText()
290 self.label.setPlainText(name) 283 name = property(getName, setName)
291 def addInput(self, i): 284 def addInput(self, i):
292 self.inputs.append(i) 285 self.inputs.append(i)
293 self.updateSize() 286 self.updateSize()
294 def addOutput(self, o): 287 def addOutput(self, o):
295 self.outputs.append(o) 288 self.outputs.append(o)