Move GraphButton from DebugVariableGraphicPanel to separate file
authorLaurent Bessard
Thu, 30 May 2013 13:23:21 +0200
changeset 1199 fc0e7d80494f
parent 1198 8b4e6bd0aa92
child 1200 501cb0bb4c05
Move GraphButton from DebugVariableGraphicPanel to separate file
controls/DebugVariablePanel/DebugVariableGraphicPanel.py
controls/DebugVariablePanel/DebugVariableTablePanel.py
controls/DebugVariablePanel/GraphButton.py
--- a/controls/DebugVariablePanel/DebugVariableGraphicPanel.py	Thu May 30 11:52:42 2013 +0200
+++ b/controls/DebugVariablePanel/DebugVariableGraphicPanel.py	Thu May 30 13:23:21 2013 +0200
@@ -41,10 +41,12 @@
 cursor_color = '#800080'
 
 from editors.DebugViewer import DebugViewer, REFRESH_PERIOD
-from controls.DebugVariablePanel.DebugVariableItem import DebugVariableItem
 from dialogs.ForceVariableDialog import ForceVariableDialog
 from util.BitmapLibrary import GetBitmap
 
+from DebugVariableItem import DebugVariableItem
+from GraphButton import GraphButton
+
 class DebugVariableDropTarget(wx.TextDropTarget):
     
     def __init__(self, parent, control=None):
@@ -195,58 +197,6 @@
         range = 1.0
     return data, center - range * 0.55, center + range * 0.55
 
-class GraphButton():
-    
-    def __init__(self, x, y, bitmap, callback):
-        self.Position = wx.Point(x, y)
-        self.Bitmap = bitmap
-        self.Shown = True
-        self.Enabled = True
-        self.Callback = callback
-    
-    def __del__(self):
-        self.callback = None
-    
-    def GetSize(self):
-        return self.Bitmap.GetSize()
-    
-    def SetPosition(self, x, y):
-        self.Position = wx.Point(x, y)
-    
-    def Show(self):
-        self.Shown = True
-        
-    def Hide(self):
-        self.Shown = False
-    
-    def IsShown(self):
-        return self.Shown
-    
-    def Enable(self):
-        self.Enabled = True
-    
-    def Disable(self):
-        self.Enabled = False
-    
-    def IsEnabled(self):
-        return self.Enabled
-    
-    def HitTest(self, x, y):
-        if self.Shown and self.Enabled:
-            w, h = self.Bitmap.GetSize()
-            rect = wx.Rect(self.Position.x, self.Position.y, w, h)
-            if rect.InsideXY(x, y):
-                return True
-        return False
-    
-    def ProcessCallback(self):
-        if self.Callback is not None:
-            wx.CallAfter(self.Callback)
-            
-    def Draw(self, dc):
-        if self.Shown and self.Enabled:
-            dc.DrawBitmap(self.Bitmap, self.Position.x, self.Position.y, True)
-
 class DebugVariableViewer:
     
     def __init__(self, window, items=[]):
@@ -350,9 +300,10 @@
                 buttons = self.Buttons[:]
                 buttons.reverse()
                 for button in buttons:
-                    w, h = button.GetSize()
-                    button.SetPosition(width - 5 - w - offset, 5)
-                    offset += w + 2
+                    if button.IsEnabled():
+                        w, h = button.GetSize()
+                        button.SetPosition(width - 5 - w - offset, 5)
+                        offset += w + 2
                 self.ParentWindow.ForceRefresh()
     
     def DrawCommonElements(self, dc, buttons=None):
@@ -473,12 +424,10 @@
         
         self.SetMinSize(wx.Size(0, 25))
         
-        self.Buttons.append(
-            GraphButton(0, 0, GetBitmap("force"), self.OnForceButton))
-        self.Buttons.append(
-            GraphButton(0, 0, GetBitmap("release"), self.OnReleaseButton))
-        self.Buttons.append(
-            GraphButton(0, 0, GetBitmap("delete_graph"), self.OnCloseButton))
+        for bitmap, callback in [("force", self.OnForceButton),
+                                 ("release", self.OnReleaseButton),
+                                 ("delete_graph", self.OnCloseButton)]:
+            self.Buttons.append(GraphButton(0, 0, bitmap, callback))
         
         self.ShowButtons(False)
         
@@ -572,11 +521,10 @@
         
         for size, bitmap in zip([SIZE_MINI, SIZE_MIDDLE, SIZE_MAXI],
                                 ["minimize_graph", "middle_graph", "maximize_graph"]):
-            self.Buttons.append(GraphButton(0, 0, GetBitmap(bitmap), self.GetOnChangeSizeButton(size)))
-        self.Buttons.append(
-            GraphButton(0, 0, GetBitmap("export_graph_mini"), self.OnExportGraphButton))
-        self.Buttons.append(
-            GraphButton(0, 0, GetBitmap("delete_graph"), self.OnCloseButton))
+            self.Buttons.append(GraphButton(0, 0, bitmap, self.GetOnChangeSizeButton(size)))
+        for bitmap, callback in [("export_graph_mini", self.OnExportGraphButton),
+                                 ("delete_graph", self.OnCloseButton)]:
+            self.Buttons.append(GraphButton(0, 0, bitmap, callback))
         
         self.ResetGraphics()
         self.RefreshLabelsPosition(canvas_size.height)
