dialogs/BlockPreviewDialog.py
author Edouard Tisserant
Wed, 25 Feb 2015 13:59:01 +0100
changeset 1451 94e620cbd9de
parent 1347 533741e5075c
child 1571 486f94a8032c
permissions -rw-r--r--
Added Ronan Bignaux (genesis) patch to use wxversion. Fixed side effects with sys.path. Other cosmetic fixes about path included.
#!/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) 2013: 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 plcopen.structures import TestIdentifier, IEC_KEYWORDS
from graphics.GraphicCommons import FREEDRAWING_MODE

#-------------------------------------------------------------------------------
#                    Dialog with preview for graphic block
#-------------------------------------------------------------------------------

"""
Class that implements a generic dialog containing a preview panel for displaying
graphic created by dialog
"""

class BlockPreviewDialog(wx.Dialog):
    
    def __init__(self, parent, controller, tagname, size, title):
        """
        Constructor
        @param parent: Parent wx.Window of dialog for modal
        @param controller: Reference to project controller
        @param tagname: Tagname of project POU edited
        @param size: wx.Size object containing size of dialog
        @param title: Title of dialog frame
        """
        wx.Dialog.__init__(self, parent, size=size, title=title)
        
        # Save reference to
        self.Controller = controller
        self.TagName = tagname
        
        # Label for preview
        self.PreviewLabel = wx.StaticText(self, label=_('Preview:'))
        
        # Create Preview panel
        self.Preview = wx.Panel(self, style=wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wx.WHITE)
        
        # Add function to preview panel so that it answers to graphic elements
        # like Viewer
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
        setattr(self.Preview, "GetScaling", lambda:None)
        setattr(self.Preview, "GetBlockType", controller.GetBlockType)
        setattr(self.Preview, "IsOfType", controller.IsOfType)
        
        # Bind paint event on Preview panel
        self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
        
        # Add default dialog buttons sizer
        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
        self.Bind(wx.EVT_BUTTON, self.OnOK, 
                  self.ButtonSizer.GetAffirmativeButton())
        
        self.Element = None            # Graphic element to display in preview
        self.MinElementSize = None     # Graphic element minimal size
        
        # Variable containing the graphic element name when dialog is opened
        self.DefaultElementName = None
        
        # List of variables defined in POU {var_name: (var_class, var_type),...}
        self.VariableList = {}
        
    def __del__(self):
        """
        Destructor
        """
        # Remove reference to project controller
        self.Controller = None
    
    def _init_sizers(self, main_rows, main_growable_row,
                            left_rows, left_growable_row,
                            right_rows, right_growable_row):
        """
        Initialize common sizers
        @param main_rows: Number of rows in main sizer
        @param main_growable_row: Row that is growable in main sizer, None if no
        row is growable
        @param left_rows: Number of rows in left grid sizer
        @param left_growable_row: Row that is growable in left grid sizer, None
        if no row is growable
        @param right_rows: Number of rows in right grid sizer
        @param right_growable_row: Row that is growable in right grid sizer,
        None if no row is growable
        """
        # Create dialog main sizer
        self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, 
                                          rows=main_rows, vgap=10)
        self.MainSizer.AddGrowableCol(0)
        if main_growable_row is not None:
            self.MainSizer.AddGrowableRow(main_growable_row)
        
        # Create a sizer for dividing parameters in two columns
        column_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.MainSizer.AddSizer(column_sizer, border=20, 
              flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
        
        # Create a sizer for left column
        self.LeftGridSizer = wx.FlexGridSizer(cols=1, hgap=0, 
                                              rows=left_rows, vgap=5)
        self.LeftGridSizer.AddGrowableCol(0)
        if left_growable_row is not None:
            self.LeftGridSizer.AddGrowableRow(left_growable_row)
        column_sizer.AddSizer(self.LeftGridSizer, 1, border=5, 
              flag=wx.GROW|wx.RIGHT)
        
        # Create a sizer for right column
        self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=0, 
                                               rows=right_rows, vgap=0)
        self.RightGridSizer.AddGrowableCol(0)
        if right_growable_row is not None:
            self.RightGridSizer.AddGrowableRow(right_growable_row)
        column_sizer.AddSizer(self.RightGridSizer, 1, border=5, 
              flag=wx.GROW|wx.LEFT)
        
        self.SetSizer(self.MainSizer)
    
    def SetMinElementSize(self, size):
        """
        Define minimal graphic element size
        @param size: Tuple containing minimal size (width, height)
        """
        self.MinElementSize = size
    
    def GetMinElementSize(self):
        """
        Get minimal graphic element size
        @return: Tuple containing minimal size (width, height) or None if no
        element defined
        May be overridden by inherited classes
        """
        if self.Element is None:
            return None
        
        return self.Element.GetMinSize()
    
    def SetPreviewFont(self, font):
        """
        Set font of Preview panel
        @param font: wx.Font object containing font style
        """
        self.Preview.SetFont(font)
    
    def RefreshVariableList(self):
        """
        Extract list of variables defined in POU
        """
        # Get list of variables defined in POU
        self.VariableList = {
            var.Name: (var.Class, var.Type)
            for var in self.Controller.GetEditedElementInterfaceVars(
                                                        self.TagName)
            if var.Edit}
        
        # Add POU name to variable list if POU is a function 
        returntype = self.Controller.GetEditedElementInterfaceReturnType(
                                                            self.TagName)
        if returntype is not None:
            self.VariableList[
                self.Controller.GetEditedElementName(self.TagName)] = \
                 ("Output", returntype)
        
        # Add POU name if POU is a transition
        words = self.TagName.split("::")
        if words[0] == "T":
            self.VariableList[words[2]] = ("Output", "BOOL")
    
    def TestElementName(self, element_name):
        """
        Test displayed graphic element name
        @param element_name: Graphic element name
        """
        # Variable containing error message format
        message_format = None
        # Get graphic element name in upper case
        uppercase_element_name = element_name.upper()
        
        # Test if graphic element name is a valid identifier
        if not TestIdentifier(element_name):
            message_format = _("\"%s\" is not a valid identifier!")
        
        # Test that graphic element name isn't a keyword
        elif uppercase_element_name in IEC_KEYWORDS:
            message_format = _("\"%s\" is a keyword. It can't be used!")
        
        # Test that graphic element name isn't a POU name
        elif uppercase_element_name in self.Controller.GetProjectPouNames():
            message_format = _("\"%s\" pou already exists!")
        
        # Test that graphic element name isn't already used in POU by a variable
        # or another graphic element
        elif ((self.DefaultElementName is None or 
               self.DefaultElementName.upper() != uppercase_element_name) and 
              uppercase_element_name in self.Controller.\
                    GetEditedElementVariables(self.TagName)):
            message_format = _("\"%s\" element for this pou already exists!")
        
        # If an error have been identify, show error message dialog
        if message_format is not None:
            self.ShowErrorMessage(message_format % element_name)
            # Test failed
            return False
        
        # Test succeed
        return True
    
    def ShowErrorMessage(self, message):
        """
        Show an error message dialog over this dialog
        @param message: Error message to display
        """
        dialog = wx.MessageDialog(self, message, 
                                  _("Error"), 
                                  wx.OK|wx.ICON_ERROR)
        dialog.ShowModal()
        dialog.Destroy()
    
    def OnOK(self, event):
        """
        Called when dialog OK button is pressed
        Need to be overridden by inherited classes to check that dialog values
        are valid
        @param event: wx.Event from OK button
        """
        # Close dialog
        self.EndModal(wx.ID_OK)
    
    def RefreshPreview(self):
        """
        Refresh preview panel of graphic element
        May be overridden by inherited classes
        """
        # Init preview panel paint device context
        dc = wx.ClientDC(self.Preview)
        dc.SetFont(self.Preview.GetFont())
        dc.Clear()
        
        # Return immediately if no graphic element defined
        if self.Element is None:
            return
        
        # Calculate block size according to graphic element min size due to its
        # parameters and graphic element min size defined
        min_width, min_height = self.GetMinElementSize()
        width = max(self.MinElementSize[0], min_width)
        height = max(self.MinElementSize[1], min_height)
        self.Element.SetSize(width, height)
        
        # Get element position and bounding box to center in preview
        posx, posy = self.Element.GetPosition()
        bbox = self.Element.GetBoundingBox()
        
        # Get Preview panel size
        client_size = self.Preview.GetClientSize()
        
        # If graphic element is too big to be displayed in preview panel,
        # calculate preview panel scale so that graphic element fit inside
        scale = (max(float(bbox.width) / client_size.width, 
                     float(bbox.height) / client_size.height) * 1.1
                 if bbox.width * 1.1 > client_size.width or 
                    bbox.height * 1.1 > client_size.height
                 else 1.0)
        dc.SetUserScale(1.0 / scale, 1.0 / scale)
        
        # Center graphic element in preview panel
        x = int(client_size.width * scale - bbox.width) / 2 + posx - bbox.x
        y = int(client_size.height * scale - bbox.height) / 2 + posy - bbox.y
        self.Element.SetPosition(x, y)
        
        # Draw graphic element
        self.Element.Draw(dc)
    
    def OnPaint(self, event):
        """
        Called when Preview panel need to be redraw
        @param event: wx.PaintEvent
        """
        self.RefreshPreview()
        event.Skip()