andrej@1571: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
andrej@1696: # Copyright (C) 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1853: from __future__ import absolute_import
Laurent@814: import wx
Laurent@814: 
Laurent@1251: from graphics.GraphicCommons import SELECTION_DIVERGENCE, \
Laurent@1251:     SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE
Laurent@1251: from graphics.SFC_Objects import SFC_Divergence
andrej@1853: from dialogs.BlockPreviewDialog import BlockPreviewDialog
Laurent@814: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                         Create New Divergence Dialog
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@1251: 
Laurent@1251: class SFCDivergenceDialog(BlockPreviewDialog):
andrej@1736:     """
andrej@1736:     Class that implements a dialog for defining parameters for creating a new
andrej@1736:     divergence graphic element
andrej@1736:     """
andrej@1730: 
andrej@1744:     def __init__(self, parent, controller, tagname, poss_div_types=None):
Laurent@1251:         """
Laurent@1251:         Constructor
Laurent@1251:         @param parent: Parent wx.Window of dialog for modal
Laurent@1251:         @param controller: Reference to project controller
Laurent@1251:         @param tagname: Tagname of project POU edited
surkovsv93@1584:         @param poss_div_types: Types of divergence that will be available in the dialog window
Laurent@1251:         """
andrej@1696:         BlockPreviewDialog.__init__(self, parent, controller, tagname,
andrej@1768:                                     title=_('Create a new divergence or convergence'))
andrej@1730: 
Laurent@1251:         # Init common sizers
Laurent@1251:         self._init_sizers(2, 0, 7, None, 2, 1)
andrej@1730: 
Laurent@1251:         # Create label for divergence type
Laurent@1251:         type_label = wx.StaticText(self, label=_('Type:'))
Laurent@1251:         self.LeftGridSizer.AddWindow(type_label, flag=wx.GROW)
andrej@1730: 
Laurent@1251:         # Create radio buttons for selecting divergence type
surkovsv93@1584:         divergence_buttons = [
surkovsv93@1584:             (SELECTION_DIVERGENCE, _('Selection Divergence')),
surkovsv93@1584:             (SELECTION_CONVERGENCE, _('Selection Convergence')),
surkovsv93@1584:             (SIMULTANEOUS_DIVERGENCE, _('Simultaneous Divergence')),
surkovsv93@1584:             (SIMULTANEOUS_CONVERGENCE, _('Simultaneous Convergence'))]
surkovsv93@1584:         poss_div_btns = []
surkovsv93@1584:         if poss_div_types is not None:
andrej@1757:             for val in poss_div_types:
andrej@1757:                 poss_div_btns.append(divergence_buttons[val])
surkovsv93@1584:         else:
surkovsv93@1584:             poss_div_btns = divergence_buttons
Laurent@1251:         self.TypeRadioButtons = {}
Laurent@1251:         first = True
surkovsv93@1584:         focusbtn = None
surkovsv93@1584:         for type, label in poss_div_btns:
andrej@1730:             radio_button = wx.RadioButton(self, label=label,
andrej@1768:                                           style=(wx.RB_GROUP if first else 0))
Laurent@1251:             radio_button.SetValue(first)
Laurent@1251:             self.Bind(wx.EVT_RADIOBUTTON, self.OnTypeChanged, radio_button)
Laurent@1251:             self.LeftGridSizer.AddWindow(radio_button, flag=wx.GROW)
Laurent@1251:             self.TypeRadioButtons[type] = radio_button
andrej@1756:             if first:
andrej@1756:                 focusbtn = type
Laurent@1251:             first = False
Laurent@814: 
Laurent@1251:         # Create label for number of divergence sequences
andrej@1730:         sequences_label = wx.StaticText(self,
andrej@1768:                                         label=_('Number of sequences:'))
Laurent@1251:         self.LeftGridSizer.AddWindow(sequences_label, flag=wx.GROW)
andrej@1730: 
Laurent@1251:         # Create spin control for defining number of divergence sequences
surkovsv93@1585:         self.Sequences = wx.SpinCtrl(self, min=2, max=20, initial=2)
Laurent@814:         self.Bind(wx.EVT_SPINCTRL, self.OnSequencesChanged, self.Sequences)
Laurent@1251:         self.LeftGridSizer.AddWindow(self.Sequences, flag=wx.GROW)
andrej@1730: 
Laurent@1251:         # Add preview panel and associated label to sizers
Laurent@1251:         self.RightGridSizer.AddWindow(self.PreviewLabel, flag=wx.GROW)
Laurent@1251:         self.RightGridSizer.AddWindow(self.Preview, flag=wx.GROW)
andrej@1730: 
Laurent@1251:         # Add buttons sizer to sizers
andrej@1768:         self.MainSizer.AddSizer(
andrej@1768:             self.ButtonSizer, border=20,
andrej@1768:             flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT)
andrej@1730: 
andrej@1696:         self.Fit()
andrej@1696: 
Laurent@1251:         # Selection divergence radio button is default control having keyboard
Laurent@1251:         # focus
surkovsv93@1584:         self.TypeRadioButtons[focusbtn].SetFocus()
andrej@1730: 
Laurent@1251:     def GetMinElementSize(self):
Laurent@1251:         """
Laurent@1251:         Get minimal graphic element size
Laurent@1251:         @return: Tuple containing minimal size (width, height) or None if no
Laurent@1251:         element defined
Laurent@1251:         """
Laurent@1259:         return self.Element.GetMinSize(True)
andrej@1730: 
Laurent@1251:     def GetDivergenceType(self):
Laurent@1251:         """
Laurent@1251:         Return type selected for SFC divergence
Laurent@1251:         @return: Type selected (None if not found)
Laurent@1251:         """
Laurent@1251:         # Go through radio buttons and return type associated to the one that
Laurent@1251:         # is selected
Laurent@1251:         for type, control in self.TypeRadioButtons.iteritems():
Laurent@1251:             if control.GetValue():
Laurent@1251:                 return type
Laurent@1251:         return None
andrej@1730: 
Laurent@814:     def GetValues(self):
Laurent@1251:         """
Laurent@1251:         Set default SFC divergence parameters
Laurent@1251:         @param values: Divergence parameters values
Laurent@1251:         """
Laurent@1251:         return {"type": self.GetDivergenceType(),
Laurent@1251:                 "number": self.Sequences.GetValue()}
Laurent@814: 
Laurent@814:     def OnTypeChanged(self, event):
Laurent@1251:         """
Laurent@1251:         Called when SFC divergence type changed
Laurent@1251:         @param event: wx.RadioButtonEvent
Laurent@1251:         """
Edouard@2591:         self.RefreshPreview()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnSequencesChanged(self, event):
Laurent@1251:         """
Laurent@1251:         Called when SFC divergence number of sequences changed
Laurent@1251:         @param event: wx.SpinEvent
Laurent@1251:         """
Edouard@2591:         self.RefreshPreview()
Laurent@814:         event.Skip()
andrej@1730: 
Edouard@2587:     def DrawPreview(self):
Laurent@1251:         """
Laurent@1251:         Refresh preview panel of graphic element
Laurent@1251:         Override BlockPreviewDialog function
Laurent@1251:         """
Laurent@1251:         # Set graphic element displayed, creating a SFC divergence
andrej@1730:         self.Element = SFC_Divergence(self.Preview,
andrej@1730:                                       self.GetDivergenceType(),
Laurent@1251:                                       self.Sequences.GetValue())
andrej@1730: 
Laurent@1251:         # Call BlockPreviewDialog function
Edouard@2587:         BlockPreviewDialog.DrawPreview(self)