Gnumed.timelinelib.wxgui.utils

Package Gnumed :: Package timelinelib :: Package wxgui :: Module utils |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.utils

      1  # Copyright (C) 2009, 2010, 2011  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  import wx
     20
     21  from timelinelib.db.exceptions import TimelineIOError
     22  from timelinelib.db.objects.category import sort_categories
     23
     24
     25  # Border, in pixels, between controls in a window (should always be used when
     26  # border is needed)
     27  BORDER = 5
     28  # Used by dialogs as a return code when a TimelineIOError has been raised
     29  ID_ERROR = wx.NewId()
     30
     31
     32 -class WildcardHelper(object):
     33
     34 -    def __init__(self, name, extensions):
     35          self.name = name
     36          self.ext_data = {}
     37          self.ext_names = []
     38          self._extract_ext_info(extensions)
     39
     40 -    def wildcard_string(self):
     41          return "%s (%s)|%s" % (
     42              self.name,
     43              ", ".join(["*." + e for e in self.ext_names]),
     44              ";".join(["*." + e for e in self.ext_names]))
     45
     46 -    def get_path(self, dialog):
     47          path = dialog.GetPath()
     48          for ext_name in self.ext_names:
     49              if path.endswith("." + ext_name):
     50                  return path
     51          return "%s.%s" % (path, self.ext_names[0])
     52
     53 -    def get_extension_data(self, path):
     54          split_path = path.split(".")
     55          if len(split_path) > 1:
     56              ext_name = split_path[-1]
     57              return self.ext_data.get(ext_name, None)
     58          return None
     59
     60 -    def _extract_ext_info(self, extensions):
     61          for ext in extensions:
     62              if isinstance(ext, tuple):
     63                  name, data = ext
     64                  self.ext_data[name] = data
     65                  self.ext_names.append(name)
     66              else:
     67                  self.ext_names.append(ext)
     68
     69
     70 -def category_tree(category_list, parent=None, remove=None):
     71      """
     72      Transform flat list of categories to a tree based on parent attribute.
     73
     74      The top-level categories have the given parent and each level in the tree
     75      is sorted.
     76
     77      If remove is given then the subtree with remove as root will not be
     78      included.
     79
     80      The tree is represented as a list of tuples, (cat, sub-tree), where cat is
     81      the parent category and subtree is the same tree representation of the
     82      children.
     83      """
     84      children = [child for child in category_list
     85                  if (child.parent is parent and child is not remove)]
     86      sorted_children = sort_categories(children)
     87      tree = [(x, category_tree(category_list, x, remove))
     88              for x in sorted_children]
     89      return tree
     90
     91
     92 -def show_modal(fn_create_dialog, fn_handle_db_error, fn_success=None):
     93      """Show a modal dialog using error handling pattern."""
     94      try:
     95          dialog = fn_create_dialog()
     96      except TimelineIOError, e:
     97          fn_handle_db_error(e)
     98      else:
     99          dialog_result = dialog.ShowModal()
    100          if dialog_result == ID_ERROR:
    101              fn_handle_db_error(dialog.error)
    102          elif fn_success:
    103              fn_success(dialog)
    104          dialog.Destroy()
    105
    106
    107 -def create_dialog_db_error_handler(dialog):
    108      def handler(error):
    109          handle_db_error_in_dialog(dialog, error)
    110      return handler
    111
    112
    113 -def handle_db_error_in_dialog(dialog, error):
    114      if dialog.IsShown():
    115          # Close the dialog and let the code that created it handle the error.
    116          # Eventually this error will end up in the main frame (which is the
    117          # only object which can handle the error properly).
    118          dialog.error = error
    119          dialog.EndModal(ID_ERROR)
    120      else:
    121          # Re-raise the TimelineIOError exception and let the code that created
    122          # the dialog handle the error.
    123          raise error
    124
    125
    126 -def _set_focus_and_select(ctrl):
    127      ctrl.SetFocus()
    128      if hasattr(ctrl, "SelectAll"):
    129          ctrl.SelectAll()
    130
    131
    132 -def _display_error_message(message, parent=None):
    133      """Display an error message in a modal dialog box"""
    134      dial = wx.MessageDialog(parent, message, _("Error"), wx.OK | wx.ICON_ERROR)
    135      dial.ShowModal()
    136
    137
    138 -def _ask_question(question, parent=None):
    139      """Ask a yes/no question and return the reply."""
    140      return wx.MessageBox(question, _("Question"),
    141                           wx.YES_NO|wx.CENTRE|wx.NO_DEFAULT, parent)
    142
    143
    144 -def set_wait_cursor(parent):
    145      parent.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
    146
    147
    148 -def set_default_cursor(parent):
    149      parent.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
    150
    151
    152 -def time_picker_for(time_type):
    153      from timelinelib.wxgui.components.numtimepicker import NumTimePicker
    154      from timelinelib.wxgui.components.pydatetimepicker import PyDateTimePicker
    155      from timelinelib.wxgui.components.wxdatetimepicker import WxDateTimePicker
    156      from timelinelib.time import NumTimeType
    157      from timelinelib.time import PyTimeType
    158      from timelinelib.time import WxTimeType
    159      if isinstance(time_type, PyTimeType):
    160          return PyDateTimePicker
    161      elif isinstance(time_type, WxTimeType):
    162          return WxDateTimePicker
    163      elif isinstance(time_type, NumTimeType):
    164          return NumTimePicker
    165      else:
    166          raise ValueError("Unsupported time type: %s" % time_type)
    167