from mac
[iramuteq] / tree.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2012, Pierre Ratinaud
5 #Lisense: GNU GPL
6
7 import wx
8 import wx.lib.agw.customtreectrl as CT
9 import logging
10 from openanalyse import OpenAnalyse
11 from corpusNG import Corpus, copycorpus
12 from functions import DoConf
13
14
15 log = logging.getLogger('iramuteq.tree')
16 #from agw import customtreectrl as CT
17
18 class InfoDialog ( wx.Dialog ):
19     
20     def __init__( self, parent, txt, parametres ):
21         wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Informations", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE )
22         
23         self.SetSizeHintsSz( wx.Size( 400,200 ), wx.DefaultSize )
24         
25         bSizer1 = wx.BoxSizer( wx.VERTICAL )
26         
27         self.m_panel2 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
28         bSizer2 = wx.BoxSizer( wx.VERTICAL )
29         
30         self.m_staticText4 = wx.StaticText( self.m_panel2, wx.ID_ANY, txt, wx.DefaultPosition, wx.DefaultSize, 0 )
31         self.m_staticText4.Wrap( -1 )
32         bSizer2.Add( self.m_staticText4, 0, wx.ALL, 5 )
33         
34         
35         self.m_panel2.SetSizer( bSizer2 )
36         self.m_panel2.Layout()
37         bSizer2.Fit( self.m_panel2 )
38         bSizer1.Add( self.m_panel2, 0, wx.EXPAND |wx.ALL, 5 )
39         
40         self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
41         fgSizer1 = wx.FlexGridSizer( 0, 2, 0, 0 )
42         fgSizer1.SetFlexibleDirection( wx.BOTH )
43         fgSizer1.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
44         
45         for val in parametres :
46             fgSizer1.Add( wx.StaticText( self.m_panel1, wx.ID_ANY, val[0], wx.DefaultPosition, wx.DefaultSize, 0 ), 0, wx.ALL, 0)
47             fgSizer1.Add( wx.StaticText( self.m_panel1, wx.ID_ANY, val[1], wx.DefaultPosition, wx.DefaultSize, 0 ), 0, wx.ALL, 0)
48             fgSizer1.Add( wx.StaticLine( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL ), 0, wx.EXPAND |wx.ALL, 0)
49             fgSizer1.Add( wx.StaticLine( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL ), 0, wx.EXPAND|wx.ALL, 0)
50
51         self.m_panel1.SetSizer( fgSizer1 )
52         self.m_panel1.Layout()
53         fgSizer1.Fit( self.m_panel1 )
54         bSizer1.Add( self.m_panel1, 0, wx.EXPAND |wx.ALL, 3 )
55         
56         m_sdbSizer1 = wx.StdDialogButtonSizer()
57         self.m_sdbSizer1OK = wx.Button( self, wx.ID_OK )
58         m_sdbSizer1.AddButton( self.m_sdbSizer1OK )
59         m_sdbSizer1.Realize();
60         
61         bSizer1.Add( m_sdbSizer1, 0, wx.EXPAND, 5 )
62         
63         
64         self.SetSizer( bSizer1 )
65         self.Layout()
66         bSizer1.Fit( self )
67         
68         self.Centre( wx.BOTH )
69     
70     def __del__( self ):
71         pass
72
73
74 class LeftTree(CT.CustomTreeCtrl):
75
76     def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
77                  size=wx.DefaultSize,
78                  style=wx.SUNKEN_BORDER|wx.WANTS_CHARS,
79                  agwStyle=CT.TR_HAS_BUTTONS|CT.TR_HAS_VARIABLE_ROW_HEIGHT):
80
81         CT.CustomTreeCtrl.__init__(self, parent, id, pos, size, style, agwStyle)
82         self.log = log
83         alldata = dir(CT)
84         treestyles = []
85         events = []
86         for data in alldata:
87             if data.startswith("TR_"):
88                 treestyles.append(data)
89             elif data.startswith("EVT_"):
90                 events.append(data)
91         self.parent = parent
92         self.events = events
93         self.styles = treestyles
94         self.item = None
95         
96         il = wx.ImageList(16, 16)
97
98 #        for items in ArtIDs[1:-1]:
99 #            bmp = wx.ArtProvider_GetBitmap(eval(items), wx.ART_TOOLBAR, (16, 16))
100 #            il.Add(bmp)
101
102 #        smileidx = il.Add(images.Smiles.GetBitmap())
103 #        numicons = il.GetImageCount()
104
105         #self.AssignImageList(il)
106         self.count = 0
107         self.log = log
108
109         self.history = parent.history
110         self.h = self.history.history
111         self.root = self.AddRoot("Iramuteq")
112
113         if not(self.GetAGWWindowStyleFlag() & CT.TR_HIDE_ROOT):
114             self.SetPyData(self.root, None)
115             self.SetItemImage(self.root, 24, CT.TreeItemIcon_Normal)
116             self.SetItemImage(self.root, 13, CT.TreeItemIcon_Expanded)
117
118         #textctrl = wx.TextCtrl(self, -1, "I Am A Simple\nMultiline wx.TexCtrl", style=wx.TE_MULTILINE)
119         #self.gauge = wx.Gauge(self, -1, 50, style=wx.GA_HORIZONTAL|wx.GA_SMOOTH)
120         #self.gauge.SetValue(0)
121         #combobox = wx.ComboBox(self, -1, choices=["That", "Was", "A", "Nice", "Holyday!"], style=wx.CB_READONLY|wx.CB_DROPDOWN)
122
123         #textctrl.Bind(wx.EVT_CHAR, self.OnTextCtrl)
124         #combobox.Bind(wx.EVT_COMBOBOX, self.OnComboBox)
125         #lenArtIds = len(ArtIDs) - 2
126         for x in range(len(self.history.corpora)) :
127             if 'corpus_name' in self.h[self.history.order[x]] :
128                 key = 'corpus_name'
129             else :
130                 key = 'name'
131             child = self.AppendItem(self.root, self.h[self.history.order[x]][key])
132             #if x == 1:
133             #    child = self.AppendItem(self.root, "Item %d" % x + "\nHello World\nHappy wxPython-ing!")
134             #    self.SetItemBold(child, True)
135             #else:
136             #    child = self.AppendItem(self.root, "Item %d" % x)
137             self.SetPyData(child, self.h[self.history.order[x]])
138             self.SetItemImage(child, 24, CT.TreeItemIcon_Normal)
139             self.SetItemImage(child, 13, CT.TreeItemIcon_Expanded)
140
141             #if random.randint(0, 3) == 0:
142             #    self.SetItemLeftImage(child, random.randint(0, lenArtIds))
143
144             #for y in range(5):
145             #    if y == 0 and x == 1:
146             #        last = self.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)), ct_type=2, wnd=self.gauge)
147             #    elif y == 1 and x == 2:
148             #        last = self.AppendItem(child, "Item %d-%s" % (x, chr(ord("a")+y)), ct_type=1, wnd=textctrl)
149             #        if random.randint(0, 3) == 1:
150             #            self.SetItem3State(last, True)
151             #            
152             #    elif 2 < y < 4:
153             #        last = self.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)))
154             #    elif y == 4 and x == 1:
155             #        last = self.AppendItem(child, "item %d-%s" % (x, chr(ord("a")+y)), wnd=combobox)
156             #    else:
157             if 'corpus_name' in self.h[self.history.order[x]] :
158                 if 'analyses' in self.h[self.history.order[x]] :
159                     for y in self.h[self.history.order[x]]['analyses'] :
160                         last = self.AppendItem(child, y['name'], ct_type=0)
161                             
162                         self.SetPyData(last, y)
163                         self.SetItemImage(last, 24, CT.TreeItemIcon_Normal)
164                         self.SetItemImage(last, 13, CT.TreeItemIcon_Expanded)
165     
166             #    if random.randint(0, 3) == 0:
167             #        self.SetItemLeftImage(last, random.randint(0, lenArtIds))
168                     
169             #for z in range(len(self.history[x]) - 1):
170
171 #                if z > 2:
172 #                    item = self.AppendItem(last,  "item %d-%s-%d" % (x, chr(ord("a")+y), z), ct_type=0)
173 #                    #if random.randint(0, 3) == 1:
174 #                    #    self.SetItem3State(item, True)
175 #                elif 0 < z <= 2:
176 #                    item = self.AppendItem(last,  "item %d-%s-%d" % (x, chr(ord("a")+y), z), ct_type=0)
177 #                elif z == 0:
178 #                    item = self.AppendItem(last,  "item %d-%s-%d" % (x, chr(ord("a")+y), z))
179 #                    #self.SetItemHyperText(item, True)
180 #                self.SetPyData(item, None)
181 #                self.SetItemImage(item, 28, CT.TreeItemIcon_Normal)
182 #                self.SetItemImage(item, 28, CT.TreeItemIcon_Selected)
183
184              #       if random.randint(0, 3) == 0:
185              #           self.SetItemLeftImage(item, random.randint(0, lenArtIds))
186
187         self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDClick)
188         #self.Bind(wx.EVT_IDLE, self.OnIdle)
189
190         self.eventdict = {'EVT_TREE_BEGIN_DRAG': self.OnBeginDrag, 'EVT_TREE_BEGIN_LABEL_EDIT': self.OnBeginEdit,
191                           'EVT_TREE_BEGIN_RDRAG': self.OnBeginRDrag, 'EVT_TREE_DELETE_ITEM': self.OnDeleteItem,
192                           'EVT_TREE_END_DRAG': self.OnEndDrag, 'EVT_TREE_END_LABEL_EDIT': self.OnEndEdit,
193                           'EVT_TREE_ITEM_ACTIVATED': self.OnActivate, 'EVT_TREE_ITEM_CHECKED': self.OnItemCheck,
194                           'EVT_TREE_ITEM_CHECKING': self.OnItemChecking, 'EVT_TREE_ITEM_COLLAPSED': self.OnItemCollapsed,
195                           'EVT_TREE_ITEM_COLLAPSING': self.OnItemCollapsing, 'EVT_TREE_ITEM_EXPANDED': self.OnItemExpanded,
196                           'EVT_TREE_ITEM_EXPANDING': self.OnItemExpanding, 'EVT_TREE_ITEM_GETTOOLTIP': self.OnToolTip,
197                           'EVT_TREE_ITEM_MENU': self.OnItemMenu, 'EVT_TREE_ITEM_RIGHT_CLICK': self.OnRightDown,
198                           'EVT_TREE_KEY_DOWN': self.OnKey, 'EVT_TREE_SEL_CHANGED': self.OnSelChanged,
199                           'EVT_TREE_SEL_CHANGING': self.OnSelChanging, "EVT_TREE_ITEM_HYPERLINK": self.OnHyperLink}
200
201         mainframe = wx.GetTopLevelParent(self)
202         
203         if not hasattr(mainframe, "leftpanel"):
204             self.Bind(CT.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded)
205             self.Bind(CT.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed)
206             self.Bind(CT.EVT_TREE_SEL_CHANGED, self.OnSelChanged)
207             self.Bind(CT.EVT_TREE_SEL_CHANGING, self.OnSelChanging)
208             self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
209             self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
210         else:
211             for combos in mainframe.treeevents:
212                 self.BindEvents(combos)
213
214         if hasattr(mainframe, "leftpanel"):
215             self.ChangeStyle(mainframe.treestyles)
216
217         if not(self.GetAGWWindowStyleFlag() & CT.TR_HIDE_ROOT):
218             self.SelectItem(self.root)
219             self.Expand(self.root)
220
221
222     def BindEvents(self, choice, recreate=False):
223
224         value = choice.GetValue()
225         text = choice.GetLabel()
226         
227         evt = "CT." + text
228         binder = self.eventdict[text]
229
230         if value == 1:
231             if evt == "CT.EVT_TREE_BEGIN_RDRAG":
232                 self.Bind(wx.EVT_RIGHT_DOWN, None)
233                 self.Bind(wx.EVT_RIGHT_UP, None)
234             self.Bind(eval(evt), binder)
235         else:
236             self.Bind(eval(evt), None)
237             if evt == "CT.EVT_TREE_BEGIN_RDRAG":
238                 self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
239                 self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
240
241
242     def ChangeStyle(self, combos):
243
244         style = 0
245         for combo in combos:
246             if combo.GetValue() == 1:
247                 style = style | eval("CT." + combo.GetLabel())
248
249         if self.GetAGWWindowStyleFlag() != style:
250             self.SetAGWWindowStyleFlag(style)
251             
252
253     def OnCompareItems(self, item1, item2):
254         
255         t1 = self.GetItemText(item1)
256         t2 = self.GetItemText(item2)
257         
258
259         if t1 < t2:
260             return -1
261         if t1 == t2:
262             return 0
263
264         return 1
265
266     
267     def OnIdle(self, event):
268
269     #    if self.gauge:
270     #        try:
271     #            if self.gauge.IsEnabled() and self.gauge.IsShown():
272     #                self.count = self.count + 1
273
274     #                if self.count >= 50:
275     #                    self.count = 0
276
277     #                self.gauge.SetValue(self.count)
278
279     #        except:
280     #            self.gauge = None
281
282         event.Skip()
283
284
285     def CloseItem(self, itemParent = None, uuid = None) :
286         if itemParent is None :
287             itemParent = self.root
288         child, cookie = self.GetFirstChild(itemParent)
289         while child :
290             pydata = self.GetPyData(child)
291             if pydata['uuid'] == uuid :
292                 self.SetItemBold(child, False)
293                 break
294             self.CloseItem(child, uuid)
295             child, cookie = self.GetNextChild(itemParent, cookie)
296
297     def GiveFocus(self, itemParent = None, uuid = None) :
298         if itemParent is None :
299             itemParent = self.root
300         child, cookie = self.GetFirstChild(itemParent)
301         while child :
302             pydata = self.GetPyData(child)
303             if pydata['uuid'] == uuid :
304                 self.SelectItem(child)
305                 break
306             self.GiveFocus(child, uuid)
307             child, cookie = self.GetNextChild(itemParent, cookie)
308
309     def OnRightDown(self, event):
310         
311         pt = event.GetPosition()
312         item, flags = self.HitTest(pt)
313
314         if item:
315             self.item = item
316             #self.log.info("OnRightClick: %s, %s, %s" % (self.GetItemText(item), type(item), item.__class__) + "\n")
317             self.SelectItem(item)
318
319
320     def OnRightUp(self, event):
321
322         item = self.item
323         
324         if not item:
325             event.Skip()
326             return
327
328         if not self.IsItemEnabled(item):
329             event.Skip()
330             return
331
332         # Item Text Appearance
333         ishtml = self.IsItemHyperText(item)
334         back = self.GetItemBackgroundColour(item)
335         fore = self.GetItemTextColour(item)
336         isbold = self.IsBold(item)
337         font = self.GetItemFont(item)
338
339         # Icons On Item
340         normal = self.GetItemImage(item, CT.TreeItemIcon_Normal)
341         selected = self.GetItemImage(item, CT.TreeItemIcon_Selected)
342         expanded = self.GetItemImage(item, CT.TreeItemIcon_Expanded)
343         selexp = self.GetItemImage(item, CT.TreeItemIcon_SelectedExpanded)
344
345         # Enabling/Disabling Windows Associated To An Item
346         haswin = self.GetItemWindow(item)
347
348         # Enabling/Disabling Items
349         enabled = self.IsItemEnabled(item)
350
351         # Generic Item's Info
352         children = self.GetChildrenCount(item)
353         itemtype = self.GetItemType(item)
354         text = self.GetItemText(item)
355         pydata = self.GetPyData(item)
356         
357         self.current = item
358         self.itemdict = {"ishtml": ishtml, "back": back, "fore": fore, "isbold": isbold,
359                          "font": font, "normal": normal, "selected": selected, "expanded": expanded,
360                          "selexp": selexp, "haswin": haswin, "children": children,
361                          "itemtype": itemtype, "text": text, "pydata": pydata, "enabled": enabled}
362         
363         menu = wx.Menu()
364         info = menu.Append(wx.ID_ANY, "Informations")
365         menu.AppendSeparator()
366
367         if 'corpus_name' in pydata :
368             stat = menu.Append(wx.ID_ANY, u"Statistiques")
369             spec = menu.Append(wx.ID_ANY, u"Spécificté et AFC")
370             classification = wx.Menu()
371             alceste = classification.Append(wx.ID_ANY, u"Méthode ALCESTE")
372             pam = classification.Append(wx.ID_ANY, u"Par matrice des distances")
373             menu.AppendMenu(-1, u"Classification", classification)
374             simi = menu.Append(wx.ID_ANY, u"Analyse de similitude")
375             wdc = menu.Append(wx.ID_ANY, u"Nuage de mots")
376             menu.AppendSeparator()
377             self.Bind(wx.EVT_MENU, self.OnAlceste, alceste)
378             self.Bind(wx.EVT_MENU, self.OnPam, pam)
379             self.Bind(wx.EVT_MENU, self.OnStat, stat)
380             self.Bind(wx.EVT_MENU, self.OnSpec, spec)
381             self.Bind(wx.EVT_MENU, self.OnSimiTxt, simi)
382             self.Bind(wx.EVT_MENU, self.OnWordCloud, wdc)
383
384         itemdelete = menu.Append(wx.ID_ANY, "Supprimer de l'historique")
385         if item == self.GetRootItem():
386             itemdelete.Enable(False)
387         #item11 = menu.Append(wx.ID_ANY, "Prepend An Item")
388         #item12 = menu.Append(wx.ID_ANY, "Append An Item")
389
390         #self.Bind(wx.EVT_MENU, self.OnItemBackground, item1)
391         #self.Bind(wx.EVT_MENU, self.OnItemForeground, item2)
392         #self.Bind(wx.EVT_MENU, self.OnItemBold, item3)
393         #self.Bind(wx.EVT_MENU, self.OnItemFont, item4)
394         #self.Bind(wx.EVT_MENU, self.OnItemHyperText, item5)
395         #self.Bind(wx.EVT_MENU, self.OnEnableWindow, item6)
396         #self.Bind(wx.EVT_MENU, self.OnDisableItem, item7)
397         #self.Bind(wx.EVT_MENU, self.OnItemIcons, item8)
398         self.Bind(wx.EVT_MENU, self.OnItemInfo, info)
399         self.Bind(wx.EVT_MENU, self.OnItemDelete, itemdelete)
400         #self.Bind(wx.EVT_MENU, self.OnItemPrepend, item11)
401         #self.Bind(wx.EVT_MENU, self.OnItemAppend, item12)
402         
403         self.PopupMenu(menu)
404         menu.Destroy()
405
406     def getcorpus(self, itemdict):
407         if itemdict['pydata']['uuid'] in self.parent.history.openedcorpus :
408             return copycorpus(self.parent.history.openedcorpus[itemdict['pydata']['uuid']])
409         else :
410             return Corpus(self.parent, parametres = DoConf(itemdict['pydata']['ira']).getoptions('corpus'), read = True)
411
412     def OnSpec(self, evt) :
413         self.parent.OnTextSpec(evt, self.getcorpus(self.itemdict))
414
415     def OnStat(self, evt) :
416         self.parent.OnTextStat(evt, self.getcorpus(self.itemdict))
417         
418     def OnAlceste(self, evt) :
419         self.parent.OnTextAlceste(evt, self.getcorpus(self.itemdict))
420
421     def OnPam(self, evt) :
422         print 'rien'
423
424     def OnSimiTxt(self, evt) :
425         self.parent.OnSimiTxt(evt, self.getcorpus(self.itemdict))
426
427     def OnWordCloud(self, evt) :
428         self.parent.OnWordCloud(evt, self.getcorpus(self.itemdict))
429
430     def OnItemBackground(self, event):
431
432         colourdata = wx.ColourData()
433         colourdata.SetColour(self.itemdict["back"])
434         dlg = wx.ColourDialog(self, colourdata)
435         
436         dlg.GetColourData().SetChooseFull(True)
437
438         if dlg.ShowModal() == wx.ID_OK:
439             data = dlg.GetColourData()
440             col1 = data.GetColour().Get()
441             self.SetItemBackgroundColour(self.current, col1)
442         dlg.Destroy()
443
444
445     def OnItemForeground(self, event):
446
447         colourdata = wx.ColourData()
448         colourdata.SetColour(self.itemdict["fore"])
449         dlg = wx.ColourDialog(self, colourdata)
450         
451         dlg.GetColourData().SetChooseFull(True)
452
453         if dlg.ShowModal() == wx.ID_OK:
454             data = dlg.GetColourData()
455             col1 = data.GetColour().Get()
456             self.SetItemTextColour(self.current, col1)
457         dlg.Destroy()
458
459
460     def OnItemBold(self, event):
461
462         self.SetItemBold(self.current, not self.itemdict["isbold"])
463
464
465     def OnItemFont(self, event):
466
467         data = wx.FontData()
468         font = self.itemdict["font"]
469         
470         if font is None:
471             font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
472             
473         data.SetInitialFont(font)
474
475         dlg = wx.FontDialog(self, data)
476         
477         if dlg.ShowModal() == wx.ID_OK:
478             data = dlg.GetFontData()
479             font = data.GetChosenFont()
480             self.SetItemFont(self.current, font)
481
482         dlg.Destroy()
483         
484
485     def OnItemHyperText(self, event):
486
487         self.SetItemHyperText(self.current, not self.itemdict["ishtml"])
488
489
490     def OnEnableWindow(self, event):
491
492         enable = self.GetItemWindowEnabled(self.current)
493         self.SetItemWindowEnabled(self.current, not enable)
494
495
496     def OnDisableItem(self, event):
497
498         self.EnableItem(self.current, False)
499         
500
501     def OnItemIcons(self, event):
502
503         bitmaps = [self.itemdict["normal"], self.itemdict["selected"],
504                    self.itemdict["expanded"], self.itemdict["selexp"]]
505
506         wx.BeginBusyCursor()        
507         dlg = TreeIcons(self, -1, bitmaps=bitmaps)
508         wx.EndBusyCursor()
509         dlg.ShowModal()
510
511
512     def SetNewIcons(self, bitmaps):
513
514         self.SetItemImage(self.current, bitmaps[0], CT.TreeItemIcon_Normal)
515         self.SetItemImage(self.current, bitmaps[1], CT.TreeItemIcon_Selected)
516         self.SetItemImage(self.current, bitmaps[2], CT.TreeItemIcon_Expanded)
517         self.SetItemImage(self.current, bitmaps[3], CT.TreeItemIcon_SelectedExpanded)
518
519
520     def OnItemInfo(self, event):
521
522         itemtext = self.itemdict["text"]
523         numchildren = str(self.itemdict["children"])
524         itemtype = self.itemdict["itemtype"]
525         pydata = self.itemdict['pydata']
526         #if 'analyses' in pydata :
527         #    toshow = dict([[val, pydata[val]] for val in pydata if val not in['analyses', 'isload']])
528         #else :
529         toshow = pydata['ira']
530         toshow = DoConf(toshow).getoptions()
531         txt = DoConf().totext(toshow)
532         parametres = [val.split('\t\t:') for val in txt.splitlines()]
533         parametres.sort()
534
535         if itemtype == 0:
536             itemtype = "Normal"
537         elif itemtype == 1:
538             itemtype = "CheckBox"
539         else:
540             itemtype = "RadioButton"
541
542         dlg = InfoDialog(self, itemtext, parametres)
543         dlg.ShowModal()
544         dlg.Destroy()
545                 
546         
547
548     def OnItemDelete(self, event):
549
550         strs = "Are You Sure You Want To Delete Item " + self.GetItemText(self.current) + "?"
551         dlg = wx.MessageDialog(None, strs, 'Deleting Item', wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_QUESTION)
552
553         if dlg.ShowModal() in [wx.ID_NO, wx.ID_CANCEL]:
554             dlg.Destroy()
555             return
556
557         dlg.Destroy()
558         
559         pydata = self.itemdict['pydata']
560         if 'corpus_name' in pydata :
561             self.history.delete(pydata['uuid'], True)
562         else :
563             self.history.delete(pydata['uuid'])
564         self.DeleteChildren(self.current)
565         self.Delete(self.current)
566         self.current = None
567         
568
569
570     def OnItemPrepend(self, event):
571
572         dlg = wx.TextEntryDialog(self, "Please Enter The New Item Name", 'Item Naming', 'Python')
573
574         if dlg.ShowModal() == wx.ID_OK:
575             newname = dlg.GetValue()
576             newitem = self.PrependItem(self.current, newname)
577             self.EnsureVisible(newitem)
578
579         dlg.Destroy()
580
581     def AddAnalyse(self, parametres, itemParent = None, bold = True) :
582         uuid = parametres['corpus']
583         if itemParent is None :
584             itemParent = self.root
585         child, cookie = self.GetFirstChild(itemParent)
586         corpus = None
587         while child :
588             pydata = self.GetPyData(child)
589             if pydata['uuid'] == uuid :
590                 corpus = child
591                 break
592             self.GiveFocus(child, uuid)
593             child, cookie = self.GetNextChild(itemParent, cookie)
594         #item = self.AppendItem(child, parametres['name'])
595         if corpus is not None : 
596             item = self.AppendItem(corpus, parametres['name'])
597         else :
598             item = self.AppendItem(self.root, parametres['name'])
599         self.SetPyData(item, parametres)
600         self.SetItemImage(item, 24, CT.TreeItemIcon_Normal)
601         self.SetItemImage(item, 13, CT.TreeItemIcon_Expanded)
602         self.SetItemBold(item, bold)
603         
604     def OnItemAppend(self, item):
605         child = self.AppendItem(self.root, item['corpus_name'])
606         self.SetPyData(child, item)
607         self.history.addtab(item)
608         self.SetItemImage(child, 24, CT.TreeItemIcon_Normal)
609         self.SetItemImage(child, 13, CT.TreeItemIcon_Expanded)
610         self.SetItemBold(child, True)
611         
612         #dlg = wx.TextEntryDialog(self, "Please Enter The New Item Name", 'Item Naming', 'Python')
613
614         #if dlg.ShowModal() == wx.ID_OK:
615         #    newname = dlg.GetValue()
616         #    newitem = self.AppendItem(self.current, newname)
617         #    self.EnsureVisible(newitem)
618         
619
620         #dlg.Destroy()
621         
622
623     def OnBeginEdit(self, event):
624         
625         #self.log.info("OnBeginEdit" + "\n")
626         # show how to prevent edit...
627         item = event.GetItem()
628         if item and self.GetItemText(item) == "The Root Item":
629             wx.Bell()
630             #self.log.info("You can't edit this one..." + "\n")
631
632             # Lets just see what's visible of its children
633             cookie = 0
634             root = event.GetItem()
635             (child, cookie) = self.GetFirstChild(root)
636
637             while child:
638                 #self.log.info("Child [%s] visible = %d" % (self.GetItemText(child), self.IsVisible(child)) + "\n")
639                 (child, cookie) = self.GetNextChild(root, cookie)
640
641             event.Veto()
642
643
644     def OnEndEdit(self, event):
645         
646         #self.log.info("OnEndEdit: %s %s" %(event.IsEditCancelled(), event.GetLabel()))
647         # show how to reject edit, we'll not allow any digits
648         for x in event.GetLabel():
649             if x in string.digits:
650                 #self.log.info(", You can't enter digits..." + "\n")
651                 event.Veto()
652                 return
653             
654         self.log.info("\n")
655
656
657     def OnLeftDClick(self, event):
658         
659         pt = event.GetPosition()
660         item, flags = self.HitTest(pt)
661         pydata = self.GetPyData(item)
662         if pydata['uuid'] in self.parent.history.opened :
663             for i in range(self.parent.nb.GetPageCount()) :
664                 page = self.parent.nb.GetPage(i)
665                 if 'parametres' in dir(page) :
666                     if page.parametres['uuid'] == pydata['uuid'] :
667                         self.parent.nb.SetSelection(i)
668                         break
669         else :
670             OpenAnalyse(self.parent, pydata)
671             self.SetItemBold(item, True)
672         #if item and (flags & CT.TREE_HITTEST_ONITEMLABEL):
673         #    if self.GetAGWWindowStyleFlag() & CT.TR_EDIT_LABELS:
674         #        self.log.info("OnLeftDClick: %s (manually starting label edit)"% self.GetItemText(item) + "\n")
675                 
676                 #self.EditLabel(item)
677         #    else:
678         #        pydata = self.GetPyData(item)
679         #        print pydata
680         #        self.log.info("OnLeftDClick: Cannot Start Manual Editing, Missing Style TR_EDIT_LABELS\n")
681
682         event.Skip()                
683         
684
685     def OnItemExpanded(self, event):
686         
687         item = event.GetItem()
688         if item:
689             self.log.info("OnItemExpanded: %s" % self.GetItemText(item) + "\n")
690
691
692     def OnItemExpanding(self, event):
693         
694         item = event.GetItem()
695         if item:
696             self.log.info("OnItemExpanding: %s" % self.GetItemText(item) + "\n")
697             
698         event.Skip()
699
700         
701     def OnItemCollapsed(self, event):
702
703         item = event.GetItem()
704         if item:
705             self.log.info("OnItemCollapsed: %s" % self.GetItemText(item) + "\n")
706             
707
708     def OnItemCollapsing(self, event):
709
710         item = event.GetItem()
711         if item:
712             self.log.info("OnItemCollapsing: %s" % self.GetItemText(item) + "\n")
713     
714         event.Skip()
715
716         
717     def OnSelChanged(self, event):
718         item = event.GetItem()
719         pydata = self.GetPyData(item)
720         if pydata is not None :
721             if pydata['uuid'] in self.parent.history.opened :
722                 for i in range(self.parent.nb.GetPageCount()) :
723                     page = self.parent.nb.GetPage(i)
724                     if 'parametres' in dir(page) :
725                         if page.parametres['uuid'] == pydata['uuid'] :
726                             self.parent.nb.SetSelection(i)
727                             break
728
729         #self.item = event.GetItem()
730         #if self.item:
731         #    self.
732             #self.log.info("OnSelChanged: %s" % self.GetItemText(self.item))
733         #    if wx.Platform == '__WXMSW__':
734                # self.log.info(", BoundingRect: %s" % self.GetBoundingRect(self.item, True) + "\n")
735        #        pass
736         event.Skip()
737
738
739     def OnSelChanging(self, event):
740
741         item = event.GetItem()
742         olditem = event.GetOldItem()
743         
744         if item:
745             if not olditem:
746                 olditemtext = "None"
747             else:
748                 olditemtext = self.GetItemText(olditem)
749             self.log.info("OnSelChanging: From %s" % olditemtext + " To %s" % self.GetItemText(item) + "\n")
750                 
751         event.Skip()
752
753
754     def OnBeginDrag(self, event):
755
756         self.item = event.GetItem()
757         if self.item:
758             self.log.info("Beginning Drag..." + "\n")
759
760             event.Allow()
761
762
763     def OnBeginRDrag(self, event):
764
765         self.item = event.GetItem()
766         if self.item:
767             self.log.info("Beginning Right Drag..." + "\n")
768
769             event.Allow()
770         
771
772     def OnEndDrag(self, event):
773
774         self.item = event.GetItem()
775         if self.item:
776             self.log.info("Ending Drag!" + "\n")
777
778         event.Skip()            
779
780
781     def OnDeleteItem(self, event):
782
783         item = event.GetItem()
784
785         if not item:
786             return
787
788         self.log.info("Deleting Item: %s" % self.GetItemText(item) + "\n")
789         event.Skip()
790         
791
792     def OnItemCheck(self, event):
793
794         item = event.GetItem()
795         self.log.info("Item " + self.GetItemText(item) + " Has Been Checked!\n")
796         event.Skip()
797
798
799     def OnItemChecking(self, event):
800
801         item = event.GetItem()
802         self.log.info("Item " + self.GetItemText(item) + " Is Being Checked...\n")
803         event.Skip()
804         
805
806     def OnToolTip(self, event):
807
808         item = event.GetItem()
809         if item:
810             event.SetToolTip(wx.ToolTip(self.GetItemText(item)))
811
812
813     def OnItemMenu(self, event):
814
815         item = event.GetItem()
816         if item:
817             self.log.info("OnItemMenu: %s" % self.GetItemText(item) + "\n")
818     
819         event.Skip()
820
821
822     def OnKey(self, event):
823
824         keycode = event.GetKeyCode()
825         keyname = keyMap.get(keycode, None)
826                 
827         if keycode == wx.WXK_BACK:
828             self.log.info("OnKeyDown: HAHAHAHA! I Vetoed Your Backspace! HAHAHAHA\n")
829             return
830
831         if keyname is None:
832             if "unicode" in wx.PlatformInfo:
833                 keycode = event.GetUnicodeKey()
834                 if keycode <= 127:
835                     keycode = event.GetKeyCode()
836                 keyname = "\"" + unichr(event.GetUnicodeKey()) + "\""
837                 if keycode < 27:
838                     keyname = "Ctrl-%s" % chr(ord('A') + keycode-1)
839                 
840             elif keycode < 256:
841                 if keycode == 0:
842                     keyname = "NUL"
843                 elif keycode < 27:
844                     keyname = "Ctrl-%s" % chr(ord('A') + keycode-1)
845                 else:
846                     keyname = "\"%s\"" % chr(keycode)
847             else:
848                 keyname = "unknown (%s)" % keycode
849                 
850         self.log.info("OnKeyDown: You Pressed '" + keyname + "'\n")
851
852         event.Skip()
853         
854         
855     def OnActivate(self, event):
856         
857         if self.item:
858             self.log.info("OnActivate: %s" % self.GetItemText(self.item) + "\n")
859
860         event.Skip()
861
862         
863     def OnHyperLink(self, event):
864
865         item = event.GetItem()
866         if item:
867             self.log.info("OnHyperLink: %s" % self.GetItemText(self.item) + "\n")
868             
869
870     def OnTextCtrl(self, event):
871
872         char = chr(event.GetKeyCode())
873         self.log.info("EDITING THE TEXTCTRL: You Wrote '" + char + \
874                        "' (KeyCode = " + str(event.GetKeyCode()) + ")\n")
875         event.Skip()
876
877
878     def OnComboBox(self, event):
879
880         selection = event.GetEventObject().GetValue()
881         self.log.info("CHOICE FROM COMBOBOX: You Chose '" + selection + "'\n")
882         event.Skip()