1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
7 # ----------------------------------------------------------------------------
9 # comes from ListCtrl.py from the demo tool of wxPython:
10 # Author: Robin Dunn & Gary Dumer
12 # Copyright: (c) 1998 by Total Control Software
13 # Licence: wxWindows license
14 # ----------------------------------------------------------------------------
16 #------------------------------------
17 # import des modules python
18 #------------------------------------
27 #------------------------------------
28 # import des modules wx
29 #------------------------------------
32 #------------------------------------
33 # import des fichiers du projet
34 #------------------------------------
35 from dialog import SearchDial, message
36 import wx.lib.mixins.listctrl as listmix
37 from functions import doconcorde
40 # ---------------------------------------------------------------------------
41 # wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
42 # ---------------------------------------------------------------------------
45 class ListPanel(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.ColumnSorterMixin):
46 def __init__(self, parent, gparent, dlist):
47 wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT | wx.LC_VIRTUAL | wx.LC_HRULES | wx.LC_VRULES)
49 self.gparent = gparent
52 search_id = wx.NewId()
53 self.parent.Bind(wx.EVT_MENU, self.onsearch, id=search_id)
54 self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('F'), search_id)])
55 self.SetAcceleratorTable(self.accel_tbl)
56 self.il = wx.ImageList(16, 16)
57 a = {"sm_up": "GO_UP", "sm_dn": "GO_DOWN", "w_idx": "WARNING", "e_idx": "ERROR", "i_idx": "QUESTION"}
58 for k, v in list(a.items()):
59 s = "self.%s= self.il.Add(wx.ArtProvider.GetBitmap(wx.ART_%s,wx.ART_TOOLBAR,(16,16)))" % (k, v)
61 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
63 # -----------------------------------------------------------------------------------------
64 self.attr1 = wx.ListItemAttr()
65 self.attr1.SetBackgroundColour((230, 230, 230))
66 self.attr2 = wx.ListItemAttr()
67 self.attr2.SetBackgroundColour("light blue")
68 self.InsertColumn(0, _('Form'), wx.LIST_FORMAT_RIGHT)
69 self.InsertColumn(1, _('Freq.'), wx.LIST_FORMAT_RIGHT)
70 self.InsertColumn(2, _('POS'), wx.LIST_FORMAT_RIGHT)
71 # self.InsertColumn(3, '', wx.LIST_FORMAT_RIGHT)
72 self.SetColumnWidth(0, 150)
73 self.SetColumnWidth(1, 100)
74 self.SetColumnWidth(2, 100)
75 # self.SetColumnWidth(3, wx.LIST_AUTOSIZE)
76 self.itemDataMap = dlist
77 self.itemIndexMap = list(dlist.keys())
78 self.SetItemCount(len(dlist))
79 # self.Bind(wx.EVT_SIZE, self.OnSize)
80 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self)
81 self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self)
82 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnPopupTwo, self)
84 self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
86 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
87 # listmix.ListCtrlAutoWidthMixin.__init__(self)
88 listmix.ColumnSorterMixin.__init__(self, 3)
89 self.SortListItems(1, False) #indice de la colonne pour le tri + sens du tri
91 # self.currentItem = 0
93 def OnGetItemColumnImage(self, item, col):
96 def OnGetItemImage(self, item):
99 def OnGetItemText(self, item, col):
100 index = self.itemIndexMap[item]
101 s = self.itemDataMap[index][col]
102 if isinstance(s, (int, float)):
105 return s #modification pour python 3
107 def OnGetItemAttr(self, item):
108 # index=self.itemIndexMap[item]
109 # genre=self.itemDataMap[index][2]
115 def OnColClick(self, event):
119 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
120 def GetListCtrl(self):
123 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
124 def GetSortImages(self):
125 return (self.sm_dn, self.sm_up)
127 # anciennement : def SortItems(self,sorter=cmp), mais plus 'cmp' en python 3
128 def SortItems(self, sorter=None):
129 listTemp = sorted(self.itemDataMap.items(),
130 key=lambda x:x[1][self._col], reverse= (self._colSortFlag[self._col]!=True))
131 dlist = dict([[line[0],line[1]] for line in listTemp])
132 self.itemDataMap = dlist
133 self.itemIndexMap = list(dlist.keys())
134 self.Refresh() # redraw the list
136 def OnRightDown(self, event):
139 item, flags = self.HitTest((x, y))
140 if flags & wx.LIST_HITTEST_ONITEM:
144 def getColumnText(self, index, col):
145 item = self.GetItem(index, col)
146 return item.GetText()
148 def OnItemSelected(self, event):
149 self.currentItem = event.GetIndex()
152 def onsearch(self, evt):
153 self.dial = SearchDial(self, self, 0, True)
154 self.dial.CenterOnParent()
156 # self.dial.Destroy()
158 def OnRightClick(self, event):
159 # only do this part the first time so the events are only bound once
160 if not hasattr(self, "popupID1"):
161 self.popupID1 = wx.NewId()
162 self.popupID2 = wx.NewId()
163 # self.popupID3 = wx.NewId()
164 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
165 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
166 # self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
170 menu.Append(self.popupID1, _("Associated forms"))
171 menu.Append(self.popupID2, _("Concordance"))
172 #menu.Append(self.popupID3, "recharger")
176 def OnPopupOne(self, event):
177 corpus = self.gparent.corpus
178 word = self.getColumnText(self.GetFirstSelected(), 0)
179 lems = corpus.getlems()
181 for forme in lems[word].formes:
182 rep.append([corpus.getforme(forme).forme, corpus.getforme(forme).freq])
183 rep.sort( key= lambda x:x[1], reverse=True)
185 [[i, '<font face="courier">' + '\t:\t'.join([str(val) for val in forme]) + '</font>'] for i, forme in
187 win = message(self, items, _("Associated forms"), (300, 200))
188 # win.html = '<html>\n' + '<br>'.join([' : '.join([str(val) for val in forme]) for forme in rep]) + '\n</html>'
189 # win.HtmlPage.SetPage(win.html)
192 def OnPopupTwo(self, event):
193 corpus = self.gparent.corpus
194 item = self.getColumnText(self.GetFirstSelected(), 0)
195 uce_ok = corpus.getlemuces(item)
196 ucis_txt, ucestxt = doconcorde(corpus, uce_ok, [item])
197 items = dict([[i, '<br><br>'.join([ucis_txt[i], ucestxt[i]])] for i in range(0, len(ucestxt))])
198 win = message(self, items, ' - '.join([_("Concordance"), "%s" % item]), (800, 500), uceids=uce_ok)
199 # win = message(self, u"Concordancier", (750, 600))
200 # 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>'
201 # win.HtmlPage.SetPage(win.html)