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
24 from dialog import SearchDial, message
25 #---------------------------------------------------------------------------
27 #class List(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
28 # def __init__(self, parent, ID, pos=wx.DefaultPosition,
29 # size=wx.DefaultSize, style=0):
30 # wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
31 # listmix.ListCtrlAutoWidthMixin.__init__(self)
33 class ListForSpec(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
34 def __init__(self, parent,gparent, dlist, first, menu = True):
35 wx.ListCtrl.__init__( self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
42 search_id = wx.NewId()
43 self.parent.Bind(wx.EVT_MENU, self.onsearch, id = search_id)
44 self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('F'), search_id)])
45 self.SetAcceleratorTable(self.accel_tbl)
47 self.il = wx.ImageList(16, 16)
48 a={"sm_up":"GO_UP","sm_dn":"GO_DOWN","w_idx":"WARNING","e_idx":"ERROR","i_idx":"QUESTION"}
50 s="self.%s= self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k,v)
52 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
56 self.attr1 = wx.ListItemAttr()
57 self.attr1.SetBackgroundColour((230, 230, 230))
58 self.attr2 = wx.ListItemAttr()
59 self.attr2.SetBackgroundColour("light blue")
65 self.InsertColumn(i,name,wx.LIST_FORMAT_LEFT)
68 self.SetColumnWidth(0, 180)
70 for i in range(1,len(first)-1):
71 self.SetColumnWidth(i, self.checkcolumnwidth(len(first[i]) * 10))
73 self.itemDataMap = dlist
74 self.itemIndexMap = dlist.keys()
75 self.SetItemCount(len(dlist))
77 #listmix.ListCtrlAutoWidthMixin.__init__(self)
78 listmix.ColumnSorterMixin.__init__(self, len(first))
79 self.SortListItems(1, 0)
81 #-----------------------------------------------------------------------------------------
82 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
85 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
88 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
90 #-----------------------------------------------------------------------------------------
92 def checkcolumnwidth(self, width) :
98 def OnGetItemText(self, item, col):
99 index=self.itemIndexMap[item]
100 s = self.itemDataMap[index][col]
103 def OnGetItemAttr(self, item):
110 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
111 def GetListCtrl(self):
114 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
115 def GetSortImages(self):
116 return (self.sm_dn, self.sm_up)
118 def OnRightDown(self, event):
121 item, flags = self.HitTest((x, y))
123 if flags & wx.LIST_HITTEST_ONITEM:
128 def getColumnText(self, index, col):
129 item = self.GetItem(index, col)
130 return item.GetText()
132 def GetItemData(self, item) :
133 index=self.itemIndexMap[item]
134 s = self.itemDataMap[index]
137 def SortItems(self,sorter=cmp):
138 items = list(self.itemDataMap.keys())
140 self.itemIndexMap = items
144 def OnItemSelected(self, event):
145 self.currentItem = event.m_itemIndex
148 def onsearch(self, evt) :
149 self.dial = SearchDial(self, self, 0, True)
150 self.dial.CenterOnParent()
151 self.dial.ShowModal()
154 def OnRightClick(self, event):
156 # only do this part the first time so the events are only bound once
157 if not hasattr(self, "popupID1"):
158 self.popupID1 = wx.NewId()
159 self.popupID2 = wx.NewId()
160 self.popupID3 = wx.NewId()
161 self.popup_Tgen_glob = wx.NewId()
163 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
164 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
165 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
166 self.Bind(wx.EVT_MENU, self.OnTgen_glob, id=self.popup_Tgen_glob)
170 menu.Append(self.popupID1, u"Formes associƩes")
171 menu.Append(self.popupID2, u"Concordancier")
172 menu.Append(self.popupID3, "Graphique")
173 #menu.Append(self.popup_Tgen_glob, "Tgen global")
178 def getselectedwords(self) :
179 words = [self.getColumnText(self.GetFirstSelected(), 0)]
180 last = self.GetFirstSelected()
181 while self.GetNextSelected(last) != -1:
182 last = self.GetNextSelected(last)
183 words.append(self.getColumnText(last, 1))
186 def OnPopupOne(self, event):
187 activenotebook = self.parent.nb.GetSelection()
188 page = self.parent.nb.GetPage(activenotebook)
190 word = self.getselectedwords()[0]
191 lems = corpus.getlems()
193 for forme in lems[word].formes :
194 rep.append([corpus.getforme(forme).forme, corpus.getforme(forme).freq])
195 win = message(self, u"Formes associƩes", (300, 200))
196 win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
197 win.HtmlPage.SetPage(win.html)
200 def OnPopupTwo(self, event):
201 activenotebook = self.parent.nb.GetSelection()
202 page = self.parent.nb.GetPage(activenotebook)
203 item=self.getColumnText(self.GetFirstSelected(), 0)
205 win = message(self, u"Concordancier", (750, 600))
207 listmot = corpus.getlems()[item].formes
208 uce_ok = corpus.getlemuces(item)
209 txt = '<h1>Concordancier</h1>'
210 res = corpus.getconcorde(uce_ok)
212 ucetxt = ' '+uce[1]+' '
213 txt += ' '.join(corpus.ucis[corpus.getucefromid(uce[0]).uci].etoiles) + '<br>'
214 for forme in listmot :
215 forme = corpus.getforme(forme).forme
216 ucetxt = ucetxt.replace(' '+forme+' ', '<font color=red> ' + forme + ' </font>')
217 txt += ucetxt + '<br><br>'
218 win.HtmlPage.SetPage(txt)
221 def getinf(self, txt) :
222 if txt == float('Inf') :
224 elif txt == float('-Inf') :
229 def OnPopupThree(self, event) :
230 datas = [self.GetItemData(self.GetFirstSelected())]
231 last = self.GetFirstSelected()
232 while self.GetNextSelected(last) != -1:
233 last = self.GetNextSelected(last)
234 data = self.GetItemData(last)
236 colnames = self.first[1:]
237 table = [[self.getinf(val) for val in line[1:]] for line in datas]
238 rownames = [val[0] for val in datas]
239 tmpgraph = tempfile.mktemp(dir=self.parent.TEMPDIR)
240 txt = barplot(table, rownames, colnames, self.parent.RscriptsPath['Rgraph'], tmpgraph)
241 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
242 with open(tmpscript,'w') as f :
244 exec_rcode(self.parent.RPath, tmpscript, wait = True)
245 win = MessageImage(self, u"Graphique", size=(700, 500))
246 win.addsaveimage(tmpgraph)
247 txt = "<img src='%s'>" % tmpgraph
248 win.HtmlPage.SetPage(txt)
251 def OnTgen_glob(self, evt) :
252 activenotebook = self.parent.nb.GetSelection()
253 page = self.parent.nb.GetPage(activenotebook)
256 tmpgraph = tempfile.mktemp(dir=self.gparent.parent.TEMPDIR)
259 """ % corpus.dictpathout['RData']
263 for (i in 1:ncol(dmf)) {
264 Tgen.glob <- rbind(Tgen.glob,colSums(dmf[which(specf[,i] > 3),]))
265 tovire <- append(tovire, which(specf[,i] > 3))
267 rownames(Tgen.glob) <- colnames(dmf)
268 Tgen.table <- dmf[-unique(tovire),]
269 Tgen.table<- rbind(Tgen.table, Tgen.glob)
270 spec.Tgen.glob <- AsLexico2(Tgen.table)
271 spec.Tgen.glob <- spec.Tgen.glob[[1]][((nrow(Tgen.table)-ncol(Tgen.table))+1):nrow(Tgen.table),]
274 txt = barplot('', '', '', self.gparent.parent.RscriptsPath['Rgraph'], tmpgraph, intxt = intxt)
275 tmpscript = tempfile.mktemp(dir=self.gparent.parent.TEMPDIR)
276 with open(tmpscript, 'w') as f :
278 exec_rcode(self.gparent.parent.RPath, tmpscript, wait = True)
279 win = MessageImage(self, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
280 win.addsaveimage(tmpgraph)
281 txt = "<img src='%s'>" % tmpgraph
282 win.HtmlPage.SetPage(txt)
287 #class message(wx.Frame):
288 # def __init__(self, *args, **kwds):
289 # # begin wxGlade: MyFrame.__init__
290 # kwds["style"] = wx.DEFAULT_FRAME_STYLE
291 # wx.Frame.__init__(self, *args, **kwds)
292 # #self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)
293 # self.HtmlPage=wx.html.HtmlWindow(self, -1)
294 # if "gtk2" in wx.PlatformInfo:
295 # self.HtmlPage.SetStandardFonts()
296 # self.HtmlPage.SetFonts('Courier','Courier')
299 # self.button_1 = wx.Button(self, -1, "Fermer")
300 # self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.button_1)
301 # self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
305 # def __do_layout(self):
306 # # begin wxGlade: MyFrame.__do_layout
307 # sizer_1 = wx.BoxSizer(wx.VERTICAL)
308 # sizer_2 = wx.BoxSizer(wx.VERTICAL)
309 # sizer_2.Add(self.HtmlPage, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
310 # sizer_2.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
311 # sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
312 # self.SetAutoLayout(True)
313 # self.SetSizer(sizer_1)
317 # def OnCloseMe(self, event):
320 # def OnCloseWindow(self, event):