laurent@703: #!/usr/bin/env python
laurent@703: # -*- coding: utf-8 -*-
laurent@703: 
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@703: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
andrej@1696: # Copyright (C) 2017: Andrey Skvortsov <andrej.skvortzov@gmail.com>
laurent@703: #
andrej@1571: # See COPYING file for copyrights details.
laurent@703: #
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@703: #
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@703: #
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@703: 
andrej@1881: 
kinsamanka@3750: 
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
andrej@1730:     """
laurent@703: 
Edouard@724:     def __init__(self, parent, name, library, default=None):
Edouard@724:         wx.Dialog.__init__(self,
andrej@1768:                            name='BrowseValueDialog', parent=parent,
andrej@1768:                            style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
andrej@1768:                            title=_('Browse %s values library') % name)
Edouard@724: 
Edouard@724:         self.staticText1 = wx.StaticText(
andrej@1878:             label=_('Choose a value for %s:') % name,
andrej@1878:             name='staticText1', parent=self,
andrej@1878:             pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
andrej@1730: 
Edouard@724:         self.ValuesLibrary = wx.TreeCtrl(
andrej@1878:             name='ValuesLibrary', parent=self, pos=wx.Point(0, 0),
andrej@1878:             size=wx.Size(400, 200),
andrej@1878:             style=wx.TR_HAS_BUTTONS | wx.TR_SINGLE | wx.SUNKEN_BORDER | wx.TR_HIDE_ROOT | wx.TR_LINES_AT_ROOT)
andrej@1730: 
andrej@1745:         self.ButtonSizer = self.CreateButtonSizer(wx.OK | wx.CANCEL | wx.CENTRE)
Edouard@724: 
edouard@3303:         self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.GetAffirmativeId())
andrej@1730: 
Edouard@724:         self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
andrej@1730: 
edouard@3303:         self.flexGridSizer1.Add(self.staticText1,   0, border=20, flag=wx.GROW | wx.TOP | wx.LEFT | wx.RIGHT)
edouard@3303:         self.flexGridSizer1.Add(self.ValuesLibrary, 0, border=20, flag=wx.GROW | wx.LEFT | wx.RIGHT)
edouard@3303:         self.flexGridSizer1.Add(self.ButtonSizer,    0, border=20, flag=wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT)
andrej@1730: 
Edouard@724:         self.flexGridSizer1.AddGrowableCol(0)
Edouard@724:         self.flexGridSizer1.AddGrowableRow(1)
andrej@1730: 
Edouard@724:         self.SetSizer(self.flexGridSizer1)
andrej@1696:         self.Fit()
andrej@1730: 
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()
kinsamanka@3789:         return self.ValuesLibrary.GetItemData(selected)
laurent@703: 
laurent@703:     def OnOK(self, event):
laurent@703:         selected = self.ValuesLibrary.GetSelection()
kinsamanka@3789:         if not selected.IsOk() or self.ValuesLibrary.GetItemData(selected) is None:
andrej@1745:             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)