graphics/RubberBand.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 17 May 2023 23:59:45 +0200
changeset 3806 b57a38ce3f34
parent 3792 365f866ee120
permissions -rw-r--r--
IDE: Fix py_ext build with python3.10
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
     9
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    14
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    19
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1273
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    25
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 3746
diff changeset
    26
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    27
import wx
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    28
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    29
from graphics.GraphicCommons import GetScaledEventPosition
1169
53e4a2b775a7 Move CustomToolTip from GraphicCommons to controls
Laurent Bessard
parents: 1166
diff changeset
    30
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
    31
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
    32
# -------------------------------------------------------------------------------
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    33
#                               Viewer RubberBand
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
    34
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    35
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    36
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    37
class RubberBand(object):
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    38
    """
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    39
    Class that implements a rubberband for graphic Viewers
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    40
    """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    41
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    42
    def __init__(self, viewer):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    43
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    44
        Constructor
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    45
        @param viewer: Viewer on which rubberband must be drawn
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    46
        """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    47
        self.Viewer = viewer
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    48
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    49
        # wx.Panel on which rubberband will be drawn
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    50
        self.DrawingSurface = viewer.Editor
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    51
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    52
        self.Reset()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    53
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
    def Reset(self):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    55
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    56
        Initialize internal attributes of rubberband
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    57
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    58
        self.StartPoint = None
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    59
        self.CurrentBBox = None
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    60
        self.LastBBox = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    61
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
    def IsShown(self):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    63
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    64
        Indicate if rubberband is drawn on viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    65
        @return: True if rubberband is drawn
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    66
        """
1743
c3c3d1318130 clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    67
        return self.CurrentBBox is not None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    68
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    69
    def GetCurrentExtent(self):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    70
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    71
        Return the rubberband bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    72
        @return: Rubberband bounding box (wx.Rect object)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    73
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    74
        # In case of rubberband not shown, return the last rubberband
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    75
        # bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    76
        if self.IsShown():
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    77
            return self.CurrentBBox
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    78
        return self.LastBBox
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    79
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
    def OnLeftDown(self, event, dc, scaling):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    81
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    82
        Called when left mouse is pressed on Viewer. Starts to edit a new
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    83
        rubberband bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    84
        @param event: Mouse event
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    85
        @param dc: Device Context of Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    86
        @param scaling: PLCOpen scaling applied on Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    87
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    88
        # Save the point where mouse was pressed in Viewer unit, position may
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    89
        # be modified by scroll and zoom applied on viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    90
        self.StartPoint = GetScaledEventPosition(event, dc, scaling)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    91
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    92
        # Initialize rubberband bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    93
        self.CurrentBBox = wx.Rect(self.StartPoint.x, self.StartPoint.y, 0, 0)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    94
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    95
        # Change viewer mouse cursor to reflect a rubberband bounding box is
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
    96
        # edited
3303
0ffb41625592 Preliminary support for WxPython 4.1.0. Needs more testing. Grid selection/focus seems broken, and probably many other bugs hidden in dialogs and editors.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1881
diff changeset
    97
        self.DrawingSurface.SetCursor(wx.Cursor(wx.CURSOR_CROSS))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    98
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
        self.Redraw()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   100
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
    def OnMotion(self, event, dc, scaling):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   102
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   103
        Called when mouse is dragging over Viewer. Update the current edited
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   104
        rubberband bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   105
        @param event: Mouse event
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   106
        @param dc: Device Context of Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   107
        @param scaling: PLCOpen scaling applied on Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   108
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   109
        # Get mouse position in Viewer unit, position may be modified by scroll
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   110
        # and zoom applied on viewer
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   111
        pos = GetScaledEventPosition(event, dc, scaling)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   112
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   113
        # Save the last bounding box drawn for erasing it later
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   114
        self.LastBBox = wx.Rect(0, 0, 0, 0)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   115
        self.LastBBox.Union(self.CurrentBBox)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   116
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   117
        # Calculate new position and size of the box
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   118
        self.CurrentBBox.x = min(pos.x, self.StartPoint.x)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   119
        self.CurrentBBox.y = min(pos.y, self.StartPoint.y)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   120
        self.CurrentBBox.width = abs(pos.x - self.StartPoint.x) + 1
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   121
        self.CurrentBBox.height = abs(pos.y - self.StartPoint.y) + 1
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   122
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
        self.Redraw()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   124
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
    def OnLeftUp(self, event, dc, scaling):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   126
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   127
        Called when mouse is release from Viewer. Erase the current edited
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   128
        rubberband bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   129
        @param event: Mouse event
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   130
        @param dc: Device Context of Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   131
        @param scaling: PLCOpen scaling applied on Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   132
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   133
        # Change viewer mouse cursor to default
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   134
        self.DrawingSurface.SetCursor(wx.NullCursor)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   135
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   136
        # Save the last edited bounding box
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   137
        self.LastBBox = self.CurrentBBox
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   138
        self.CurrentBBox = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   139
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
        self.Redraw()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   141
