Commit 50d89b6284ad34c1afd8f13cd4d003b5f690a976

Authored by tatiana
1 parent a167ab76

STL: Comments / code cleanup

Showing 1 changed file with 119 additions and 82 deletions   Show diff stats
invesalius/invesalius.py
... ... @@ -16,66 +16,97 @@
16 16 # COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
17 17 # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
18 18 # detalhes.
19   -#--------------------------------------------------------------------------
  19 +#-------------------------------------------------------------------------
  20 +
20 21 import multiprocessing
21   -from optparse import OptionParser
  22 +import optparse as op
22 23 import os
23 24 import sys
24 25  
25   -from session import Session
  26 +import wx
  27 +import wx.lib.pubsub as ps
  28 +
26 29 import gui.language_dialog as lang_dlg
27 30 import i18n
  31 +import session as ses
28 32 import utils
29 33  
  34 +# ------------------------------------------------------------------
30 35  
31   -# TODO: This should be called during installation
32   -# ----------------------------------------------------------------------
33   -
34   -path = os.path.join(os.path.expanduser('~'), ".invesalius", "presets")
35   -
36   -try:
37   - os.makedirs(path)
38   -except OSError:
39   - #print "Warning: Directory (probably) exists"
40   - pass
41   -# ----------------------------------------------------------------------
  36 +class InVesalius(wx.App):
  37 + """
  38 + InVesalius wxPython application class.
  39 + """
  40 + def OnInit(self):
  41 + """
  42 + Initialize splash screen and main frame.
  43 + """
  44 + self.SetAppName("InVesalius 3")
  45 + splash = SplashScreen()
  46 + self.control = splash.control
  47 + self.frame = splash.main
  48 + splash.Show()
  49 + return True
42 50  
43   -import wx
44   -import wx.lib.pubsub as ps
  51 + def MacOpenFile(self, filename):
  52 + """
  53 + Open drag & drop files under darwin
  54 + """
  55 + path = os.path.abspath(filename)
  56 + ps.Publisher().sendMessage('Open project', path)
45 57  
  58 +# ------------------------------------------------------------------
46 59  
47 60 class SplashScreen(wx.SplashScreen):
  61 + """
  62 + Splash screen to be shown in InVesalius initialization.
  63 + """
48 64 def __init__(self):
  65 + # Splash screen image will depend on currently language
49 66 lang = False
50   - save_session = False
51   - session = Session()
  67 +
  68 + # Language information is available in session configuration
  69 + # file. First we need to check if this file exist, if now, it
  70 + # should be created
  71 + create_session = False
  72 + session = ses.Session()
52 73 if not (session.ReadSession()):
53   - save_session = True
  74 + create_session = True
54 75  
55   - if not(session.ReadLanguage()):
  76 + # Check if there is a language set (if session file exists
  77 + if session.ReadLanguage():
  78 + lang = session.GetLanguage()
  79 + _ = i18n.InstallLanguage(lang)
56 80  
57   - ldlg = lang_dlg.LanguageDialog()
  81 + # If no language is set into session file, show dialog so
  82 + # user can select language
  83 + else:
  84 + dialog = lang_dlg.LanguageDialog()
58 85  
  86 + # FIXME: This works ok in linux2, darwin and win32,
  87 + # except on win64, due to wxWidgets bug
59 88 try:
60   - if (ldlg.ShowModal() == wx.ID_OK):
61   - lang = ldlg.GetSelectedLanguage()
  89 + ok = (dialog.ShowModal() == wx.ID_OK)
  90 + except wx._core.PyAssertionError:
  91 + ok = True
  92 + finally:
  93 + if ok:
  94 + lang = dialog.GetSelectedLanguage()
62 95 session.SetLanguage(lang)
63 96 _ = i18n.InstallLanguage(lang)
64   - except(wx._core.PyAssertionError): #TODO: error win64
65   - lang = ldlg.GetSelectedLanguage()
66   - session.SetLanguage(lang)
67   - _ = i18n.InstallLanguage(lang)
68   - else:
69   - lang = session.GetLanguage()
70   - _ = i18n.InstallLanguage(lang)
71 97  
72   - if (save_session):
  98 + # Session file should be created... So we set the recent
  99 + # choosen language
  100 + if (create_session):
73 101 session.CreateItens()
74 102 session.SetLanguage(lang)
75 103 session.CreateSessionFile()
76 104  
  105 + # Only after language was defined, splash screen will be
  106 + # shown
77 107 if lang:
78   - if (lang.startswith('pt')): #Necessy, pt noted as pt_BR
  108 + # For pt_BR, splash_pt.png should be used
  109 + if (lang.startswith('pt')):
79 110 icon_file = "splash_pt.png"
80 111 else:
81 112 icon_file = "splash_" + lang + ".png"
... ... @@ -84,18 +115,22 @@ class SplashScreen(wx.SplashScreen):
84 115  
85 116 bmp = wx.Image(path).ConvertToBitmap()
86 117  
87   - wx.SplashScreen.__init__(self, bitmap=bmp,
88   - splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
89   - milliseconds=1500, id=-1, parent=None)
  118 + style = wx.SPLASH_TIMEOUT | wx.SPLASH_CENTRE_ON_SCREEN
  119 + wx.SplashScreen.__init__(self,
  120 + bitmap=bmp,
  121 + splashStyle=style,
  122 + milliseconds=1500,
  123 + id=-1,
  124 + parent=None)
90 125 self.Bind(wx.EVT_CLOSE, self.OnClose)
91 126  
  127 + # Importing takes sometime, therefore it will be done
  128 + # while splash is being shown
