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, MessageImage
22 from chemins import ffr
23 from PrintRScript import barplot, dendroandbarplot
24 #---------------------------------------------------------------------------
26 class List(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
27 def __init__(self, parent, ID, pos=wx.DefaultPosition,
28 size=wx.DefaultSize, style=0):
29 wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
30 listmix.ListCtrlAutoWidthMixin.__init__(self)
32 class SearchList(wx.Panel, listmix.ColumnSorterMixin):
33 def __init__(self, parent,gparent, dlist,first, nbactives, nbetoiles, add_dendro=True):
37 self.add_dendro=add_dendro
38 self.first = ['id','formes'] + first
39 self.nbactives = nbactives
40 self.nbetoiles = nbetoiles
41 wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
44 self.il = wx.ImageList(16, 16)
46 self.sm_up = self.il.Add(getSmallUpArrowBitmap())
47 self.sm_dn = self.il.Add(getSmallDnArrowBitmap())
51 self.list = List(self, tID,
55 | wx.LC_SORT_ASCENDING
58 self.list.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
61 self.PopulateList(dlist,first)
62 self.Bind(wx.EVT_SIZE, self.OnSize)
64 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
65 self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
68 self.list.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
71 self.list.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
74 self.itemDataMap = dlist
75 listmix.ColumnSorterMixin.__init__(self, len(first)+2)
76 self.SortListItems(1, False)
79 #-----------------------------------------------------------------------------------------
81 def PopulateList(self, dlist,first):
82 first = ['id','formes']+first
83 for i, name in enumerate(first) :
84 self.list.InsertColumn(i, name, wx.LIST_FORMAT_LEFT)
87 index = self.list.InsertStringItem(sys.maxint, '%5i' % key)
88 for i, val in enumerate(dlist[key][1:]) :
89 self.list.SetStringItem(index, i+1, str(dlist[key][i+1]))
90 self.list.SetItemData(index, key)
92 self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
93 for i in range(1,len(first)-1):
94 self.list.SetColumnWidth(i, 130)
96 if self.nbactives + self.nbetoiles != len(dlist) :
98 for i in range(self.nbactives, (len(dlist) - self.nbetoiles)) :
99 item = self.list.GetItem(i)
100 item.SetTextColour(wx.RED)
101 self.list.SetItem(item)
102 for i in range((len(dlist) - self.nbetoiles), len(dlist)) :
103 item = self.list.GetItem(i)
104 item.SetTextColour(wx.BLUE)
105 self.list.SetItem(item)
107 def do_greyline(self):
108 for row in xrange(self.list.GetItemCount()):
110 self.list.SetItemBackgroundColour(row,(230,230,230))
112 self.list.SetItemBackgroundColour(row,wx.WHITE)
115 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
116 def GetListCtrl(self):
119 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
120 def GetSortImages(self):
121 return (self.sm_dn, self.sm_up)
124 def OnRightDown(self, event):
127 item, flags = self.list.HitTest((x, y))
129 if flags & wx.LIST_HITTEST_ONITEM:
130 self.list.Select(item)
135 def getColumnText(self, index, col):
136 item = self.list.GetItem(index, col)
137 return item.GetText()
140 def OnItemSelected(self, event):
141 self.currentItem = event.m_itemIndex
144 def OnRightClick(self, event):
146 # only do this part the first time so the events are only bound once
147 if not hasattr(self, "popupID1"):
148 # self.popupID1 = wx.NewId()
149 #self.popupID2 = wx.NewId()
150 self.popupID3 = wx.NewId()
152 self.id_adddendro = wx.NewId()
153 self.Bind(wx.EVT_MENU, self.ongraphdendro, id = self.id_adddendro)
154 # self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
155 #self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
156 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
162 # menu.Append(self.popupID1, u"Formes associƩes")
163 #menu.Append(self.popupID2, u"Concordancier")
164 menu.Append(self.popupID3, "Graphique")
166 menu.Append(self.id_adddendro, u"Graphique + dendrogramme")
172 def OnPopupOne(self, event):
173 activenotebook = self.parent.nb.GetSelection()
174 page = self.parent.nb.GetPage(activenotebook)
176 word = self.getColumnText(self.list.GetFirstSelected(), 0)
179 for forme in lems[word] :
180 rep.append([forme, corpus.formes[forme][0]])
181 win = message(self, -1, u"Formes associƩes", size=(300, 200), style=wx.DEFAULT_FRAME_STYLE)
182 win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
183 win.HtmlPage.SetPage(win.html)
186 def OnPopupTwo(self, event):
187 activenotebook = self.parent.nb.GetSelection()
188 page = self.parent.nb.GetPage(activenotebook)
189 item=self.getColumnText(self.list.GetFirstSelected(), 0)
191 win = message(self, -1, u"Concordancier", size=(600, 200),style = wx.DEFAULT_FRAME_STYLE)
193 listmot = corpus.lems[item]
194 uce_ok = [corpus.formes[forme][1] for forme in listmot]
195 uce_ok = list(set([tuple(val) for line in uce_ok for val in line]))
196 txt = '<h1>Concordancier</h1>'
198 content = ' '+' '.join(corpus.ucis_paras_uces[uce[0]][uce[1]][uce[2]])+' '
199 for form in listmot :
202 forme = ' ' + form + ' '
203 while i < len(content):
204 coordword = content[i:].find(forme)
205 if coordword != -1 and i == 0:
206 txt += '<br><b>' + ' '.join(corpus.ucis[uce[0]][0]) + '</b><br>'
208 sp = ' ' * (avap - coordword)
211 deb = i + coordword - avap
212 if len(content) < i + coordword + avap:
213 fin = len(content) - 1
215 fin = i + coordword + avap
216 txt += '<TT>' + sp + content[deb:fin].replace(forme, '<font color=red>' + forme + '</font>') + '</TT><br>'
217 i += coordword + len(forme)
219 elif coordword != -1 and i != 0 :
221 sp = ' ' * (avap - coordword)
224 deb = i + coordword - avap
225 if len(content) < i + coordword + avap:
226 fin = len(content) - 1
228 fin = i + coordword + avap
229 txt += '<TT>' + sp + content[deb:fin].replace(forme, '<font color=red>' + forme + '</font>') + '</TT><br>'
230 i += coordword + len(forme)
235 win.HtmlPage.SetPage(txt)
238 def OnPopupThree(self, event) :
239 datas = [self.dlist[self.list.GetItemData(self.list.GetFirstSelected())]]
240 last = self.list.GetFirstSelected()
241 while self.list.GetNextSelected(last) != -1:
242 last = self.list.GetNextSelected(last)
243 data = self.dlist[self.list.GetItemData(last)]
245 colnames = self.first[2:]
246 rownames = [val[1] for val in datas]
247 table = [[str(val) for val in line[2:]] for line in datas]
248 tmpgraph = tempfile.mktemp(dir=self.parent.parent.TEMPDIR)
249 txt = barplot(table, rownames, colnames, self.parent.parent.RscriptsPath['Rgraph'], tmpgraph)
250 tmpscript = tempfile.mktemp(dir=self.parent.parent.TEMPDIR)
251 with open(tmpscript,'w') as f :
253 exec_rcode(self.parent.parent.RPath, tmpscript, wait = True)
254 win = MessageImage(self, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
255 win.addsaveimage(tmpgraph)
256 txt = "<img src='%s'>" % tmpgraph
257 win.HtmlPage.SetPage(txt)
260 def ongraphdendro(self, evt) :
261 corpus = self.parent.corpus
262 datas = [self.dlist[self.list.GetItemData(self.list.GetFirstSelected())]]
263 last = self.list.GetFirstSelected()
264 while self.list.GetNextSelected(last) != -1:
265 last = self.list.GetNextSelected(last)
266 data = self.dlist[self.list.GetItemData(last)]
268 colnames = self.first[2:]
269 rownames = [val[1] for val in datas]
270 table = [[str(val) for val in line[2:]] for line in datas]
271 tmpgraph = tempfile.mktemp(dir=self.parent.parent.TEMPDIR)
272 txt = dendroandbarplot(table, rownames, colnames, self.parent.parent.RscriptsPath['Rgraph'], tmpgraph, dendro=corpus.dictpathout['Rdendro'])
273 tmpscript = tempfile.mktemp(dir=self.parent.parent.TEMPDIR)
274 with open(tmpscript,'w') as f :
276 exec_rcode(self.parent.parent.RPath, tmpscript, wait = True)
277 win = MessageImage(self, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
278 win.addsaveimage(tmpgraph)
279 txt = "<img src='%s'>" % tmpgraph
280 win.HtmlPage.SetPage(txt)
283 def OnSize(self, event):
284 w,h = self.GetClientSizeTuple()
285 self.list.SetDimensions(0, 0, w, h)
287 def OnColClick(self,event):
290 class message(wx.Frame):
291 def __init__(self, *args, **kwds):
292 # begin wxGlade: MyFrame.__init__
293 kwds["style"] = wx.DEFAULT_FRAME_STYLE
294 wx.Frame.__init__(self, *args, **kwds)
295 self.HtmlPage=wx.html.HtmlWindow(self, -1)
296 if "gtk2" in wx.PlatformInfo:
297 self.HtmlPage.SetStandardFonts()
298 self.HtmlPage.SetFonts('Courier','Courier')
301 self.button_1 = wx.Button(self, -1, "Fermer")
302 self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.button_1)
303 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
307 def __do_layout(self):
308 # begin wxGlade: MyFrame.__do_layout
309 sizer_1 = wx.BoxSizer(wx.VERTICAL)
310 sizer_2 = wx.BoxSizer(wx.VERTICAL)
311 sizer_2.Add(self.HtmlPage, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
312 sizer_2.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
313 sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
314 self.SetAutoLayout(True)
315 self.SetSizer(sizer_1)
319 def OnCloseMe(self, event):
322 def OnCloseWindow(self, event):
326 def getSmallUpArrowData():
328 '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
329 \x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
330 \x00\x00<IDAT8\x8dcddbf\xa0\x040Q\xa4{h\x18\xf0\xff\xdf\xdf\xffd\x1b\x00\xd3\
331 \x8c\xcf\x10\x9c\x06\xa0k\xc2e\x08m\xc2\x00\x97m\xd8\xc41\x0c \x14h\xe8\xf2\
332 \x8c\xa3)q\x10\x18\x00\x00R\xd8#\xec\xb2\xcd\xc1Y\x00\x00\x00\x00IEND\xaeB`\
335 def getSmallUpArrowBitmap():
336 return wx.BitmapFromImage(getSmallUpArrowImage())
338 def getSmallUpArrowImage():
339 stream = cStringIO.StringIO(getSmallUpArrowData())
340 return wx.ImageFromStream(stream)
342 #----------------------------------------------------------------------
343 def getSmallDnArrowData():
345 "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
346 \x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
347 \x00\x00HIDAT8\x8dcddbf\xa0\x040Q\xa4{\xd4\x00\x06\x06\x06\x06\x06\x16t\x81\
348 \xff\xff\xfe\xfe'\xa4\x89\x91\x89\x99\x11\xa7\x0b\x90%\ti\xc6j\x00>C\xb0\x89\
349 \xd3.\x10\xd1m\xc3\xe5*\xbc.\x80i\xc2\x17.\x8c\xa3y\x81\x01\x00\xa1\x0e\x04e\
350 ?\x84B\xef\x00\x00\x00\x00IEND\xaeB`\x82"
352 def getSmallDnArrowBitmap():
353 return wx.BitmapFromImage(getSmallDnArrowImage())
355 def getSmallDnArrowImage():
356 stream = cStringIO.StringIO(getSmallDnArrowData())
357 return wx.ImageFromStream(stream)