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