92 129 from gui.frame import Frame
93 130 from control import Controller
94 131 from project import Project
95 132  
96   - print "antes primeiro import session"
97 133 self.main = Frame(None)
98   - print "depois primeiro import session"
99 134 self.control = Controller(self.main)
100 135  
101 136 self.fc = wx.FutureCall(1, self.ShowMain)
... ... @@ -106,72 +141,57 @@ class SplashScreen(wx.SplashScreen):
106 141 evt.Skip()
107 142 self.Hide()
108 143  
109   - # if the timer is still running then go ahead and show the
  144 + # If the timer is still running then go ahead and show the
110 145 # main frame now
111 146 if self.fc.IsRunning():
112 147 self.fc.Stop()
113   -
114   - #session = Session()
115   - #if not (session.ReadSession()):
116   - # session.CreateItens()
117   -
118   - #lang = session.GetLanguage()
119   - #print lang
120   -
121   - #i18n.InstallLanguage(lang)
122 148 self.ShowMain()
123 149  
124   -
125 150 def ShowMain(self):
  151 + # Show main frame
126 152 self.main.Show()
127 153  
128 154 if self.fc.IsRunning():
129 155 self.Raise()
130 156  
131   -class InVesalius(wx.App):
132   - def OnInit(self):
133   - self.SetAppName("InVesalius 3")
134   - splash = SplashScreen()
135   - self.control = splash.control
136   - self.frame = splash.main
137   - splash.Show()
  157 +# ------------------------------------------------------------------
138 158  
139   - return True
140 159  
141   - def MacOpenFile(self, filename):
142   - path = os.path.abspath(file)
143   - ps.Publisher().sendMessage('Open project', path)
144 160  
145 161 def parse_comand_line():
146 162 """
147 163 Handle command line arguments.
148 164 """
149   - parser = OptionParser()
  165 + session = ses.Session()
150 166  
151   - # Add comand line option debug(-d or --debug) to print all pubsub message is
152   - # being sent
153   - parser.add_option("-d", "--debug", action="store_true", dest="debug")
154   - parser.add_option("-i", "--import", action="store", dest="dicom_dir")
  167 + # Parse command line arguments
  168 + parser = op.OptionParser()
155 169  
156   - options, args = parser.parse_args()
  170 + # -d or --debug: print all pubsub messagessent
  171 + parser.add_option("-d", "--debug",
  172 + action="store_true",
  173 + dest="debug")
157 174  
158   - session = Session()
  175 + # -i or --import: import DICOM directory
  176 + # chooses largest series
  177 + parser.add_option("-i", "--import",
  178 + action="store",
  179 + dest="dicom_dir")
  180 + options, args = parser.parse_args()
159 181  
  182 + # If debug argument...
160 183 if options.debug:
161   - # The user passed the debug option?
162   - # Yes!
163   - # Then all pubsub message must be printed.
164 184 ps.Publisher().subscribe(print_events, ps.ALL_TOPICS)
165   -
166 185 session.debug = 1
167 186  
  187 + # If import DICOM argument...
168 188 if options.dicom_dir:
169   - # The user passed directory to me?
170 189 import_dir = options.dicom_dir
171 190 ps.Publisher().sendMessage('Import directory', import_dir)
172 191 return True
173 192  
174 193 # Check if there is a file path somewhere in what the user wrote
  194 + # In case there is, try opening as it was a inv3
175 195 else:
176 196 i = len(args)
177 197 while i:
... ... @@ -186,27 +206,44 @@ def parse_comand_line():
186 206  
187 207  
188 208 def print_events(data):
  209 + """
  210 + Print pubsub messages
  211 + """
189 212 utils.debug(data.topic)
190 213  
191 214 def main():
  215 + """
  216 + Initialize InVesalius GUI
  217 + """
192 218 application = InVesalius(0)
193 219 parse_comand_line()
194 220 application.MainLoop()
195 221  
196 222 if __name__ == '__main__':
197   -
198 223 # Needed in win 32 exe
199 224 if hasattr(sys,"frozen") and sys.frozen == "windows_exe":
200   - multiprocessing.freeze_support()
201   -
202   - folder_log = os.path.join(os.path.expanduser('~'), '.invesalius', 'logs')
203   -
204   - if not os.path.isdir(folder_log):
205   - os.makedirs(folder_log)
206   - path = os.path.join(folder_log, "stderr.log")
207   - sys.stderr = open(path, "w")
208   -
209   - # Add current directory to PYTHONPATH
  225 + multiprocessing.freeze_support()
  226 +
  227 + # Create raycasting presets' folder, if it doens't exist
  228 + dirpath = os.path.join(os.path.expanduser('~'),
  229 + ".invesalius",
  230 + "presets")
  231 + if not os.path.isdir(dirpath):
  232 + os.makedirs(dirpath)
  233 +
  234 + # Create logs' folder, if it doesn't exist
  235 + dirpath = os.path.join(os.path.expanduser('~'),
  236 + ".invesalius",
  237 + "logs")
  238 + if not os.path.isdir(dirpath):
  239 + os.makedirs(dirpath)
  240 +
  241 + # Set system standard error output to file
  242 + path = os.path.join(dirpath, "stderr.log")
  243 + sys.stderr = open(path, "w")
  244 +
  245 + # Add current directory to PYTHONPATH, so other classes can
  246 + # import modules as they were on root invesalius folder
210 247 sys.path.append(".")
211 248  
212 249 # Init application
... ...