codemonkey

SaveAsOziWaypoingFileDialog (rev 2)

Jan 14th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SaveAsOziWaypointFileDialog(wx.Dialog):
  2.     def __init__(self, parent, document, path):
  3.         wx.Dialog.__init__(self, parent, -1, 'Save as Ozi waypoint file',
  4.                            style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
  5.  
  6.         self.document = document
  7.         self.path = path
  8.  
  9.         self.initialize_ui()
  10.  
  11.         # Increase the width of the dialog and centre it on the screen.
  12.         self.SetSizeWH(600, -1)
  13.         self.CenterOnScreen()
  14.  
  15.         self.Bind(wx.EVT_BUTTON, self.on_button)
  16.         self.Bind(wx.EVT_RADIOBUTTON, self.on_radio_button)
  17.  
  18.     def _column_picker_row(self, parent, grid, row_index, label):
  19.         # The label.
  20.         grid.Add(
  21.             wx.StaticText(parent, -1, label), (row_index, 0),
  22.             (1, 1), wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL
  23.         )
  24.  
  25.         # The text control.
  26.         tc = wx.TextCtrl(parent, -1)
  27.         tc.Font = wx.Font(*const.FONT_FIXEDWIDTH)
  28.  
  29.         grid.Add(
  30.             tc, (row_index, 1), (1, 1),
  31.             wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND
  32.         )
  33.  
  34.         # The "add column" button.
  35.         add_btn = wx.Button(parent, -1, '+', style=wx.BU_EXACTFIT)
  36.         self.Bind(
  37.             wx.EVT_BUTTON, partial(self.on_add_to_tc, tc), source=add_btn
  38.         )
  39.  
  40.         grid.Add(add_btn, (row_index, 2), (1, 1), wx.ALIGN_CENTER_VERTICAL)
  41.  
  42.         # Tie the add button to the text control for future use.
  43.         tc.add_column_button = add_btn
  44.  
  45.         return tc
  46.  
  47.     def initialize_ui(self):
  48.         # Create a main sizer to get padding from the edges of the window, and
  49.         # create a layout sizer inside it for positioning controls.
  50.         main_sizer = wx.BoxSizer(wx.VERTICAL)
  51.  
  52.         # Add the text controls for the waypoint info.
  53.         waypoint_info_box = wx.StaticBox(self, -1, 'Waypoint info')
  54.         waypoint_info_box_sizer = wx.StaticBoxSizer(waypoint_info_box)
  55.         waypoint_info_layout = wx.GridBagSizer(5, 5)
  56.  
  57.         self.tc_wpname = self._column_picker_row(
  58.             waypoint_info_box, waypoint_info_layout, 0, 'Waypoint name'
  59.         )
  60.         self.tc_wpdescr = self._column_picker_row(
  61.             waypoint_info_box, waypoint_info_layout, 1, 'Waypoint description'
  62.         )
  63.         waypoint_info_layout.AddGrowableCol(1)
  64.  
  65.         waypoint_info_box_sizer.Add(
  66.             waypoint_info_layout, 1, wx.ALL | wx.EXPAND, 10
  67.         )
  68.         main_sizer.Add(
  69.             waypoint_info_box_sizer, 0, flag=wx.ALL | wx.EXPAND, border=10
  70.         )
  71.  
  72.         # Add the controls for waypoint coordinates.
  73.         coords_box = wx.StaticBox(self, -1, 'Waypoint coordinates')
  74.         coords_box_sizer = wx.StaticBoxSizer(coords_box,
  75.                                                       wx.HORIZONTAL)
  76.         waypoint_coords_layout = wx.GridBagSizer(5, 5)
  77.  
  78.         wgs84_sizer = wx.GridBagSizer(5, 5)
  79.  
  80.         # The WGS 84 radio button.
  81.         self.rb_wgs84 = wx.RadioButton(coords_box, -1, 'WGS 84 decimals')
  82.         self.rb_wgs84.Value = True
  83.         wgs84_sizer.Add(self.rb_wgs84, (0, 0), (1, 2), wx.BOTTOM, 10)
  84.  
  85.         # The column WGS 84 column pickers.
  86.         self.tc_lat = self._column_picker_row(
  87.             coords_box, wgs84_sizer, 1, 'Latitude'
  88.         )
  89.         self.tc_lng = self._column_picker_row(
  90.             coords_box, wgs84_sizer, 2, 'Longitude'
  91.         )
  92.         wgs84_sizer.AddGrowableCol(1)
  93.  
  94.         # The UTM coordinate radio button and column pickers.
  95.         utm_sizer = wx.GridBagSizer(5, 5)
  96.  
  97.         self.rb_utm = wx.RadioButton(coords_box, -1, 'UTM')
  98.         utm_sizer.Add(self.rb_utm, (0, 0), (1, 2), wx.BOTTOM, 10)
  99.  
  100.         self.tc_utmz = self._column_picker_row(
  101.             coords_box, utm_sizer, 1, 'Zone'
  102.         )
  103.         self.tc_utmx = self._column_picker_row(
  104.             coords_box, utm_sizer, 2, 'Northing'
  105.         )
  106.         self.tc_utmy = self._column_picker_row(
  107.             coords_box, utm_sizer, 3, 'Easting'
  108.         )
  109.         utm_sizer.AddGrowableCol(1)
  110.  
  111.         # Disable the UTM controls initially.
  112.         for tc in [self.tc_utmz, self.tc_utmx, self.tc_utmy]:
  113.             tc.Disable()
  114.             tc.add_column_button.Disable()
  115.  
  116.         coords_box_sizer.Add(wgs84_sizer, 1, wx.EXPAND | wx.ALL, 10)
  117.         coords_box_sizer.Add(utm_sizer, 1, wx.EXPAND | wx.ALL, 10)
  118.         main_sizer.Add(coords_box_sizer, 0, wx.ALL | wx.EXPAND, border=10)
  119.  
  120.         # Add the save and cancel buttons.
  121.         button_sizer = wx.BoxSizer(wx.HORIZONTAL)
  122.         button_sizer.Add(wx.Button(self, wx.ID_OK, 'Save'), 0, wx.RIGHT, 5)
  123.         button_sizer.Add(wx.Button(self, wx.ID_CANCEL, 'Cancel'), 0)
  124.  
  125.         main_sizer.AddStretchSpacer()
  126.         main_sizer.Add(wx.StaticLine(self, -1), flag=wx.EXPAND | wx.ALL,
  127.                        border=5)
  128.         main_sizer.Add(button_sizer, flag=wx.ALIGN_RIGHT | wx.ALL,
  129.                        border=5)
  130.  
  131.         # Set the OK button to the default button.
  132.         self.FindWindowById(wx.ID_OK).SetDefault()
  133.  
  134.         self.SetSizerAndFit(main_sizer)
  135.  
  136.     def on_add_to_tc(self, tc, event):
  137.         """Event handler for the small + buttons."""
  138.         # Give the user a list of column headers to choose from.
  139.         headers = self.document.table.column_headers
  140.  
  141.         dialog = wx.SingleChoiceDialog(
  142.             self, 'Which column would you like to add?', 'Choose a column',
  143.             headers, wx.CHOICEDLG_STYLE
  144.         )
  145.  
  146.         if dialog.ShowModal() == wx.ID_OK:
  147.             choice = dialog.StringSelection
  148.             tc.AppendText('{{{}}}'.format(choice))
  149.  
  150.     def on_button(self, event):
  151.         # Validate input when OK is clicked. If there are any errors, inform
  152.         # the user and stop the dialog from closing.
  153.         if event.Id == wx.ID_OK:
  154.             pass
  155.         else:
  156.             event.Skip()
  157.  
  158.     def on_radio_button(self, event):
  159.         source = self.FindWindowById(event.Id)
  160.         wgs84_checked = source is self.rb_wgs84 and event.Checked()
  161.         utm_checked = not wgs84_checked
  162.  
  163.         # Set the control states.
  164.         for tc in [self.tc_lat, self.tc_lng]:
  165.             tc.Enable(wgs84_checked)
  166.             tc.add_column_button.Enable(wgs84_checked)
  167.  
  168.         for tc in [self.tc_utmz, self.tc_utmx, self.tc_utmy]:
  169.             tc.Enable(utm_checked)
  170.             tc.add_column_button.Enable(utm_checked)
Advertisement
Add Comment
Please, Sign In to add comment