controls/DebugVariablePanel/GraphButton.py
changeset 1199 fc0e7d80494f
parent 1198 8b4e6bd0aa92
child 1200 501cb0bb4c05
equal deleted inserted replaced
1198:8b4e6bd0aa92 1199:fc0e7d80494f
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 from util.BitmapLibrary import GetBitmap
       
    28 
       
    29 #-------------------------------------------------------------------------------
       
    30 #                        Custom button for Graphic Viewer Class
       
    31 #-------------------------------------------------------------------------------
       
    32 
       
    33 """
       
    34 Class that implements a custom button for graphic Viewer
       
    35 """
       
    36 
       
    37 class GraphButton():
       
    38     
       
    39     def __init__(self, x, y, bitmap, callback):
       
    40         """
       
    41         Constructor
       
    42         @param x: X coordinate of Button in Graphic Viewer
       
    43         @param y: Y coordinate of Button in Graphic Viewer
       
    44         @param bitmap: Name of bitmap to use for button
       
    45         @param callback: Reference to function to call when button is pressed
       
    46         """
       
    47         # Save button position
       
    48         self.SetPosition(x, y)
       
    49         # Get wx.Bitmap object corresponding to bitmap
       
    50         self.Bitmap = GetBitmap(bitmap)
       
    51         
       
    52         # By default button is shown and enabled
       
    53         self.Shown = True
       
    54         self.Enabled = True
       
    55         
       
    56         # Save reference to callback function
       
    57         self.Callback = callback
       
    58     
       
    59     def __del__(self):
       
    60         """
       
    61         Destructor
       
    62         """
       
    63         # Remove reference to callback function
       
    64         self.callback = None
       
    65     
       
    66     def GetSize(self):
       
    67         """
       
    68         Return size of button
       
    69         @return: wx.Size object containing button size
       
    70         """
       
    71         # Button size is size of bitmap
       
    72         return self.Bitmap.GetSize()
       
    73     
       
    74     def SetPosition(self, x, y):
       
    75         """
       
    76         Set button position
       
    77         @param x: X coordinate of Button in Graphic Viewer
       
    78         @param y: Y coordinate of Button in Graphic Viewer
       
    79         """
       
    80         self.Position = wx.Point(x, y)
       
    81     
       
    82     def Show(self, show=True):
       
    83         """
       
    84         Mark if button to be displayed in Graphic Viewer
       
    85         @param show: True if button to be displayed in Graphic Viewer
       
    86         (default True)
       
    87         """
       
    88         self.Shown = show
       
    89         
       
    90     def Hide(self):
       
    91         """
       
    92         Hide button from Graphic Viewer
       
    93         """
       
    94         self.Show(False)
       
    95     
       
    96     def IsShown(self):
       
    97         """
       
    98         Return if button is displayed in Graphic Viewer
       
    99         @return: True if button is displayed in Graphic Viewer
       
   100         """
       
   101         return self.Shown
       
   102     
       
   103     def Enable(self, enable=True):
       
   104         """
       
   105         Mark if button is active in Graphic Viewer
       
   106         @param enable: True if button is active in Graphic Viewer
       
   107         (default True)
       
   108         """
       
   109         self.Enabled = True
       
   110     
       
   111     def Disable(self):
       
   112         """
       
   113         Deactivate button in Graphic Viewer
       
   114         """
       
   115         self.Enabled = False
       
   116     
       
   117     def IsEnabled(self):
       
   118         """
       
   119         Return if button is active in Graphic Viewer
       
   120         @return: True if button is active in Graphic Viewer
       
   121         """
       
   122         return self.Enabled
       
   123     
       
   124     def HitTest(self, x, y):
       
   125         """
       
   126         Test if point is inside button
       
   127         @param x: X coordinate of point
       
   128         @param y: Y coordinate of point
       
   129         @return: True if button is active and displayed and point is inside
       
   130         button 
       
   131         """
       
   132         # Return immediately if button is hidden or inactive
       
   133         if not (self.IsShown() and self.IsEnabled()):
       
   134             return False
       
   135         
       
   136         # Test if point is inside button
       
   137         w, h = self.Bitmap.GetSize()
       
   138         rect = wx.Rect(self.Position.x, self.Position.y, w, h)
       
   139         return rect.InsideXY(x, y)
       
   140     
       
   141     def ProcessCallback(self):
       
   142         """
       
   143         Call callback function if defined
       
   144         """
       
   145         if self.Callback is not None:
       
   146             wx.CallAfter(self.Callback)
       
   147     
       
   148     def Draw(self, dc):
       
   149         """
       
   150         Draw button in Graphic Viewer
       
   151         @param dc: wx.DC object corresponding to Graphic Viewer device context
       
   152         """
       
   153         # Only draw button if button is active and displayed
       
   154         if self.Shown and self.Enabled:
       
   155             dc.DrawBitmap(self.Bitmap, self.Position.x, self.Position.y, True)