1 # -*- coding: utf-8 -*-
3 #----------------------------------------------------------------------------
5 # Author: Pierre Ratinaud
8 #comes from ListCtrl.py from the demo tool of wxPython:
9 # Author: Robin Dunn & Gary Dumer
12 # Copyright: (c) 1998 by Total Control Software
13 # Licence: wxWindows license
14 #----------------------------------------------------------------------------
19 from dialog import SearchDial, message
20 import wx.lib.mixins.listctrl as listmix
21 from operator import itemgetter
23 #---------------------------------------------------------------------------
25 class ListPanel(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
26 def __init__(self, parent, gparent, dlist):
27 wx.ListCtrl.__init__( self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
29 self.gparent = gparent
32 #wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
34 search_id = wx.NewId()
35 self.parent.Bind(wx.EVT_MENU, self.onsearch, id = search_id)
36 self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('F'), search_id)])
37 self.SetAcceleratorTable(self.accel_tbl)
39 self.il = wx.ImageList(16, 16)
40 a={"sm_up":"GO_UP","sm_dn":"GO_DOWN","w_idx":"WARNING","e_idx":"ERROR","i_idx":"QUESTION"}
42 s="self.%s= self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k,v)
44 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
49 #-----------------------------------------------------------------------------------------
50 self.attr1 = wx.ListItemAttr()
51 self.attr1.SetBackgroundColour((230, 230, 230))
52 self.attr2 = wx.ListItemAttr()
53 self.attr2.SetBackgroundColour("light blue")
56 self.InsertColumn(0, 'forme', wx.LIST_FORMAT_RIGHT)
57 self.InsertColumn(1, 'nb', wx.LIST_FORMAT_RIGHT)
58 self.InsertColumn(2, 'type', wx.LIST_FORMAT_RIGHT)
59 #self.InsertColumn(3, '', wx.LIST_FORMAT_RIGHT)
61 self.SetColumnWidth(0, 150)
62 self.SetColumnWidth(1, 100)
63 self.SetColumnWidth(2, 100)
64 #self.SetColumnWidth(3, wx.LIST_AUTOSIZE)
66 self.itemDataMap = dlist
67 self.itemIndexMap = dlist.keys()
68 self.SetItemCount(len(dlist))
70 #self.Bind(wx.EVT_SIZE, self.OnSize)
72 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
73 self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self)
75 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
78 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
80 #listmix.ListCtrlAutoWidthMixin.__init__(self)
81 listmix.ColumnSorterMixin.__init__(self, 3)
82 self.SortListItems(1, False)
87 def OnGetItemText(self, item, col):
88 index=self.itemIndexMap[item]
89 s = self.itemDataMap[index][col]
92 def OnGetItemAttr(self, item):
93 #index=self.itemIndexMap[item]
94 #genre=self.itemDataMap[index][2]
100 def OnColClick(self, event):
104 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
105 def GetListCtrl(self):
108 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
109 def GetSortImages(self):
110 return (self.sm_dn, self.sm_up)
112 def SortItems(self,sorter=cmp):
113 items = list(self.itemDataMap.keys())
115 self.itemIndexMap = items
121 def OnRightDown(self, event):
124 item, flags = self.HitTest((x, y))
126 if flags & wx.LIST_HITTEST_ONITEM:
132 def getColumnText(self, index, col):
133 item = self.GetItem(index, col)
134 return item.GetText()
137 def OnItemSelected(self, event):
138 self.currentItem = event.m_itemIndex
141 def onsearch(self, evt) :
142 self.dial = SearchDial(self, self, 0, True)
143 self.dial.CenterOnParent()
147 def OnRightClick(self, event):
149 # only do this part the first time so the events are only bound once
150 if not hasattr(self, "popupID1"):
151 self.popupID1 = wx.NewId()
152 self.popupID2 = wx.NewId()
153 # self.popupID3 = wx.NewId()
155 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
156 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
157 # 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, "recharger")
170 def OnPopupOne(self, event):
171 corpus = self.gparent.corpus
172 word = self.getColumnText(self.GetFirstSelected(), 0)
173 lems = corpus.getlems()
175 for forme in lems[word].formes :
176 rep.append([corpus.getforme(forme).forme, corpus.getforme(forme).freq])
177 rep.sort(key = itemgetter(1), reverse = True)
178 win = message(self, u"Formes associƩes", (300, 200))
179 win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
180 win.HtmlPage.SetPage(win.html)
183 def OnPopupTwo(self, event):
184 corpus = self.gparent.corpus
185 win = message(self, u"Concordancier", (750, 600))
187 item = self.getColumnText(self.GetFirstSelected(), 0)
188 listmot = corpus.getlems()[item].formes
189 #uce_ok = [corpus.formes[corpus.idformes[forme].forme][1] for forme in listmot]
190 uce_ok = corpus.getlemuces(item)#list(set([tuple(val) for line in uce_ok for val in line]))
191 txt = '<h1>Concordancier</h1>'
192 res = corpus.getconcorde(uce_ok)
194 ucetxt = ' '+uce[1]+' '
195 txt += ' '.join(corpus.ucis[corpus.getucefromid(uce[0]).uci].etoiles) + '<br>'
196 for forme in listmot :
197 forme = corpus.getforme(forme).forme
198 ucetxt = ucetxt.replace(' '+forme+' ', '<font color=red> ' + forme + ' </font>')
199 txt += ucetxt + '<br><br>'
200 win.HtmlPage.SetPage(txt)