3746
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   142
    def SetRubberBandPen(self, dc):
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   143
        # Set DC drawing style
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   144
        dc.SetPen(wx.Pen(wx.WHITE, style=wx.DOT))
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   145
        dc.SetLogicalFunction(wx.XOR)
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   146
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   147
    def DrawBoundingBoxes(self, bboxes, dc=None):
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   148
        """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   149
        Draw a list of bounding box on Viewer in the order given using XOR
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   150
        logical function
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   151
        @param bboxes: List of bounding boxes to draw on viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   152
        @param dc: Device Context of Viewer (default None)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   153
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   154
        # Get viewer Device Context if not given
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
        if dc is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
            dc = self.Viewer.GetLogicalDC()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   157
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   158
        # Save current viewer scale factors before resetting them in order to
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   159
        # avoid rubberband pen to be scaled
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
        scalex, scaley = dc.GetUserScale()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
        dc.SetUserScale(1, 1)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   162
3746
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   163
        self.SetRubberBandPen(dc)
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   164
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   165
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   166
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   167
        # Draw the bounding boxes using viewer scale factor
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   168
        for bbox in bboxes:
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   169
            if bbox is not None:
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   170
                dc.DrawRectangle(
3792
365f866ee120 IDE: python-3.10: wxPython calls don't accept float anymore when int is expected
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   171
                    round(bbox.x * scalex), round(bbox.y * scaley),
365f866ee120 IDE: python-3.10: wxPython calls don't accept float anymore when int is expected
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3750
diff changeset
   172
                    round(bbox.width * scalex), round(bbox.height * scaley))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   173
1273
921858d68a13 Fix refresh bug when scrolling Viewer while dragging graphic element or rubberband
Laurent Bessard
parents: 1173
diff changeset
   174
        dc.SetLogicalFunction(wx.COPY)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   175
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   176
        # Restore Viewer scale factor
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
        dc.SetUserScale(scalex, scaley)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   178
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
   179
    def Redraw(self, dc=None):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   180
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   181
        Redraw rubberband on Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   182
        @param dc: Device Context of Viewer (default None)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   183
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   184
        # Erase last bbox and draw current bbox
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   185
        self.DrawBoundingBoxes([self.LastBBox, self.CurrentBBox], dc)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   186
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
   187
    def Erase(self, dc=None):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   188
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   189
        Erase rubberband from Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   190
        @param dc: Device Context of Viewer (default None)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   191
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   192
        # Erase last bbox
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   193
        self.DrawBoundingBoxes([self.LastBBox], dc)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
   195
    def Draw(self, dc=None):
1173
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   196
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   197
        Draw rubberband on Viewer
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   198
        @param dc: Device Context of Viewer (default None)
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   199
        """
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   200
        # Erase last bbox and draw current bbox
ad09b4a755ce Move RubberBand from GraphicCommons to individual file
Laurent Bessard
parents: 1170
diff changeset
   201
        self.DrawBoundingBoxes([self.CurrentBBox], dc)
3306
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   202
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   203
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   204
def PatchRubberBandForGTK3():
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   205
    """
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   206
    GTK3 implementation of DC doesn't support SetLogicalFuntion(XOR)
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   207
    Then Rubberband can't be erased by just redrawing it on the same place
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   208
    So this is a complete refresh instead, eating a lot of CPU.
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   209
    """
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   210
    def Redraw(self, dc=None):
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   211
        self.Viewer.Refresh()
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   212
        self.Draw()
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   213
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   214
    RubberBand.Redraw = Redraw
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   215
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   216
    def Erase(self, dc=None):
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   217
        self.Viewer.Refresh()
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   218
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   219
    RubberBand.Erase = Erase
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   220
3746
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   221
    def SetRubberBandPen(self, dc):
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   222
        dc.SetPen(wx.Pen(wx.BLACK, style=wx.DOT))
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   223
41be039fbb8c IDE: fix again ruberband with gtk3.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3306
diff changeset
   224
    RubberBand.SetRubberBandPen = SetRubberBandPen
3306
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   225
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   226
if "gtk3" in wx.PlatformInfo:
841fb2ee1213 Workaround RubberBand drawing problem on GTK3
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3303
diff changeset
   227
    PatchRubberBandForGTK3()