|
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 from Zeroconf import * |
|
27 import socket |
|
28 import wx.lib.mixins.listctrl as listmix |
|
29 |
|
30 class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): |
|
31 def __init__(self, parent, ID, pos=wx.DefaultPosition, |
|
32 size=wx.DefaultSize, style=0): |
|
33 wx.ListCtrl.__init__(self, parent, ID, pos, size, style) |
|
34 listmix.ListCtrlAutoWidthMixin.__init__(self) |
|
35 |
|
36 class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin): |
|
37 def __init__(self, parent, id=-1, title='Service Discovery'): |
|
38 self.my_result=None |
|
39 self.itemDataMap = {} |
|
40 wx.Dialog.__init__(self, parent, id, title, size=(600,600), style=wx.DEFAULT_DIALOG_STYLE) |
|
41 |
|
42 self.list = TestListCtrl(self, -1, |
|
43 pos=(50,20), |
|
44 size=(500,300), |
|
45 style=wx.LC_REPORT |
|
46 | wx.LC_EDIT_LABELS |
|
47 | wx.LC_SORT_ASCENDING |
|
48 ) |
|
49 self.PopulateList() |
|
50 |
|
51 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list) |
|
52 self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list) |
|
53 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list) |
|
54 self.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnItemDelete, self.list) |
|
55 self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list) |
|
56 self.list.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick) |
|
57 |
|
58 b = wx.Button(self,20, "Connect", (175, 500)) |
|
59 self.Bind(wx.EVT_BUTTON, self.OnConnect, b) |
|
60 b.SetSize(b.GetBestSize()) |
|
61 |
|
62 b = wx.Button(self, 40, "Cancel", (350, 500)) |
|
63 self.Bind(wx.EVT_BUTTON, self.OnClose, b) |
|
64 b.SetSize(b.GetBestSize()) |
|
65 |
|
66 #type = "_http._tcp.local." |
|
67 type = "_PYRO._tcp.local." |
|
68 self.r = Zeroconf() |
|
69 browser = ServiceBrowser(self.r, type, self) |
|
70 |
|
71 listmix.ColumnSorterMixin.__init__(self, 4) |
|
72 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py |
|
73 def GetListCtrl(self): |
|
74 return self.list |
|
75 |
|
76 def PopulateList(self): |
|
77 self.list.InsertColumn(0, 'NAME') |
|
78 self.list.InsertColumn(1, 'TYPE') |
|
79 self.list.InsertColumn(2, 'IP') |
|
80 self.list.InsertColumn(3, 'PORT') |
|
81 self.list.SetColumnWidth(0, 150) |
|
82 self.list.SetColumnWidth(1, 150) |
|
83 self.list.SetColumnWidth(2, 150) |
|
84 self.list.SetColumnWidth(3, 150) |
|
85 |
|
86 def getColumnText(self, index, col): |
|
87 item = self.list.GetItem(index, col) |
|
88 return item.GetText() |
|
89 |
|
90 def OnItemSelected(self, event): |
|
91 self.currentItem = event.m_itemIndex |
|
92 print "OnItemSelected: %s, %s, %s, %s\n"%(self.currentItem, |
|
93 self.list.GetItemText(self.currentItem), |
|
94 self.getColumnText(self.currentItem, 1), |
|
95 self.getColumnText(self.currentItem, 2)) |
|
96 event.Skip() |
|
97 |
|
98 |
|
99 def OnItemDeselected(self, evt): |
|
100 item = evt.GetItem() |
|
101 print "OnItemDeselected: %d" % evt.m_itemIndex |
|
102 |
|
103 def OnItemActivated(self, event): |
|
104 self.currentItem = event.m_itemIndex |
|
105 print "OnItemActivated: %s\nTopItem: %s" %(self.list.GetItemText(self.currentItem), self.list.GetTopItem()) |
|
106 |
|
107 def OnItemDelete(self, event): |
|
108 print "OnItemDelete\n" |
|
109 |
|
110 def OnColClick(self, event): |
|
111 print "OnColClick: %d\n" % event.GetColumn() |
|
112 event.Skip() |
|
113 |
|
114 def OnColRightClick(self, event): |
|
115 item = self.list.GetColumn(event.GetColumn()) |
|
116 print "OnColRightClick: %d %s\n" %(event.GetColumn(), (item.GetText(), item.GetAlign(), |
|
117 item.GetWidth(), item.GetImage())) |
|
118 def OnDoubleClick(self, event): |
|
119 connect_type = self.getColumnText(self.currentItem, 1) |
|
120 connect_address = self.getColumnText(self.currentItem, 2) |
|
121 connect_port = self.getColumnText(self.currentItem, 3) |
|
122 |
|
123 uri = self.CreateURI(connect_type, connect_address, connect_port) |
|
124 self.my_result=uri |
|
125 event.Skip() |
|
126 |
|
127 def GetResult(self): |
|
128 return self.my_result |
|
129 |
|
130 def OnClick(self, event): |
|
131 print "Click! (%d)\n" %event.GetId() |
|
132 index = self.list.GetFocusedItem() |
|
133 self.list.DeleteItem(index) |
|
134 print "Service", name, "removed" |
|
135 |
|
136 def removeService(self, zeroconf, type, name): |
|
137 index = self.list.GetFocusedItem() |
|
138 |
|
139 def addService(self, zeroconf, type, name): |
|
140 info = self.r.getServiceInfo(type, name) |
|
141 typename = type.split(".")[0][1:] |
|
142 num_items = self.list.GetItemCount() |
|
143 self.itemDataMap[num_items] = (name, "%s"%type, "%s"%str(socket.inet_ntoa(info.getAddress())), "%s"%info.getPort()) |
|
144 self.list.InsertStringItem(num_items, name.split(".")[0]) |
|
145 self.list.SetStringItem(num_items, 1, "%s"%typename) |
|
146 self.list.SetStringItem(num_items, 2, "%s"%str(socket.inet_ntoa(info.getAddress()))) |
|
147 self.list.SetStringItem(num_items, 3, "%s"%info.getPort()) |
|
148 |
|
149 def CreateURI(self, connect_type, connect_address, connect_port): |
|
150 uri = "%s://%s:%s"%(connect_type, connect_address, connect_port) |
|
151 print uri |
|
152 return uri |
|
153 |
|
154 def OnAdd(self, event): |
|
155 num_items = self.list.GetItemCount() |
|
156 self.list.InsertStringItem(num_items, self.tc1.GetValue()) |
|
157 self.list.SetStringItem(num_items, 1, self.tc2.GetValue()) |
|
158 |
|
159 def OnRemove(self, event): |
|
160 index = self.list.GetFocusedItem() |
|
161 self.list.DeleteItem(index) |
|
162 |
|
163 def OnConnect(self, event): |
|
164 index = self.list.GetFocusedItem() |
|
165 print self.list.GetItemData(index) |
|
166 |
|
167 def OnClose(self, event): |
|
168 self.Close() |
|
169 |
|
170 def OnClear(self, event): |
|
171 self.list.DeleteAllItems() |