multisplit
[iramuteq] / Liste.py
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
5 #License: GNU/GPL
6
7 # ----------------------------------------------------------------------------
8 # Name:         ListCtrl.py
9 # comes from ListCtrl.py from the demo tool of wxPython:
10 # Author:       Robin Dunn & Gary Dumer
11 # Created:
12 # Copyright:    (c) 1998 by Total Control Software
13 # Licence:      wxWindows license
14 # ----------------------------------------------------------------------------
15
16 #------------------------------------
17 # import des modules python
18 #------------------------------------
19 import os
20 import sys
21 import operator
22
23 import langue
24 langue.run()
25
26
27 #------------------------------------
28 # import des modules wx
29 #------------------------------------
30 import wx
31
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
38
39
40 # ---------------------------------------------------------------------------
41  # wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
42 # ---------------------------------------------------------------------------
43
44
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)
48         self.parent = parent
49         self.gparent = gparent
50         self.source = gparent
51         self.dlist = dlist
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)
60             exec(s)
61         self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
62         tID = wx.NewId()
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)
83         # for wxMSW
84         self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
85         # for wxGTK
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
90         # self.do_greyline()
91         # self.currentItem = 0
92
93     def OnGetItemColumnImage(self, item, col):
94         return -1
95
96     def OnGetItemImage(self, item):
97         pass
98
99     def OnGetItemText(self, item, col):
100         index = self.itemIndexMap[item]
101         s = self.itemDataMap[index][col]
102         if isinstance(s, (int, float)):
103             return str(s)
104         else:
105             return s #modification pour python 3
106
107     def OnGetItemAttr(self, item):
108         # index=self.itemIndexMap[item]
109         # genre=self.itemDataMap[index][2]
110         if item % 2:
111             return self.attr1
112         else:
113             return self.attr2
114
115     def OnColClick(self, event):
116         pass
117         # self.do_greyline()
118
119     # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
120     def GetListCtrl(self):
121         return self
122
123     # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
124     def GetSortImages(self):
125         return (self.sm_dn, self.sm_up)
126
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
135
136     def OnRightDown(self, event):
137         x = event.GetX()
138         y = event.GetY()
139         item, flags = self.HitTest((x, y))
140         if flags & wx.LIST_HITTEST_ONITEM:
141             self.Select(item)
142         event.Skip()
143
144     def getColumnText(self, index, col):
145         item = self.GetItem(index, col)
146         return item.GetText()
147
148     def OnItemSelected(self, event):
149         self.currentItem = event.GetIndex()
150         event.Skip()
151
152     def onsearch(self, evt):
153         self.dial = SearchDial(self, self, 0, True)
154         self.dial.CenterOnParent()
155         self.dial.Show()
156         # self.dial.Destroy()
157
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)
167         # make a menu
168         menu = wx.Menu()
169         # add some items
170         menu.Append(self.popupID1, _("Associated forms"))
171         menu.Append(self.popupID2, _("Concordance"))
172         #menu.Append(self.popupID3, "recharger")
173         self.PopupMenu(menu)
174         menu.Destroy()
175
176     def OnPopupOne(self, event):
177         corpus = self.gparent.corpus
178         word = self.getColumnText(self.GetFirstSelected(), 0)
179         lems = corpus.getlems()
180         rep = []
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)
184         items = dict(
185             [[i, '<font face="courier">' + '\t:\t'.join([str(val) for val in forme]) + '</font>'] for i, forme in
186              enumerate(rep)])
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)
190         win.Show(True)
191
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)
202         win.Show(True)