/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/application.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/UI/application.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 39 by bh, Thu Sep 6 17:18:22 2001 UTC revision 374 by jan, Mon Jan 27 14:20:02 2003 UTC
# Line 1  Line 1 
1  # Copyright (C) 2001 by Intevation GmbH  # Copyright (C) 2001, 2002 by Intevation GmbH
2  # Authors:  # Authors:
3  # Jan-Oliver Wagner <[email protected]>  # Jan-Oliver Wagner <[email protected]>
4    # Bernhard Herzog <[email protected]>
5  #  #
6  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
7  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 11  Thuban's application object. Line 12  Thuban's application object.
12    
13  __version__ = "$Revision$"  __version__ = "$Revision$"
14    
15    import sys, os
16    import traceback
17    
18  from wxPython.wx import *  from wxPython.wx import *
19    
20  from Thuban.Lib.connector import Publisher  from Thuban.Lib.connector import Publisher
21    
22    from Thuban import _
23  from Thuban.Model.session import create_empty_session  from Thuban.Model.session import create_empty_session
24  from Thuban.Model.save import save_session  from Thuban.Model.save import save_session
25  from Thuban.Model.load import load_session  from Thuban.Model.load import load_session
26    from Thuban.Model.messages import MAPS_CHANGED
27    
28  import view  import view
29  import tree  import tree
30  from interactor import Interactor  from interactor import Interactor
31  from mainwindow import MainWindow  import mainwindow
32    
33  from messages import SESSION_CHANGED  from messages import SESSION_CHANGED
34    
# Line 39  class ThubanApplication(wxApp, Publisher Line 45  class ThubanApplication(wxApp, Publisher
45      """      """
46    
47      def OnInit(self):      def OnInit(self):
48            self.read_startup_files()
49          self.interactor = Interactor(None)          self.interactor = Interactor(None)
50          top = MainWindow(NULL, -1, self.interactor)          top = self.CreateMainWindow()
51          top.Show(true)          top.Show(true)
52          self.top = top          self.top = top
53          self.SetTopWindow(top)          self.SetTopWindow(top)
# Line 48  class ThubanApplication(wxApp, Publisher Line 55  class ThubanApplication(wxApp, Publisher
55          self.create_session()          self.create_session()
56          return true          return true
57    
58        def OnExit(self):
59            """Clean up code.
60    
61            Extend this in derived classes if needed.
62            """
63            self.session.Destroy()
64            self.interactor.Destroy()
65            Publisher.Destroy(self)
66    
67        def MainLoop(self):
68            """Call the inherited MainLoop method and then call OnExit.
69    
70            In wxPython OnExit isn't called automatically, unfortunately, so
71            we do it here.
72            """
73            wxApp.MainLoop(self)
74            self.OnExit()
75    
76        def read_startup_files(self):
77            """Read the startup files."""
78            # for now the startup file is ~/.thuban/thubanstart.py
79            dir =os.path.expanduser("~/.thuban")
80            if os.path.isdir(dir):
81                sys.path.append(dir)
82                try:
83                    import thubanstart
84                except ImportError:
85                    tb = sys.exc_info()[2]
86                    try:
87                        if tb.tb_next is not None:
88                            # The ImportError exception was raised from
89                            # inside the thubanstart module.
90                            sys.stderr.write(_("Cannot import the thubanstart"
91                                             "module\n"))
92                            traceback.print_exc(None, sys.stderr)
93                        else:
94                            # There's no thubanstart module.
95                            sys.stderr.write(_("No thubanstart module available\n"))
96                    finally:
97                        # make sure we delete the traceback object,
98                        # otherwise there's be circular references involving
99                        # the current stack frame
100                        del tb
101                except:
102                    sys.stderr.write(_("Cannot import the thubanstart module\n"))
103                    traceback.print_exc(None, sys.stderr)
104            else:
105                # There's no .thuban directory
106                sys.stderr.write(_("No ~/.thuban directory\n"))
107    
108        def CreateMainWindow(self):
109            """Create and return the main window for the application.
110    
111            Override this in subclasses to instantiate the Thuban mainwindow
112            with different parameters or to use a different class for the
113            main window.
114    
115            when this method is called by OnInit self.interactor (to be used
116            for the interactor argument of the standard Thuban main window
117            class) has already been instantiated.
118            """
119            msg = (_("This is the wxPython-based Graphical User Interface"
120                   " for exploring geographic data"))
121            return mainwindow.MainWindow(NULL, -1, "Thuban", self, self.interactor,
122                                         initial_message = msg)
123    
124        def Session(self):
125            """Return the application's session object"""
126            return self.session
127    
128      def SetSession(self, session):      def SetSession(self, session):
129            """Make session the new session.
130    
131            Issue SESSION_CHANGED after self.session has become the new
132            session. After the session has been assigned call
133            self.subscribe_session() with the new session and
134            self.unsubscribe_session with the old one.
135            """
136          oldsession = self.session          oldsession = self.session
137          self.session = session          self.session = session
138            self.subscribe_session(self.session)
139          self.issue(SESSION_CHANGED)          self.issue(SESSION_CHANGED)
140          self.interactor.SetSession(session)          self.interactor.SetSession(session)
141          self.set_map()          self.maps_changed()
142          if oldsession is not None:          if oldsession is not None:
143                self.unsubscribe_session(oldsession)
144              oldsession.Destroy()              oldsession.Destroy()
145    
146        def subscribe_session(self, session):
147            """Subscribe to some of the sessions channels.
148    
149            Extend this method in derived classes if you need additional
150            channels.
151            """
152            session.Subscribe(MAPS_CHANGED, self.maps_changed)
153    
154        def unsubscribe_session(self, session):
155            """Unsubscribe from the sessions channels.
156    
157            Extend this method in derived classes if you subscribed to
158            additional channels in subscribe_session().
159            """
160            session.Unsubscribe(MAPS_CHANGED, self.maps_changed)
161    
162      def create_session(self):      def create_session(self):
163          # Create a simple default session          """Create a default session.
164    
165            Override this method in derived classes to instantiate the
166            session differently or to use a different session class. Don't
167            subscribe to channels here yet. Do that in the
168            subscribe_session() method.
169            """
170          self.SetSession(create_empty_session())          self.SetSession(create_empty_session())
171    
172      def OpenSession(self, filename):      def OpenSession(self, filename):
# Line 70  class ThubanApplication(wxApp, Publisher Line 178  class ThubanApplication(wxApp, Publisher
178      def SaveSession(self):      def SaveSession(self):
179          save_session(self.session, self.session.filename)          save_session(self.session, self.session.filename)
180    
181      def set_map(self):      def maps_changed(self, *args):
182          if self.session.HasMaps():          if self.session.HasMaps():
183              self.top.SetMap(self.session.Maps()[0])              self.top.SetMap(self.session.Maps()[0])
184          else:          else:

Legend:
Removed from v.39  
changed lines
  Added in v.374

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26