dialogs/BlockPreviewDialog.py
author Laurent Bessard
Mon, 10 Jun 2013 09:54:24 +0200
changeset 1238 24577755485d
parent 1237 0c8b8ef9559b
child 1241 368f8516706c
permissions -rw-r--r--
Fixed bug with InstanceChoice values in PouInstanceVariablesPanel
1236
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     3
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     5
#based on the plcopen standard. 
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     6
#
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     7
#Copyright (C) 2013: Edouard TISSERANT and Laurent BESSARD
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     8
#
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    10
#
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    15
#
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    19
#General Public License for more details.
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    20
#
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    24
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    25
import wx
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    26
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    27
from plcopen.structures import TestIdentifier, IEC_KEYWORDS
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    28
from graphics.GraphicCommons import FREEDRAWING_MODE
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    29
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    30
#-------------------------------------------------------------------------------
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    31
#                    Dialog with preview for graphic block
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    32
#-------------------------------------------------------------------------------
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    33
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    34
"""
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    35
Class that implements a generic dialog containing a preview panel for displaying
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    36
graphic created by dialog
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    37
"""
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    38
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    39
class BlockPreviewDialog(wx.Dialog):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    40
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    41
    def __init__(self, parent, controller, tagname, size, title):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    42
        wx.Dialog.__init__(self, parent, size=size, title=title)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    43
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    44
        self.Controller = controller
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    45
        self.TagName = tagname
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    46
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    47
        self.PreviewLabel = wx.StaticText(self, label=_('Preview:'))
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    48
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    49
        self.Preview = wx.Panel(self, style=wx.SIMPLE_BORDER)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    50
        self.Preview.SetBackgroundColour(wx.WHITE)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    51
        setattr(self.Preview, "GetDrawingMode", lambda:FREEDRAWING_MODE)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    52
        setattr(self.Preview, "GetScaling", lambda:None)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    53
        setattr(self.Preview, "GetBlockType", controller.GetBlockType)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    54
        setattr(self.Preview, "IsOfType", controller.IsOfType)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    55
        self.Preview.Bind(wx.EVT_PAINT, self.OnPaint)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    56
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    57
        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    58
        self.Bind(wx.EVT_BUTTON, self.OnOK, 
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    59
                  self.ButtonSizer.GetAffirmativeButton())
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    60
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    61
        self.Block = None
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    62
        self.DefaultBlockName = None
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    63
        self.MinBlockSize = None
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    64
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    65
    def __del__(self):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    66
        self.Controller = None
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    67
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    68
    def SetMinBlockSize(self, size):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    69
        self.MinBlockSize = size
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    70
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    71
    def SetPreviewFont(self, font):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    72
        self.Preview.SetFont(font)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    73
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    74
    def TestBlockName(self, block_name):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    75
        format = None
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    76
        uppercase_block_name = block_name.upper()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    77
        if not TestIdentifier(block_name):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    78
            format = _("\"%s\" is not a valid identifier!")
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    79
        elif uppercase_block_name in IEC_KEYWORDS:
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    80
            format = _("\"%s\" is a keyword. It can't be used!")
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    81
        elif uppercase_block_name in self.Controller.GetProjectPouNames():
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    82
            format = _("\"%s\" pou already exists!")
1237
0c8b8ef9559b Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents: 1236
diff changeset
    83
        elif ((self.DefaultBlockName is None or 
0c8b8ef9559b Fixed support for default function block name in FBDBlockDialog
Laurent Bessard
parents: 1236
diff changeset
    84
               self.DefaultBlockName.upper() != uppercase_block_name) and 
1236
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    85
              uppercase_block_name in self.Controller.GetEditedElementVariables(
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    86
                                                                self.TagName)):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    87
            format = _("\"%s\" element for this pou already exists!")
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    88
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    89
        if format is not None:
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    90
            self.ShowErrorMessage(format % block_name)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    91
            return False
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    92
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    93
        return True
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    94
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    95
    def ShowErrorMessage(self, message):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    96
        dialog = wx.MessageDialog(self, message, 
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    97
                                  _("Error"), 
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    98
                                  wx.OK|wx.ICON_ERROR)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
    99
        dialog.ShowModal()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   100
        dialog.Destroy()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   101
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   102
    def OnOK(self, event):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   103
        self.EndModal(wx.ID_OK)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   104
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   105
    def RefreshPreview(self):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   106
        dc = wx.ClientDC(self.Preview)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   107
        dc.SetFont(self.Preview.GetFont())
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   108
        dc.Clear()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   109
        
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   110
        if self.Block is not None:
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   111
            min_width, min_height = self.Block.GetMinSize()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   112
            width = max(self.MinBlockSize[0], min_width)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   113
            height = max(self.MinBlockSize[1], min_height)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   114
            self.Block.SetSize(width, height)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   115
            clientsize = self.Preview.GetClientSize()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   116
            x = (clientsize.width - width) / 2
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   117
            y = (clientsize.height - height) / 2
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   118
            self.Block.SetPosition(x, y)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   119
            self.Block.Draw(dc)
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   120
    
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   121
    def OnPaint(self, event):
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   122
        self.RefreshPreview()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   123
        event.Skip()
a5d1d2a2f366 Added support for default function block name in FBDBlockDialog
Laurent Bessard
parents:
diff changeset
   124