dialogs/SFCStepDialog.py
author Sergey Surkov <surkovsv93@gmail.com>
Tue, 18 Oct 2016 17:44:08 +0300
changeset 1544 2969c2123105
parent 1493 6dbebfcec074
child 1571 486f94a8032c
permissions -rw-r--r--
Fix bug with two or more wires connected to one input. Now only one wire can be connected to one input, except BOOLean signals in LD and SFC. If user trying to connect wire with already connected input, wire highlight will become red.
Signed-off-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C) 2007: 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 graphics.SFC_Objects import SFC_Step
from BlockPreviewDialog import BlockPreviewDialog

#-------------------------------------------------------------------------------
#                         Set SFC Step Parameters Dialog
#-------------------------------------------------------------------------------

"""
Class that implements a dialog for defining parameters of a SFC step graphic
element
"""

class SFCStepDialog(BlockPreviewDialog):
    
    def __init__(self, parent, controller, tagname, initial=False):
        """
        Constructor
        @param parent: Parent wx.Window of dialog for modal
        @param controller: Reference to project controller
        @param tagname: Tagname of project POU edited
        @param initial: True if step is initial (default: False)
        """
        BlockPreviewDialog.__init__(self,parent, controller, tagname,  
              size=wx.Size(400, 280), title=_('Edit Step'))
        
        # Init common sizers
        self._init_sizers(2, 0, 6, None, 2, 1)
        
        # Create label for SFC step name
        name_label = wx.StaticText(self, label=_('Name:'))
        self.LeftGridSizer.AddWindow(name_label, flag=wx.GROW)
        
        # Create text control for defining SFC step name
        self.StepName = wx.TextCtrl(self)
        self.Bind(wx.EVT_TEXT, self.OnNameChanged, self.StepName)
        self.LeftGridSizer.AddWindow(self.StepName, flag=wx.GROW)
        
        # Create label for SFC step connectors
        connectors_label = wx.StaticText(self, label=_('Connectors:'))
        self.LeftGridSizer.AddWindow(connectors_label, flag=wx.GROW)
        
        # Create check boxes for defining connectors available on SFC step
        self.ConnectorsCheckBox = {}
        for name, label in [("input", _("Input")),
                            ("output", _("Output")),
                            ("action", _("Action"))]:
            check_box = wx.CheckBox(self, label=label)
            self.Bind(wx.EVT_CHECKBOX, self.OnConnectorsChanged, check_box)
            self.LeftGridSizer.AddWindow(check_box, flag=wx.GROW)
            self.ConnectorsCheckBox[name] = check_box
        
        # Add preview panel and associated label to sizers
        self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
        self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW)
        
        # Add buttons sizer to sizers
        self.MainSizer.AddSizer(self.ButtonSizer, border=20, 
              flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
        
        # Save flag that indicates that step is initial
        self.Initial = initial
        
        # Set default name for step
        self.StepName.ChangeValue(controller.GenerateNewName(
               tagname, None, "Step%d", 0))
        
        # Step name text control is default control having keyboard focus
        self.StepName.SetFocus()
    
    def SetValues(self, values):
        """
        Set default block parameters
        @param values: Block parameters values
        """
        # For each parameters defined, set corresponding control value
        for name, value in values.items():
            
            # Parameter is step name
            if name == "name":
                self.StepName.ChangeValue(value)
        
            # Set value of other controls
            else:
                control = self.ConnectorsCheckBox.get(name, None)
                if control is not None:
                    control.SetValue(value)
        
        # Refresh preview panel
        self.RefreshPreview()
        
    def GetValues(self):
        """
        Return step parameters defined in dialog
        @return: {parameter_name: parameter_value,...}
        """
        values = {"name": self.StepName.GetValue()}
        values.update({
            name: control.IsChecked()
            for name, control in self.ConnectorsCheckBox.iteritems()})
        values["width"], values["height"] = self.Element.GetSize()
        return values
    
    def OnOK(self, event):
        """
        Called when dialog OK button is pressed
        Test if step name defined is valid
        @param event: wx.Event from OK button
        """
        message = None
        
        # Get step name typed by user
        step_name = self.StepName.GetValue()
        
        # Test that a name have been defined
        if step_name == "":
            message = _("Form isn't complete. Name must be filled!")
        
        # If an error have been identify, show error message dialog
        if message is not None:
            self.ShowErrorMessage(message)
        
        # Test step name validity
        elif self.TestElementName(step_name):
            # Call BlockPreviewDialog function
            BlockPreviewDialog.OnOK(self, event)
    
    def OnConnectorsChanged(self, event):
        """
        Called when a step connector value changed
        @param event: wx.CheckBoxEvent
        """
        self.RefreshPreview()
        event.Skip()

    def OnNameChanged(self, event):
        """
        Called when step name value changed
        @param event: wx.TextEvent
        """
        self.RefreshPreview()
        event.Skip()
    
    def RefreshPreview(self):
        """
        Refresh preview panel of graphic element
        Override BlockPreviewDialog function
        """
        # Set graphic element displayed, creating a SFC step element
        self.Element = SFC_Step(self.Preview, 
                                self.StepName.GetValue(), 
                                self.Initial)
        
        # Update connectors of SFC step element according to check boxes value
        for name, control in self.ConnectorsCheckBox.iteritems():
            if control.IsChecked():
                getattr(self.Element, "Add" + name.capitalize())()
            else:
                getattr(self.Element, "Remove" + name.capitalize())()
        
        # Call BlockPreviewDialog function
        BlockPreviewDialog.RefreshPreview(self)