1 # -*- coding: utf-8 -*-
3 #----------------------------------------------------------------------------
4 # Author: Pierre Ratinaud
7 #comes from ListCtrl.py from the demo tool of wxPython:
8 # Author: Robin Dunn & Gary Dumer
11 # Copyright: (c) 1998 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------------
18 import wx.lib.mixins.listctrl as listmix
21 from functions import exec_rcode
22 from dialog import message, BarFrame
23 from chemins import ffr
24 #---------------------------------------------------------------------------
26 class SearchList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin): #wx.Panel, listmix.ColumnSorterMixin):
27 def __init__(self, parent,gparent, dlist,first, nbactives, nbetoiles, add_dendro=True):
28 wx.ListCtrl.__init__( self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
32 self.add_dendro=add_dendro
33 self.first = ['id','formes'] + first
34 self.lenact = nbactives
35 self.lensup = len(dlist) - (self.lenact + nbetoiles)
38 self.il = wx.ImageList(16, 16)
39 a={"sm_up":"GO_UP","sm_dn":"GO_DOWN","w_idx":"WARNING","e_idx":"ERROR","i_idx":"QUESTION"}
41 s="self.%s= self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k,v)
43 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
45 self.attr1 = wx.ListItemAttr()
46 self.attr1.SetBackgroundColour((220, 220, 220))
47 self.attrsg = wx.ListItemAttr()
48 self.attrsg.SetBackgroundColour((230, 230, 230))
49 self.attr2 = wx.ListItemAttr()
50 self.attr2.SetBackgroundColour((190, 249, 236))
51 self.attr2s = wx.ListItemAttr()
52 self.attr2s.SetBackgroundColour((211, 252, 244))
53 self.attr3 = wx.ListItemAttr()
54 self.attr3.SetBackgroundColour((245, 180, 180))
55 self.attr3s = wx.ListItemAttr()
56 self.attr3s.SetBackgroundColour((245, 190, 190))
62 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
63 #self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
66 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
69 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
72 self.itemDataMap = dlist
73 self.itemIndexMap = dlist.keys()
74 self.SetItemCount(len(dlist))
75 listmix.ColumnSorterMixin.__init__(self, len(first)+2)
78 #-----------------------------------------------------------------------------------------
80 first = ['id','formes']+first
81 for i, name in enumerate(first) :
82 self.InsertColumn(i, name, wx.LIST_FORMAT_LEFT)
84 self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
85 for i in range(1,len(first)-1):
86 self.SetColumnWidth(i, 130)
88 self.SortListItems(0, True)
90 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
91 def GetListCtrl(self):
94 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
95 def GetSortImages(self):
96 return (self.sm_dn, self.sm_up)
99 def OnRightDown(self, event):
102 item, flags = self.HitTest((x, y))
104 if flags & wx.LIST_HITTEST_ONITEM:
109 def getColumnText(self, index, col):
110 item = self.GetItem(index, col)
111 return item.GetText()
113 def OnGetItemText(self, item, col):
114 index=self.itemIndexMap[item]
115 s = self.itemDataMap[index][col]
118 def OnGetItemData(self, item) :
119 index = self.itemIndexMap[item]
120 s = self.itemDataMap[index]
123 def OnItemSelected(self, event):
124 self.currentItem = event.m_itemIndex
127 def OnGetItemAttr(self, item):
128 index=self.itemIndexMap[item]
129 #genre=self.itemDataMap[index][2]
130 if index < self.lenact :
135 elif index >= self.lenact and index < (self.lenact + self.lensup) :
140 elif index >= (self.lenact + self.lensup) :
148 def SortItems(self,sorter=cmp):
149 items = list(self.itemDataMap.keys())
151 self.itemIndexMap = items
156 def OnRightClick(self, event):
158 # only do this part the first time so the events are only bound once
159 if not hasattr(self, "popupID1"):
160 # self.popupID1 = wx.NewId()
161 #self.popupID2 = wx.NewId()
162 self.popupID3 = wx.NewId()
164 self.id_adddendro = wx.NewId()
165 self.Bind(wx.EVT_MENU, self.ongraphdendro, id = self.id_adddendro)
166 # self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
167 #self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
168 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
174 # menu.Append(self.popupID1, u"Formes associƩes")
175 #menu.Append(self.popupID2, u"Concordancier")
176 menu.Append(self.popupID3, "Graphique")
178 menu.Append(self.id_adddendro, u"Graphique + dendrogramme")
183 def getselectedwords(self) :
184 words = [self.getColumnText(self.GetFirstSelected(), 6)]
185 last = self.GetFirstSelected()
186 while self.GetNextSelected(last) != -1:
187 last = self.GetNextSelected(last)
188 words.append(self.getColumnText(last, 6))
191 def OnPopupOne(self, event):
192 activenotebook = self.parent.nb.GetSelection()
193 page = self.parent.nb.GetPage(activenotebook)
195 word = self.getColumnText(self.GetFirstSelected(), 0)
198 for forme in lems[word] :
199 rep.append([forme, corpus.formes[forme][0]])
200 win = message(self, u"Formes associƩes", size=(300, 200))
201 win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
202 win.HtmlPage.SetPage(win.html)
205 def OnPopupTwo(self, event):
206 activenotebook = self.parent.nb.GetSelection()
207 page = self.parent.nb.GetPage(activenotebook)
208 item=self.getColumnText(self.GetFirstSelected(), 0)
210 win = message(self, u"Concordancier", size=(600, 200))
212 listmot = corpus.lems[item]
213 uce_ok = [corpus.formes[forme][1] for forme in listmot]
214 uce_ok = list(set([tuple(val) for line in uce_ok for val in line]))
215 txt = '<h1>Concordancier</h1>'
217 content = ' '+' '.join(corpus.ucis_paras_uces[uce[0]][uce[1]][uce[2]])+' '
218 for form in listmot :
221 forme = ' ' + form + ' '
222 while i < len(content):
223 coordword = content[i:].find(forme)
224 if coordword != -1 and i == 0:
225 txt += '<br><b>' + ' '.join(corpus.ucis[uce[0]][0]) + '</b><br>'
227 sp = ' ' * (avap - coordword)
230 deb = i + coordword - avap
231 if len(content) < i + coordword + avap:
232 fin = len(content) - 1
234 fin = i + coordword + avap
235 txt += '<TT>' + sp + content[deb:fin].replace(forme, '<font color=red>' + forme + '</font>') + '</TT><br>'
236 i += coordword + len(forme)
238 elif coordword != -1 and i != 0 :
240 sp = ' ' * (avap - coordword)
243 deb = i + coordword - avap
244 if len(content) < i + coordword + avap:
245 fin = len(content) - 1
247 fin = i + coordword + avap
248 txt += '<TT>' + sp + content[deb:fin].replace(forme, '<font color=red>' + forme + '</font>') + '</TT><br>'
249 i += coordword + len(forme)
254 win.HtmlPage.SetPage(txt)
257 def OnPopupThree(self, event) :
258 datas = [self.OnGetItemData(self.GetFirstSelected())]
259 last = self.GetFirstSelected()
260 while self.GetNextSelected(last) != -1:
261 last = self.GetNextSelected(last)
262 data = self.OnGetItemData(last)
264 colnames = self.first[2:]
265 rownames = [val[1] for val in datas]
266 table = [[str(val) for val in line[2:]] for line in datas]
267 BarFrame(self.parent.parent, table, colnames, rownames)
269 def ongraphdendro(self, evt) :
270 corpus = self.parent.corpus
271 datas = [self.OnGetItemData(self.GetFirstSelected())]
272 last = self.GetFirstSelected()
273 while self.GetNextSelected(last) != -1:
274 last = self.GetNextSelected(last)
275 data = self.OnGetItemData(last)
277 colnames = self.first[2:]
278 rownames = [val[1] for val in datas]
279 table = [[str(val) for val in line[2:]] for line in datas]
280 tmpgraph = tempfile.mktemp(dir=self.parent.parent.TEMPDIR)
281 BarFrame(self.parent.parent, table, colnames, rownames, tree = corpus.dictpathout['Rdendro'])