@@ -629,13 +577,11 @@
             
             if self.ContextualButtonsItem.IsForced():
                 self.ContextualButtons.append(
-                    GraphButton(0, 0, GetBitmap("release"), self.OnReleaseButton))
-            self.ContextualButtons.append(
-                GraphButton(0, 0, GetBitmap("force"), self.OnForceButton))
-            self.ContextualButtons.append(
-                GraphButton(0, 0, GetBitmap("export_graph_mini"), self.OnExportItemGraphButton))
-            self.ContextualButtons.append(
-                GraphButton(0, 0, GetBitmap("delete_graph"), self.OnRemoveItemButton))
+                    GraphButton(0, 0, "release", self.OnReleaseButton))
+            for bitmap, callback in [("force", self.OnForceButton),
+                                     ("export_graph_mini", self.OnExportItemGraphButton),
+                                     ("delete_graph", self.OnRemoveItemButton)]:
+                self.ContextualButtons.append(GraphButton(0, 0, bitmap, callback))
             
             offset = 0
             buttons = self.ContextualButtons[:]
--- a/controls/DebugVariablePanel/DebugVariableTablePanel.py	Thu May 30 11:52:42 2013 +0200
+++ b/controls/DebugVariablePanel/DebugVariableTablePanel.py	Thu May 30 13:23:21 2013 +0200
@@ -29,10 +29,11 @@
 
 from editors.DebugViewer import DebugViewer
 from controls import CustomGrid, CustomTable
-from controls.DebugVariablePanel.DebugVariableItem import DebugVariableItem
 from dialogs.ForceVariableDialog import ForceVariableDialog
 from util.BitmapLibrary import GetBitmap
 
+from DebugVariableItem import DebugVariableItem
+
 def GetDebugVariablesTableColnames():
     _ = lambda x : x
     return [_("Variable"), _("Value")]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/controls/DebugVariablePanel/GraphButton.py	Thu May 30 13:23:21 2013 +0200
@@ -0,0 +1,155 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
+#based on the plcopen standard. 
+#
+#Copyright (C) 2012: Edouard TISSERANT and Laurent BESSARD
+#
+#See COPYING file for copyrights details.
+#
+#This library is free software; you can redistribute it and/or
+#modify it under the terms of the GNU General Public
+#License as published by the Free Software Foundation; either
+#version 2.1 of the License, or (at your option) any later version.
+#
+#This library is distributed in the hope that it will be useful,
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#General Public License for more details.
+#
+#You should have received a copy of the GNU General Public
+#License along with this library; if not, write to the Free Software
+#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import wx
+
+from util.BitmapLibrary import GetBitmap
+
+#-------------------------------------------------------------------------------
+#                        Custom button for Graphic Viewer Class
+#-------------------------------------------------------------------------------
+
+"""
+Class that implements a custom button for graphic Viewer
+"""
+
+class GraphButton():
+    
+    def __init__(self, x, y, bitmap, callback):
+        """
+        Constructor
+        @param x: X coordinate of Button in Graphic Viewer
+        @param y: Y coordinate of Button in Graphic Viewer
+        @param bitmap: Name of bitmap to use for button
+        @param callback: Reference to function to call when button is pressed
+        """
+        # Save button position
+        self.SetPosition(x, y)
+        # Get wx.Bitmap object corresponding to bitmap
+        self.Bitmap = GetBitmap(bitmap)
+        
+        # By default button is shown and enabled
+        self.Shown = True
+        self.Enabled = True
+        
+        # Save reference to callback function
+        self.Callback = callback
+    
+    def __del__(self):
+        """
+        Destructor
+        """
+        # Remove reference to callback function
+        self.callback = None
+    
+    def GetSize(self):
+        """
+        Return size of button
+        @return: wx.Size object containing button size
+        """
+        # Button size is size of bitmap
+        return self.Bitmap.GetSize()
+    
+    def SetPosition(self, x, y):
+        """
+        Set button position
+        @param x: X coordinate of Button in Graphic Viewer
+        @param y: Y coordinate of Button in Graphic Viewer
+        """
+        self.Position = wx.Point(x, y)
+    
+    def Show(self, show=True):
+        """
+        Mark if button to be displayed in Graphic Viewer
+        @param show: True if button to be displayed in Graphic Viewer
+        (default True)
+        """
+        self.Shown = show
+        
+    def Hide(self):
+        """
+        Hide button from Graphic Viewer
+        """
+        self.Show(False)
+    
+    def IsShown(self):
+        """
+        Return if button is displayed in Graphic Viewer
+        @return: True if button is displayed in Graphic Viewer
+        """
+        return self.Shown
+    
+    def Enable(self, enable=True):
+        """
+        Mark if button is active in Graphic Viewer
+        @param enable: True if button is active in Graphic Viewer
+        (default True)
+        """
+        self.Enabled = True
+    
+    def Disable(self):
+        """
+        Deactivate button in Graphic Viewer
+        """
+        self.Enabled = False
+    
+    def IsEnabled(self):
+        """
+        Return if button is active in Graphic Viewer
+        @return: True if button is active in Graphic Viewer
+        """
+        return self.Enabled
+    
+    def HitTest(self, x, y):
+        """
+        Test if point is inside button
+        @param x: X coordinate of point
+        @param y: Y coordinate of point
+        @return: True if button is active and displayed and point is inside
+        button 
+        """
+        # Return immediately if button is hidden or inactive
+        if not (self.IsShown() and self.IsEnabled()):
+            return False
+        
+        # Test if point is inside button
+        w, h = self.Bitmap.GetSize()
+        rect = wx.Rect(self.Position.x, self.Position.y, w, h)
+        return rect.InsideXY(x, y)
+    
+    def ProcessCallback(self):
+        """
+        Call callback function if defined
+        """
+        if self.Callback is not None:
+            wx.CallAfter(self.Callback)
+    
+    def Draw(self, dc):
+        """
+        Draw button in Graphic Viewer
+        @param dc: wx.DC object corresponding to Graphic Viewer device context
+        """
+        # Only draw button if button is active and displayed
+        if self.Shown and self.Enabled:
+            dc.DrawBitmap(self.Bitmap, self.Position.x, self.Position.y, True)