opengeode.py 136 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
#!/usr/bin/env python3
Maxime Perrotin's avatar
Maxime Perrotin committed
2
3
4
5
6
7
8
9
# -*- coding: utf-8 -*-

# pylint: disable=C0302
"""
    OpenGEODE - A tiny, free SDL Editor for TASTE

    SDL is the Specification and Description Language (Z100 standard from ITU)

Maxime Perrotin's avatar
Maxime Perrotin committed
10
    Copyright (c) 2012-2020 European Space Agency
Maxime Perrotin's avatar
Maxime Perrotin committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

    Designed and implemented by Maxime Perrotin

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

import signal
import sys
import os
import argparse
import logging
import traceback
import re
import code
import pprint
26
import random
Maxime Perrotin's avatar
Maxime Perrotin committed
27
from functools import partial
Maxime Perrotin's avatar
Maxime Perrotin committed
28
from itertools import chain
29
from importlib import reload
Maxime Perrotin's avatar
Maxime Perrotin committed
30

Maxime Perrotin's avatar
Maxime Perrotin committed
31
32
33
# To freeze the application on Windows, all modules must be imported even
# when they are not directly used from this module (py2exe bug)
# NOQA makes flake8 ignore locally-ununsed modules
Maxime Perrotin's avatar
Maxime Perrotin committed
34
# pylint: disable=W0611
35
import enum  # NOQA
36
import string  # NOQA
37
import fnmatch  # NOQA
38
import operator  # NOQA
39
import subprocess  # NOQA
Maxime Perrotin's avatar
Maxime Perrotin committed
40
import distutils  # NOQA
41
42
43
import tempfile  # NOQA
import uuid  # NOQA
import importlib  # NOQA
Maxime Perrotin's avatar
Maxime Perrotin committed
44
45
46
import antlr3  # NOQA
import antlr3.tree  # NOQA
import importlib  # NOQA
Maxime Perrotin's avatar
Maxime Perrotin committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from functools import singledispatch

from . import(undoCommands,  # NOQA
              sdl92Lexer,  # NOQA
              sdl92Parser,  # NOQA
              genericSymbols,  # NOQA
              Asn1scc,  # NOQA
              sdlSymbols,
              AdaGenerator,
              ogParser,
              ogAST,
              Renderer,
              Clipboard,
              Statechart,
              Lander,
              Helper,
              Pr,
              CGenerator,
              QGenSDL,
              Connectors,  # NOQA
              TextInteraction)  # NOQA
Maxime Perrotin's avatar
Maxime Perrotin committed
68
import pygraphviz  # NOQA
Maxime Perrotin's avatar
Maxime Perrotin committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from typing import List, Union, Dict, Set, Any, Tuple


from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtUiTools import *
from PySide2 import QtSvg

from .genericSymbols import Symbol, Comment, Cornergrabber, Connection, Channel
from .sdlSymbols import(Input,
                        Output,
                        Decision,
                        DecisionAnswer,
                        Task,
                        ProcedureCall,
                        TextSymbol,
                        State,
                        Start,
                        Join,
                        Label,
                        Procedure,
                        ProcedureStart,
                        ProcedureStop,
                        StateStart,
                        Connect,
                        Process,
                        ContinuousSignal,
                        ProcessType)

from .TextInteraction import EditableText
Maxime Perrotin's avatar
Maxime Perrotin committed
100
101

# Icons and png files generated from the resource file:
Maxime Perrotin's avatar
Maxime Perrotin committed
102
from . import icons  # NOQA
Maxime Perrotin's avatar
Maxime Perrotin committed
103

Maxime Perrotin's avatar
Maxime Perrotin committed
104
# Logging: ist of properly loaded modules that will use it
Maxime Perrotin's avatar
Maxime Perrotin committed
105
LOG = logging.getLogger(__name__)
Maxime Perrotin's avatar
Maxime Perrotin committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
MODULES = [
    sdlSymbols,
    genericSymbols,
    ogAST,
    ogParser,
    Lander,
    AdaGenerator,
    undoCommands,
    Renderer,
    Clipboard,
    Statechart,
    Helper,
    Asn1scc,
    Connectors,
    Pr,
    TextInteraction,
    Connectors,
    CGenerator,
124
    QGenSDL,
Maxime Perrotin's avatar
Maxime Perrotin committed
125
] # type: List[module]
126

127
128
129
# Define custom UserRoles
ANCHOR = Qt.UserRole + 1

Maxime Perrotin's avatar
Maxime Perrotin committed
130
131
try:
    import LlvmGenerator
Maxime Perrotin's avatar
Maxime Perrotin committed
132
    MODULES.append(LlvmGenerator)
Maxime Perrotin's avatar
Maxime Perrotin committed
133
except ImportError:
Maxime Perrotin's avatar
Maxime Perrotin committed
134
    pass
Maxime Perrotin's avatar
Maxime Perrotin committed
135

Maxime Perrotin's avatar
Maxime Perrotin committed
136
137
try:
    import StgBackend
Maxime Perrotin's avatar
Maxime Perrotin committed
138
    MODULES.append(StgBackend)
Maxime Perrotin's avatar
Maxime Perrotin committed
139
except ImportError:
Maxime Perrotin's avatar
Maxime Perrotin committed
140
    pass
Maxime Perrotin's avatar
Maxime Perrotin committed
141
142


Maxime Perrotin's avatar
Maxime Perrotin committed
143
__all__ = ['opengeode', 'SDL_Scene', 'SDL_View', 'parse']
144
__version__ = '3.3.5'
Maxime Perrotin's avatar
Maxime Perrotin committed
145
146
147
148

if hasattr(sys, 'frozen'):
    # Detect if we are running on Windows (py2exe-generated)
    try:
Maxime Perrotin's avatar
Maxime Perrotin committed
149
150
        CUR_DIR = os.path.dirname(
                str(sys.executable, sys.getfilesystemencoding()))
Maxime Perrotin's avatar
Maxime Perrotin committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    except TypeError:
        CUR_DIR = os.path.dirname(os.path.realpath(__file__))
    else:
        # Redirect stderr to a log file - to avoid py2exe error message
        # that pops up at application closure when app logs errors
        sys.stdout = open('opengeode_stdout.log', 'w')
        sys.stderr = open('opengeode_stderr.log', 'w')
else:
    CUR_DIR = os.path.dirname(os.path.realpath(__file__))


# Global handling all top-level elements
# It seems that if we don't keep a global list of these elements
# (START, STATE, and Text symbols)
# they sometimes get destroyed and disappear from the scene.
# As if a GC was deleting these object *even if they belong to the scene*
# (but have no parentItem). Most likely a Qt/Pyside bug.
168
# NOTE: This was not re-evaluated with PySide2
Maxime Perrotin's avatar
Maxime Perrotin committed
169
170
G_SYMBOLS = set()

171
172
173
174
175
176
177
# There is a bug in Pyside2 with the setData(..) function. It should
# normally allow to store any type (as QVariant does in C++), however
# it does not manage to store QGraphicsItems (i.e. symbols). Because
# of that we must keep a table to find symbols that contain an error
# The table is cleared each time the Check Model is called
G_ERRORS = list()

Maxime Perrotin's avatar
Maxime Perrotin committed
178
179
180
181
182
183
184
185
186
187

# Other Qt bug:
# QGraphicsTextItem don't stand that their parent item (usually an
# SDL symbol) is removed from the scene (scene.removeItem()). It
# provokes segfault when application exits.
# Workaround is to use hide()/show() to make items disappear
# from the scene (when deleted). Seems OK (MP 2013-03-26)

# Lookup table used to configure the context-dependent toolbars
ACTIONS = {
188
    'block': [Process, ProcessType, Comment, TextSymbol],
189
190
191
    'process': [Start, State, Input, Connect, ContinuousSignal, Task, Decision,
                DecisionAnswer, Output, ProcedureCall, TextSymbol, Comment,
                Label, Join, Procedure],
Maxime Perrotin's avatar
Maxime Perrotin committed
192
193
194
    'procedure': [ProcedureStart, Task, Decision,
                  DecisionAnswer, Output, ProcedureCall, TextSymbol,
                  Comment, Label, Join, ProcedureStop],
195
    'statechart': [],
196
197
198
    'state': [StateStart, State, Input, Connect, ContinuousSignal, Task,
              Decision, DecisionAnswer, Output, ProcedureCall, TextSymbol,
              Comment, Label, Join, ProcedureStop, Procedure],
Maxime Perrotin's avatar
Maxime Perrotin committed
199
200
    'clipboard': [Start, State, Input, Connect, Task, Decision, DecisionAnswer,
                  Output, ProcedureCall, TextSymbol, Comment, Label,
201
202
                  Join, Procedure, Process, StateStart, ProcedureStop,
                  ContinuousSignal],
Maxime Perrotin's avatar
Maxime Perrotin committed
203
204
    'lander': [],
    'asn1': []
Maxime Perrotin's avatar
Maxime Perrotin committed
205
206
207
}


208
209
def log_errors(window, errors, warnings, clearfirst=True):
    ''' Report Error and Warnings on the console and in the log window '''
210
    G_ERRORS.clear()
211
212
213
214
215
216
217
218
    if window and clearfirst:
        window.clear()
    for error in errors:
        if type(error[0]) == list:
            # should be fixed now, CHECKME - NO, NOT FULLY FIXED
            # problem is in decision answers branches
            error[0] = 'Internal error - ' + str(error[0])
        LOG.error(error[0])
219
        item = QListWidgetItem('[ERROR] ' + error[0])
220
221
222
223
        if len(error) == 3:
            item.setData(Qt.UserRole, error[1])
            #found = self.scene().symbol_near(QPoint(*error[1]), 1)
            # Pyside bug: setData cannot store 'found' directly
224
            #item.setData(Qt.UserRole + 2, id(found))
225
226
227
228
229
            item.setData(Qt.UserRole + 1, error[2])
        if window:
            window.addItem(item)
    for warning in warnings:
        LOG.warning(warning[0])
230
        item = QListWidgetItem('[WARNING] ' + str(warning[0]))
231
232
233
234
235
236
237
238
239
        if len(warning) == 3:
            item.setData(Qt.UserRole, warning[1])
            item.setData(Qt.UserRole + 1, warning[2])
        if window:
            window.addItem(item)
    if not errors and not warnings and window:
        window.addItem('No errors, no warnings!')


Maxime Perrotin's avatar
Maxime Perrotin committed
240
class Vi_bar(QLineEdit, object):
Maxime Perrotin's avatar
Maxime Perrotin committed
241
242
243
    ''' Line editor for the Vi-like command mode '''
    def __init__(self):
        ''' Create the bar - no need for parent '''
244
        super().__init__()
245
246
        self.history = []
        self.pointer = 0
Maxime Perrotin's avatar
Maxime Perrotin committed
247
248

    def keyPressEvent(self, key_event):
249
        ''' Handle key press - Escape, command history '''
250
        super().keyPressEvent(key_event)
Maxime Perrotin's avatar
Maxime Perrotin committed
251
252
        if key_event.key() == Qt.Key_Escape:
            self.clearFocus()
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
            self.pointer = len(self.history)
        elif key_event.key() == Qt.Key_Return:
            self.history.append(self.text())
            self.pointer = len(self.history)
        elif key_event.key() == Qt.Key_Up:
            if self.text() and self.text() not in self.history:
                self.history.insert(self.pointer + 1, self.text())
            self.pointer = max(0, self.pointer - 1)
            try:
                self.setText(self.history[self.pointer])
            except IndexError as err:
                pass
        elif key_event.key() == Qt.Key_Down:
            self.pointer = min(len(self.history), self.pointer + 1)
            try:
                self.setText(self.history[self.pointer])
            except IndexError:
                pass
            pass
Maxime Perrotin's avatar
Maxime Perrotin committed
272
273


Maxime Perrotin's avatar
Maxime Perrotin committed
274
class File_toolbar(QToolBar, object):
Maxime Perrotin's avatar
Maxime Perrotin committed
275
276
277
    ''' Toolbar with file open, save, etc '''
    def __init__(self, parent):
        ''' Create the toolbar using standard icons '''
278
        super().__init__(parent)
279
        self.setObjectName("File Toolbar")  # needed to save geometry
Maxime Perrotin's avatar
Maxime Perrotin committed
280
281
282
        self.setMovable(False)
        self.setFloatable(False)
        self.new_button = self.addAction(self.style().standardIcon(
Maxime Perrotin's avatar
Maxime Perrotin committed
283
            QStyle.SP_FileIcon), 'New model')
Maxime Perrotin's avatar
Maxime Perrotin committed
284
        self.open_button = self.addAction(self.style().standardIcon(
Maxime Perrotin's avatar
Maxime Perrotin committed
285
            QStyle.SP_DialogOpenButton), 'Open model')
Maxime Perrotin's avatar
Maxime Perrotin committed
286
        self.save_button = self.addAction(self.style().standardIcon(
Maxime Perrotin's avatar
Maxime Perrotin committed
287
            QStyle.SP_DialogSaveButton), 'Save model')
Maxime Perrotin's avatar
Maxime Perrotin committed
288
        self.check_button = self.addAction(self.style().standardIcon(
Maxime Perrotin's avatar
Maxime Perrotin committed
289
            QStyle.SP_DialogApplyButton), 'Check model')
Maxime Perrotin's avatar
Maxime Perrotin committed
290
291
292
        self.addSeparator()
        # Up arrow to come back from a subscene (e.g. procedure)
        self.up_button = self.addAction(self.style().standardIcon(
Maxime Perrotin's avatar
Maxime Perrotin committed
293
            QStyle.SP_ArrowUp), 'Go one level above')
Maxime Perrotin's avatar
Maxime Perrotin committed
294
295
296
        self.up_button.setEnabled(False)


Maxime Perrotin's avatar
Maxime Perrotin committed
297
class Sdl_toolbar(QToolBar, object):
Maxime Perrotin's avatar
Maxime Perrotin committed
298
299
300
301
302
303
304
305
306
    '''
        Toolbar with SDL symbols
        The list of symbols is passed as paramters at creation time ; the class
        looks for icons for the name of the symbols and .png extension.
        The buttons activation is context dependent. Configuration is done
        directly at symbol leval (using the "allowed_followers" property)
    '''
    def __init__(self, parent):
        ''' Create the toolbar, get icons and link actions '''
307
        super().__init__(parent)
308
        self.setObjectName("SDL Toolbar")  # needed to save geometry
Maxime Perrotin's avatar
Maxime Perrotin committed
309
310
311
312
313
314
        self.setMovable(False)
        self.setFloatable(False)
        self.setIconSize(QSize(35, 35))
        self.actions = {}

    def set_actions(self, bar_items):
Maxime Perrotin's avatar
Maxime Perrotin committed
315
        ''' Set the icons and actions on the toolbar '''
Maxime Perrotin's avatar
Maxime Perrotin committed
316
317
318
319
320
        self.actions = {}
        self.clear()
        for item in bar_items:
            item_name = item.__name__
            self.actions[item_name] = self.addAction(
Maxime Perrotin's avatar
Maxime Perrotin committed
321
                           QIcon(':icons/{}.png'
Maxime Perrotin's avatar
Maxime Perrotin committed
322
                                       .format(item_name.lower())), item_name)
Maxime Perrotin's avatar
Maxime Perrotin committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
        self.update_menu()

    def enable_action(self, action):
        ''' Used as a slot to allow a scene to enable a menu action,
            e.g. if the Start symbol is deleted, the Start button
            shall be activated '''
        self.actions[action].setEnabled(True)

    def disable_action(self, action):
        ''' See description in enable_action '''
        self.actions[action].setEnabled(False)

    def update_menu(self, scene=None):
        ''' Context-dependent enabling/disabling of menu buttons '''
        try:
Maxime Perrotin's avatar
Maxime Perrotin committed
338
            selection = list(scene.selected_symbols)
Maxime Perrotin's avatar
Maxime Perrotin committed
339
340
341
342
343
344
345
346
347
348
        except AttributeError:
            selection = []
        if not selection:
            # Second chance, check if an item has focus (editable text)
            try:
                selection.append(scene.focusItem().parentItem())
            except AttributeError:
                pass
        if len(selection) > 1:
            # When several items are selected, disable all menu entries
Maxime Perrotin's avatar
Maxime Perrotin committed
349
            for _, action in self.actions.items():
Maxime Perrotin's avatar
Maxime Perrotin committed
350
351
352
353
354
                action.setEnabled(False)
        elif not selection:
            # When nothing is selected:
            # activate everything, and when user selects an icon,
            # keep the action on hold until he clicks on the scene
Maxime Perrotin's avatar
Maxime Perrotin committed
355
            for action in self.actions.keys():
Maxime Perrotin's avatar
Maxime Perrotin committed
356
357
358
359
                self.actions[action].setEnabled(True)

            # Check for singletons (e.g. START symbol)
            try:
Maxime Perrotin's avatar
Maxime Perrotin committed
360
                for item in scene.visible_symb:
Maxime Perrotin's avatar
Maxime Perrotin committed
361
                    try:
362
                        if item.is_singleton:
Maxime Perrotin's avatar
Maxime Perrotin committed
363
364
365
366
367
368
369
370
371
                            self.actions[
                                    item.__class__.__name__].setEnabled(False)
                    except (AttributeError, KeyError) as error:
                        LOG.debug(str(error))
            except AttributeError:
                pass
        else:
            # Only one selected item
            selection, = selection
Maxime Perrotin's avatar
Maxime Perrotin committed
372
            for action in self.actions.keys():
Maxime Perrotin's avatar
Maxime Perrotin committed
373
                self.actions[action].setEnabled(False)
374
            for action in getattr(selection, 'allowed_followers', []):
Maxime Perrotin's avatar
Maxime Perrotin committed
375
376
377
                try:
                    self.actions[action].setEnabled(True)
                except KeyError:
378
379
                    pass
                    #LOG.debug('No menu item for symbol "' + action + '"')
Maxime Perrotin's avatar
Maxime Perrotin committed
380
381


Maxime Perrotin's avatar
Maxime Perrotin committed
382
class SDL_Scene(QGraphicsScene):
Maxime Perrotin's avatar
Maxime Perrotin committed
383
384
    ''' Main graphic scene (canvas) where the user can place SDL symbols '''
    # Signal to be emitted when the scene is left (e.g. UP button)
Maxime Perrotin's avatar
Maxime Perrotin committed
385
386
387
    scene_left = Signal()
    context_change = Signal()
    word_under_cursor = Signal(str)
Maxime Perrotin's avatar
Maxime Perrotin committed
388

389
    def __init__(self, context='process', readonly=False):
390
391
392
393
394
        ''' Create a Scene for a given context:
            process, procedure, composite state, clipboard, etc.
            Design note: creating subclasses per context was evaluated but
            rejected - there are too few behavioural differences between them
            Creating tons of files / classes is not right. Keep it simple.
395
396
            "readonly" is a command line argument that allows to prevent some
            scenes to be modified by the user.
397
        '''
398
        super().__init__()
Maxime Perrotin's avatar
Maxime Perrotin committed
399
400
        # Reference to the parent scene
        self.parent_scene = None
401
402
        # mode can be "idle", "wait_connection_source", "select_items",
        # "wait_next_connection_point", "wait_placement"
Maxime Perrotin's avatar
Maxime Perrotin committed
403
404
        self.mode = 'idle'
        self.context = context
Maxime Perrotin's avatar
Maxime Perrotin committed
405
        self.allowed_symbols = ACTIONS[context]
406
407
        self.readonly = readonly
        self.set_readonly(readonly)
Maxime Perrotin's avatar
Maxime Perrotin committed
408
409
        # Configure the action menu
        all_possible_actions = set()
Maxime Perrotin's avatar
Maxime Perrotin committed
410
        for action in ACTIONS.values():
Maxime Perrotin's avatar
Maxime Perrotin committed
411
412
413
414
415
416
            all_possible_actions |= set(action)
        self.actions = {action.__name__: partial(self.add_symbol, action)
                for action in all_possible_actions}

        # Create a stack for handling undo/redo commands
        # (defined in undoCommands.py)
Maxime Perrotin's avatar
Maxime Perrotin committed
417
        self.undo_stack = QUndoStack(self)
Maxime Perrotin's avatar
Maxime Perrotin committed
418
419
420
        # buttonSelected is used to set which symbol to draw
        # on next scene click (see mousePressEvent)
        self.button_selected = None
Maxime Perrotin's avatar
Maxime Perrotin committed
421
422
        self.setBackgroundBrush(QBrush(QImage(':icons/texture.png')))
        self.messages_window = QListWidget()  # default
Maxime Perrotin's avatar
Maxime Perrotin committed
423
        self.click_coordinates = None
424
        self.orig_pos = None
425
        # When connecting symbols, store list of intermediate points
Maxime Perrotin's avatar
Maxime Perrotin committed
426
427
        self.edge_points = []   # type is List[QPointF] in scene coordinates
        self.temp_lines = []    # type is List[QGraphicsLineItem]
Maxime Perrotin's avatar
Maxime Perrotin committed
428
        self.process_name = 'opengeode'
Maxime Perrotin's avatar
Maxime Perrotin committed
429
430
        # Scene name is used to update the tab window name when scene changes
        self.name = ''
Maxime Perrotin's avatar
Maxime Perrotin committed
431
432
433
434
435
        # search_item/search_pattern are used for search/replace function
        self.search_item = None
        self.search_pattern = None
        # Selection rectangle when user clicks on the scene and moves mouse
        self.select_rect = None
Maxime Perrotin's avatar
Maxime Perrotin committed
436
437
        # Keep a list of composite states: {'stateName': SDL_Scene}
        self._composite_states = {}
438
439
        # Keep a track of highlighted symbols: { symbol: brush }
        self.highlighted = {}
440
        self.refresh_requested = False
441
442
        # Flag indicating the presence of unsolved semantic errors in the model
        self.semantic_errors = False
Maxime Perrotin's avatar
Maxime Perrotin committed
443

444
445
446
447
    def close(self):
        ''' close function is needed by py.test-qt '''
        pass

448
449
    def set_readonly(self, readonly=True):
        ''' Set the current scene as read-only, discard all new actions '''
Maxime Perrotin's avatar
Maxime Perrotin committed
450
        if self.context == 'process' and readonly:
451
452
453
454
            # only applies to 1st level hierarchy (process) allowing to have
            # unmodifiable list of DCL and STATES at the 1st level of hierarchy
            ACTIONS[self.context] = []
            #self.allowed_symbols = [] if readonly else ACTIONS[self.context]
455

456
457
458
459
460
461
462
463
464
465
    def is_aggregation(self):
        ''' Determine if the current scene is a state aggregation, i.e. if
        if contains only floating states without children
        '''
        for each in self.visible_symb:
            if each.hasParent:
                return False
            if not isinstance(each, State):
                # At the moment do not support Text Areas
                return False
Maxime Perrotin's avatar
Maxime Perrotin committed
466
            if any(child for child in each.childSymbols()
467
468
469
470
471
                    if isinstance(child, (Input, ContinuousSignal))):
                return False
        return True


Maxime Perrotin's avatar
Maxime Perrotin committed
472
473
474
    @property
    def visible_symb(self):
        ''' Return the visible items of a scene '''
Maxime Perrotin's avatar
Maxime Perrotin committed
475
476
477
        return (it for it in self.items() if it.isVisible() and
                isinstance(it, Symbol))

478

Maxime Perrotin's avatar
Maxime Perrotin committed
479
480
481
482
483
    @property
    def editable_texts(self):
        ''' Return all EditableText areas of a scene '''
        return (it for it in self.items() if it.isVisible() and
                isinstance(it, EditableText))
Maxime Perrotin's avatar
Maxime Perrotin committed
484

485

Maxime Perrotin's avatar
Maxime Perrotin committed
486
487
488
489
    @property
    def floating_symb(self):
        ''' Return the top level floating items of a scene '''
        return (it for it in self.visible_symb if not it.hasParent)
Maxime Perrotin's avatar
Maxime Perrotin committed
490

491

492
493
494
    @property
    def processes(self):
        ''' Return visible processes components of the scene '''
495
496
497
        return (it for it in self.visible_symb
                if (isinstance(it, Process) and not isinstance(it, Procedure))
                or isinstance(it, ProcessType))
498

499

500
501
502
503
504
    @property
    def procedures(self):
        ''' Return visible procedures components of the scene '''
        return (it for it in self.visible_symb if isinstance(it, Procedure))

505

Maxime Perrotin's avatar
Maxime Perrotin committed
506
507
508
509
510
    @property
    def states(self):
        ''' Return visible state components of the scene '''
        return (it for it in self.visible_symb if isinstance(it, State))

511

Maxime Perrotin's avatar
Maxime Perrotin committed
512
513
514
515
516
    @property
    def texts(self):
        ''' Return visible text areas components of the scene '''
        return (it for it in self.visible_symb if isinstance(it, TextSymbol))

517

Maxime Perrotin's avatar
Maxime Perrotin committed
518
519
520
521
522
    @property
    def procs(self):
        ''' Return visible procedure declaration components of the scene '''
        return (it for it in self.visible_symb if isinstance(it, Procedure))

523

Maxime Perrotin's avatar
Maxime Perrotin committed
524
525
526
527
528
    @property
    def start(self):
        ''' Return visible start components of the scene '''
        return (it for it in self.visible_symb if isinstance(it, Start))

529

Maxime Perrotin's avatar
Maxime Perrotin committed
530
531
532
    @property
    def floating_labels(self):
        ''' Return visible floating label components of the scene '''
Maxime Perrotin's avatar
Maxime Perrotin committed
533
        return (it for it in self.floating_symb if isinstance(it, Label))
Maxime Perrotin's avatar
Maxime Perrotin committed
534

535

Maxime Perrotin's avatar
Maxime Perrotin committed
536
537
538
539
540
541
    @property
    def returns(self):
        ''' Return visible return components of the scene '''
        return (it for it in self.visible_symb if isinstance(it,
                                                              ProcedureStop))

542

Maxime Perrotin's avatar
Maxime Perrotin committed
543
544
545
    @property
    def composite_states(self):
        ''' Return states that contain a composite part '''
Maxime Perrotin's avatar
Maxime Perrotin committed
546
547
548
        # Update the list first
        for each in self.states:
            if each.is_composite() and \
Maxime Perrotin's avatar
Maxime Perrotin committed
549
550
                  each.nested_scene not in self._composite_states.values():
                self._composite_states[str(each).split()[0].lower()] = \
Maxime Perrotin's avatar
Maxime Perrotin committed
551
                                                            each.nested_scene
Maxime Perrotin's avatar
Maxime Perrotin committed
552
553
        return self._composite_states

554

Maxime Perrotin's avatar
Maxime Perrotin committed
555
556
    @composite_states.setter
    def composite_states(self, value):
557
        ''' Attribute setter '''
Maxime Perrotin's avatar
Maxime Perrotin committed
558
559
        self._composite_states = value

560

Maxime Perrotin's avatar
Maxime Perrotin committed
561
562
563
564
    @property
    def all_nested_scenes(self):
        ''' Return all nested scenes, recursively '''
        for each in self.visible_symb:
565
            if each.nested_scene and isinstance(each.nested_scene, SDL_Scene):
Maxime Perrotin's avatar
Maxime Perrotin committed
566
567
568
                yield each.nested_scene
                for sub in each.nested_scene.all_nested_scenes:
                    yield sub
Maxime Perrotin's avatar
Maxime Perrotin committed
569

570

571
572
573
574
575
576
577
578
579
580
    @property
    def path(self):
        ''' Get the path to the current scene as a list
        e.g. ['BLOCK a', 'PROCESS b', ...]
        '''
        if not self.parent_scene:
            return [self.name[0:-3]]
        return self.parent_scene.path + [self.name[0:-3]]


Maxime Perrotin's avatar
Maxime Perrotin committed
581
582
583
584
585
586
587
588
589
    @property
    def selected_symbols(self):
        ''' Generate the list of selected symbols (excluding grabbers) '''
        for selection in self.selectedItems():
            if isinstance(selection, Symbol):
                yield selection
            elif isinstance(selection, Cornergrabber):
                yield selection.parent

590

Maxime Perrotin's avatar
Maxime Perrotin committed
591
592
593
594
    def quit_scene(self):
        ''' Called in case of scene switch (e.g. UP button) '''
        pass

595

596
    def render_everything(self, ast):
Maxime Perrotin's avatar
Maxime Perrotin committed
597
        ''' Render a process and its children scenes, recursively '''
Maxime Perrotin's avatar
Maxime Perrotin committed
598
        already_created = []
599

Maxime Perrotin's avatar
Maxime Perrotin committed
600
        def recursive_render(content, dest_scene):
601
602
603
604
            ''' Process the rendering in scenes and nested scenes '''
            if isinstance(content, ogAST.Process):
                # XXX - should be set when entering the process
                self.process_name = ast.processName
Maxime Perrotin's avatar
Maxime Perrotin committed
605

606
607
608
609
            try:
                # Render top-level items and their children:
                for each in Renderer.render(content, dest_scene):
                    G_SYMBOLS.add(each)
610
611
                # Refreshing the scene may result in resizing some symbols
                dest_scene.refresh()
612
613
614
615
616
                # Once everything is rendered, adjust position of each
                # symbol to the value from the AST (positions may be
                # slightly altered by the reshaping functions)
                def fix_pos_from_ast(symbol):
                    try:
617
618
                        if(symbol.ast.pos_x is not None
                                and symbol.ast.pos_y is not None):
619
620
621
622
623
                            relpos = symbol.mapFromScene(symbol.ast.pos_x,
                                                         symbol.ast.pos_y)
                            if not symbol.hasParent:
                                symbol.pos_x = symbol.ast.pos_x
                                symbol.pos_y = symbol.ast.pos_y
624
                            else:
625
626
627
628
629
630
631
632
633
                                symbol.position += relpos
                            symbol.update_connections()
                            # Update_position is called here because it
                            # is not possible to be sure that the
                            # positionning stored in the file will be
                            # rendered correctly on the host plaform.
                            # Font rendering may cause slight differences
                            # between Linux and Windows for example.
                            symbol.update_position()
Maxime Perrotin's avatar
Maxime Perrotin committed
634
                        else:
635
636
637
638
639
640
641
642
643
644
645
                            # No CIF coordinates: fix COMMENT position
                            if isinstance(symbol, genericSymbols.Comment):
                                symbol.pos_x = \
                                  symbol.parent.boundingRect().width() + 15
                                symbol.pos_y = 0
                            if not symbol.hasParent:
                                sc_br = dest_scene.itemsBoundingRect()
                                sy_br = symbol.mapRectToScene(
                                            symbol.boundingRect() |
                                            symbol.childrenBoundingRect())
                                symbol.pos_x += (sc_br.width() - sy_br.x())
646
                    except AttributeError as err:
647
648
                        # no AST, ignore (e.g. Connections, Cornergrabbers)
                        pass
649
                        #LOG.debug("[render everything] " + str(err))
650
651
652
653
654
655
656
657
                    else:
                        # Recursively fix pos of sub branches and followers
                        for branch in (elm for elm in symbol.childSymbols()
                                   if isinstance(elm,
                                         genericSymbols.HorizontalSymbol)):
                            fix_pos_from_ast(branch)
                        fix_pos_from_ast(symbol.next_aligned_symbol())
                        fix_pos_from_ast(symbol.comment)
Maxime Perrotin's avatar
Maxime Perrotin committed
658
659
                for each in dest_scene.floating_symb:
                    fix_pos_from_ast(each)
660
            except TypeError:
661
                LOG.error(traceback.format_exc())
Maxime Perrotin's avatar
Maxime Perrotin committed
662
663
664
665

            # Render nested scenes, recursively:
            for each in (item for item in dest_scene.visible_symb
                         if item.nested_scene):
Maxime Perrotin's avatar
Maxime Perrotin committed
666
                LOG.debug(u'Recursive scene: ' + str(each))
Maxime Perrotin's avatar
Maxime Perrotin committed
667
668
669
670
                if isinstance(each.nested_scene, ogAST.CompositeState) \
                        and (not each.nested_scene.statename
                             or each.nested_scene in already_created):
                    # Ignore nested state scenes that already exist
Maxime Perrotin's avatar
Maxime Perrotin committed
671
                    LOG.debug('Subscene "{}" ignored'.format(str(each)))
Maxime Perrotin's avatar
Maxime Perrotin committed
672
                    continue
673
                subscene = \
674
                        self.create_subscene(each.context_name,
675
                                             dest_scene)
676

Maxime Perrotin's avatar
Maxime Perrotin committed
677
                already_created.append(each.nested_scene)
Maxime Perrotin's avatar
Maxime Perrotin committed
678
                subscene.name = str(each)
Maxime Perrotin's avatar
Maxime Perrotin committed
679
                LOG.debug('Created scene: {}'.format(subscene.name))
Maxime Perrotin's avatar
Maxime Perrotin committed
680
681
682
683
684
685
                recursive_render(each.nested_scene.content, subscene)
                each.nested_scene = subscene

            # Make sure all composite states are initially up to date
            # (Needed for the symbol shape to have dashed lines)
            for each in dest_scene.states:
Maxime Perrotin's avatar
Maxime Perrotin committed
686
687
                if str(each).lower() in \
                        dest_scene.composite_states.keys():
Maxime Perrotin's avatar
Maxime Perrotin committed
688
                    each.nested_scene = dest_scene.composite_states[
Maxime Perrotin's avatar
Maxime Perrotin committed
689
                                                         str(each).lower()]
Maxime Perrotin's avatar
Maxime Perrotin committed
690

691
692
693
694
695
            # Readonly user flag
            if dest_scene.context == 'process' and dest_scene.readonly:
                for each in dest_scene.editable_texts:
                    each.setTextInteractionFlags(Qt.NoTextInteraction)

696
        recursive_render(ast, self)
Maxime Perrotin's avatar
Maxime Perrotin committed
697

698

Maxime Perrotin's avatar
Maxime Perrotin committed
699
    def refresh(self):
700
        ''' Scene refresh - make sure it happens only once per cycle '''
Maxime Perrotin's avatar
Maxime Perrotin committed
701
702
        #LOG.debug('scene refresh requested by '
        #          + str(traceback.extract_stack(limit=2)[-2][1:3]))
703
704
705
706
707
        if not self.refresh_requested:
            self.refresh_requested = True
            QTimer.singleShot(0, self.scene_refresh)

    def scene_refresh(self):
Maxime Perrotin's avatar
Maxime Perrotin committed
708
        ''' Refresh the symbols and connections in the scene '''
709
        self.refresh_requested = False
Maxime Perrotin's avatar
Maxime Perrotin committed
710
711
712
713
        #LOG.debug('scene refresh done')
#       for symbol in self.visible_symb:
#           symbol.updateConnectionPointPosition()
#           symbol.updateConnectionPoints()
Maxime Perrotin's avatar
Maxime Perrotin committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
        for symbol in self.editable_texts:
            # EditableText refreshing - design explanation:
            # The first one is tricky: at symbol initialization,
            # the bounding rect of the text is computed from the raw
            # text value, without any formatting. However, it can
            # happen that text (especially when a model is loaded)
            # contains highlighted data (bold), which has the effect
            # of making the width of the text in fact wider than
            # the bounding rect. The set_text_alignment function,
            # that is applying the aligment of the text within its
            # bounding rect, can work only if the text width is fixed.
            # It has to set it according to the bounding rect, which,
            # therefore can be too small, and this has the effect of
            # pushing the exceeding character to the next line.
            # The only way to avoid this is to call setTextWidth
            # with the value -1 before the aligment is computed.
            # This has the effect of re-computing the bounding rect
            # and fixing the width issue.
            symbol.setTextWidth(-1)
Maxime Perrotin's avatar
Maxime Perrotin committed
733
            #symbol.set_textbox_position()
Maxime Perrotin's avatar
Maxime Perrotin committed
734
735
            symbol.try_resize()
            symbol.set_text_alignment()
736
737
            # connect the signal that is emitted when text edit changes word
            symbol.word_under_cursor.connect(self.word_under_cursor.emit)
Maxime Perrotin's avatar
Maxime Perrotin committed
738
739
        for symbol in self.visible_symb:
            symbol.update_connections()
Maxime Perrotin's avatar
Maxime Perrotin committed
740

741

Maxime Perrotin's avatar
Maxime Perrotin committed
742
743
744
745
746
747
748
749
750
751
752
753
    def set_cursor(self, follower):
        ''' Set the cursor shape depending on the selected menu item '''
        for item in self.items():
            try:
                if follower.__name__ not in item.allowed_followers:
                    item.setCursor(Qt.ForbiddenCursor)
                else:
                    item.setCursor(Qt.PointingHandCursor)
            except AttributeError:
                # if there are items not having allowed_followers
                pass

754

Maxime Perrotin's avatar
Maxime Perrotin committed
755
756
757
758
759
760
761
762
    def reset_cursor(self):
        ''' Reset the default cursor of an item '''
        for item in self.items():
            try:
                item.setCursor(item.default_cursor)
            except AttributeError:
                pass

763

Maxime Perrotin's avatar
Maxime Perrotin committed
764
765
766
767
768
769
    def translate_to_origin(self):
        '''
            Translate all items to coordinate system starting at (0,0),
            in order to avoid negative coordinates
        '''
        try:
770
771
            min_x = min(item.scenePos().x() for item in self.visible_symb)
            min_y = min(item.scenePos().y() for item in self.visible_symb)
Maxime Perrotin's avatar
Maxime Perrotin committed
772
773
        except ValueError:
            # No item in the scene
774
            return 0, 0
Maxime Perrotin's avatar
Maxime Perrotin committed
775
776
777
        delta_x = -min_x if min_x < 0 else 0
        delta_y = -min_y if min_y < 0 else 0
        for item in self.floating_symb:
778
779
            item.pos_x += delta_x
            item.pos_y += delta_y
Maxime Perrotin's avatar
Maxime Perrotin committed
780
781
        return delta_x, delta_y

782

Maxime Perrotin's avatar
Maxime Perrotin committed
783
784
785
    def set_selection(self, toolbar):
        ''' When the selection has changed, update menu, etc '''
        toolbar.update_menu(self)
Maxime Perrotin's avatar
Maxime Perrotin committed
786
        for item in self.selected_symbols:
Maxime Perrotin's avatar
Maxime Perrotin committed
787
788
            item.grabber.display()

789
790
791
792
793
794

    def syntax_errors(self, symb):
        ''' Parse a symbol and return a list of syntax errors '''
        return symb.check_syntax('\n'.join(Pr.generate(symb, recursive=False)))


795
    def check_syntax(self, symbol) -> bool:
796
        ''' Check syntax of a symbol and display a pop-up in case of errors '''
797
798
        # return True if syntax errors were found

799
800
        errors = self.syntax_errors(symbol)

Maxime Perrotin's avatar
Maxime Perrotin committed
801
        if not errors:
Maxime Perrotin's avatar
Maxime Perrotin committed
802
            symbol.syntax_error = False
803
            return False
804

Maxime Perrotin's avatar
Maxime Perrotin committed
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
        for view in self.views():
            errs = []
            for error in errors:
                split = error.split()
                if split[0] == 'line' and len(split) > 1:
                    line_col = split[1].split(':')
                    if len(line_col) == 2:
                        # Get line number and column..to locate error
                        # line_nb, col = line_col
                        errs.append(' '.join(split[2:]))
                    else:
                        errs.append(error)
                else:
                    errs.append(error)
            self.clear_focus()
Maxime Perrotin's avatar
Maxime Perrotin committed
820
            msg_box = QMessageBox(view)
Maxime Perrotin's avatar
Maxime Perrotin committed
821
            msg_box.setFont(QFont('UbuntuMono', 10))
Maxime Perrotin's avatar
Maxime Perrotin committed
822
            msg_box.setIcon(QMessageBox.Warning)
Maxime Perrotin's avatar
Maxime Perrotin committed
823
824
825
            msg_box.setWindowTitle('OpenGEODE - Syntax Error')
            msg_box.setInformativeText('\n'.join(errs))
            msg_box.setText("Syntax error!")
Maxime Perrotin's avatar
Maxime Perrotin committed
826
827
            msg_box.setStandardButtons(QMessageBox.Discard)
            msg_box.setDefaultButton(QMessageBox.Discard)
Maxime Perrotin's avatar
Maxime Perrotin committed
828
            msg_box.exec_()
Maxime Perrotin's avatar
Maxime Perrotin committed
829
        # There were syntax errors: force user to fix them
830
831
        # by returning True to the caller (TextInteraction), which
        # will keep focus
Maxime Perrotin's avatar
Maxime Perrotin committed
832
        symbol.syntax_error = True
833
834
        return True
        #symbol.edit_text()
Maxime Perrotin's avatar
Maxime Perrotin committed
835

836

837
    def global_syntax_check(self, ignore=set()):
838
        ''' Parse each visible symbol in the current scene and its children
839
840
841
842
            and check syntax using the parser
            Use a mutable parameter to avoid recursion on already visited
            scenes
        '''
843
        res = True
844
845
        reset = not ignore
        ignore.add(self)
846
        for each in self.visible_symb:
847
848
849
850
851
852
853
854
855
856
857
858
            errors = self.syntax_errors(each)
            if errors:
                res = False
            for err in errors:
                split = err.split()
                if split[0] == 'line' and len(split) > 1:
                    line_col = split[1].split(':')
                    if len(line_col) == 2:
                        # get line and col. line must be decremented because
                        # line 1 is the CIF comment which is not visible
                        line_nb, col = line_col
                        line_nb = int(line_nb) - 1
859
                        split[1] = f'{line_nb}:{col}'
860
                pos = each.scenePos()
861
                split.append (f'in "{str(each)}"')
862
                fmt = [[' '.join(split), [pos.x(), pos.y()], self.path]]
863
                log_errors(self.messages_window, fmt, [], clearfirst=False)
864
865
                for view in self.views():
                    view.find_symbols_and_update_errors()
866
867

        for each in self.all_nested_scenes:
868
869
870
871
872
            if each not in ignore:
                if not each.global_syntax_check():
                    res = False
        if reset:
            ignore.clear()
873
        return res
874

Maxime Perrotin's avatar
Maxime Perrotin committed
875
876
877
878
879

    def update_completion_list(self, symbol):
        ''' When text has changed on a symbol, update the data dictionnary '''
        pr_text = '\n'.join(Pr.generate(symbol,
                                        recursive=False,
880
                                        nextstate=False, cpy=True))
Maxime Perrotin's avatar
Maxime Perrotin committed
881
        symbol.update_completion_list(pr_text=pr_text)
882
        self.context_change.emit()
Maxime Perrotin's avatar
Maxime Perrotin committed
883

884

885
886
887
888
889
    def highlight(self, item):
        ''' Highlight a symbol '''
        if item in self.highlighted:
            return
        bound = item.boundingRect()
890
        center = bound.center().x()
Maxime Perrotin's avatar
Maxime Perrotin committed
891
        gradient = QLinearGradient(center, 0, center, bound.height())
892
893
894
        gradient.setColorAt(0, Qt.cyan)
        gradient.setColorAt(1, Qt.white)
        self.highlighted[item] = item.brush()
Maxime Perrotin's avatar
Maxime Perrotin committed
895
        item.setBrush(QBrush(gradient))
896
897
898
899
900
901

    def clear_highlight(self, item=None, reset=True):
        ''' Remove the highlighting of one item or all items on the scene '''
        if item in self.highlighted:
            self.setBrush(self.highlighted.pop(item))
        if reset:
Maxime Perrotin's avatar
Maxime Perrotin committed
902
            for item, brush in self.highlighted.items():
903
904
905
                item.setBrush(brush)
            self.highlighted = {}

906

Maxime Perrotin's avatar
Maxime Perrotin committed
907
908
909
910
911
    def find_text(self, pattern):
        ''' Return all symbols with matching text '''
        for item in (symbol for symbol in self.items()
                     if isinstance(symbol, EditableText)
                     and symbol.isVisible()):
912
            try:
Maxime Perrotin's avatar
Maxime Perrotin committed
913
                res = re.search(pattern, str(item), flags=re.IGNORECASE)
914
915
916
917
            except re.error:
                # invalid pattern
                raise StopIteration
            if res:
918
                yield item
Maxime Perrotin's avatar
Maxime Perrotin committed
919

920

921
922
923
924
925
    def search(self, pattern, replace_with=None, cmd=None):
        ''' Search and replace function ; get next search result with key n
        cmd is a user string from the vi bar that by default for a replace
        is "s" (substittute string) but that can be a different command,
        e.g. "state" to limit the substitution to State components'''
Maxime Perrotin's avatar
Maxime Perrotin committed
926
        self.clearSelection()
927
        self.clear_highlight()
Maxime Perrotin's avatar
Maxime Perrotin committed
928
929
930
931
932
933
934
935
936
        self.clear_focus()
        if pattern.endswith('\\'):
            # Avoid buggy pattern ending with a single backslash
            pattern += '\\'
        self.search_item = self.find_text(pattern)
        self.search_pattern = pattern
        if replace_with:
            with undoCommands.UndoMacro(self.undo_stack, 'Search and Replace'):
                for item in self.search_item:
937
938
939
940
941
                    if(cmd and cmd != "s" and
                        item.parentItem().__class__.__name__.lower()
                                                               != cmd.lower()):
                        # filter symbols based on the user command
                        continue
Maxime Perrotin's avatar
Maxime Perrotin committed
942
943
                    new_string = re.sub(pattern,
                                        replace_with,
Maxime Perrotin's avatar
Maxime Perrotin committed
944
                                        str(item),
Maxime Perrotin's avatar
Maxime Perrotin committed
945
946
                                        flags=re.IGNORECASE)
                    undo_cmd = undoCommands.ReplaceText(
Maxime Perrotin's avatar
Maxime Perrotin committed
947
                                     item, str(item), new_string)
Maxime Perrotin's avatar
Maxime Perrotin committed
948
                    self.undo_stack.push(undo_cmd)
949
950
                    item.try_resize()
                    item.parentItem().select()
951
                    self.update_completion_list(item.parent)
Maxime Perrotin's avatar
Maxime Perrotin committed
952
953
954
            self.refresh()
        else:
            try:
955
                item = next(self.search_item)
956
                item = item.parentItem()
Maxime Perrotin's avatar
Maxime Perrotin committed
957
                item.select()
958
                self.highlight(item)
Maxime Perrotin's avatar
Maxime Perrotin committed
959
960
961
962
                item.ensureVisible()
            except StopIteration:
                LOG.info('Pattern not found')

963

Maxime Perrotin's avatar
Maxime Perrotin committed
964
965
966
967
    def delete_selected_symbols(self):
        '''
            Remove selected symbols from the scene, with proper re-connections
        '''
968
969
970
        if self.context == 'process' and self.readonly:
            # with readonly flag, forbid item delettion
            return
Maxime Perrotin's avatar
Maxime Perrotin committed
971
        self.undo_stack.beginMacro('Delete items')
Maxime Perrotin's avatar
Maxime Perrotin committed
972
        for item in self.selected_symbols:
Maxime Perrotin's avatar
Maxime Perrotin committed
973
974
975
976
977
978
979
980
981
982
983
984
985
            if not item.scene():
                # Ignore if item has already been deleted
                # (in case of multiple selection)
                continue
            undo_cmd = undoCommands.DeleteSymbol(item)
            self.undo_stack.push(undo_cmd)
            try:
                item.branchEntryPoint.parent.updateConnectionPointPosition()
                item.branchEntryPoint.parent.updateConnectionPoints()
            except AttributeError:
                pass
        self.undo_stack.endMacro()

986

Maxime Perrotin's avatar
Maxime Perrotin committed
987
988
989
990
991
992
993
    def copy_selected_symbols(self):
        '''
            Create a copy of selected symbols to a buffer (in AST form)
            Called when user presses Ctrl-C
        '''
        self.click_coordinates = None
        try:
Maxime Perrotin's avatar
Maxime Perrotin committed
994
            Clipboard.copy(self.selected_symbols)
Maxime Perrotin's avatar
Maxime Perrotin committed
995
996
        except TypeError as error_msg:
            try:
Maxime Perrotin's avatar
Maxime Perrotin committed
997
                self.messages_window.addItem(str(error_msg))
Maxime Perrotin's avatar
Maxime Perrotin committed
998
            except AttributeError:
Maxime Perrotin's avatar
Maxime Perrotin committed
999
                LOG.error(str(error_msg))
Maxime Perrotin's avatar
Maxime Perrotin committed
1000
            raise