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

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

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

revision 747 by jonathan, Fri Apr 25 11:02:08 2003 UTC revision 760 by jonathan, Tue Apr 29 09:01:52 2003 UTC
# Line 1  Line 1 
1  #!/usr/bin/env python  # Copyright (c) 2003 by Intevation GmbH
2  # generated by wxGlade 0.2.1 on Thu Apr 17 11:51:39 2003  # Authors:
3    # Jonathan Coles <[email protected]>
4    #
5    # This program is free software under the GPL (>=v2)
6    # Read the file COPYING coming with Thuban for details.
7                                                                                    
8  import os, sys  import os, sys
9  from wxPython.wx import *  from wxPython.wx import *
10    
# Line 33  CLIENT_PROJFILE = 1 Line 37  CLIENT_PROJFILE = 1
37    
38  class ProjFrame(NonModalDialog):  class ProjFrame(NonModalDialog):
39    
40      def __init__(self, parent, name, receiver, *args, **kwds):      def __init__(self, parent, name, title, receiver):
41          """Initialize the projection dialog.          """Initialize the projection dialog.
42    
43          receiver -- An object that implements the following methods:          receiver -- An object that implements the following methods:
# Line 42  class ProjFrame(NonModalDialog): Line 46  class ProjFrame(NonModalDialog):
46          """          """
47                    
48          self.receiver = receiver          self.receiver = receiver
49          self.originalProjection = -1          self.haveTried = False
50          self.curProjPanel = None          self.curProjPanel = None
51    
52          self.projPanels = []          self.projPanels = []
# Line 55  class ProjFrame(NonModalDialog): Line 59  class ProjFrame(NonModalDialog):
59          self.projPanels.append(          self.projPanels.append(
60              ("latlong", _("Geographic"), GeoPanel))              ("latlong", _("Geographic"), GeoPanel))
61    
62          NonModalDialog.__init__(self, parent, name, "")          NonModalDialog.__init__(self, parent, name, title)
63          # originally generate by wxGlade          # originally generate by wxGlade
64          self.panel_1 = wxPanel(self, -1)          self.panel_1 = wxPanel(self, -1)
65          self.panel_edit = wxPanel(self, -1)          self.panel_edit = wxPanel(self, -1)
# Line 83  class ProjFrame(NonModalDialog): Line 87  class ProjFrame(NonModalDialog):
87          self.__do_layout()          self.__do_layout()
88          # wxGlade          # wxGlade
89    
90            self.originalProjection = self.receiver.GetProjection()
91    
92          self.__DoOnProjAvail()          self.__DoOnProjAvail()
93                    
94          EVT_BUTTON(self, ID_PROJ_TRY, self._OnTry)          EVT_BUTTON(self, ID_PROJ_TRY, self._OnTry)
# Line 101  class ProjFrame(NonModalDialog): Line 107  class ProjFrame(NonModalDialog):
107    
108          EVT_TEXT(self, ID_PROJ_PROJNAME, self._OnProjName)          EVT_TEXT(self, ID_PROJ_PROJNAME, self._OnProjName)
109    
     def GetReceiver(self):  
         return self.receiver  
   
110      def _OnTry(self, event):      def _OnTry(self, event):
111          self.__SetProjection()          self.__SetProjection()
112            self.haveTried = True
113    
114      def _OnRevert(self, event):      def _OnRevert(self, event):
115          if self.originalProjection != -1:          if self.haveTried:
116              self.receiver.SetProjection(self.originalProjection)              self.receiver.SetProjection(self.originalProjection)
117                self.haveTried = False
118    
119      def _OnOK(self, event):      def _OnOK(self, event):
120          self.__SetProjection()          self.__SetProjection()
# Line 119  class ProjFrame(NonModalDialog): Line 124  class ProjFrame(NonModalDialog):
124          self.Close()          self.Close()
125    
126      def _OnNew(self, event):      def _OnNew(self, event):
127          self.__DoOnNew()  
128            # clear all selections
129            sel = self.availprojs.GetSelections()
130            for index in sel:
131                self.availprojs.SetSelection(index, False)
132    
133            self.projname.Clear()
134    
135            # supply a projection panel if there wasn't one
136            if self.curProjPanel is None:
137                self.projchoice.SetSelection(0)
138                self.__DoOnProjChoice()
139    
140            self.curProjPanel.Clear()
141    
142      def _OnSave(self, event):      def _OnSave(self, event):
143    
# Line 133  class ProjFrame(NonModalDialog): Line 151  class ProjFrame(NonModalDialog):
151          newproj = self.__GetProjection()          newproj = self.__GetProjection()
152    
153          if newproj is not None:          if newproj is not None:
154              projfile.Remove(proj)              projfile.Replace(proj, newproj)
             projfile.Add(newproj)  
