# This file is part of Beremiz, a Integrated Development Environment for
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
# Copyright (C) 2013: Edouard TISSERANT and Laurent BESSARD
# See COPYING file for copyrights details.
# This program 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
# of the License, or (at your option) any later version.
# This program 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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):
@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)
self.Controller = controller
self.PreviewLabel = wx.StaticText(self, label=_('Preview:'))
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
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),...}
# Remove reference to project controller
def _init_sizers(self, main_rows, main_growable_row,
left_rows, left_growable_row,
right_rows, right_growable_row):
@param main_rows: Number of rows in main sizer
@param main_growable_row: Row that is growable in main sizer, None if no
@param left_rows: Number of rows in left grid sizer
@param left_growable_row: Row that is growable in left grid sizer, None
@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,
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,
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,
# Create a sizer for right column
self.RightGridSizer = wx.FlexGridSizer(cols=1, hgap=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,
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
May be overridden by inherited classes
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
var.Name: (var.Class, var.Type)
for var in self.Controller.GetEditedElementInterfaceVars(
# Add POU name to variable list if POU is a function
returntype = self.Controller.GetEditedElementInterfaceReturnType(
if returntype is not None:
self.Controller.GetEditedElementName(self.TagName)] = \
# Add POU name if POU is a transition
words = self.TagName.split("::")
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
# 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)
def ShowErrorMessage(self, message):
Show an error message dialog over this dialog
@param message: Error message to display
dialog = wx.MessageDialog(self, message,
Called when dialog OK button is pressed
Need to be overridden by inherited classes to check that dialog values
@param event: wx.Event from OK button
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())
# Return immediately if no graphic element defined
# 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()
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
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)
def OnPaint(self, event):
Called when Preview panel need to be redraw
@param event: wx.PaintEvent