util/BrowseValuesLibraryDialog.py
changeset 814 5743cbdff669
parent 813 1460273f40ed
child 815 e4f24593a758
equal deleted inserted replaced
813:1460273f40ed 814:5743cbdff669
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of Beremiz, a Integrated Development Environment for
       
     5 #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 
       
    27 
       
    28 class BrowseValuesLibraryDialog(wx.Dialog):
       
    29     """
       
    30     Modal dialog that helps in selecting predefined XML attributes sets out of hierarchically organized list
       
    31     """    
       
    32 
       
    33     def __init__(self, parent, name, library, default=None):
       
    34         wx.Dialog.__init__(self,
       
    35               name='BrowseValueDialog', parent=parent,
       
    36               size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
       
    37               title=_('Browse %s values library') % name)
       
    38 
       
    39         self.SetClientSize(wx.Size(600, 400))
       
    40 
       
    41         self.staticText1 = wx.StaticText(
       
    42               label=_('Choose a value for %s:') % name, name='staticText1', parent=self,
       
    43               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
       
    44         
       
    45         self.ValuesLibrary = wx.TreeCtrl(
       
    46               name='ValuesLibrary', parent=self, pos=wx.Point(0, 0),
       
    47               size=wx.Size(0, 0), style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
       
    48         
       
    49         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
    50 
       
    51         self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
       
    52         
       
    53         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
       
    54         
       
    55         self.flexGridSizer1.AddWindow(self.staticText1, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
    56         self.flexGridSizer1.AddWindow(self.ValuesLibrary, 0, border=20, flag=wx.GROW|wx.LEFT|wx.RIGHT)
       
    57         self.flexGridSizer1.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
    58     
       
    59         self.flexGridSizer1.AddGrowableCol(0)
       
    60         self.flexGridSizer1.AddGrowableRow(1)
       
    61         
       
    62         self.SetSizer(self.flexGridSizer1)
       
    63         
       
    64         root = self.ValuesLibrary.AddRoot("")
       
    65         self.GenerateValuesLibraryBranch(root, library, default)
       
    66 
       
    67     def GenerateValuesLibraryBranch(self, root, children, default):
       
    68         for infos in children:
       
    69             item = self.ValuesLibrary.AppendItem(root, infos["name"])
       
    70             self.ValuesLibrary.SetPyData(item, infos["infos"])
       
    71             if infos["infos"] is not None and infos["infos"] == default:
       
    72                 self.ValuesLibrary.SelectItem(item)
       
    73                 self.ValuesLibrary.EnsureVisible(item)
       
    74             self.GenerateValuesLibraryBranch(item, infos["children"], default)
       
    75 
       
    76     def GetValueInfos(self):
       
    77         selected = self.ValuesLibrary.GetSelection()
       
    78         return self.ValuesLibrary.GetPyData(selected)
       
    79 
       
    80     def OnOK(self, event):
       
    81         selected = self.ValuesLibrary.GetSelection()
       
    82         if not selected.IsOk() or self.ValuesLibrary.GetPyData(selected) is None:
       
    83             message = wx.MessageDialog(self, _("No valid value selected!"), _("Error"), wx.OK|wx.ICON_ERROR)
       
    84             message.ShowModal()
       
    85             message.Destroy()
       
    86         else:
       
    87             self.EndModal(wx.ID_OK)
       
    88