You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. '''
  2. Created on Jan 19, 2014
  3. @author: Chris
  4. '''
  5. import sys
  6. import wx
  7. from gooey.gui import image_repository, events
  8. from gooey.gui.lang.i18n import _
  9. from gooey.gui.pubsub import pub
  10. from gooey.gui.util import wx_util
  11. from gooey.gui.windows import footer, header, layouts
  12. from gooey.gui.windows.runtime_display_panel import RuntimeDisplay
  13. YES = 5103
  14. NO = 5104
  15. class BaseWindow(wx.Frame):
  16. '''
  17. Primary Frame under which all sub-Panels are organized.
  18. '''
  19. def __init__(self, layout_type):
  20. wx.Frame.__init__(self, parent=None, id=-1)
  21. self.SetDoubleBuffered(True)
  22. # type of gui to render
  23. self.layout_type = layout_type
  24. # Components
  25. self.icon = None
  26. self.head_panel = None
  27. self.config_panel = None
  28. self.runtime_display = None
  29. self.foot_panel = None
  30. self.panels = None
  31. self._init_properties()
  32. self._init_components()
  33. self._do_layout()
  34. self.Bind(wx.EVT_SIZE, self.onResize)
  35. self.Bind(wx.EVT_CLOSE, self.onClose)
  36. @property
  37. def window_size(self):
  38. return self.GetSize()
  39. @window_size.setter
  40. def window_size(self, size_tuple):
  41. self.SetSize(size_tuple)
  42. @property
  43. def window_title(self):
  44. return self.GetTitle()
  45. @window_title.setter
  46. def window_title(self, title):
  47. self.SetTitle(title)
  48. @property
  49. def heading_title(self):
  50. return self.head_panel.title
  51. @heading_title.setter
  52. def heading_title(self, text):
  53. self.head_panel.title = text
  54. @property
  55. def heading_subtitle(self):
  56. return self.head_panel.subtitle
  57. @heading_subtitle.setter
  58. def heading_subtitle(self, text):
  59. self.head_panel.subtitle = text
  60. @property
  61. def required_section(self):
  62. return self.config_panel.main_content.required_section
  63. @property
  64. def optional_section(self):
  65. return self.config_panel.main_content.optional_section
  66. @property
  67. def progress_bar(self):
  68. return self.foot_panel.progress_bar
  69. def set_list_contents(self, contents):
  70. self.config_panel.sidebar.set_list_contents(contents)
  71. def set_display_font_style(self, style):
  72. # TODO: make this not stupid
  73. # TODO: _actual_ font support
  74. self.runtime_display.set_font_style(
  75. wx.MODERN if style == 'monospace' else wx.DEFAULT)
  76. def _init_properties(self):
  77. # self.SetTitle(self.build_spec['program_name'])
  78. # self.SetSize(self.build_spec['default_size'])
  79. # # self.SetMinSize((400, 300))
  80. self.icon = wx.Icon(image_repository.program_icon, wx.BITMAP_TYPE_ICO)
  81. self.SetIcon(self.icon)
  82. def _init_components(self):
  83. self.runtime_display = RuntimeDisplay(self)
  84. self.head_panel = header.FrameHeader(parent=self)
  85. self.foot_panel = footer.Footer(self)
  86. self.panels = [self.head_panel, self.config_panel, self.foot_panel]
  87. def _do_layout(self):
  88. sizer = wx.BoxSizer(wx.VERTICAL)
  89. sizer.Add(self.head_panel, 0, wx.EXPAND)
  90. sizer.Add(wx_util.horizontal_rule(self), 0, wx.EXPAND)
  91. if self.layout_type == layouts.COLUMN:
  92. self.config_panel = layouts.ColumnLayout(self)
  93. else:
  94. self.config_panel = layouts.FlatLayout(self)
  95. sizer.Add(self.config_panel, 1, wx.EXPAND)
  96. sizer.Add(self.runtime_display, 1, wx.EXPAND)
  97. self.runtime_display.Hide()
  98. sizer.Add(wx_util.horizontal_rule(self), 0, wx.EXPAND)
  99. sizer.Add(self.foot_panel, 0, wx.EXPAND)
  100. self.SetSizer(sizer)
  101. self.sizer = sizer
  102. def freeze(self):
  103. self.Freeze()
  104. def thaw(self):
  105. self.Thaw()
  106. def enable_stop_button(self):
  107. self.foot_panel.stop_button.Enable()
  108. def disable_stop_button(self):
  109. self.foot_panel.stop_button.Disable()
  110. def show(self, *args):
  111. '''
  112. Looks up the attribute across all available
  113. panels and calls `Show()`
  114. '''
  115. self._set_visibility('Show', *args)
  116. def hide(self, *args):
  117. '''
  118. Looks up the attribute across all available
  119. panels and calls `Show()`
  120. '''
  121. self._set_visibility('Hide', *args)
  122. def _set_visibility(self, action, *args):
  123. '''
  124. Checks for the existence `attr` on a given `panel` and
  125. performs `action` if found
  126. '''
  127. def _set_visibility(obj, attrs):
  128. for attr in attrs:
  129. if hasattr(obj, attr):
  130. instance = getattr(obj, attr)
  131. getattr(instance, action)()
  132. instance.Layout()
  133. for panel in [self, self.head_panel, self.foot_panel, self.config_panel]:
  134. _set_visibility(panel, args)
  135. def hide_all_buttons(self):
  136. self.foot_panel.hide_all_buttons()
  137. def update_console_async(self, msg):
  138. wx.CallAfter(self.runtime_display.append_text, msg)
  139. def update_progress_aync(self, progress, disable_animation=False):
  140. wx.CallAfter(self.UpdateProgressBar, progress, disable_animation)
  141. def onResize(self, evt):
  142. evt.Skip()
  143. def onClose(self, evt):
  144. if evt.CanVeto():
  145. evt.Veto()
  146. pub.send_message(str(events.WINDOW_CLOSE))
  147. def UpdateProgressBar(self, value, disable_animation=False):
  148. pb = self.foot_panel.progress_bar
  149. if value < 0:
  150. pb.Pulse()
  151. else:
  152. value = min(int(value), pb.GetRange())
  153. if pb.GetValue() != value:
  154. # Windows 7 progress bar animation hack
  155. # http://stackoverflow.com/questions/5332616/disabling-net-progressbar-animation-when-changing-value
  156. if disable_animation and sys.platform.startswith("win"):
  157. if pb.GetRange() == value:
  158. pb.SetValue(value)
  159. pb.SetValue(value-1)
  160. else:
  161. pb.SetValue(value+1)
  162. pb.SetValue(value)
  163. def show_dialog(self, title, content, style):
  164. dlg = wx.MessageDialog(None, content, title, style)
  165. result = dlg.ShowModal()
  166. dlg.Destroy()
  167. return result
  168. def show_missing_args_dialog(self):
  169. self.show_dialog(_('error_title'), _('error_required_fields'), wx.ICON_ERROR)
  170. def confirm_exit_dialog(self):
  171. result = self.show_dialog(_('sure_you_want_to_exit'), _('close_program'), wx.YES_NO)
  172. return result == YES
  173. def confirm_stop_dialog(self):
  174. result = self.show_dialog(_('sure_you_want_to_stop'), _('stop_task'), wx.YES_NO)
  175. return result == YES
  176. if __name__ == '__main__':
  177. pass