dialogs/BrowseLocationsDialog.py
changeset 714 131ea7f237b9
parent 681 c141dad94ff4
child 729 a47f641e50f0
equal deleted inserted replaced
713:95a0a427f3ef 714:131ea7f237b9
    22 import wx
    22 import wx
    23 
    23 
    24 from plcopen.structures import LOCATIONDATATYPES
    24 from plcopen.structures import LOCATIONDATATYPES
    25 from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
    25 from PLCControler import LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY
    26 
    26 
       
    27 #-------------------------------------------------------------------------------
       
    28 #                                   Helpers
       
    29 #-------------------------------------------------------------------------------
       
    30 
    27 CWD = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
    31 CWD = os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]
    28 
    32 
    29 def GetDirChoiceOptions():
    33 def GetDirChoiceOptions():
    30     _ = lambda x : x
    34     _ = lambda x : x
    31     return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), 
    35     return [(_("All"), [LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY]), 
    38 LOCATION_SIZES = {}
    42 LOCATION_SIZES = {}
    39 for size, types in LOCATIONDATATYPES.iteritems():
    43 for size, types in LOCATIONDATATYPES.iteritems():
    40     for type in types:
    44     for type in types:
    41         LOCATION_SIZES[type] = size
    45         LOCATION_SIZES[type] = size
    42 
    46 
    43 [ID_BROWSELOCATIONSDIALOG, ID_BROWSELOCATIONSDIALOGLOCATIONSTREE, 
    47 #-------------------------------------------------------------------------------
    44  ID_BROWSELOCATIONSDIALOGDIRCHOICE, ID_BROWSELOCATIONSDIALOGSTATICTEXT1, 
    48 #                            Browse Locations Dialog
    45  ID_BROWSELOCATIONSDIALOGSTATICTEXT2, 
    49 #-------------------------------------------------------------------------------
    46 ] = [wx.NewId() for _init_ctrls in range(5)]
       
    47 
    50 
    48 class BrowseLocationsDialog(wx.Dialog):
    51 class BrowseLocationsDialog(wx.Dialog):
    49     
    52     
    50     if wx.VERSION < (2, 6, 0):
    53     def __init__(self, parent, var_type, locations):
    51         def Bind(self, event, function, id = None):
    54         wx.Dialog.__init__(self, parent,  
    52             if id is not None:
    55               size=wx.Size(600, 400), title=_('Browse Locations'))
    53                 event(self, id, function)
       
    54             else:
       
    55                 event(self, function)
       
    56     
       
    57     def _init_coll_MainSizer_Items(self, parent):
       
    58         parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
       
    59         parent.AddWindow(self.LocationsTree, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW)
       
    60         parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
       
    61         
    56         
    62     def _init_coll_MainSizer_Growables(self, parent):
    57         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
    63         parent.AddGrowableCol(0)
    58         main_sizer.AddGrowableCol(0)
    64         parent.AddGrowableRow(1)
    59         main_sizer.AddGrowableRow(1)
    65     
       
    66     def _init_coll_ButtonGridSizer_Items(self, parent):
       
    67         parent.AddWindow(self.staticText2, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL)
       
    68         parent.AddWindow(self.DirChoice, 0, border=0, flag=wx.ALIGN_CENTER_VERTICAL)
       
    69         parent.AddSizer(self.ButtonSizer, 0, border=0, flag=wx.ALIGN_RIGHT)
       
    70         
    60         
    71     def _init_coll_ButtonGridSizer_Growables(self, parent):
    61         locations_label = wx.StaticText(self, label=_('Locations available:'))
    72         parent.AddGrowableCol(2)
    62         main_sizer.AddWindow(locations_label, border=20, 
    73         parent.AddGrowableRow(0)
    63               flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
    74     
       
    75     def _init_sizers(self):
       
    76         self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
       
    77         self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0)
       
    78         
    64         
    79         self._init_coll_MainSizer_Items(self.MainSizer)
    65         self.LocationsTree = wx.TreeCtrl(self, 
    80         self._init_coll_MainSizer_Growables(self.MainSizer)
    66               style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
    81         self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer)
    67         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, 
    82         self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer)
    68                   self.LocationsTree)
       
    69         main_sizer.AddWindow(self.LocationsTree, border=20, 
       
    70               flag=wx.LEFT|wx.RIGHT|wx.GROW)
    83         
    71         
    84         self.SetSizer(self.MainSizer)
    72         button_gridsizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0)
    85     
    73         button_gridsizer.AddGrowableCol(2)
    86     def _init_ctrls(self, prnt):
    74         button_gridsizer.AddGrowableRow(0)
    87         wx.Dialog.__init__(self, id=ID_BROWSELOCATIONSDIALOG, 
    75         main_sizer.AddSizer(button_gridsizer, border=20, 
    88               name='BrowseLocationsDialog', parent=prnt,  
    76               flag=wx.BOTTOM|wx.LEFT|wx.RIGHT|wx.GROW)
    89               size=wx.Size(600, 400), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
       
    90               title=_('Browse Locations'))
       
    91     
       
    92         self.staticText1 = wx.StaticText(id=ID_BROWSELOCATIONSDIALOGSTATICTEXT1,
       
    93               label=_('Locations available:'), name='staticText1', parent=self,
       
    94               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
       
    95         
    77         
    96         if wx.Platform == '__WXMSW__':
    78         direction_label = wx.StaticText(self, label=_('Direction:'))
    97             treestyle = wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER
    79         button_gridsizer.AddWindow(direction_label,
    98         else:
    80               flag=wx.ALIGN_CENTER_VERTICAL)
    99             treestyle = wx.TR_HAS_BUTTONS|wx.TR_HIDE_ROOT|wx.TR_SINGLE|wx.SUNKEN_BORDER
       
   100         self.LocationsTree = wx.TreeCtrl(id=ID_BROWSELOCATIONSDIALOGLOCATIONSTREE,
       
   101               name='LocationsTree', parent=self, pos=wx.Point(0, 0),
       
   102               size=wx.Size(0, 0), style=treestyle)
       
   103         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnLocationsTreeItemActivated, 
       
   104                   id=ID_BROWSELOCATIONSDIALOGLOCATIONSTREE)
       
   105         
    81         
   106         self.staticText2 = wx.StaticText(id=ID_BROWSELOCATIONSDIALOGSTATICTEXT2,
    82         self.DirChoice = wx.ComboBox(self, style=wx.CB_READONLY)
   107               label=_('Direction:'), name='staticText2', parent=self,
    83         self.Bind(wx.EVT_COMBOBOX, self.OnDirChoice, self.DirChoice)
   108               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
    84         button_gridsizer.AddWindow(self.DirChoice,
       
    85               flag=wx.ALIGN_CENTER_VERTICAL)
   109         
    86         
   110         self.DirChoice = wx.ComboBox(id=ID_BROWSELOCATIONSDIALOGDIRCHOICE,
    87         button_sizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
   111               name='DirChoice', parent=self, pos=wx.Point(0, 0),
    88         self.Bind(wx.EVT_BUTTON, self.OnOK, button_sizer.GetAffirmativeButton())
   112               size=wx.DefaultSize, style=wx.CB_READONLY)
    89         button_gridsizer.AddWindow(button_sizer, flag=wx.ALIGN_RIGHT)
   113         self.Bind(wx.EVT_COMBOBOX, self.OnDirChoice, id=ID_BROWSELOCATIONSDIALOGDIRCHOICE)
       
   114         
    90         
   115         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
    91         self.SetSizer(main_sizer)
   116         if wx.VERSION >= (2, 5, 0):
       
   117             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetAffirmativeButton().GetId())
       
   118         else:
       
   119             self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.ButtonSizer.GetChildren()[0].GetSizer().GetChildren()[0].GetWindow().GetId())
       
   120         
    92         
   121         self._init_sizers()
       
   122     
       
   123     def __init__(self, parent, var_type, locations):
       
   124         self._init_ctrls(parent)
       
   125         self.VarType = var_type
    93         self.VarType = var_type
   126         self.Locations = locations
    94         self.Locations = locations
   127         
    95         
   128         # Define Tree item icon list
    96         # Define Tree item icon list
   129         self.TreeImageList = wx.ImageList(16, 16)
    97         self.TreeImageList = wx.ImageList(16, 16)
   154         self.Filter = DIRCHOICE_OPTIONS_FILTER[self.DirChoice.GetStringSelection()]
   122         self.Filter = DIRCHOICE_OPTIONS_FILTER[self.DirChoice.GetStringSelection()]
   155     
   123     
   156     def RefreshLocationsTree(self):
   124     def RefreshLocationsTree(self):
   157         root = self.LocationsTree.GetRootItem()
   125         root = self.LocationsTree.GetRootItem()
   158         if not root.IsOk():
   126         if not root.IsOk():
   159             if wx.Platform == '__WXMSW__':
   127             root = self.LocationsTree.AddRoot("")
   160                 root = self.LocationsTree.AddRoot(_('ConfNodes'))
       
   161             else:
       
   162                 root = self.LocationsTree.AddRoot("")
       
   163         self.GenerateLocationsTreeBranch(root, self.Locations)
   128         self.GenerateLocationsTreeBranch(root, self.Locations)
   164         self.LocationsTree.Expand(root)
   129         self.LocationsTree.Expand(root)
   165     
   130     
   166     def GenerateLocationsTreeBranch(self, root, locations):
   131     def GenerateLocationsTreeBranch(self, root, locations):
   167         to_delete = []
   132         to_delete = []
   168         if wx.VERSION >= (2, 6, 0):
   133         item, root_cookie = self.LocationsTree.GetFirstChild(root)
   169             item, root_cookie = self.LocationsTree.GetFirstChild(root)
       
   170         else:
       
   171             item, root_cookie = self.LocationsTree.GetFirstChild(root, 0)
       
   172         for loc_infos in locations:
   134         for loc_infos in locations:
   173             infos = loc_infos.copy()
   135             infos = loc_infos.copy()
   174             if infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP] or\
   136             if infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP] or\
   175                infos["type"] in self.Filter and (infos["IEC_type"] == self.VarType or
   137                infos["type"] in self.Filter and (infos["IEC_type"] == self.VarType or
   176                infos["IEC_type"] is None and LOCATION_SIZES[self.VarType] == infos["size"]):
   138                infos["IEC_type"] is None and LOCATION_SIZES[self.VarType] == infos["size"]):
   209         selected = self.LocationsTree.GetSelection()
   171         selected = self.LocationsTree.GetSelection()
   210         var_infos = None
   172         var_infos = None
   211         if selected.IsOk():
   173         if selected.IsOk():
   212             var_infos = self.LocationsTree.GetPyData(selected)
   174             var_infos = self.LocationsTree.GetPyData(selected)
   213         if var_infos is None or var_infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]:
   175         if var_infos is None or var_infos["type"] in [LOCATION_CONFNODE, LOCATION_MODULE, LOCATION_GROUP]:
   214             message = wx.MessageDialog(self, _("A location must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR)
   176             dialog = wx.MessageDialog(self, _("A location must be selected!"), _("Error"), wx.OK|wx.ICON_ERROR)
   215             message.ShowModal()
   177             dialog.ShowModal()
   216             message.Destroy()
   178             dialog.Destroy()
   217         else:
   179         else:
   218             self.EndModal(wx.ID_OK)
   180             self.EndModal(wx.ID_OK)