genericSymbols.py 50.5 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    Generic library containing the definition of top-level symbols
    that can be used to create diagrams.

    The Symbol class contains common behaviour shared by inherited
    symobols - in particular the VerticalSymbol is a class of symbols
    that can only be vertically aligned ; Horizontal symbols, on the
    other hand, can be move in both directions.

    The "Comment" class of symbols is a floating symbol that is
    connected to a parent symbol on the right.

    This library also contains the defintition of connections
    (the Connection class).

    Symbols can have an editable text area, which behaviour is
    defined in the EditableText class. This class uses the Completer
    and Highlighter class for text auto-completion and syntax
    highlighting.


    Major functionalities offered by the generic Symbol classes are
    the insersion and deletion of items (possibly recursively if there
    are child symbols), the moving and resizing, the collision
    avoidance manoeuvres (when moving a group of symbols on top
    of another group, it has the effect of "pushing" the colliders
    so that symbols never "touch" each others - keeping the diagram
    clean.

    In order to create a specific diagram editor, the user of this
    library shall create his own symbols, that inherit either from
    Horizontal- or VerticalSymbol classes. The geometry of the symbol
    has to be defined in these subclass by defining the polygon
    and other properties (colours, filling, etc.). Some methods can
    be redefined if a particular behaviour is required (e.g. resizing
    differently, holding different connections points, etc).

    For a complete example, look at the "sdlSymbols.py" module, that
    provide symbol definitions that correspond to an SDL editor.

    Copyright (c) 2012-2013 European Space Agency

    Designed and implemented by Maxime Perrotin for the TASTE project

    Contact: maxime.perrotin@esa.int
"""

Maxime Perrotin's avatar
Maxime Perrotin committed
51
__all__ = ['Symbol', 'VerticalSymbol', 'HorizontalSymbol', 'Comment']
Maxime Perrotin's avatar
Maxime Perrotin committed
52
53
54
import os
import logging

55
from PySide.QtCore import Qt, QPoint, QPointF, QRect, QFile, QObject, Property
Maxime Perrotin's avatar
Maxime Perrotin committed
56

57
58
59
from PySide.QtGui import(QGraphicsPathItem, QGraphicsPolygonItem, QPainterPath,
                         QGraphicsItem, QPen, QColor, QMenu, QFileDialog,
                         QPainter, QLineEdit, QTextBlockFormat)
Maxime Perrotin's avatar
Maxime Perrotin committed
60
61
62
63
64
65

from PySide.QtUiTools import QUiLoader

import undoCommands
import ogAST
import ogParser
Maxime Perrotin's avatar
Maxime Perrotin committed
66
67
from Connectors import Connection, VerticalConnection, CommentConnection, \
                       RakeConnection, JoinConnection
Maxime Perrotin's avatar
Maxime Perrotin committed
68

69
from TextInteraction import EditableText
Maxime Perrotin's avatar
Maxime Perrotin committed
70

71
LOG = logging.getLogger(__name__)
Maxime Perrotin's avatar
Maxime Perrotin committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85


# pylint: disable=R0904, R0902
class Symbol(QObject, QGraphicsPathItem, object):
    '''
        Top-level class used to handle all SDL symbols
        Inherits from QObject to allow animations
    '''
    # Symbols of a given type share a text-autocompletion list:
    completion_list = set()
    # Flexible lists of symbol types that can be set as child of this symbol
    _unique_followers = []  # unique : e.g. comment symbol
    _insertable_followers = []  # no limit to insert below current symbol
    _terminal_followers = []  # cannot be inserted between two symbols
Maxime Perrotin's avatar
Maxime Perrotin committed
86
87
    # By default a symbol is resizeable
    resizeable = True
Maxime Perrotin's avatar
Maxime Perrotin committed
88
89
90
    # By default symbol size may expand when inner text exceeds border
    auto_expand = True
    # By default connections between symbols are lines, not arrows
Maxime Perrotin's avatar
Maxime Perrotin committed
91
92
    arrow_head = None
    arrow_tail = None
Maxime Perrotin's avatar
Maxime Perrotin committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    # Default mouse cursor
    default_cursor = Qt.SizeAllCursor
    # Decide if a symbol can be copy-pasted several times
    is_singleton = False
    # default textbox alignment: centered in the symbol
    # can differ on specific symbols (e.g. TextArea or label)
    textbox_alignment = Qt.AlignCenter
    # Common name is the name of the symbol as used in a parser or backend
    common_name = ''
    # Specify if a symbol always needs to be the child of another symbol
    # or if it can live standalone on the scene (yet can have children)
    needs_parent = True
    # nested_scene : a symbol may have content that can be visible on demand
    # (e.g. a subscene that appears when double-clicking on the item)
107
108
    _allow_nesting = False
    _nested_scene = None
Maxime Perrotin's avatar
Maxime Perrotin committed
109
110
111
112
113
    # keywords for the syntax highlighter
    blackbold = ()
    redbold = ()
    # Specify if the symbol can be drawn with anti-aliasing
    _antialiasing = True
Maxime Perrotin's avatar
Maxime Perrotin committed
114
115
    # Specify if the symbol has a text area
    has_text_area = True
Maxime Perrotin's avatar
Maxime Perrotin committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

    def __init__(self, parent=None):
        '''
            Create top level symbol and propagate important properties
            from parent items
        '''
        super(Symbol, self).__init__(parent)
        QGraphicsPathItem.__init__(self, parent)
        self.mode = ''
        self.comment = None
        self.text = None
        # Grabber allowing to resize the item
        self.grabber = Cornergrabber(parent=self)
        self.hyperlink_dialog = None
        self.hlink_field = None
        self.loadHyperlinkDialog()
        # hasParent compensates a Qt (or PySide) bug when calling parentItem()
        # on top-level items
        self.hasParent = False
        self.parent = None
        # Optional parser for the symbol textual representation
        self.parser = None
        # branch entry point allows working with aligned symbols
        self.branch_entrypoint = None
        # Terminal symbol can be used to identify last items of a branch
        self._terminal_symbol = False
        # and default text alignment within a textbox
        self.text_alignment = Qt.AlignLeft
        # Activate cache mode to boost rendering by calling paint less often
        self.setCacheMode(QGraphicsItem.DeviceCoordinateCache)
        # Apply symbol default mouse cursor
        self.setCursor(self.default_cursor)
        # De-ativate cache mode otherwise paint is not properly updated
        # for the comment symbol (need to refresh on_the_right property)
        self.setCacheMode(QGraphicsItem.NoCache)
        # Initialize variables used when moving/resizing
Maxime Perrotin's avatar
Maxime Perrotin committed
152
        self.coord = self.position
Maxime Perrotin's avatar
Maxime Perrotin committed
153
154
155
156
157
158
        self.height = 0
        self.old_rect = self.boundingRect()
        # List of visible connection points (that can move)
        self.movable_points = []


159
160
161
162
163
    # The "position" property cannot be defined as a standard Python
    # property because it is used in a QPropertyAnimation, which only
    # works using Qt properties. However it behaves the same way.
    position = Property(QPointF, lambda self: self.pos(),
                                 lambda self, val: self.setPos(val))
Maxime Perrotin's avatar
Maxime Perrotin committed
164

165
166
167
168
169
    pos_x = Property(float, lambda self: self.x(),
                            lambda self, val: self.setX(val))

    pos_y = Property(float, lambda self: self.y(),
                            lambda self, val: self.setY(val))
Maxime Perrotin's avatar
Maxime Perrotin committed
170

Maxime Perrotin's avatar
Maxime Perrotin committed
171
172
173
    def is_composite(self):
        ''' Return True if nested scene has something in it '''
        try:
Maxime Perrotin's avatar
Maxime Perrotin committed
174
            return any(self._nested_scene.visible_symb)
Maxime Perrotin's avatar
Maxime Perrotin committed
175
176
177
        except AttributeError:
            return False

Maxime Perrotin's avatar
Maxime Perrotin committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    @property
    def allowed_followers(self):
        '''
            Compute dynamically the list of connectable symbols based on
            the list of current connections and authorized children
        '''
        followers = list(self._insertable_followers)
        for item_type in self._unique_followers:
            # Unique followers: can only be one of this type (e.g. comment)
            if not [child for child in self.childSymbols()
                    if isinstance(child, eval(item_type))]:
                followers.append(item_type)
        if not self.next_aligned_symbol():
            followers.extend(self._terminal_followers)
        #LOG.debug(str(followers))
        return followers

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    @property
    def allow_nesting(self):
        ''' Dynamically check if the scene can have a nested subscene
            May be redefined in subclasses and checked on double-click
        '''
        return self._allow_nesting

    @property
    def nested_scene(self):
        ''' Return the nested scene. Can be redefined '''
        return self._nested_scene

    @nested_scene.setter
    def nested_scene(self, value):
        ''' Set the value of the nested scene '''
        self._nested_scene = value

Maxime Perrotin's avatar
Maxime Perrotin committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    def closest_connection_point(self, coord):
        '''
           Given a position (QPointF), expected in this symbol's
           coordinates, return the closest connection
           point on the current symbol.
           By default, return the RELATIVE POSITION of a point on the
           bounding rectangle.
        '''
        width = self.boundingRect().width()
        height = self.boundingRect().height()
        top, bottom = self.y(), self.y() + height
        left, right = self.x(), self.x() + width
        x, y = left + coord.x(), top + coord.y()
        if (x > right or x < left) and (y < top or y > bottom):
            # Coord is completely outside the rectangle area
            # in that case, return one of the angles of the bounding rect
            nearest_x = (coord.x() - width if x > right else coord.x())
            nearest_y = (coord.y() if y < top else coord.y() - height)
        elif x >= left and x <= right:
            # x is within the rectangle
            nearest_x = 0
            nearest_y = (coord.y() if abs(y - top) < abs(y - bottom)
                                   else coord.y() - height)
        elif y >= top and y <= bottom:
            # y is within the rectangle (lower priority than x)
            nearest_x = (coord.x() if abs(x - left) < abs(x - right)
                                   else coord.x() - width)
            nearest_y = 0
        return nearest_x, nearest_y

    @property
    def terminal_symbol(self):
        ''' Way to determine if a symbol is terminal (useful for branches) '''
        return self._terminal_symbol

    @terminal_symbol.setter
    def terminal_symbol(self, value):
        ''' Attribute is set per symbol '''
        self._terminal_symbol = value

    def __str__(self):
        ''' Print the text inside the symbol '''
254
255
        import traceback
        print traceback.print_stack()
Maxime Perrotin's avatar
Maxime Perrotin committed
256
257
258
259
260
        raise TypeError('Use unicode() not str()')

    def __unicode__(self):
        ''' Return the text inside the symbol '''
        return unicode(self.text) or u'no_name'
Maxime Perrotin's avatar
Maxime Perrotin committed
261

Maxime Perrotin's avatar
Maxime Perrotin committed
262
    def get_ast(self, pr_text):
Maxime Perrotin's avatar
Maxime Perrotin committed
263
        ''' Return the symbol in the AST form, as returned by the parser '''
Maxime Perrotin's avatar
Maxime Perrotin committed
264
265
        ast, _, _, _, terminators = \
                self.parser.parseSingleElement(self.common_name, pr_text)
Maxime Perrotin's avatar
Maxime Perrotin committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
        return ast, terminators

    def edit_text(self, pos=None):
        '''
            Set the focus on the text area for edition. Position is optional
            Can be called when user double clicks on an item
            Position is in scene coordinates
        '''
        _ = pos
        try:
            # Only one text area is supported for now - ignoring position
            self.text.setFocus()
        except AttributeError:
            return

Maxime Perrotin's avatar
Maxime Perrotin committed
281
    def update_completion_list(self, **kwargs):
Maxime Perrotin's avatar
Maxime Perrotin committed
282
283
284
285
286
287
288
        '''
            Update the text autocompletion list based on the symbol text
            This function is typically called when user has typed new text
            and it must be redefined per symbol
        '''
        pass

Maxime Perrotin's avatar
Maxime Perrotin committed
289
    def check_syntax(self, text):
Maxime Perrotin's avatar
Maxime Perrotin committed
290
291
        ''' Check the syntax of the text inside the symbol (if any) '''
        try:
Maxime Perrotin's avatar
Maxime Perrotin committed
292
293
            _, syntax_errors, _, _, _ = self.parser.parseSingleElement(
                                           self.common_name, text)
Maxime Perrotin's avatar
Maxime Perrotin committed
294
        except (AssertionError, AttributeError):
Maxime Perrotin's avatar
Maxime Perrotin committed
295
296
            LOG.error('Checker failed - no parser for this construct?')
        else:
Maxime Perrotin's avatar
Maxime Perrotin committed
297
            return syntax_errors
Maxime Perrotin's avatar
Maxime Perrotin committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

    def paint(self, painter, _, ___):
        ''' Apply anti-aliasing or not (symbol attribute) '''
        painter.setRenderHint(QPainter.Antialiasing, self._antialiasing)
        super(Symbol, self).paint(painter, _, ___)

    def update_position(self):
        ''' VIRTUAL - implemented in subclasses '''
        pass

    def select(self, selected=True):
        ''' When item is selected, effectively select its grabber '''
        self.grabber.setSelected(selected)

    def insert_symbol(self, parent, x, y):
        '''
            Set attributes related to the parent item when inserting the symbol
            in the scene. This method is redefined in subclasses
        '''
        if parent:
            self.hasParent = True
            self.parent = parent
            self.setParentItem(parent)

    def delete_symbol(self):
        '''
            Remove the symbol: pass ownership from parent to caller
            (undo command)
        '''
        entrypoint_parent = None
        if self.branch_entrypoint:
            # If the item is the last in a branch, make clean connections
            bep = self.branch_entrypoint
            entrypoint_parent = bep.parentItem()
            if bep.last_branch_item is self:
                # Last item of a branch, remove or reconnect the link
                # to the connection point
                for child in self.childItems():
                    # Find the connection point below
Maxime Perrotin's avatar
Maxime Perrotin committed
337
                    if isinstance(child, JoinConnection):
Maxime Perrotin's avatar
Maxime Perrotin committed
338
339
340
341
342
343
                        connection_below = child
                        break
                if bep is not self:
                    # Item is not the branch entry point itself
                    bep.last_branch_item = self.parentItem()
                    connection_below.setParentItem(self.parentItem())
Maxime Perrotin's avatar
Maxime Perrotin committed
344
                    #self.parentItem().connectionBelow = connection_below
Maxime Perrotin's avatar
Maxime Perrotin committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
                else:
                    # delete the link to the connection point
                    connection_below.setParentItem(None)
                    self.scene().removeItem(connection_below)
        child_below = self.next_aligned_symbol()
        if(child_below and self.hasParent and
                self.parentItem().next_aligned_symbol() is self):
            # Delete the connection to the child below if
            # it is not a full branch to be deleted
            child_below.connection.setParentItem(None)
            self.scene().removeItem(child_below.connection)
        if self.hasParent:
            if (not child_below or not self.parentItem().next_aligned_symbol()
                    or self.branch_entrypoint is self):
                # If nothing below or item is branch entry point,
                # remove the connection with the parent
                self.connection.setParentItem(None)
                self.scene().removeItem(self.connection)
            else:
                # Otherwise connect the item below with the parent
                child_below.connection = self.connection
                self.connection.child = child_below
                child_below.parent = self.parentItem()
                child_below.setParentItem(child_below.parent)
                # Update position of child - take place of deleted item
370
                child_below.pos_y = self.pos_y
Maxime Perrotin's avatar
Maxime Perrotin committed
371
                child_below.update_position()
Maxime Perrotin's avatar
Maxime Perrotin committed
372
373
            self.parentItem().cam(self.parentItem().position,
                                  self.parentItem().position)
Maxime Perrotin's avatar
Maxime Perrotin committed
374
            self.setParentItem(None)
Maxime Perrotin's avatar
Maxime Perrotin committed
375
        # Following line causes segfault on exit:
Maxime Perrotin's avatar
Maxime Perrotin committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
        #scene().removeItem(self)
        try:
            entrypoint_parent.updateConnectionPointPosition()
            entrypoint_parent.updateConnectionPoints()
        except AttributeError:
            pass

    def connect_to_parent(self):
        ''' Add a connection (wire) with the parent item '''
        return Connection(self.parent, self)

    def connection_to_parent(self):
        ''' Return the connection above the symbol '''
        try:
            connection, = [cnx for cnx in self.parent.connections()
Maxime Perrotin's avatar
Maxime Perrotin committed
391
                           if cnx.child == self]
Maxime Perrotin's avatar
Maxime Perrotin committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
            return connection
        except ValueError:
            return None

    def next_aligned_symbol(self):
        ''' Return the next symbol in the flow - implemented in subclasses '''
        return None

    def connections(self):
        ''' Return all child connections of this symbol '''
        return (c for c in self.childItems() if isinstance(c, Connection))

    def loadHyperlinkDialog(self):
        ''' Load dialog from ui file for defining hyperlink '''
        loader = QUiLoader()
        ui_file = QFile(':/hyperlink.ui')  # UI_DIALOG_FILE)
        ui_file.open(QFile.ReadOnly)
        self.hyperlink_dialog = loader.load(ui_file)
        ui_file.close()
        self.hyperlink_dialog.accepted.connect(self.hyperlinkChanged)
        self.hlink_field = self.hyperlink_dialog.findChild(QLineEdit, 'hlink')

    def hyperlinkChanged(self):
        ''' Update hyperlink field '''
        if not self.text:
            return
        hlink = self.hlink_field.text()
        if hlink:
            self.text.setHtml('<a href="{hlink}">{text}</a>'.format
Maxime Perrotin's avatar
Maxime Perrotin committed
421
                  (hlink=hlink, text=unicode(self.text).replace('\n', '<br>')))
Maxime Perrotin's avatar
Maxime Perrotin committed
422
423
424
            self.text.hyperlink = hlink
        else:
            self.text.hyperlink = None
Maxime Perrotin's avatar
Maxime Perrotin committed
425
            self.text.setPlainText(unicode(self.text))
Maxime Perrotin's avatar
Maxime Perrotin committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460

    def contextMenuEvent(self, event):
        ''' When user right-clicks: display context menu '''
        png_action = 'Export branch to PNG, SVG or PDF'
        hl_action = 'Hyperlink'
        my_menu = QMenu(png_action)
        if not hasattr(self, '_no_hyperlink'):
            my_menu.addAction(hl_action)
        my_menu.addAction(png_action)
        action = my_menu.exec_(event.screenPos())
        if action:
            if action.text() == png_action:
                # Save a picture of the selected symbol and all its children
                filename = QFileDialog.getSaveFileName(self.window(),
                        'Export picture', '.',
                        'Picture (*.png, *.svg, *.pdf)')[0]
                if not filename:
                    return
                save_fmt = filename.split(os.extsep)[-1]
                if save_fmt not in ('png', 'svg', 'pdf'):
                    return
                self.scene().export_branch_to_picture(self, filename, save_fmt)
            elif action.text() == hl_action:
                if self.text:
                    self.hyperlink_dialog.setParent(
                            self.scene().views()[0], Qt.Dialog)
                    self.hlink_field.setText(self.text.hyperlink)
                    self.hyperlink_dialog.show()

    def childSymbols(self):
        ''' Return the list of child symbols, excluding text/connections '''
        return (item for item in self.childItems() if isinstance(item, Symbol))

    def resize_item(self, rect):
        ''' resize item, e.g. when editing text - move children accordingly '''
Maxime Perrotin's avatar
Maxime Perrotin committed
461
462
        if not self.resizeable:
            return
463
        #pos = self.position
Maxime Perrotin's avatar
Maxime Perrotin committed
464
465
466
467
468
469
        delta_x = (self.boundingRect().width() - rect.width()) / 2.0
        delta_y = self.boundingRect().height() - rect.height()
        self.set_shape(rect.width(), rect.height())
        # Align children properly when resizing
        try:
            self.text.set_textbox_position()
Maxime Perrotin's avatar
Maxime Perrotin committed
470
        except AttributeError:
Maxime Perrotin's avatar
Maxime Perrotin committed
471
472
473
            # if called before text is initialized - or if no textbox
            pass
        for child in self.childSymbols():
474
475
            child.pos_x -= delta_x
            child.pos_y -= delta_y
Maxime Perrotin's avatar
Maxime Perrotin committed
476
        # X-pos must be updated when resizing,
477
478
        # independently from update_position -->> XXX WHY ??
        #self.setPos(pos.x() + delta_x, self.y())
Maxime Perrotin's avatar
Maxime Perrotin committed
479
        if self.comment:
480
481
            self.comment.pos_x -= delta_x
            self.comment.pos_y += delta_y / 2.0
Maxime Perrotin's avatar
Maxime Perrotin committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
        self.update_connections()

    def update_connections(self):
        '''
           When symbol moves or is resized, update the shape of all
           its connections - can be redefined in subclasses
        '''
        for cnx in self.connections():
            cnx.reshape()
        try:
            self.connection_to_parent().reshape()
        except AttributeError:
            pass
        try:
            self.branch_entrypoint.parent.update_connections()
        except AttributeError:
            pass

    def set_shape(self, width, height):
        ''' to be implemented per symbol in subclasses '''
        _, ___ = width, height
        self.prepareGeometryChange()
        self.updateConnectionPoints()
        # Update grabber size to fit the new shape size
        self.grabber.display()
        if self.hasParent:
            self.update_position()

    def mouse_click(self, _):
        '''
            Handle resizing and moving of items when grabbing
            the lower right corner
        '''
        # Save current position to be able to revert move
Maxime Perrotin's avatar
Maxime Perrotin committed
516
        self.coord = self.position
Maxime Perrotin's avatar
Maxime Perrotin committed
517
518
519
520
521
522
523
524
        rect = self.boundingRect()
        self.height = rect.height()
        if self.grabber.resize_mode != '':
            self.mode = 'Resize'
            self.old_rect = self.boundingRect()
        else:
            self.mode = 'Move'

Maxime Perrotin's avatar
Maxime Perrotin committed
525
526
527
528
    def double_click(self):
        ''' Handle double click on symbol - redefined at symbol level '''
        pass

Maxime Perrotin's avatar
Maxime Perrotin committed
529
530
531
    def mouse_move(self, event):
        ''' Handle resizing of items - moving is handled in subclass '''
        self.updateConnectionPoints()
532
        # If any, update movable end points of connections
Maxime Perrotin's avatar
Maxime Perrotin committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
        for point in self.movable_points:
            point.edge.end_connection.update_position()
        if self.mode == 'Resize':
            # Define the resizing based on where item has been grabbed
            if self.grabber.resize_mode.endswith('right'):
                user_width = user_width = event.pos().x()
            elif self.grabber.resize_mode.endswith('left'):
                user_width = self.boundingRect().width() - event.pos().x()
            else:
                user_width = self.boundingRect().width()
            if self.grabber.resize_mode.startswith('bottom'):
                user_height = event.pos().y()
            else:
                user_height = self.boundingRect().height()
            # Minimum size is the size of the text inside the symbol
            try:
                height = max(user_height,
550
                             self.text.boundingRect().height() + 10)
Maxime Perrotin's avatar
Maxime Perrotin committed
551
                width = max(user_width,
552
                            self.text.boundingRect().width() + 30)
Maxime Perrotin's avatar
Maxime Perrotin committed
553
554
555
556
557
558
559
560
561
562
563
564
565
            except AttributeError:
                height = max(user_height, 15)
                width = max(user_width, 30)
            self.resize_item(QRect(0, 0, width, height))

    def mouse_release(self, _):
        ''' Default action when mouse is released: reset mode '''
        if self.mode == 'Resize' and self.old_rect != self.boundingRect():
            with undoCommands.UndoMacro(self.scene().undo_stack, 'Resize'):
                undo_cmd = undoCommands.ResizeSymbol(
                                           self, self.old_rect,
                                           self.boundingRect())
                self.scene().undo_stack.push(undo_cmd)
Maxime Perrotin's avatar
Maxime Perrotin committed
566
                self.cam(self.coord, self.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
567
                self.updateConnectionPoints()
Maxime Perrotin's avatar
Maxime Perrotin committed
568
        elif self.mode == 'Move' and self.coord != self.position:
Maxime Perrotin's avatar
Maxime Perrotin committed
569
570
            with undoCommands.UndoMacro(self.scene().undo_stack, 'Move'):
                undo_cmd = undoCommands.MoveSymbol(
Maxime Perrotin's avatar
Maxime Perrotin committed
571
                                            self, self.coord, self.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
572
                self.scene().undo_stack.push(undo_cmd)
Maxime Perrotin's avatar
Maxime Perrotin committed
573
                self.cam(self.coord, self.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
        self.mode = ''

    def updateConnectionPoints(self):
        ''' Recursively update connection points (decision branch lengths) '''
        if(self.branch_entrypoint and self.branch_entrypoint.hasParent):
            self.branch_entrypoint.parentItem().updateConnectionPointPosition()
            self.branch_entrypoint.parentItem().updateConnectionPoints()

    def updateConnectionPointPosition(self):
        ''' Implemented in the relevant subclass '''
        pass

    # pylint: disable=R0914
    def cam(self, old_pos, new_pos, ignore=None):
        ''' Collision Avoidance Manoeuvre for top level symbols '''
        if not self.scene():
            # Make sure the item is in a scene. For instance, when loading
            # a model from a file, some items may be connected together
            # and CAM called *before* the top-level item has been inserted.
            return
        if self.hasParent:
            # Exectute CAM on top level of this item
            top_level = self
            while top_level.hasParent:
                # The "or top_level.parent" below is due to a Pyside/Qt bug
                # of the parentItem() function. It can happen that even when
                # the parent has explicitely been set with "setParentItem",
                # a subsequent call to parentItem returns None. Seems to happen
                # if the parent has not been added yet to the scene.
                top_level = top_level.parentItem() or top_level.parent
Maxime Perrotin's avatar
Maxime Perrotin committed
604
            top_level.cam(top_level.position, top_level.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
605
606
607
608
609
610
611
612
613
614
615
            return

        # In case CAM is called due to object move, go to the new position
        delta = new_pos - old_pos

        # Rectangle of current group of item in scene coordinates
        rect = (self.sceneBoundingRect() |
                self.mapRectToScene(self.childrenBoundingRect()))

        # Move the rectangle to the new position, and move the current item
        animation = False
616
        if self.position != new_pos:
Maxime Perrotin's avatar
Maxime Perrotin committed
617
618
619
620
621
            animation = True
            rect.adjust(delta.x(), delta.y(), delta.x(), delta.y())
            undo_cmd = undoCommands.MoveSymbol(
                    self, old_pos, new_pos, animate=animation)
            self.scene().undo_stack.push(undo_cmd)
622
            self.position = new_pos
Maxime Perrotin's avatar
Maxime Perrotin committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654

        # Get all items in the rectangle when placed at the new position
        items = self.scene().items(rect)

        # Filters: we keep only items that collide with the group
        # at the new position

        # (a) Filter out items from the current group
        items = [i for i in items
                if (not self.isAncestorOf(i) and i is not self)
                and isinstance(i, Symbol)]

        # (b) Filter out items that are in the rectangle but that do not
        #     actually collide with the current group
        items = [i for i in items if [j for j in i.collidingItems() if
            self.isAncestorOf(j) or j is self]]

        # (c) Filter out the potentially colliding items
        #     if they belong in the cam caller
        items = [i for i in items if not ignore or not
                i.commonAncestorItem(ignore)]

        # Get the top level item of each collider
        top_level_colliders = set()
        for item in items:
            while item.hasParent:
                item = item.parentItem() or item.parent
            top_level_colliders.add(item)

        # Determine how much we need to move the colliding groups and call
        # their CAM with this delta
        # Save colliders positions in case they are moved by a sub cam call
Maxime Perrotin's avatar
Maxime Perrotin committed
655
        col_pos = {i: i.position for i in top_level_colliders}
Maxime Perrotin's avatar
Maxime Perrotin committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
        for col in top_level_colliders:
            collider_rect = (col.sceneBoundingRect() |
                    col.mapRectToScene(col.childrenBoundingRect()))
            if old_pos.y() + rect.height() <= collider_rect.y():
                # Collision from the top: move down the collider
                delta.setX(col.x())
                delta.setY(rect.y() + rect.height() + 10)
            elif old_pos.y() >= collider_rect.y() + collider_rect.height():
                # Collision from below: move up the collider
                delta.setX(col.x())
                delta.setY(rect.y() - collider_rect.height() - 10)
            elif old_pos.x() <= col.x():
                # Collision from the left: move right
                delta.setX(rect.x() + rect.width() + 10 +
                        col.x() - collider_rect.x())
                delta.setY(col.y())
            else:
                delta.setX(col.x() - collider_rect.x() -
                        collider_rect.width() - 10 + rect.x())
                delta.setY(col.y())
Maxime Perrotin's avatar
Maxime Perrotin committed
676
677
            if col.position == col_pos[col]:
                col.cam(col.position, delta, ignore=self)
Maxime Perrotin's avatar
Maxime Perrotin committed
678
679
680
                # Put it back at the new position to make sure recursive
                # CAM can happen properly with all object in new positions
                # (End of CAM reset to the old position for animation)
681
                col.position = delta
Maxime Perrotin's avatar
Maxime Perrotin committed
682
683
        # Place top level colliders in old position so that animation can run
        for col in top_level_colliders:
684
            col.position = col_pos[col]
Maxime Perrotin's avatar
Maxime Perrotin committed
685
686
687
688
689
        self.update_connections()
        if animation:
            # If animation is planned, it will trigger only when Qt
            # control loop runs again. Going back to original position
            # so that the animation can be done from the starting point
690
            self.position = old_pos
Maxime Perrotin's avatar
Maxime Perrotin committed
691
692


Maxime Perrotin's avatar
Maxime Perrotin committed
693
class Comment(Symbol):
Maxime Perrotin's avatar
Maxime Perrotin committed
694
695
696
697
698
699
700
701
702
    '''
        Class used to handle right connected comments
    '''
    # Define reserved keywords for the syntax highlighter
    blackbold = ('TODO', 'FIXME', 'XXX')
    redbold = ()

    def __init__(self, parent=None, ast=None):
        ast = ast or ogAST.Comment()
703
        self.ast = ast
Maxime Perrotin's avatar
Maxime Perrotin committed
704
705
706
        super(Comment, self).__init__(parent)
        self.connection = None
        self.set_shape(ast.width, ast.height)
Maxime Perrotin's avatar
Maxime Perrotin committed
707
708
709
        self.text = EditableText(parent=self,
                                 text=ast.inputString,
                                 hyperlink=ast.hyperlink)
710
711
712
713
        if parent:
            local_pos = parent.mapFromScene(ast.pos_x or 0, ast.pos_y or 0)
            self.insert_symbol(parent, local_pos.x(), local_pos.y())
        #self.set_shape(ast.width, ast.height)
Maxime Perrotin's avatar
Maxime Perrotin committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
        self.common_name = 'end'
        self.parser = ogParser

    def __str__(self):
        return 'Comment'

    @property
    def on_the_right(self):
        ''' Determine if the comment symbol needs to be flipped '''
        return self.x() > self.parent.boundingRect().width() + 5

    def insert_symbol(self, parent, x, y):
        ''' Align the symbol on the right of the parent '''
        if not parent:
            return
        parent.comment = self
        super(Comment, self).insert_symbol(parent, x, y)
731
732
733
        self.pos_x = x if x is not None else parent.boundingRect().width() + 20
        self.pos_y = y if y is not None else (parent.boundingRect().height() -
                                              self.boundingRect().height()) / 2
Maxime Perrotin's avatar
Maxime Perrotin committed
734
        self.connection = self.connect_to_parent()
735
        parent.cam(parent.position, parent.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
736

Maxime Perrotin's avatar
Maxime Perrotin committed
737
738
739
740
    def connect_to_parent(self):
        ''' Redefinition of the function to use a comment connector '''
        return CommentConnection(self.parent, self)

Maxime Perrotin's avatar
Maxime Perrotin committed
741
742
743
744
745
746
747
748
749
750
751
752
753
    def delete_symbol(self):
        '''
            Specific delete actions for Comment:
            reset comment field in parent
        '''
        self.parentItem().comment = None
        super(Comment, self).delete_symbol()

    def resize_item(self, rect):
        '''
            Redefinition of the Resize function
            (Comment symbol only resizes in one direction)
        '''
Maxime Perrotin's avatar
Maxime Perrotin committed
754
        if not self.resizeable or self.grabber.resize_mode.endswith('left'):
Maxime Perrotin's avatar
Maxime Perrotin committed
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
            return
        self.set_shape(rect.width(), rect.height())
        self.update_connections()

    def set_shape(self, width, height):
        ''' Set a box - actual shape is computed in the paint function '''
        path = QPainterPath()
        path.addRect(0, 0, width, height)
        self.setPath(path)
        super(Comment, self).set_shape(width, height)

    def paint(self, painter, _, ___):
        ''' Draw the comment symbol '''
        rect = self.boundingRect()
        pen = QPen()
        pen.setStyle(Qt.DashLine)
        pen.setColor(Qt.darkGray)
        painter.setPen(pen)
        x, y, w, h = rect.x(), rect.y(), rect.width(), rect.height()
        if self.on_the_right:
            painter.drawLines([QPoint(w, y), QPoint(x, y),
                               QPoint(x, y), QPoint(x, h),
                               QPoint(x, h), QPoint(w, h)])
        else:
            painter.drawLines([QPoint(x, y), QPoint(w, y),
                               QPoint(w, y), QPoint(w, h),
                               QPoint(w, h), QPoint(x, h)])

    def mouse_move(self, event):
        ''' Handle item move '''
        super(Comment, self).mouse_move(event)
        if self.mode == 'Move':
Maxime Perrotin's avatar
Maxime Perrotin committed
787
788
            self.pos_y += event.pos().y() - event.lastPos().y()
            self.pos_x += event.pos().x() - event.lastPos().x()
Maxime Perrotin's avatar
Maxime Perrotin committed
789
790
791
792
793
794
795
796
797
798
799
800
            self.update_connections()

    def mouse_release(self, event):
        '''
            Check if the new position is valid (no collision)
            undo otherwise
        '''
        move_accepted = True
        for item in self.collidingItems():
            if isinstance(item, Symbol):
                move_accepted = False
        if not move_accepted:
801
            self.position = self.coord
Maxime Perrotin's avatar
Maxime Perrotin committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
            self.update_connections()
        return super(Comment, self).mouse_release(event)

    def updateConnectionPoints(self):
        '''
            Disable the update of connection points
            (comments do not influence them)
        '''
        pass


class Cornergrabber(QGraphicsPolygonItem, object):
    '''
        Corner grabber is used to resize an item, whatever its shape.
        Such an item is needed because when symbols have a non-rectangular
        shape, the selection area being limited to the item actual borders,
        there is no possibility to grab all items. The cornergrabber is
        a rectangle that covers the full parent bounding rect, allowing
        to resize it from any border/corner.
    '''
    hasParent = False
Maxime Perrotin's avatar
Maxime Perrotin committed
823

Maxime Perrotin's avatar
Maxime Perrotin committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
    def __init__(self, parent):
        ''' Create the grabber '''
        super(Cornergrabber, self).__init__(parent)
        self.setParentItem(parent)
        self.parent = parent
        self.setFlags(QGraphicsItem.ItemIsSelectable |
                QGraphicsItem.ItemIsMovable)
        self.setPos(0, 0)
        self.setPen(QColor(0, 0, 0, 0))
        # Accept hover events (when mouse passes over the item)
        self.setAcceptHoverEvents(True)
        self.show()
        # resize_mode indicates the direction when resizing
        self.resize_mode = ''

    def __repr__(self):
        ''' Pretty string for the print function '''
Maxime Perrotin's avatar
Maxime Perrotin committed
841
        return u'Cornergrabber of ' + unicode(self.parentItem())
Maxime Perrotin's avatar
Maxime Perrotin committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870

    def display(self):
        ''' Polygon is a rectangle of the size of the parent item '''
        self.prepareGeometryChange()
        rect = self.parent.boundingRect()
        self.setPos(0, 0)
        self.setPolygon(rect)
        self.show()

    def mousePressEvent(self, event):
        ''' Handle Qt event '''
        self.parent.mouse_click(event)
        super(Cornergrabber, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        ''' Handle Qt event '''
        self.parent.mouse_move(event)

    def mouseReleaseEvent(self, event):
        ''' Handle Qt event '''
        self.parent.mouse_release(event)

    def hoverMoveEvent(self, event):
        ''' When mouse moves inside the box, set the mouse cursor '''
        pos = event.pos()
        if self.parent.cursor().shape() != self.parent.default_cursor:
            # Parent item may have changed its cursor (e.g. when inserting
            # items). In that case, don't override the cursor for that area
            cursor = self.parent.cursor()
Maxime Perrotin's avatar
Maxime Perrotin committed
871
872
873
        elif not self.parent.resizeable:
            cursor = self.parent.default_cursor
            self.resize_mode = ''
Maxime Perrotin's avatar
Maxime Perrotin committed
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
        # Left side
        elif 0.0 <= pos.x() <= 10.0:
            # Top-left corner: disabled
            #if pos.y() <= 10.0:
            #    cursor = Qt.SizeFDiagCursor
            if pos.y() > self.boundingRect().height() - 10.0:
                cursor = Qt.SizeBDiagCursor
                self.resize_mode = 'bottom_left'
            else:
                cursor = Qt.SizeHorCursor
                self.resize_mode = 'left'
        # Middle of item
        elif 5.0 < pos.x() < self.boundingRect().width() - 10.0 and (
                     pos.y() > self.boundingRect().height() - 10.0):
            cursor = Qt.SizeVerCursor
            self.resize_mode = 'bottom'
        # Right side
        elif(self.boundingRect().width() - 5.0 <= pos.x()
                                <= self.boundingRect().width()):
            # Top-right corner: disabled
            #if pos.y() <= 10.0:
            #    cursor = Qt.SizeBDiagCursor
            if pos.y() > self.boundingRect().height() - 10.0:
                cursor = Qt.SizeFDiagCursor
                self.resize_mode = 'bottom_right'
            else:
                cursor = Qt.SizeHorCursor
                self.resize_mode = 'right'
        else:
            cursor = self.parent.default_cursor
            self.resize_mode = ''
        self.setCursor(cursor)


# pylint: disable=R0904
class HorizontalSymbol(Symbol, object):
    '''
        Class used to handle horizontal symbols
        In case of SDL: INPUT, DECISION answers, Text Symbol, Start
    '''
    def __init__(self, parent=None, text='...',
            x=None, y=None, hyperlink=None):
        super(HorizontalSymbol, self).__init__(parent)
        self.minDistanceToSymbolAbove = 20
        self.connection = None
Maxime Perrotin's avatar
Maxime Perrotin committed
919
        if self.has_text_area:
Maxime Perrotin's avatar
Maxime Perrotin committed
920
921
922
923
924
925
            self.text = EditableText(parent=self, text=text,
                    hyperlink=hyperlink)
        if parent:
            local_pos = parent.mapFromScene(x, y)
            self.insert_symbol(parent, local_pos.x(), local_pos.y())
        else:
926
            self.position = QPointF(x or 0, y or 0)
Maxime Perrotin's avatar
Maxime Perrotin committed
927

Maxime Perrotin's avatar
Maxime Perrotin committed
928
929
930
931
    def connect_to_parent(self):
        ''' Redefined: connect to parent item '''
        return RakeConnection(self.parent, self)

Maxime Perrotin's avatar
Maxime Perrotin committed
932
933
934
    def insert_symbol(self, parent, pos_x, pos_y):
        ''' Insert the symbol in the scene - Align below the parent '''
        if not parent:
935
            self.position = QPointF(pos_x, pos_y)
Maxime Perrotin's avatar
Maxime Perrotin committed
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
            return
        super(HorizontalSymbol, self).insert_symbol(parent, pos_x, pos_y)
        if pos_x is None or pos_y is None:
            # Usually for first insertion when item is created:
            # compute position and (if relevant) move siblings
            first, last = None, None
            has_siblings = False
            for sibling in self.siblings():
                has_siblings = True
                first = min(first, sibling.x()) if(
                        first is not None) else sibling.x()
                last = max(last, sibling.x() +
                        sibling.boundingRect().width()) if(
                                last is not None) else(sibling.x() +
                                        sibling.boundingRect().width())
            group_width = last - first if first is not None else 0
            for sibling in self.siblings():
                sib_x = sibling.x() - (self.boundingRect().width()) / 2 - 10
Maxime Perrotin's avatar
Maxime Perrotin committed
954
                sib_oldpos = sibling.position
955
                sibling.pos_x = sib_x
Maxime Perrotin's avatar
Maxime Perrotin committed
956
957
958
                undo_cmd = undoCommands.MoveSymbol(sibling,
                                                   sib_oldpos,
                                                   sibling.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
959
960
961
962
963
964
965
966
967
968
969
                self.scene().undo_stack.push(undo_cmd)
            most_left = min([sibling.x()
                for sibling in self.siblings()] or [0])
            if has_siblings:
                pos_x = most_left + group_width + 20
            else:
                # Verical alignment (x-axis):
                pos_x = (parent.boundingRect().width() -
                        self.boundingRect().width()) / 2
            pos_y = (parent.boundingRect().height() +
                    self.minDistanceToSymbolAbove)
970
        self.position = QPointF(pos_x, pos_y)
Maxime Perrotin's avatar
Maxime Perrotin committed
971
972
        self.connection = self.connect_to_parent()
        self.updateConnectionPoints()
Maxime Perrotin's avatar
Maxime Perrotin committed
973
        self.cam(self.position, self.position)
974
975
        LOG.debug('{} positionned at {}'.format(unicode(self),
                                         unicode(self.scenePos())))
Maxime Perrotin's avatar
Maxime Perrotin committed
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993

    def update_connections(self):
        '''
           Redefined from Symbol class
           Horizontal symbols may have siblings - check their shape.
        '''
        super(HorizontalSymbol, self).update_connections()
        try:
            for sibling in self.siblings():
                for cnx in sibling.last_branch_item.connections():
                    cnx.reshape()
        except AttributeError:
            pass

    def siblings(self):
        ''' Return all the items's sibling symbols '''
        try:
            return (item for item in self.parent.childItems()
Maxime Perrotin's avatar
Maxime Perrotin committed
994
                    if item is not self and (isinstance(self, type(item)) or
Maxime Perrotin's avatar
Maxime Perrotin committed
995
                        isinstance(item, type(self))))
Maxime Perrotin's avatar
Maxime Perrotin committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
        except:
            return ()

    def next_aligned_symbol(self):
        ''' Return the next symbol in the flow '''
        for symbol in self.childSymbols():
            if not isinstance(symbol, (HorizontalSymbol, Comment)):
                return symbol
        return None

    def mouse_move(self, event):
        ''' Prevent move from being above the parent '''
        if self.mode == 'Move':
            event_pos = event.pos()
Maxime Perrotin's avatar
Maxime Perrotin committed
1010
1011
            new_y = self.pos_y + (event_pos.y() - event.lastPos().y())
            new_x = self.pos_x + (event_pos.x() - event.lastPos().x())
Maxime Perrotin's avatar
Maxime Perrotin committed
1012
1013
1014
            if self.hasParent:
                new_y = max(new_y, self.parent.boundingRect().height() +
                        self.minDistanceToSymbolAbove)
1015
            self.position = QPointF(new_x, new_y)
Maxime Perrotin's avatar
Maxime Perrotin committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
            self.update_connections()
        super(HorizontalSymbol, self).mouse_move(event)

    def cam(self, old_pos, new_pos, ignore=None):
        '''
            Collision avoidance manoeuvre for parallel branches
            (for SDL: input, decision answers)
        '''
        if self.hasParent:
            # Rectangle of current group of item in scene coordinates
            try:
                # Disconnect the connection below the last item
                # (otherwise the rectangle will be too big)
                last_cnx, = (cnx for cnx in self.last_branch_item.connections()
                        if cnx.child == self.parentItem())
                last_cnx.setParentItem(None)
            except (AttributeError, ValueError):
                last_cnx = None

            rect = (self.sceneBoundingRect() |
                    self.mapRectToScene(self.childrenBoundingRect()))
            try:
                # Set back the last connection
                last_cnx.setParentItem(self.last_branch_item)
            except AttributeError:
                pass
            # Get all siblings (e.g. surrounding inputs/decision answers)
            for sibling in self.siblings():
                try:
                    # Disconnect the connection below the last item
                    last_cnx, = (cnx for cnx in
                            sibling.last_branch_item.connections()
                            if cnx.child == self.parentItem())
                    last_cnx.setParentItem(None)
                except (AttributeError, ValueError):
                    last_cnx = None
                sib_rect = (sibling.sceneBoundingRect() |
                        sibling.mapRectToScene(sibling.childrenBoundingRect()))
                try:
                    # Set back the last connection
                    last_cnx.setParentItem(sibling.last_branch_item)
                except AttributeError:
                    pass
                if rect.intersects(sib_rect):
                    width = (sib_rect & rect).width() + 10
Maxime Perrotin's avatar
Maxime Perrotin committed
1061
                    old_sib_pos = sibling.position
1062
1063
                    sibling.pos_x += width if self.pos_x <= sibling.pos_x \
                                           else -width
Maxime Perrotin's avatar
Maxime Perrotin committed
1064
1065
1066
                    undo_cmd = undoCommands.MoveSymbol(sibling,
                                                       old_sib_pos,
                                                       sibling.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
1067
1068
1069
1070
1071
                    try:
                        self.scene().undo_stack.push(undo_cmd)
                    except AttributeError:
                        # If there is no scene or no undo stack
                        pass
Maxime Perrotin's avatar
Maxime Perrotin committed
1072
                    sibling.cam(sibling.position, sibling.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
1073
1074
1075
        super(HorizontalSymbol, self).cam(old_pos, new_pos, ignore)
        # Recursively call the cam of the parent
        try:
Maxime Perrotin's avatar
Maxime Perrotin committed
1076
1077
            self.parentItem().cam(self.parentItem().position,
                                  self.parentItem().position)
Maxime Perrotin's avatar
Maxime Perrotin committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
        except AttributeError:
            pass
        self.update_connections()


class VerticalSymbol(Symbol, object):
    '''
        Class used to handle vertically-aligned symbols
        In case of SDL: STATE, OUTPUT, PROCEDURE, DECISION, TASK
    '''
    def __init__(self, parent=None, text='...',
            x=None, y=None, hyperlink=None):
        super(VerticalSymbol, self).__init__(parent)
        self.connection = None
Maxime Perrotin's avatar
Maxime Perrotin committed
1092
        if self.has_text_area:
1093
1094
1095
            self.text = EditableText(parent=self,
                                     text=text,
                                     hyperlink=hyperlink)
Maxime Perrotin's avatar
Maxime Perrotin committed
1096
1097
1098
1099
1100
        self.minDistanceToSymbolAbove = 15
        if parent:
            local_pos = self.mapFromScene(0, y or 0)
            self.insert_symbol(parent=parent, x=None, y=local_pos.y())
        else:
1101
            self.position = QPointF(x or 0, y or 0)
Maxime Perrotin's avatar
Maxime Perrotin committed
1102
1103
1104
1105
1106
1107
1108
1109

    def next_aligned_symbol(self):
        ''' Return the next symbol in the flow '''
        for symbol in self.childSymbols():
            if isinstance(symbol, VerticalSymbol):
                return symbol
        return None

Maxime Perrotin's avatar
Maxime Perrotin committed
1110
1111
1112
1113
    def connect_to_parent(self):
        ''' Redefined: connect to parent item with a straight line '''
        return VerticalConnection(self.parent, self)

Maxime Perrotin's avatar
Maxime Perrotin committed
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
    def insert_symbol(self, parent, x, y):
        '''
            Vertical symbol: place the symbol in the scene.
            Determine the coordinates based on the position
            and size of the parent item, and make proper connections
        '''
        if not parent:
            # Place standalone item on the scene at given coordinates
            # (e.g. floating state)
            if x is not None and y is not None:
1124
                self.position = QPointF(x, y)
Maxime Perrotin's avatar
Maxime Perrotin committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
            return
        super(VerticalSymbol, self).insert_symbol(parent, x, y)
        # in a branch (e.g. DECISION) all items must know the first element
        # (used for computing the branch size and the connection point)
        self.branch_entrypoint = parent.branch_entrypoint
        # Check if parent has a connection point (e.g. DECISION)
        # i.e. Check if we are inserting below a decision group
        if hasattr(parent, 'connectionPoint'):
            # Move the new symbol below the connection point:
            # First, check if the parent was the last item of another branch
            for child in parent.childItems():
                if(child not in (self, parent.text, parent.grabber) and not
                        isinstance(child, HorizontalSymbol)):
                    # Insertion: change parent and position of previous child:
                    if (isinstance(child, Connection) and
                            isinstance(child.child, Comment) or
                            isinstance(child, Comment)):
                        continue
                    if not isinstance(child, Connection) or not isinstance(
                            child.child, HorizontalSymbol):
                        child.setParentItem(self)
                        child.parent = self
                        if isinstance(child, Symbol):
                            child_y_diff = (
                                    child.y() -
                                    self.parentItem().connectionPoint.y() +
                                    self.boundingRect().height() +
                                    self.minDistanceToSymbolAbove)
1153
                            child.pos_y = child_y_diff
Maxime Perrotin's avatar
Maxime Perrotin committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
                            if not isinstance(child, Comment):
                                child.update_position()
        else:
            # If parent already had children,
            # change their parent to the inserted symbol
            for child in parent.childItems():
                if child not in (
                            self, parent.text, parent.comment, parent.grabber):
                    if (isinstance(child, Connection) and
                            isinstance(child.child, Comment)):
                        # Don't change the parent of a comment
                        continue
                    child.parent = self
                    child.setParentItem(self)
                    # move child position down when inserting
                    if isinstance(child, Symbol):
1170
                        child.pos_y = 0.0
Maxime Perrotin's avatar
Maxime Perrotin committed
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
                        child.update_position()

        # If inserting an symbol at the end of a branch (e.g. DECISION),
        # inform the branch entry point
        if self.branch_entrypoint and not self.next_aligned_symbol():
            self.branch_entrypoint.last_branch_item = self

        # Create the connection with the parent symbol
        self.connection = self.connect_to_parent()
        self.update_position()
        self.updateConnectionPoints()
        if y is not None:
1183
            self.pos_y = y
Maxime Perrotin's avatar
Maxime Perrotin committed
1184
        self.cam(self.position, self.position)
1185
1186
        LOG.debug('{} positionned at {}'.format(unicode(self),
                                         unicode(self.scenePos())))
Maxime Perrotin's avatar
Maxime Perrotin committed
1187
1188
1189
1190
1191
1192
1193
1194

    def update_position(self):
        '''
            Update the symbol position -
            always below its parent (check collisions, etc.)
        '''
        # 'or self.parent' because of pyside/qt bug
        parent = self.parentItem() or self.parent
1195
1196
        self.pos_x = -((self.boundingRect().width() -
                      parent.boundingRect().width()) / 2)
Maxime Perrotin's avatar
Maxime Perrotin committed
1197
1198
        # In case of collision with parent item, move down
        try:
1199
            self.pos_y = max(self.y(), parent.connectionPoint.y())
Maxime Perrotin's avatar
Maxime Perrotin committed
1200
        except AttributeError:
1201
            self.pos_y = max(self.y(), parent.boundingRect().height() + 15)
Maxime Perrotin's avatar
Maxime Perrotin committed
1202
1203
1204
1205
1206

    def mouse_move(self, event):
        ''' Click and move: forbid symbol to move on the x axis '''
        super(VerticalSymbol, self).mouse_move(event)
        if self.mode == 'Move':
Maxime Perrotin's avatar
Maxime Perrotin committed
1207
            new_y = self.pos_y + (event.pos().y() - event.lastPos().y())
Maxime Perrotin's avatar
Maxime Perrotin committed
1208
            if not self.parent:
1209
                self.pos_x += event.pos().x() - event.lastPos().x()
Maxime Perrotin's avatar
Maxime Perrotin committed
1210
            if not self.hasParent or (new_y >=
1211
1212
1213
                                      self.connection.start_point.y() +
                                      self.parent.minDistanceToSymbolAbove):
                self.pos_y = new_y
Maxime Perrotin's avatar
Maxime Perrotin committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
            self.update_connections()
            self.updateConnectionPoints()

    def cam(self, old_pos, new_pos, ignore=None):
        ''' Collision avoidance manoeuvre for vertical symbols '''
        if self.hasParent:
            branch_entry = self
            while branch_entry.hasParent and isinstance(
                    branch_entry, VerticalSymbol):
                # See cam of symbol for explanation about
                # the 'or branch_entry.parent' (pyside/qt bug)
                branch_entry = branch_entry.parentItem() or branch_entry.parent
Maxime Perrotin's avatar
Maxime Perrotin committed
1226
            branch_entry.cam(branch_entry.position, branch_entry.position)
Maxime Perrotin's avatar
Maxime Perrotin committed
1227
1228
        else:
            super(VerticalSymbol, self).cam(old_pos, new_pos)