from mac
[iramuteq] / guifunct.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2011 Pierre Ratinaud
4 #Lisense: GNU/GPL
5
6 import wx
7 import os
8 import sys
9 from copy import copy
10 import dialog
11 from listlex import *
12 from functions import treat_var_mod
13
14
15 def OnOpen(self, type):
16         if type == "Data":
17              wildcard = u"Fichiers supportés|*.ods;*.xls;*.csv;*.txt|Openoffice Clac|*.ods|Fichier excel|*.xls|Fichier csv|*.csv|Fichier texte|*.txt|Tous les fichiers|*"
18         elif type == "Texte":
19             wildcard = "Fichier texte|*.txt|Tous les fichiers|*"
20         elif type == "Analyse":
21             wildcard = "Fichier analyse/Corpus|*.ira;*.cira"
22         defaultDir = self.PathPath.get('PATHS', 'lastpath')
23         if defaultDir.strip() == '':
24             defaultDir = self.UserConfigPath.replace('.iramuteq','')
25         dlg = wx.FileDialog(
26         self, message="Choisissez un fichier", defaultDir=defaultDir,
27         defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.CHANGE_DIR)
28         dlg.CenterOnParent()
29         if dlg.ShowModal() == wx.ID_OK :
30             fileName = dlg.GetFilename()
31             path = dlg.GetPaths()
32             dlg.Destroy()
33             self.PathPath.set('PATHS', 'lastpath', os.path.dirname(path[0]))
34             self.type = type
35             return fileName, path
36         else:
37             dlg.Destroy()
38             if type == "Data":
39                 return False, [False]
40             elif type == "Texte":
41                 return False, [False]
42             elif type == "Analyse":
43                 return [False]
44
45 def getfileextension(file) :
46     return os.path.splitext(file)[1]
47
48 def get_table_param(self, filename) :
49     if getfileextension(filename) == '.csv':
50         dlg = dialog.FileOptionDialog(self, -1, u"Format du fichier", sep=True, size=(350, 200),
51                      style=wx.DEFAULT_DIALOG_STYLE)
52         dlg.CenterOnParent()
53         val = dlg.ShowModal()
54         if val == wx.ID_OK:
55             self.tableau.parametre['colsep'] = dlg.colsep[dlg.choice3.GetSelection()]
56             self.tableau.parametre['txtsep'] = dlg.txtsep[dlg.choice4.GetSelection()]
57             if self.tableau.parametre['colsep'] == 'tabulation' :
58                 self.tableau.parametre['colsep'] = '\t'
59             self.tableau.parametre['filetype'] = 'csv'
60         else :
61             dlg.Destroy()
62     elif  getfileextension(filename) == '.xls' :
63         dlg = dialog.FileOptionDialog(self, -1, u"Format du fichier", sep=False, sheet = True, size=(350, 200),
64                      style=wx.DEFAULT_DIALOG_STYLE)
65         dlg.CenterOnParent()
66         val = dlg.ShowModal()
67         if val == wx.ID_OK:    
68             self.tableau.parametre['colsep'] = ';'
69             self.tableau.parametre['txtsep'] = '\"'
70             self.tableau.parametre['encodage'] = sys.getdefaultencoding()
71             self.tableau.parametre['sheetnb'] = dlg.spin1.GetValue()
72             self.tableau.parametre['filetype'] = 'xls'
73         else :
74             dlg.Destroy()
75     elif getfileextension(filename) == '.ods':
76         dlg = dialog.FileOptionDialog(self, -1, u"Format du fichier", sep=False, size=(350, 200),
77                      style=wx.DEFAULT_DIALOG_STYLE)
78         dlg.CenterOnParent()
79         val = dlg.ShowModal()
80         if val == wx.ID_OK:          
81             self.tableau.parametre['colsep'] = ';'
82             self.tableau.parametre['txtsep'] = '\"'
83             self.tableau.parametre['filetype'] = 'ods'
84         else :
85             dlg.Destroy()
86     else :
87         val = False
88     if val == wx.ID_OK:       
89         if dlg.radio_box_1.GetSelection() == 0:
90             self.tableau.firstrowiscolnames = True
91         else:
92             self.tableau.firstrowiscolnames = False
93         if dlg.radio_box_2.GetSelection() == 0:
94             self.tableau.firstcolisrownames = True
95         else:
96             self.tableau.firstcolisrownames = False
97         dlg.Destroy()
98
99 def getPage(ira) :
100     if '_mgr' in dir(ira) :
101         if not ira._mgr.GetPane('Text').IsShown() :
102             if ira.nb.GetPageCount() >= 1:
103                 return ira.nb.GetPage(ira.nb.GetSelection())
104             else :
105                 return None
106         else :
107             return None
108     else :
109         return None
110
111 def getCorpus(page) :
112     if 'corpus' in page.__dict__:
113         return copy(page.corpus)
114     else :
115         return None
116             
117 class SelectColumn :
118     def __init__(self, parent, dictcol, actives, pathout, selected = None, dlg = False) :
119         self.ira = parent
120         if dlg :
121             dial = dialog.SelectColDial(self.ira)
122             listcol = ListForSpec(dial, self, dictcol, ['forme', 'eff'])
123             dial.bSizer2.Add( listcol, 2, wx.ALL|wx.EXPAND, 5 )
124             dial.m_sdbSizer2.AddButton( dial.m_sdbSizer2OK )
125             dial.m_sdbSizer2.Realize()
126             dial.bSizer2.Add( dial.m_sdbSizer2, 0, wx.EXPAND, 5 )
127             dial.Layout()
128             if selected is None :
129                 for row in xrange(listcol.GetItemCount()):
130                     listcol.Select(row)
131             else :
132                 orderlex = dict([[listcol.getColumnText(i,0),i] for i in range(0,listcol.GetItemCount())])
133                 for row in selected :
134                     listcol.Select(orderlex[actives[row]])
135             dial.CenterOnParent()
136             val = dial.ShowModal()        
137             last = listcol.GetFirstSelected()
138             lastl = [listcol.GetFirstSelected()]
139             indexes = [listcol.getColumnText(listcol.GetFirstSelected(),0)]
140             while listcol.GetNextSelected(last) != -1:
141                 last = listcol.GetNextSelected(last)
142                 lastl.append(last)
143                 indexes.append(listcol.getColumnText(last,0))
144             dial.Destroy()
145             column = [actives.index(val) for val in indexes]
146             column.sort()
147             with open(pathout, 'w') as f :
148                 f.write('\n'.join([`val` for val in column]))
149         else :
150             with open(pathout, 'w') as f :
151                 f.write('\n'.join([`i` for i in range(0,len(actives))]))
152
153 class PrepSimi :
154     def __init__(self, parent, parametres, indices_simi) :    
155         self.parametres = parametres
156         self.dial = dialog.PrefSimi(parent, -1, self.parametres, indices_simi) 
157         self.dial.CenterOnParent()
158         self.val = self.dial.ShowModal()
159         if self.val == wx.ID_OK :
160             if self.dial.check_bystar.GetValue() :
161                 variables = treat_var_mod(self.parametres['stars'])
162                 vardial = dialog.OptLexi(parent) 
163                 vardial.listet = self.parametres['stars']
164                 vardial.variables = [v for v in variables]
165                 for et in vardial.variables :
166                     vardial.list_box_1.Append(et)
167                 nval = vardial.ShowModal()
168                 if nval == wx.ID_OK :
169                     if vardial.choice.GetSelection() == 1 :
170                         listet = [vardial.listet[i] for i in dial.list_box_1.GetSelections()]
171                     else :
172                         listet = variables[vardial.variables[dial.list_box_1.GetSelections()[0]]]
173                     dial.Destroy()
174                     self.tableau.etline = self.Source.corpus.make_etline(listet)
175             self.make_param()
176
177     def make_param(self) :
178         self.select = self.dial.check_colch.GetValue()
179         if self.parametres.get('first', True) :
180             keep_coord = False
181         else :
182             keep_coord = self.dial.check_coord.GetValue()
183         param = {'coeff' : self.dial.choice1.GetSelection(),
184                           'layout' : self.dial.choice2.GetSelection(),
185                           'type_graph' : self.dial.choice3.GetSelection(),
186                           'arbremax' : self.dial.check1.GetValue(),
187                           'coeff_tv' : self.dial.check_s_size.GetValue(),
188                           'coeff_tv_nb' : self.dial.spin_tv.GetValue(),
189                           'tvprop' : self.dial.check2.GetValue(),
190                           'tvmin' : self.dial.spin_tvmin.GetValue(),
191                           'tvmax' : self.dial.spin_tvmax.GetValue(),
192                           'coeff_te' : self.dial.check3.GetValue(),
193                           'coeff_temin' : self.dial.spin_temin.GetValue(),
194                           'coeff_temax' : self.dial.spin_temax.GetValue(),
195                           'label_e' : self.dial.check_elab.GetValue(),
196                           'label_v' : self.dial.check_vlab.GetValue(),
197                           'vcex' : self.dial.check_vcex.GetValue(),
198                           'vcexmin' : self.dial.spin_vcexmin.GetValue(),
199                           'vcexmax' : self.dial.spin_vcexmax.GetValue(),
200                           'cex' : self.dial.spin_cex.GetValue(),
201                           'seuil_ok' : self.dial.check_seuil.GetValue(),
202                           'seuil' : self.dial.spin_seuil.GetValue(),
203                           'cols' : self.dial.cols.GetColour(),
204                           'cola' : self.dial.cola.GetColour(),
205                           'width' : self.dial.spin_width.GetValue(),
206                           'height' : self.dial.spin_height.GetValue(),
207                           'first' : False,
208                           'keep_coord' : keep_coord,
209                           'alpha' : self.dial.slider_sphere.GetValue(),
210                           'film' : self.dial.film.GetValue()
211                           }
212         if 'cexfromchi' in self.parametres :
213             param['cexfromchi'] = self.dial.checkit.GetValue()
214         if 'sfromchi' in self.parametres :
215             param['sfromchi'] = self.dial.checki.GetValue()
216         if 'vlabcolor' in self.parametres :
217            param['vlabcolor'] = self.parametres['vlabcolor']
218         if 'check_bystar' in dir(self.dial) :
219             param['bystar'] = self.dial.check_bystar.GetValue()
220             param['stars'] = self.parametres['stars']
221         self.parametres.update(param)
222