/[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 750 by jonathan, Fri Apr 25 11:02:08 2003 UTC revision 751 by jonathan, Fri Apr 25 14:23:19 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)              proj.SetProjection(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)
                     _("The following error occured:\n") +  
                     projfile.GetFilename() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
             self.__FillAvailList()  
159    
160      def _OnAddToList(self, event):      def _OnAddToList(self, event):
161    
# Line 152  class ProjFrame(NonModalDialog): Line 165  class ProjFrame(NonModalDialog):
165              try:              try:
166                  WriteProjFile(self.__usrProjFile)                  WriteProjFile(self.__usrProjFile)
167              except IOError, (errno, errstr):              except IOError, (errno, errstr):
168                  wxMessageDialog(self,                  self.__ShowError(self.__userProjFile.GetFilename(), errstr)
169                      _("The following error occured:\n") +  
                     self.__usrProjFile.GetFilename() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
170              self.__FillAvailList()              self.__FillAvailList()
171    
172      def _OnProjAvail(self, event):      def _OnProjAvail(self, event):
# Line 174  class ProjFrame(NonModalDialog): Line 185  class ProjFrame(NonModalDialog):
185                      self.__usrProjFile.Add(proj)                      self.__usrProjFile.Add(proj)
186                  WriteProjFile(self.__usrProjFile)                  WriteProjFile(self.__usrProjFile)
187              except IOError, (errno, errstr):              except IOError, (errno, errstr):
188                  wxMessageDialog(self,                  self.__ShowError(dlg.GetPath(), errstr)
                     _("The following error occured:\n") +  
                     dlg.GetPath() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
189    
190              self.__FillAvailList()              self.__FillAvailList()
191    
# Line 204  class ProjFrame(NonModalDialog): Line 212  class ProjFrame(NonModalDialog):
212              try:              try:
213                  WriteProjFile(projFile)                  WriteProjFile(projFile)
214              except IOError, (errno, errstr):              except IOError, (errno, errstr):
215                  wxMessageDialog(self,                  self.__ShowError(dlg.GetPath(), errstr)
                     _("The following error occured:\n") +  
                     dlg.GetPath() + "\n" + errstr,  
                     _("Error"), wxOK | wxICON_ERROR).ShowModal()  
216    
217          dlg.Destroy()          dlg.Destroy()
218    
# Line 242  class ProjFrame(NonModalDialog): Line 247  class ProjFrame(NonModalDialog):
247          try:          try:
248              WriteProjFile(projfile)              WriteProjFile(projfile)
249          except IOError, (errno, errstr):          except IOError, (errno, errstr):
250              wxMessageDialog(self,              self.__ShowError(projfile.GetFilename(), errstr)
                 _("The following error occured:\n") +  
                 projfile.GetFilename() + "\n" + errstr,  
                 _("Error"), wxOK | wxICON_ERROR).ShowModal()  
   
251    
252          self.__FillAvailList()          self.__FillAvailList()
253    
# Line 258  class ProjFrame(NonModalDialog): Line 259  class ProjFrame(NonModalDialog):
259          if newselection != -1 and self.availprojs.GetCount() > 0:          if newselection != -1 and self.availprojs.GetCount() > 0:
260              self.availprojs.SetSelection(newselection)              self.availprojs.SetSelection(newselection)
261    
262            self.__VerifyButtons()
263    
264      def _OnProjName(self, event):      def _OnProjName(self, event):
265          self.__VerifyButtons()          self.__VerifyButtons()
266    
267        def __ShowError(self, filename, errstr):
268            wxMessageDialog(self,
269                _("The following error occured:\n") +
270                filename + "\n" + errstr,
271                _("Error"), wxOK | wxICON_ERROR).ShowModal()
272    
273      def __VerifyButtons(self):      def __VerifyButtons(self):
274            """Enable or disable the appropriate buttons based on the
275            current state of the dialog.
276            """
277    
278          sel = self.availprojs.GetSelections()          sel = self.availprojs.GetSelections()
279    
280          self.button_import.Enable(True)          self.button_import.Enable(True)
# Line 271  class ProjFrame(NonModalDialog): Line 284  class ProjFrame(NonModalDialog):
284    
285          self.panel_edit.Enable(True)          self.panel_edit.Enable(True)
286    
287            for ctrl in [self.button_import,
288                         self.button_export,
289                         self.button_remove,
290                         self.button_save,
291                         self.button_add,
292                         self.projchoice,
293                         self.projname,
294                         self.panel_edit]:
295                ctrl.Enable(True)
296    
297            if self.curProjPanel is not None:
298                self.curProjPanel.Enable(True)
299    
300          if len(sel) == 0:          if len(sel) == 0:
301              self.button_import.Enable(True)              self.button_import.Enable(True)
302              self.button_export.Enable(False)              self.button_export.Enable(False)
# Line 284  class ProjFrame(NonModalDialog): Line 310  class ProjFrame(NonModalDialog):
310              self.button_add.Enable(len(self.projname.GetValue()) > 0)              self.button_add.Enable(len(self.projname.GetValue()) > 0)
311    
312              if proj is None:              if proj is None:
313                  self.button_export.Enable(False)                  # <None> is selected
314                    for ctrl in [self.button_export,
315                                 self.button_remove,
316                                 self.button_save,
317                                 self.button_add,
318                                 self.projchoice,
319                                 self.projname]:
320                        ctrl.Enable(False)
321    
322                    if self.curProjPanel is not None:
323                        self.curProjPanel.Enable(False)
324    
325                elif proj is self.originalProjection:
326                  self.button_remove.Enable(False)                  self.button_remove.Enable(False)
327    
328              if projFile is None:              if projFile is None:
# Line 293  class ProjFrame(NonModalDialog): Line 331  class ProjFrame(NonModalDialog):
331          else:          else:
332              self.panel_edit.Enable(False)              self.panel_edit.Enable(False)
333    
     def __DoOnNew(self):  
         sel = self.availprojs.GetSelections()  
         if len(sel) != 0:  
             self.availprojs.SetSelection(sel, False)  
         self.projname.Clear()  
         self.curProjPanel.Clear()  
   
334      def __DoOnProjAvail(self):      def __DoOnProjAvail(self):
335    
336          sel = self.availprojs.GetSelections()          sel = self.availprojs.GetSelections()
# Line 311  class ProjFrame(NonModalDialog): Line 342  class ProjFrame(NonModalDialog):
342              if proj is None:              if proj is None:
343                  # user selected <None>                  # user selected <None>
344                  self.projname.Clear()                  self.projname.Clear()
345                    
346              else:              else:
347                            
348                  if projfile is not None:                  if projfile is not None:
# Line 327  class ProjFrame(NonModalDialog): Line 359  class ProjFrame(NonModalDialog):
359                      if myProjType == projType:                      if myProjType == projType:
360                          self.projchoice.SetSelection(i)                          self.projchoice.SetSelection(i)
361                          self.__DoOnProjChoice()                          self.__DoOnProjChoice()
362    
363                            #
364                            # self.curProjPanel should not be null
365                            # after a call to __DoOnProjChoice
366                            #
367                            assert self.curProjPanel is not None
368    
369                          self.curProjPanel.SetProjection(proj)                          self.curProjPanel.SetProjection(proj)
370                      i += 1                      i += 1
371    
# Line 336  class ProjFrame(NonModalDialog): Line 375  class ProjFrame(NonModalDialog):
375          self.__DoOnProjChoice()          self.__DoOnProjChoice()
376    
377      def __DoOnProjChoice(self):      def __DoOnProjChoice(self):
378            """Create and layout a projection panel based on the selected
379            projection type.
380    
381            This is necessary to have in seperate method since calls to
382            wxChoice.SetSelection() do not trigger an event.
383    
384            At the end of this method self.curProjPanel will not be None
385            if there was a item selected.
386            """
387    
388          choice = self.projchoice          choice = self.projchoice
389    
390          sel = choice.GetSelection()          sel = choice.GetSelection()
# Line 361  class ProjFrame(NonModalDialog): Line 410  class ProjFrame(NonModalDialog):
410              self.topBox.SetSizeHints(self)              self.topBox.SetSizeHints(self)
411    
412      def __SetProjection(self):      def __SetProjection(self):
413            """Set the receiver's projection."""
414    
415          #          #
416          # save the original projection only once in case          # save the original projection only once in case
417          # we try to apply several different projections          # we try to apply several different projections
418          #          #
         if self.originalProjection == -1:  
             self.originalProjection = self.receiver.GetProjection()  
   
419          self.receiver.SetProjection(self.__GetProjection())          self.receiver.SetProjection(self.__GetProjection())
420    
421      def __GetProjection(self):      def __GetProjection(self):
422          """Return the packaged projection.          """Return a suitable Projection object based on the current
423            state of the dialog box selections.
424    
425          Could be None.          Could be None.
426          """          """
427    
428          proj = None          sel = self.availprojs.GetSelections()
429            assert len(sel) < 2, "button should be disabled"
430    
431    
432            if len(sel) == 1:
433                proj = self.availprojs.GetClientData(sel[0])[CLIENT_PROJ]
434                if proj is None:
435                    # <None> is selected
436                    return None
437    
438            #
439            # self.curProjPanel should always contain the most
440            # relevant data for a projection
441            #
442          if self.curProjPanel is not None:          if self.curProjPanel is not None:
443              proj = Projection(self.curProjPanel.GetParameters())              return Projection(self.curProjPanel.GetParameters(),
444              proj.SetName(self.projname.GetValue())                                self.projname.GetValue())
445    
446          return proj          return None
447    
448      def __FillAvailList(self, selectCurrent = False):      def __FillAvailList(self, selectCurrent = False):
449            """Populate the list of available projections.
450            
451            selectCurrent -- if True, select the projection used by
452                             the receiver, otherwise select nothing.
453            """
454    
455          self.availprojs.Clear()          self.availprojs.Clear()
456    
457          #          #
# Line 409  class ProjFrame(NonModalDialog): Line 475  class ProjFrame(NonModalDialog):
475          #          #
476          self.availprojs.Append("<None>", (None, None))          self.availprojs.Append("<None>", (None, None))
477    
478          self.__sysProjFile = None          # proj is only None when <None> should be selected
479          self.__usrProjFile = None          if proj is None and selectCurrent:
480                self.availprojs.SetSelection(0)
481                self.availprojs.SetFirstItem(0)
482    
483          projfile = GetSystemProjFiles()          projfile = GetSystemProjFiles()
484          if len(projfile) > 0:          projfile = projfile[0]
485              projfile = projfile[0]          for proj in projfile.GetProjections():
486              for proj in projfile.GetProjections():              self.availprojs.Append(proj.GetName(), [proj, projfile])
487                  self.availprojs.Append(proj.GetName(), [proj, projfile])          self.__sysProjFile = projfile
             self.__sysProjFile = projfile  
488    
489          projfile = GetUserProjFiles()          projfile = GetUserProjFiles()
490          if len(projfile) > 0:          projfile = projfile[0]
491              projfile = projfile[0]          for proj in projfile.GetProjections():
492              for proj in projfile.GetProjections():              self.availprojs.Append(proj.GetName(), [proj, projfile])
493                  self.availprojs.Append(proj.GetName(), [proj, projfile])          self.__usrProjFile = projfile
             self.__usrProjFile = projfile  
494    
495          for proj, name, clazz in self.projPanels:          for proj, name, clazz in self.projPanels:
496              self.projchoice.Append(name, [clazz, None])              self.projchoice.Append(name, [clazz, None])
497    
498      def __set_properties(self):      def __set_properties(self):
499    
         self.SetTitle(_("Projections"))  
500          self.availprojs.SetSelection(0)          self.availprojs.SetSelection(0)
501          self.projchoice.SetSelection(0)          self.projchoice.SetSelection(0)
502    

Legend:
Removed from v.750  
changed lines
  Added in v.751

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26