32
32
33
33
class ModelerGraphicItem (QtGui .QGraphicsItem ):
34
34
35
- BOX_HEIGHT = 70
35
+ BOX_HEIGHT = 30
36
36
BOX_WIDTH = 200
37
37
38
38
def __init__ (self , element , elementIndex , model ):
39
39
super (ModelerGraphicItem , self ).__init__ (None , None )
40
40
self .model = model
41
41
self .element = element
42
42
self .elementIndex = elementIndex
43
+ self .inputFolded = True
44
+ self .outputFolded = True
43
45
if isinstance (element , Parameter ):
44
46
icon = QtGui .QIcon (os .path .dirname (__file__ ) + "/../images/input.png" )
45
47
self .pixmap = icon .pixmap (20 , 20 , state = QtGui .QIcon .On )
46
48
self .text = element .description
47
49
else :
50
+ state = QtGui .QIcon .On
51
+ if self .elementIndex in self .model .deactivated :
52
+ state = QtGui .QIcon .Off
48
53
self .text = element .name
49
- self .pixmap = element .getIcon ().pixmap (20 , 20 , state = QtGui . QIcon . On )
54
+ self .pixmap = element .getIcon ().pixmap (15 , 15 , state = state )
50
55
self .arrows = []
51
56
self .setFlag (QtGui .QGraphicsItem .ItemIsMovable , True )
52
57
self .setFlag (QtGui .QGraphicsItem .ItemIsSelectable , True )
53
58
self .setZValue (1000 )
59
+
60
+ icon = QtGui .QIcon (os .path .dirname (__file__ ) + "/../images/edit.png" )
61
+ pt = QtCore .QPointF (ModelerGraphicItem .BOX_WIDTH / 2 - FlatButtonGraphicItem .WIDTH / 2 , ModelerGraphicItem .BOX_HEIGHT / 2 - FlatButtonGraphicItem .HEIGHT / 2 + 1 )
62
+ self .editButton = FlatButtonGraphicItem (icon , pt , self .editElement )
63
+ self .editButton .setParentItem (self )
64
+ icon = QtGui .QIcon (os .path .dirname (__file__ ) + "/../images/delete.png" )
65
+ pt = QtCore .QPointF (ModelerGraphicItem .BOX_WIDTH / 2 - FlatButtonGraphicItem .WIDTH / 2 , - ModelerGraphicItem .BOX_HEIGHT / 2 + FlatButtonGraphicItem .HEIGHT / 2 + 1 )
66
+ self .deleteButton = FlatButtonGraphicItem (icon , pt , self .removeElement )
67
+ self .deleteButton .setParentItem (self )
68
+
69
+ if isinstance (element , GeoAlgorithm ):
70
+ if element .parameters :
71
+ pt = self .getLinkPointForParameter (- 1 )
72
+ x = self .getXPositionForFoldButton ()
73
+ pt = QtCore .QPointF (x , pt .y ())
74
+ self .inButton = FoldButtonGraphicItem (pt , self .foldInput )
75
+ self .inButton .setParentItem (self )
76
+ if element .outputs :
77
+ pt = self .getLinkPointForOutput (- 1 )
78
+ x = self .getXPositionForFoldButton ()
79
+ pt = QtCore .QPointF (x , pt .y ())
80
+ self .outButton = FoldButtonGraphicItem (pt , self .foldOutput )
81
+ self .outButton .setParentItem (self )
82
+
54
83
84
+ def foldInput (self , folded ):
85
+ self .inputFolded = folded
86
+ self .prepareGeometryChange ()
87
+ if self .element .outputs :
88
+ pt = self .getLinkPointForOutput (- 1 )
89
+ x = self .getXPositionForFoldButton ()
90
+ pt = QtCore .QPointF (x , pt .y ())
91
+ self .outButton .position = pt
92
+ self .update ()
93
+
94
+ def foldOutput (self , folded ):
95
+ self .outputFolded = folded
96
+ self .prepareGeometryChange ()
97
+ self .update ()
98
+
55
99
def addArrow (self , arrow ):
56
100
self .arrows .append (arrow )
57
101
58
102
def boundingRect (self ):
103
+ font = QtGui .QFont ("Verdana" , 8 )
104
+ fm = QtGui .QFontMetricsF (font )
105
+ numParams = 0 if self .inputFolded else len (self .element .parameters )
106
+ numOutputs = 0 if self .outputFolded else len (self .element .outputs )
107
+ numElements = numParams + numOutputs + 3
108
+ h = (fm .height () * 1.2 ) * numElements
59
109
rect = QtCore .QRectF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , - (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 ,
60
- ModelerGraphicItem .BOX_WIDTH + 2 , ModelerGraphicItem .BOX_HEIGHT + 2 )
110
+ ModelerGraphicItem .BOX_WIDTH + 2 , ModelerGraphicItem .BOX_HEIGHT + h )
61
111
return rect
62
112
63
113
def mouseDoubleClickEvent (self , event ):
@@ -79,9 +129,11 @@ def contextMenuEvent(self, event):
79
129
popupmenu .exec_ (event .screenPos ())
80
130
81
131
def deactivateAlgorithm (self ):
132
+ self .model .setPositions (self .scene ().getParameterPositions (), self .scene ().getAlgorithmPositions ())
82
133
self .model .deactivateAlgorithm (self .elementIndex , True )
83
134
84
135
def activateAlgorithm (self ):
136
+ self .model .setPositions (self .scene ().getParameterPositions (), self .scene ().getAlgorithmPositions ())
85
137
if not self .model .activateAlgorithm (self .elementIndex , True ):
86
138
QtGui .QMessageBox .warning (None , "Could not activate Algorithm" ,
87
139
"The selected algorithm depends on other currently non-active algorithms.\n Activate them them before trying to activate it." )
@@ -119,44 +171,96 @@ def getAdjustedText(self, text):
119
171
font = QtGui .QFont ("Verdana" , 8 )
120
172
fm = QtGui .QFontMetricsF (font )
121
173
w = fm .width (text )
122
- if w < self .BOX_WIDTH :
174
+ if w < self .BOX_WIDTH - 25 - FlatButtonGraphicItem . WIDTH :
123
175
return text
124
176
125
177
text = text [0 :- 3 ] + "..."
126
178
w = fm .width (text )
127
- while (w > self .BOX_WIDTH ):
179
+ while (w > self .BOX_WIDTH - 25 - FlatButtonGraphicItem . WIDTH ):
128
180
text = text [0 :- 4 ] + "..."
129
181
w = fm .width (text )
130
182
return text
131
183
132
184
133
185
def paint (self , painter , option , widget = None ):
134
186
135
- rect = QtCore .QRectF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , - (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 ,
187
+ rect = QtCore .QRectF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2.0 , - (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2.0 ,
136
188
ModelerGraphicItem .BOX_WIDTH + 2 , ModelerGraphicItem .BOX_HEIGHT + 2 )
137
189
painter .setPen (QtGui .QPen (QtCore .Qt .gray , 1 ))
138
190
painter .setBrush (QtGui .QBrush (QtCore .Qt .white , QtCore .Qt .SolidPattern ))
139
191
painter .drawRect (rect )
140
192
font = QtGui .QFont ("Verdana" , 8 )
141
193
painter .setFont (font )
194
+ painter .setPen (QtGui .QPen (QtCore .Qt .black ))
142
195
if self .isSelected ():
143
196
painter .setPen (QtGui .QPen (QtCore .Qt .blue ))
144
- else :
145
- painter .setPen (QtGui .QPen (QtCore .Qt .black ))
197
+ if isinstance (self .element , GeoAlgorithm ):
198
+ if self .elementIndex in self .model .deactivated :
199
+ painter .setPen (QtGui .QPen (QtCore .Qt .lightGray ))
146
200
fm = QtGui .QFontMetricsF (font )
147
201
text = self .getAdjustedText (self .text )
148
- w = fm .width (QtCore .QString (text ))
149
202
h = fm .height ()
150
- pt = QtCore .QPointF (- w / 2 , h / 2 )
203
+ pt = QtCore .QPointF (- ( ModelerGraphicItem . BOX_WIDTH ) / 2 + 25 , h / 2.0 )
151
204
painter .drawText (pt , text )
152
- if isinstance (self .element , GeoAlgorithm ):
153
- if self .elementIndex in self .model .deactivated :
154
- painter .setPen (QtGui .QPen (QtCore .Qt .red ))
155
- w = fm .width (QtCore .QString ("[deactivated]" ))
156
- pt = QtCore .QPointF (- w / 2 , h + h / 2 )
157
- painter .drawText (pt , "[deactivated]" )
158
- painter .drawPixmap (- 10 , - (ModelerGraphicItem .BOX_HEIGHT )/ 3 ,self .pixmap )
205
+ painter .setPen (QtGui .QPen (QtCore .Qt .black ))
206
+ if isinstance (self .element , GeoAlgorithm ):
207
+ h = (fm .height () * 1.2 )
208
+ h = h + ModelerGraphicItem .BOX_HEIGHT / 2.0
209
+ pt = QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH )/ 2 + 25 , h )
210
+ painter .drawText (pt , "In" )
211
+ i = 1
212
+ if not self .inputFolded :
213
+ for param in self .element .parameters :
214
+ text = self .getAdjustedText (param .description )
215
+ h = (fm .height () * 1.2 ) * (i + 1 )
216
+ h = h + ModelerGraphicItem .BOX_HEIGHT / 2.0
217
+ pt = QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH )/ 2 + 33 , h )
218
+ painter .drawText (pt , text )
219
+ i += 1
220
+ h = (fm .height () * 1.2 ) * (i + 1 )
221
+ h = h + ModelerGraphicItem .BOX_HEIGHT / 2.0
222
+ pt = QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH )/ 2 + 25 , h )
223
+ painter .drawText (pt , "Out" )
224
+ i += 1
225
+ if not self .outputFolded :
226
+ for out in self .element .outputs :
227
+ text = self .getAdjustedText (out .description )
228
+ h = (fm .height () * 1.2 ) * (i + 1 )
229
+ h = h + ModelerGraphicItem .BOX_HEIGHT / 2.0
230
+ pt = QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH )/ 2 + 33 , h )
231
+ painter .drawText (pt , text )
232
+ i += 1
233
+ painter .drawPixmap (- (ModelerGraphicItem .BOX_WIDTH / 2.0 ) + 3 , - 8 , self .pixmap )
159
234
235
+ def getLinkPointForParameter (self , paramIndex ):
236
+ offsetX = 30
237
+ if self .inputFolded :
238
+ paramIndex = - 1
239
+ offsetX = 22
240
+ font = QtGui .QFont ("Verdana" , 8 )
241
+ fm = QtGui .QFontMetricsF (font )
242
+ h = (fm .height () * 1.2 ) * (paramIndex + 2 ) - fm .height () / 2.0
243
+ h = h + ModelerGraphicItem .BOX_HEIGHT / 2.0
244
+ return QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH )/ 2 + offsetX , h )
245
+
246
+
247
+ def getXPositionForFoldButton (self ):
248
+ return 0
249
+
250
+ def getLinkPointForOutput (self , outputIndex ):
251
+ if isinstance (self .element , GeoAlgorithm ):
252
+ numParams = 0 if self .inputFolded else len (self .element .parameters )
253
+ outputIndex = outputIndex if not self .outputFolded else - 1
254
+ text = self .getAdjustedText (self .element .outputs [outputIndex ].description )
255
+ font = QtGui .QFont ("Verdana" , 8 )
256
+ fm = QtGui .QFontMetricsF (font )
257
+ w = fm .width (QtCore .QString (text ))
258
+ h = (fm .height () * 1.2 ) * (outputIndex + 3 + numParams ) - fm .height () / 2.0
259
+ y = h + ModelerGraphicItem .BOX_HEIGHT / 2.0
260
+ x = - (ModelerGraphicItem .BOX_WIDTH )/ 2 + 33 + w + 5 if not self .outputFolded else 10
261
+ return QtCore .QPointF (x , y )
262
+ else :
263
+ return QtCore .QPointF (0 , 0 )
160
264
161
265
def itemChange (self , change , value ):
162
266
if change == QtGui .QGraphicsItem .ItemPositionChange :
@@ -166,11 +270,72 @@ def itemChange(self, change, value):
166
270
return value
167
271
168
272
def polygon (self ):
273
+ font = QtGui .QFont ("Verdana" , 8 )
274
+ fm = QtGui .QFontMetricsF (font )
275
+ numElements = len (self .element .parameters ) + len (self .element .outputs ) + 3
276
+ h = (fm .height () * 1.2 ) * numElements
169
277
pol = QtGui .QPolygonF ([
170
278
QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , - (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 ),
171
- QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 ),
172
- QtCore .QPointF ((ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 ),
279
+ QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 + h ),
280
+ QtCore .QPointF ((ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 + h ),
173
281
QtCore .QPointF ((ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , - (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 ),
174
282
QtCore .QPointF (- (ModelerGraphicItem .BOX_WIDTH + 2 )/ 2 , - (ModelerGraphicItem .BOX_HEIGHT + 2 )/ 2 )])
175
283
return pol
176
284
285
+ class FlatButtonGraphicItem (QtGui .QGraphicsItem ):
286
+
287
+ WIDTH = 16
288
+ HEIGHT = 16
289
+
290
+ def __init__ (self , icon , position , action ):
291
+ super (FlatButtonGraphicItem , self ).__init__ (None , None )
292
+ self .setAcceptHoverEvents (True )
293
+ self .setFlag (QtGui .QGraphicsItem .ItemIsMovable , False )
294
+ self .pixmap = icon .pixmap (self .WIDTH , self .HEIGHT , state = QtGui .QIcon .On )
295
+ self .position = position
296
+ self .isIn = False
297
+ self .action = action
298
+
299
+ def mousePressEvent (self , event ):
300
+ self .action ()
301
+
302
+ def paint (self , painter , option , widget = None ):
303
+ pt = QtCore .QPointF (- self .WIDTH / 2 , - self .HEIGHT / 2 ) + self .position
304
+ rect = QtCore .QRectF (pt .x (), pt .y (), self .WIDTH , self .HEIGHT )
305
+ if self .isIn :
306
+ painter .setPen (QtGui .QPen (QtCore .Qt .transparent , 1 ))
307
+ painter .setBrush (QtGui .QBrush (QtCore .Qt .lightGray , QtCore .Qt .SolidPattern ))
308
+ else :
309
+ painter .setPen (QtGui .QPen (QtCore .Qt .transparent , 1 ))
310
+ painter .setBrush (QtGui .QBrush (QtCore .Qt .white , QtCore .Qt .SolidPattern ))
311
+ painter .drawRect (rect )
312
+ painter .drawPixmap (pt .x (), pt .y (), self .pixmap )
313
+
314
+ def boundingRect (self ):
315
+ rect = QtCore .QRectF (self .position .x () - self .WIDTH / 2 , self .position .y () - self .HEIGHT / 2 , self .WIDTH , self .HEIGHT )
316
+ return rect
317
+
318
+ def hoverEnterEvent (self , event ):
319
+ self .isIn = True
320
+ self .update ()
321
+
322
+ def hoverLeaveEvent (self , event ):
323
+ self .isIn = False
324
+ self .update ()
325
+
326
+ class FoldButtonGraphicItem (FlatButtonGraphicItem ):
327
+
328
+ icons = { True : QtGui .QIcon (os .path .dirname (__file__ ) + "/../images/plus.gif" ),
329
+ False : QtGui .QIcon (os .path .dirname (__file__ ) + "/../images/minus.gif" )}
330
+
331
+ def __init__ (self , position , action ):
332
+ self .folded = True
333
+ icon = self .icons [True ]
334
+ super (FoldButtonGraphicItem , self ).__init__ (icon , position , action )
335
+
336
+ def mousePressEvent (self , event ):
337
+ self .folded = not self .folded
338
+ icon = self .icons [self .folded ]
339
+ self .pixmap = icon .pixmap (self .WIDTH , self .HEIGHT , state = QtGui .QIcon .On )
340
+ self .action (self .folded )
341
+
0 commit comments