laurent@703: #!/usr/bin/env python
laurent@703: # -*- coding: utf-8 -*-
laurent@703: 
laurent@703: #This file is part of Beremiz, a Integrated Development Environment for
laurent@703: #programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
laurent@703: #
laurent@703: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
laurent@703: #
laurent@703: #See COPYING file for copyrights details.
laurent@703: #
laurent@703: #This library is free software; you can redistribute it and/or
laurent@703: #modify it under the terms of the GNU General Public
laurent@703: #License as published by the Free Software Foundation; either
laurent@703: #version 2.1 of the License, or (at your option) any later version.
laurent@703: #
laurent@703: #This library is distributed in the hope that it will be useful,
laurent@703: #but WITHOUT ANY WARRANTY; without even the implied warranty of
laurent@703: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
laurent@703: #General Public License for more details.
laurent@703: #
laurent@703: #You should have received a copy of the GNU General Public
laurent@703: #License along with this library; if not, write to the Free Software
laurent@703: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
laurent@703: 
laurent@703: import wx
laurent@703: 
laurent@703: 
laurent@703: class BrowseValuesLibraryDialog(wx.Dialog):
Edouard@724:     """
laurent@801:     Modal dialog that helps in selecting predefined XML attributes sets out of hierarchically organized list
Edouard@724:     """    
laurent@703: 
Edouard@724:     def __init__(self, parent, name, library, default=None):
Edouard@724:         wx.Dialog.__init__(self,
Edouard@724:               name='BrowseValueDialog', parent=parent,
laurent@703:               size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
laurent@801:               title=_('Browse %s values library') % name)
Edouard@724: 
laurent@703:         self.SetClientSize(wx.Size(600, 400))
laurent@703: 
Edouard@724:         self.staticText1 = wx.StaticText(
laurent@801:               label=_('Choose a value for %s:') % name, name='staticText1', parent=self,
laurent@703:               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
laurent@703:         
Edouard@724:         self.ValuesLibrary = wx.TreeCtrl(
laurent@703:               name='ValuesLibrary', parent=self, pos=wx.Point(0, 0),
laurent@703:               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)
laurent@703:         
laurent@703:         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
Edouard@724: 
Edouard@724:         self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
laurent@703:         
Edouard@724:         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
Edouard@724:         
Edouard@724:         self.flexGridSizer1.AddWindow(self.staticText1, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
Edouard@724:         self.flexGridSizer1.AddWindow(self.ValuesLibrary, 0, border=20, flag=wx.GROW|wx.LEFT|wx.RIGHT)
Edouard@724:         self.flexGridSizer1.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
Edouard@724:     
Edouard@724:         self.flexGridSizer1.AddGrowableCol(0)
Edouard@724:         self.flexGridSizer1.AddGrowableRow(1)
Edouard@724:         
Edouard@724:         self.SetSizer(self.flexGridSizer1)
laurent@703:         
laurent@703:         root = self.ValuesLibrary.AddRoot("")
laurent@703:         self.GenerateValuesLibraryBranch(root, library, default)
laurent@703: 
laurent@703:     def GenerateValuesLibraryBranch(self, root, children, default):
laurent@703:         for infos in children:
laurent@703:             item = self.ValuesLibrary.AppendItem(root, infos["name"])
laurent@703:             self.ValuesLibrary.SetPyData(item, infos["infos"])
laurent@703:             if infos["infos"] is not None and infos["infos"] == default:
laurent@703:                 self.ValuesLibrary.SelectItem(item)
laurent@703:                 self.ValuesLibrary.EnsureVisible(item)
laurent@703:             self.GenerateValuesLibraryBranch(item, infos["children"], default)
laurent@703: 
laurent@703:     def GetValueInfos(self):
laurent@703:         selected = self.ValuesLibrary.GetSelection()
laurent@703:         return self.ValuesLibrary.GetPyData(selected)
laurent@703: 
laurent@703:     def OnOK(self, event):
laurent@703:         selected = self.ValuesLibrary.GetSelection()
laurent@703:         if not selected.IsOk() or self.ValuesLibrary.GetPyData(selected) is None:
laurent@703:             message = wx.MessageDialog(self, _("No valid value selected!"), _("Error"), wx.OK|wx.ICON_ERROR)
laurent@703:             message.ShowModal()
laurent@703:             message.Destroy()
laurent@703:         else:
laurent@703:             self.EndModal(wx.ID_OK)
laurent@703: