Redesign DiscoveryDialog class to conform to others dialogs
authorlaurent
Fri, 04 Sep 2009 17:33:51 +0200
changeset 392 6617d3fb43e2
parent 389 bde723abfdfc
child 393 af20e07e53c5
Redesign DiscoveryDialog class to conform to others dialogs
discovery.py
plugger.py
--- a/discovery.py	Fri Sep 04 11:24:25 2009 +0200
+++ b/discovery.py	Fri Sep 04 17:33:51 2009 +0200
@@ -27,188 +27,185 @@
 import  wx.lib.mixins.listctrl  as  listmix
 
 class AutoWidthListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
-    def __init__(self, parent, ID, pos=wx.DefaultPosition,
+    def __init__(self, parent, id, name, pos=wx.DefaultPosition,
                  size=wx.DefaultSize, style=0):
-        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
+        wx.ListCtrl.__init__(self, parent, id, pos, size, style, name=name)
         listmix.ListCtrlAutoWidthMixin.__init__(self)
 
+[ID_DISCOVERYDIALOG, ID_DISCOVERYDIALOGSTATICTEXT1, 
+ ID_DISCOVERYDIALOGSERVICESLIST, ID_DISCOVERYDIALOGREFRESHBUTTON, 
+ ID_DISCOVERYDIALOGLOCALBUTTON, 
+] = [wx.NewId() for _init_ctrls in range(5)]
+
 class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin):
-    def __init__(self, parent, id=-1, title=_('Service Discovery')):
-        self.my_result=None
-        wx.Dialog.__init__(self, parent, id, title, size=(600,600), style=wx.DEFAULT_DIALOG_STYLE)
-
-        # set up dialog sizer
-
-        sizer = wx.FlexGridSizer(2, 1, 2, 2)  # rows, cols, vgap, hgap
-        sizer.AddGrowableRow(0)
-        sizer.AddGrowableCol(0)
-
-        # set up list control
-
-        self.list = AutoWidthListCtrl(self, -1,
-                                      #pos=(50,20), 
-                                      #size=(500,300),
-                                      style=wx.LC_REPORT 
-                                     | wx.LC_EDIT_LABELS
-                                     | wx.LC_SORT_ASCENDING
-                                     | wx.LC_SINGLE_SEL 
-                                     )
-        sizer.Add(self.list, 1, wx.EXPAND)
-
-        btsizer = wx.FlexGridSizer(1, 6, 2, 2)  # rows, cols, vgap, hgap
-        
-        sizer.Add(btsizer, 1, wx.EXPAND)
-
-        self.PopulateList()
-
-        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
-        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
-
-        # set up buttons
-
-        local_id = wx.NewId()
-        b = wx.Button(self, local_id, _("Refresh"))
-        self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, b)
-        btsizer.Add(b)
-
-        btsizer.AddSpacer(0)
-        btsizer.AddGrowableCol(1)
-
-        local_id = wx.NewId()
-        b = wx.Button(self, local_id, _("Local"))
-        self.Bind(wx.EVT_BUTTON, self.ChooseLocalID, b)
-        btsizer.Add(b)
-
-        btsizer.AddSpacer(0)
-        btsizer.AddGrowableCol(3)
-
-        b = wx.Button(self, wx.ID_CANCEL, _("Cancel"))
-        self.Bind(wx.EVT_BUTTON, self.OnCancel, b)
-        btsizer.Add(b)
-
-        b = wx.Button(self, wx.ID_OK, _("OK"))
-        self.Bind(wx.EVT_BUTTON, self.OnOk, b)
-        b.SetDefault()
-        btsizer.Add(b)
-
-        self.SetSizer(sizer)
-
+    
+    def _init_coll_MainSizer_Items(self, parent):
+        parent.AddWindow(self.staticText1, 0, border=20, flag=wx.TOP|wx.LEFT|wx.RIGHT|wx.GROW)
+        parent.AddWindow(self.ServicesList, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.GROW)
+        parent.AddSizer(self.ButtonGridSizer, 0, border=20, flag=wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.GROW)
+        
+    def _init_coll_MainSizer_Growables(self, parent):
+        parent.AddGrowableCol(0)
+        parent.AddGrowableRow(1)
+    
+    def _init_coll_ButtonGridSizer_Items(self, parent):
+        parent.AddWindow(self.RefreshButton, 0, border=0, flag=0)
+        parent.AddWindow(self.LocalButton, 0, border=0, flag=0)
+        parent.AddSizer(self.ButtonSizer, 0, border=0, flag=0)
+        
+    def _init_coll_ButtonGridSizer_Growables(self, parent):
+        parent.AddGrowableCol(0)
+        parent.AddGrowableCol(1)
+        parent.AddGrowableRow(1)
+    
+    def _init_sizers(self):
+        self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=10)
+        self.ButtonGridSizer = wx.FlexGridSizer(cols=3, hgap=5, rows=1, vgap=0)
+        
+        self._init_coll_MainSizer_Items(self.MainSizer)
+        self._init_coll_MainSizer_Growables(self.MainSizer)
+        self._init_coll_ButtonGridSizer_Items(self.ButtonGridSizer)
+        self._init_coll_ButtonGridSizer_Growables(self.ButtonGridSizer)
+        
+        self.SetSizer(self.MainSizer)
+    
+    def _init_ctrls(self, prnt):
+        wx.Dialog.__init__(self, id=ID_DISCOVERYDIALOG, 
+              name='DiscoveryDialog', parent=prnt,  
+              size=wx.Size(600, 600), style=wx.DEFAULT_DIALOG_STYLE,
+              title='Service Discovery')
+        
+        self.staticText1 = wx.StaticText(id=ID_DISCOVERYDIALOGSTATICTEXT1,
+              label=_('Services available:'), name='staticText1', parent=self,
+              pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
+        
+        # Set up list control
+        self.ServicesList = AutoWidthListCtrl(id=ID_DISCOVERYDIALOGSERVICESLIST,
+              name='ServicesList', parent=self, pos=wx.Point(0, 0), size=wx.Size(0, 0), 
+              style=wx.LC_REPORT|wx.LC_EDIT_LABELS|wx.LC_SORT_ASCENDING|wx.LC_SINGLE_SEL)
+        self.ServicesList.InsertColumn(0, 'NAME')
+        self.ServicesList.InsertColumn(1, 'TYPE')
+        self.ServicesList.InsertColumn(2, 'IP')
+        self.ServicesList.InsertColumn(3, 'PORT')
+        self.ServicesList.SetColumnWidth(0, 150)
+        self.ServicesList.SetColumnWidth(1, 150)
+        self.ServicesList.SetColumnWidth(2, 150)
+        self.ServicesList.SetColumnWidth(3, 150)
+        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, id=ID_DISCOVERYDIALOGSERVICESLIST)
+        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, id=ID_DISCOVERYDIALOGSERVICESLIST)
+        
         listmix.ColumnSorterMixin.__init__(self, 4)
-
-        # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
+        
+        self.RefreshButton = wx.Button(id=ID_DISCOVERYDIALOGREFRESHBUTTON,
+              label=_('Refresh'), name='RefreshButton', parent=self,
+              pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
+        self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, id=ID_DISCOVERYDIALOGREFRESHBUTTON)
+        
+        self.LocalButton = wx.Button(id=ID_DISCOVERYDIALOGLOCALBUTTON,
+              label=_('Local'), name='LocalButton', parent=self,
+              pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
+        self.Bind(wx.EVT_BUTTON, self.OnLocalButton, id=ID_DISCOVERYDIALOGLOCALBUTTON)
+        
+        self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTER)
+        
+        self._init_sizers()
+        
+    def __init__(self, parent):
+        self._init_ctrls(parent)
+        
         self.itemDataMap = {}
-
-        # a counter used to assign a unique id to each listctrl item
         self.nextItemId = 0
-
-        self.browser = None
-        self.zConfInstance = Zeroconf()
+        
+        self.URI = None
+        self.Browser = None
+        
+        self.ZeroConfInstance = Zeroconf()
         self.RefreshList()
-
-        self.Bind(wx.EVT_CLOSE, self.OnClose)
-
+        
+    def __del__(self):
+        self.Browser.cancel()
+        self.ZeroConfInstance.close()
+        
     def RefreshList(self):
-        type = "_PYRO._tcp.local."
-        self.browser = ServiceBrowser(self.zConfInstance, type, self)        
+        self.Browser = ServiceBrowser(self.ZeroConfInstance, "_PYRO._tcp.local.", self)        
 
     def OnRefreshButton(self, event):
-        self.list.DeleteAllItems()
-        self.browser.cancel()
+        self.ServicesList.DeleteAllItems()
+        self.Browser.cancel()
         self.RefreshList()
 
-    def OnClose(self, event):
-        self.zConfInstance.close()
-        event.Skip()
-
-    def OnCancel(self, event):
-        self.zConfInstance.close()
-        event.Skip()
-
-    def OnOk(self, event):
-        self.zConfInstance.close()
+    def OnLocalButton(self, event):
+        self.URI = "LOCAL://"
+        self.EndModal(wx.ID_OK)
         event.Skip()
 
     # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
     def GetListCtrl(self):
-        return self.list
-
-    def PopulateList(self):
-        self.list.InsertColumn(0, 'NAME')
-        self.list.InsertColumn(1, 'TYPE')
-        self.list.InsertColumn(2, 'IP')
-        self.list.InsertColumn(3, 'PORT')
-        self.list.SetColumnWidth(0, 150)
-        self.list.SetColumnWidth(1, 150)
-        self.list.SetColumnWidth(2, 150)
-        self.list.SetColumnWidth(3, 150)
+        return self.ServicesList
 
     def getColumnText(self, index, col):
-        item = self.list.GetItem(index, col)
+        item = self.ServicesList.GetItem(index, col)
         return item.GetText()
 
     def OnItemSelected(self, event):
-        self.currentItem = event.m_itemIndex
-        self.setresult()
+        self.SetURI(event.m_itemIndex)
         event.Skip()
 
     def OnItemActivated(self, event):
-        self.currentItem = event.m_itemIndex
-        self.setresult()
-        self.Close()
+        self.SetURI(event.m_itemIndex)
+        self.EndModal(wx.ID_OK)
         event.Skip()
 
-    def setresult(self):
-        connect_type = self.getColumnText(self.currentItem, 1)
-        connect_address = self.getColumnText(self.currentItem, 2)
-        connect_port = self.getColumnText(self.currentItem, 3)
-        
-        uri = self.CreateURI(connect_type, connect_address, connect_port)
-        self.my_result=uri
-
-    def GetResult(self):
-        return self.my_result
+    def SetURI(self, idx):
+        connect_type = self.getColumnText(idx, 1)
+        connect_address = self.getColumnText(idx, 2)
+        connect_port = self.getColumnText(idx, 3)
+        
+        self.URI = "%s://%s:%s"%(connect_type, connect_address, connect_port)
+
+    def GetURI(self):
+        return self.URI
         
     def removeService(self, zeroconf, type, name):
         '''
         called when a service with the desired type goes offline.
         '''
-
+        
         # loop through the list items looking for the service that went offline
-        for idx in xrange(self.list.GetItemCount()):
+        for idx in xrange(self.ServicesList.GetItemCount()):
             # this is the unique identifier assigned to the item
-            item_id = self.list.GetItemData(idx)
+            item_id = self.ServicesList.GetItemData(idx)
 
             # this is the full typename that was received by addService
             item_name = self.itemDataMap[item_id][4]
 
             if item_name == name:
-                self.list.DeleteItem(idx)
+                self.ServicesList.DeleteItem(idx)
                 break
-
+        
     def addService(self, zeroconf, type, name):
         '''
         called when a service with the desired type is discovered.
         '''
-
-        info = self.zConfInstance.getServiceInfo(type, name)
+        info = self.ZeroConfInstance.getServiceInfo(type, name)
 
         svcname  = name.split(".")[0]
         typename = type.split(".")[0][1:]
         ip       = str(socket.inet_ntoa(info.getAddress()))
         port     = info.getPort()
 
-        num_items = self.list.GetItemCount()
+        num_items = self.ServicesList.GetItemCount()
 
         # display the new data in the list
-        new_item = self.list.InsertStringItem(num_items, svcname)
-        self.list.SetStringItem(new_item, 1, "%s" % typename)
-        self.list.SetStringItem(new_item, 2, "%s" % ip)
-        self.list.SetStringItem(new_item, 3, "%s" % port)
+        new_item = self.ServicesList.InsertStringItem(num_items, svcname)
+        self.ServicesList.SetStringItem(new_item, 1, "%s" % typename)
+        self.ServicesList.SetStringItem(new_item, 2, "%s" % ip)
+        self.ServicesList.SetStringItem(new_item, 3, "%s" % port)
 
         # record the new data for the ColumnSorterMixin
         # we assign every list item a unique id (that won't change when items
         # are added or removed)
-        self.list.SetItemData(new_item, self.nextItemId)
+        self.ServicesList.SetItemData(new_item, self.nextItemId)
  
         # the value of each column has to be stored in the itemDataMap
         # so that ColumnSorterMixin knows how to sort the column.
@@ -218,11 +215,4 @@
         self.itemDataMap[self.nextItemId] = [ svcname, typename, ip, port, name ]
 
         self.nextItemId += 1
-
-    def CreateURI(self, connect_type, connect_address, connect_port):
-        uri = "%s://%s:%s"%(connect_type, connect_address, connect_port)
-        return uri
-
-    def ChooseLocalID(self, event):
-        self.my_result = "LOCAL://"
-        self.Close()
+        
\ No newline at end of file
--- a/plugger.py	Fri Sep 04 11:24:25 2009 +0200
+++ b/plugger.py	Fri Sep 04 17:33:51 2009 +0200
@@ -1639,11 +1639,14 @@
         # if uri is empty launch discovery dialog
         if uri == "":
             # Launch Service Discovery dialog
-            dia = DiscoveryDialog(self.AppFrame)
-            dia.ShowModal()
-            uri = dia.GetResult()
+            dialog = DiscoveryDialog(self.AppFrame)
+            answer = dialog.ShowModal()
+            uri = dialog.GetURI()
+            dialog.Destroy()
+            
             # Nothing choosed or cancel button
-            if uri is None:
+            if uri is None or answer == wx.ID_CANCEL:
+                self.logger.write_error(_("Connection canceled!\n"))
                 return
             else:
                 self.\