...
[iramuteq] / Liste.py
1 # -*- coding: utf-8 -*-
2
3 #----------------------------------------------------------------------------
4 # Name:         ListCtrl.py
5 # Author:       Pierre Ratinaud
6
7
8 #comes from ListCtrl.py from the demo tool of wxPython:
9 # Author:       Robin Dunn & Gary Dumer
10 #
11 # Created:
12 # Copyright:    (c) 1998 by Total Control Software
13 # Licence:      wxWindows license
14 #----------------------------------------------------------------------------
15
16 import os
17 import sys
18 import  wx
19 from dialog import SearchDial, message
20 import wx.lib.mixins.listctrl as listmix 
21 from operator import itemgetter
22 from functions import doconcorde
23 #---------------------------------------------------------------------------
24
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)
28         self.parent = parent
29         self.gparent = gparent
30         self.source = gparent
31         self.dlist = dlist
32         #wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
33         
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)
38         
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"}
41         for k,v in a.items():
42             s="self.%s= self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k,v)
43             exec(s)
44         self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
45
46         tID = wx.NewId()
47
48         
49 #-----------------------------------------------------------------------------------------    
50         self.attr1 = wx.ListItemAttr()
51         self.attr1.SetBackgroundColour((230, 230, 230))
52         self.attr2 = wx.ListItemAttr()
53         self.attr2.SetBackgroundColour("light blue")
54
55         
56         self.InsertColumn(0, _(u'Form').decode('utf8'), wx.LIST_FORMAT_RIGHT)
57         self.InsertColumn(1, _(u'Freq.').decode('utf8'), wx.LIST_FORMAT_RIGHT)
58         self.InsertColumn(2, _(u'POS').decode('utf8'), wx.LIST_FORMAT_RIGHT)
59         #self.InsertColumn(3, '', wx.LIST_FORMAT_RIGHT)
60
61         self.SetColumnWidth(0, 150)   
62         self.SetColumnWidth(1, 100)  
63         self.SetColumnWidth(2, 100)
64         #self.SetColumnWidth(3, wx.LIST_AUTOSIZE)
65
66         self.itemDataMap = dlist
67         self.itemIndexMap = dlist.keys()
68         self.SetItemCount(len(dlist))
69
70         #self.Bind(wx.EVT_SIZE, self.OnSize)
71
72         self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
73         self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self)
74         self.Bind(wx.EVT_LIST_ITEM_ACTIVATED , self.OnPopupTwo, self)
75         # for wxMSW
76         self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
77
78         # for wxGTK
79         self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
80
81         #listmix.ListCtrlAutoWidthMixin.__init__(self)
82         listmix.ColumnSorterMixin.__init__(self, 3)
83         self.SortListItems(1, False)
84         #self.do_greyline()
85
86         #self.currentItem = 0
87
88     def OnGetItemText(self, item, col):
89         index=self.itemIndexMap[item]
90         s = self.itemDataMap[index][col]
91         return s
92
93     def OnGetItemAttr(self, item):
94         #index=self.itemIndexMap[item]
95         #genre=self.itemDataMap[index][2]
96         if item % 2 :
97             return self.attr1
98         else :
99             return self.attr2
100
101     def OnColClick(self, event):
102         pass
103         #self.do_greyline()
104         
105     # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
106     def GetListCtrl(self):
107         return self
108
109     # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
110     def GetSortImages(self):
111         return (self.sm_dn, self.sm_up)
112
113     def SortItems(self,sorter=cmp):
114         items = list(self.itemDataMap.keys())
115         items.sort(sorter)
116         self.itemIndexMap = items
117         
118         # redraw the list
119         self.Refresh()
120
121
122     def OnRightDown(self, event):
123         x = event.GetX()
124         y = event.GetY()
125         item, flags = self.HitTest((x, y))
126
127         if flags & wx.LIST_HITTEST_ONITEM:
128             self.Select(item)
129
130         event.Skip()
131
132
133     def getColumnText(self, index, col):
134         item = self.GetItem(index, col)
135         return item.GetText()
136
137
138     def OnItemSelected(self, event):
139         self.currentItem = event.m_itemIndex
140         event.Skip()
141
142     def onsearch(self, evt) :
143         self.dial = SearchDial(self, self, 0, True)
144         self.dial.CenterOnParent()
145         self.dial.Show()
146         #self.dial.Destroy()
147
148     def OnRightClick(self, event):
149
150         # only do this part the first time so the events are only bound once
151         if not hasattr(self, "popupID1"):
152             self.popupID1 = wx.NewId()
153             self.popupID2 = wx.NewId()
154  #           self.popupID3 = wx.NewId()
155
156             self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
157             self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
158 #            self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
159
160         # make a menu
161         menu = wx.Menu()
162         # add some items
163         menu.Append(self.popupID1, _(u"Associated forms").decode('utf8'))
164         menu.Append(self.popupID2, _(u"Concordance").decode('utf8'))
165 #        menu.Append(self.popupID3, "recharger")
166
167         self.PopupMenu(menu)
168         menu.Destroy()
169
170
171     def OnPopupOne(self, event):
172         corpus = self.gparent.corpus
173         word = self.getColumnText(self.GetFirstSelected(), 0)
174         lems = corpus.getlems()
175         rep = []
176         for forme in lems[word].formes :
177             rep.append([corpus.getforme(forme).forme, corpus.getforme(forme).freq])
178         rep.sort(key = itemgetter(1), reverse = True)
179         items = dict([[i, '<font face="courier">' + '\t:\t'.join([str(val) for val in forme]) + '</font>'] for i, forme in enumerate(rep)])
180         win = message(self, items, _(u"Associated forms").decode('utf8'), (300, 200))
181         #win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
182         #win.HtmlPage.SetPage(win.html)
183         win.Show(True)
184
185     def OnPopupTwo(self, event):
186         corpus = self.gparent.corpus
187         item = self.getColumnText(self.GetFirstSelected(), 0)
188         uce_ok = corpus.getlemuces(item)
189         ucis_txt, ucestxt = doconcorde(corpus, uce_ok, [item])
190         items = dict([[i, '<br><br>'.join([ucis_txt[i], ucestxt[i]])] for i in range(0,len(ucestxt))])
191         win = message(self, items, ' - '.join([_(u"Concordance").decode('utf8'),  "%s" % item]), (800, 500), uceids = uce_ok)
192         #win = message(self, u"Concordancier", (750, 600))
193         #win.html = ('<html>\n<h1>%s</h1>' % item) + '<br>'.join(['<br>'.join([ucis_txt[i], ucestxt[i]]) for i in range(0,len(ucestxt))]) + '\n</html>'
194         #win.HtmlPage.SetPage(win.html)
195         win.Show(True)