Package Gnumed :: Package timelinelib :: Package wxgui :: Package dialogs :: Package importevents :: Module controller
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.wxgui.dialogs.importevents.controller

 1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg 
 2  # 
 3  # This file is part of Timeline. 
 4  # 
 5  # Timeline is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  # 
10  # Timeline is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14  # 
15  # You should have received a copy of the GNU General Public License 
16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>. 
17   
18   
19  import os.path 
20   
21  from timelinelib.canvas.data.exceptions import TimelineIOError 
22  from timelinelib.db import db_open 
23  from timelinelib.utils import ex_msg 
24  from timelinelib.wxgui.framework import Controller 
25   
26   
27 -class ImportEventsDialogController(Controller):
28
29 - def on_init(self, db):
30 self._db = db 31 self._db_to_import = None 32 self._show_preview()
33
34 - def on_file_path_changed(self, event):
35 self._show_preview()
36
37 - def on_ok_clicked(self, event):
38 if not self._db_to_import: 39 return 40 self._db.import_db(self._db_to_import) 41 self.view.Close()
42
43 - def _show_preview(self):
44 if self._path_exists(): 45 self._handle_valid_path() 46 else: 47 self._handle_invalid_path()
48
49 - def _path_exists(self):
50 path = self.view.GetFilePath() 51 return path in [":tutorial:", ":numtutorial:"] or os.path.exists(path)
52
53 - def _handle_invalid_path(self):
54 self._set_error(_("File does not exist."))
55
56 - def _handle_valid_path(self):
57 try: 58 db_to_import = db_open(self.view.GetFilePath()) 59 except Exception as e: 60 self._set_error(_("Unable to load events: %s.") % ex_msg(e)) 61 else: 62 self._report_nbr_of_events_in_db(db_to_import)
63
64 - def _report_nbr_of_events_in_db(self, db_to_import):
65 if self._is_same_timetype(db_to_import): 66 self._set_success(db_to_import, _("%d events will be imported." % len(db_to_import.get_all_events()))) 67 else: 68 self._set_error(_("The selected timeline has a different time type."))
69
70 - def _is_same_timetype(self, db_to_import):
71 return db_to_import.get_time_type() == self._db.get_time_type()
72
73 - def _set_success(self, db_to_import, text):
74 self._db_to_import = db_to_import 75 self.view.SetSuccess(text)
76
77 - def _set_error(self, text):
78 self._db_to_import = None 79 self.view.SetError(text)
80