155              try:              try:
156                  WriteProjFile(projfile)                  WriteProjFile(projfile)
157              except IOError, (errno, errstr):              except IOError, (errno, errstr):
158                  wxMessageDialog(self,                  self.__ShowError(projfile.GetFilename(), errstr)
159                      _("The following error occured:\n") +              self.availprojs.SetClientData([newproj, projfile])
                     projfile.GetFilename() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
             self.__FillAvailList()  
160    
161      def _OnAddToList(self, event):      def _OnAddToList(self, event):
162    
# Line 152  class ProjFrame(NonModalDialog): Line 166  class ProjFrame(NonModalDialog):
166              try:              try:
167                  WriteProjFile(self.__usrProjFile)                  WriteProjFile(self.__usrProjFile)
168              except IOError, (errno, errstr):              except IOError, (errno, errstr):
169                  wxMessageDialog(self,                  self.__ShowError(self.__userProjFile.GetFilename(), errstr)
170                      _("The following error occured:\n") +  
                     self.__usrProjFile.GetFilename() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
171              self.__FillAvailList()              self.__FillAvailList()
172    
173      def _OnProjAvail(self, event):      def _OnProjAvail(self, event):
# Line 174  class ProjFrame(NonModalDialog): Line 186  class ProjFrame(NonModalDialog):
186                      self.__usrProjFile.Add(proj)                      self.__usrProjFile.Add(proj)
187                  WriteProjFile(self.__usrProjFile)                  WriteProjFile(self.__usrProjFile)
188              except IOError, (errno, errstr):              except IOError, (errno, errstr):
189                  wxMessageDialog(self,                  self.__ShowError(dlg.GetPath(), errstr)
                     _("The following error occured:\n") +  
                     dlg.GetPath() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
190    
191              self.__FillAvailList()              self.__FillAvailList()
192    
# Line 204  class ProjFrame(NonModalDialog): Line 213  class ProjFrame(NonModalDialog):
213              try:              try:
214                  WriteProjFile(projFile)                  WriteProjFile(projFile)
215              except IOError, (errno, errstr):              except IOError, (errno, errstr):
216                  wxMessageDialog(self,                  self.__ShowError(dlg.GetPath(), errstr)
                     _("The following error occured:\n") +  
                     dlg.GetPath() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
217    
218          dlg.Destroy()          dlg.Destroy()
219    
# Line 242  class ProjFrame(NonModalDialog): Line 248  class ProjFrame(NonModalDialog):
248          try:          try:
249              WriteProjFile(projfile)              WriteProjFile(projfile)
250          except IOError, (errno, errstr):          except IOError, (errno, errstr):
251              wxMessageDialog(self,              self.__ShowError(projfile.GetFilename(), errstr)
                 _("The following error occured:\n") +  
                 projfile.GetFilename() + "\n" + errstr,  
                 _("Error"), wxOK | wxICON_ERROR).ShowModal()  
   
252    
253          self.__FillAvailList()          self.__FillAvailList()
254    
# Line 258  class ProjFrame(NonModalDialog): Line 260  class ProjFrame(NonModalDialog):
260          if newselection != -1 and self.availprojs.GetCount() > 0:          if newselection != -1 and self.availprojs.GetCount() > 0:
261              self.availprojs.SetSelection(newselection)              self.availprojs.SetSelection(newselection)
262    
263            self.__VerifyButtons()
264    
265      def _OnProjName(self, event):      def _OnProjName(self, event):
266          self.__VerifyButtons()          self.__VerifyButtons()
267    
268        def __ShowError(self, filename, errstr):
269            wxMessageDialog(self,
270                _("The following error occured:\n") +
271                filename + "\n" + errstr,
272                _("Error"), wxOK | wxICON_ERROR).ShowModal()
273    
274      def __VerifyButtons(self):      def __VerifyButtons(self):
275            """Enable or disable the appropriate buttons based on the
276            current state of the dialog.
277            """
278    
279          sel = self.availprojs.GetSelections()          sel = self.availprojs.GetSelections()
280    
281          self.button_import.Enable(True)          self.button_import.Enable(True)
# Line 271  class ProjFrame(NonModalDialog): Line 285  class ProjFrame(NonModalDialog):
285    
286          self.panel_edit.Enable(True)          self.panel_edit.Enable(True)
287    
288            for ctrl in [self.button_import,
289                         self.button_export,
290                         self.button_remove,
291                         self.button_save,
292                         self.button_add,
293                         self.projchoice,
294                         self.projname,
295                         self.panel_edit]:
296                ctrl.Enable(True)
297    
298            if self.curProjPanel is not None:
299                self.curProjPanel.Enable(True)
300    
301          if len(sel) == 0:          if len(sel) == 0:
302              self.button_import.Enable(True)              self.button_import.Enable(True)
303              self.button_export.Enable(False)              self.button_export.Enable(False)
# Line 284  class ProjFrame(NonModalDialog): Line 311  class ProjFrame(NonModalDialog):
311              self.button_add.Enable(len(self.projname.GetValue()) > 0)              self.button_add.Enable(len(self.projname.GetValue()) > 0)
312    
313              if proj is None:              if proj is None:
314                  self.button_export.Enable(False)                  # <None> is selected
315                    for ctrl in [self.button_export,
316                                 self.button_remove,
317                                 self.button_save,
318                                 self.button_add,
319                                 self.projchoice,
320                                 self.projname]:
321                        ctrl.Enable(False)
322    
323                    if self.curProjPanel is not None:
324                        self.curProjPanel.Enable(False)
325    
326                elif proj is self.originalProjection:
327                  self.button_remove.Enable(False)                  self.button_remove.Enable(False)
328    
329              if projFile is None:              if projFile is None:
# Line 293  class ProjFrame(NonModalDialog): Line 332  class ProjFrame(NonModalDialog):
332          else:          else:
333              self.panel_edit.Enable(False)              self.panel_edit.Enable(False)
334    
     def __DoOnNew(self):  
         sel = self.availprojs.GetSelections()  
         if len(sel) != 0:  
             self.availprojs.SetSelection(sel, False)  
         self.projname.Clear()  
         self.curProjPanel.Clear()  
   
335      def __DoOnProjAvail(self):      def __DoOnProjAvail(self):
336    
337          sel = self.availprojs.GetSelections()          sel = self.availprojs.GetSelections()
# Line 311  class ProjFrame(NonModalDialog): Line 343  class ProjFrame(NonModalDialog):
343              if proj is None:              if proj is None:
344                  # user selected <None>                  # user selected <None>
345                  self.projname.Clear()                  self.projname.Clear()
346                    
347              else:              else:
348                            
349                  if projfile is not None:                  if projfile is not None:
# Line 327  class ProjFrame(NonModalDialog): Line 360  class ProjFrame(NonModalDialog):
360                      if myProjType == projType:                      if myProjType == projType:
361                          self.projchoice.SetSelection(i)                          self.projchoice.SetSelection(i)
362                          self.__DoOnProjChoice()                          self.__DoOnProjChoice()
363    
364                            #
365                            # self.curProjPanel should not be null
366                            # after a call to __DoOnProjChoice
367                            #
368                            assert self.curProjPanel is not None
369    
370                          self.curProjPanel.SetProjection(proj)                          self.curProjPanel.SetProjection(proj)
371                      i += 1                      i += 1
372    
# Line 336  class ProjFrame(NonModalDialog): Line 376  class ProjFrame(NonModalDialog):
376          self.__DoOnProjChoice()          self.__DoOnProjChoice()
377    
378      def __DoOnProjChoice(self):      def __DoOnProjChoice(self):
379            """Create and layout a projection panel based on the selected
380            projection type.
381    
382            This is necessary to have in seperate method since calls to
383            wxChoice.SetSelection() do not trigger an event.
384    
385            At the end of this method self.curProjPanel will not be None
386            if there was a item selected.
387            """
388    
389          choice = self.projchoice          choice = self.projchoice
390    
391          sel = choice.GetSelection()          sel = choice.GetSelection()
# Line 361  class ProjFrame(NonModalDialog): Line 411  class ProjFrame(NonModalDialog):
411              self.topBox.SetSizeHints(self)              self.topBox.SetSizeHints(self)
412    
413      def __SetProjection(self):      def __SetProjection(self):
414            """Set the receiver's projection."""
415    
416          #          #
417          # save the original projection only once in case          # save the original projection only once in case
418          # we try to apply several different projections          # we try to apply several different projections
419          #          #
         if self.originalProjection == -1:  
             self.originalProjection = self.receiver.GetProjection()  
   
420          self.receiver.SetProjection(self.__GetProjection())          self.receiver.SetProjection(self.__GetProjection())
421    
422      def __GetProjection(self):      def __GetProjection(self):
423          """Return the packaged projection.          """Return a suitable Projection object based on the current
424            state of the dialog box selections.
425    
426          Could be None.          Could be None.
427          """          """
428    
429          proj = None          sel = self.availprojs.GetSelections()
430            assert len(sel) < 2, "button should be disabled"
431    
432    
433            if len(sel) == 1:
434                proj = self.availprojs.GetClientData(sel[0])[CLIENT_PROJ]
435                if proj is None:
436                    # <None> is selected
437                    return None
438    
439            #
440            # self.curProjPanel should always contain the most
441            # relevant data for a projection
442            #
443          if self.curProjPanel is not None:          if self.curProjPanel is not None:
444              proj = Projection(self.curProjPanel.GetParameters())              return Projection(self.curProjPanel.GetParameters(),
445              proj.SetName(self.projname.GetValue())                                self.projname.GetValue())
446    
447          return proj          return None
448    
449      def __FillAvailList(self, selectCurrent = False):      def __FillAvailList(self, selectCurrent = False):
450            """Populate the list of available projections.
451            
452            selectCurrent -- if True, select the projection used by
453                             the receiver, otherwise select nothing.
454            """
455    
456          self.availprojs.Clear()          self.availprojs.Clear()
457    
458          #          #
# Line 409  class ProjFrame(NonModalDialog): Line 476  class ProjFrame(NonModalDialog):
476          #          #
477          self.availprojs.Append("<None>", (None, None))          self.availprojs.Append("<None>", (None, None))
478    
479          self.__sysProjFile = None          # proj is only None when <None> should be selected
480          self.__usrProjFile = None          if proj is None and selectCurrent:
481                self.availprojs.SetSelection(0)
482                self.availprojs.SetFirstItem(0)
483    
484          projfile = GetSystemProjFiles()          projfile = GetSystemProjFiles()
485          if len(projfile) > 0:          projfile = projfile[0]
486              projfile = projfile[0]          for proj in projfile.GetProjections():
487              for proj in projfile.GetProjections():              self.availprojs.Append(proj.GetName(), [proj, projfile])
488                  self.availprojs.Append(proj.GetName(), [proj, projfile])          self.__sysProjFile = projfile
             self.__sysProjFile = projfile  
489    
490          projfile = GetUserProjFiles()          projfile = GetUserProjFiles()
491          if len(projfile) > 0:          projfile = projfile[0]
492              projfile = projfile[0]          for proj in projfile.GetProjections():
493              for proj in projfile.GetProjections():              self.availprojs.Append(proj.GetName(), [proj, projfile])
494                  self.availprojs.Append(proj.GetName(), [proj, projfile])          self.__usrProjFile = projfile
             self.__usrProjFile = projfile  
495    
496          for proj, name, clazz in self.projPanels:          for proj, name, clazz in self.projPanels:
497              self.projchoice.Append(name, [clazz, None])              self.projchoice.Append(name, [clazz, None])
498    
499      def __set_properties(self):      def __set_properties(self):
500    
501          self.SetTitle(_("Projections"))          #self.availprojs.SetSelection(0)
         self.availprojs.SetSelection(0)  
502          self.projchoice.SetSelection(0)          self.projchoice.SetSelection(0)
503    
504          self.__FillAvailList(selectCurrent = True)          self.__FillAvailList(selectCurrent = True)

Legend:
Removed from v.747  
changed lines
  Added in v.760

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26