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, 2012, 2013, 2014, 2015, 2016, 2017, 2018  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.canvas.data import sort_categories
     22
     23
     24  # Border, in pixels, between controls in a window (should always be used when
     25  # border is needed)
     26  BORDER = 5
     27  unlock_function = None
     28
     29
     30 -class WildcardHelper(object):
     31
     32 -    def __init__(self, name, extensions):
     33          self.name = name
     34          self.ext_data = {}
     35          self.ext_names = []
     36          self._extract_ext_info(extensions)
     37
     38 -    def wildcard_string(self):
     39          return "%s (%s)|%s" % (
     40              self.name,
     41              ", ".join(["*." + e for e in self.ext_names]),
     42              ";".join(["*." + e for e in self.ext_names]))
     43
     44 -    def get_path(self, dialog):
     45          path = dialog.GetPath()
     46          for ext_name in self.ext_names:
     47              if path.endswith("." + ext_name):
     48                  return path
     49          return "%s.%s" % (path, self.ext_names[0])
     50
     51 -    def get_extension_data(self, path):
     52          split_path = path.split(".")
     53          if len(split_path) > 1:
     54              ext_name = split_path[-1]
     55              return self.ext_data.get(ext_name, None)
     56          return None
     57
     58 -    def _extract_ext_info(self, extensions):
     59          for ext in extensions:
     60              if isinstance(ext, tuple):
     61                  name, data = ext
     62                  self.ext_data[name] = data
     63                  self.ext_names.append(name)
     64              else:
     65                  self.ext_names.append(ext)
     66
     67
     68 -class PopupTextWindow(wx.PopupTransientWindow):
     69
     70 -    def __init__(self, parent, text, color="#D3F4B8", timeout=1200, pos=None):
     71          self.timeout = timeout
     72          wx.PopupTransientWindow.__init__(self, parent, wx.NO_BORDER)
     73          self.SetBackgroundColour(color)
     74          st = wx.StaticText(self, wx.ID_ANY, text, pos=(10, 10))
     75          sz = st.GetBestSize()
     76          self.SetSize((sz.width + 20, sz.height + 20))
     77          if pos:
     78              self.Position(pos, (-1, -1))
     79          self.Popup()
     80
     81 -    def ProcessLeftDown(self, evt):
     82          return False
     83
     84 -    def Popup(self):
     85          super(PopupTextWindow, self).Popup()
     86          wx.CallLater(self.timeout, self.Dismiss)
     87
     88
     89 -def category_tree(category_list, parent=None, remove=None):
     90      """
     91      Transform flat list of categories to a tree based on parent attribute.
     92
     93      The top-level categories have the given parent and each level in the tree
     94      is sorted.
     95
     96      If remove is given then the subtree with remove as root will not be
     97      included.
     98
     99      The tree is represented as a list of tuples, (cat, sub-tree), where cat is
    100      the parent category and subtree is the same tree representation of the
    101      children.
    102      """
    103      children = [child for child in category_list
    104                  if (child._get_parent() is parent and child is not remove)]
    105      sorted_children = sort_categories(children)
    106      tree = [(x, category_tree(category_list, x, remove))
    107              for x in sorted_children]
    108      return tree
    109
    110
    111 -def _set_focus_and_select(ctrl):
    112      ctrl.SetFocus()
    113      if hasattr(ctrl, "SelectAll"):
    114          ctrl.SelectAll()
    115
    116
    117 -def display_error_message(message, parent=None):
    118      """Display an error message in a modal dialog box"""
    119      dial = wx.MessageDialog(parent, message, _("Error"), wx.OK | wx.ICON_ERROR)
    120      dial.ShowModal()
    121
    122
    123 -def display_warning_message(message, parent=None):
    124      dial = wx.MessageDialog(parent, message, _("Warning"), wx.OK | wx.ICON_WARNING)
    125      dial.ShowModal()
    126
    127
    128 -def display_information_message(caption, message, parent=None):
    129      dialog = wx.MessageDialog(parent, message, caption,
    130                                wx.OK | wx.ICON_INFORMATION)
    131      dialog.ShowModal()
    132      dialog.Destroy()
    133
    134
    135 -def display_categories_editor_moved_message(parent):
    136      display_information_message(
    137          caption=_("Dialog moved"),
    138          message=_("This dialog has been removed. Edit categories in the sidebar instead."),
    139          parent=parent)
    140
    141
    142 -def get_user_ack(question, parent=None):
    143      return wx.MessageBox(question, _("Question"),
    144                           wx.YES_NO | wx.CENTRE | wx.NO_DEFAULT, parent) == wx.YES
    145
    146
    147 -def _ask_question(question, parent=None):
    148      """Ask a yes/no question and return the reply."""
    149      return wx.MessageBox(question, _("Question"),
    150                           wx.YES_NO | wx.CENTRE | wx.NO_DEFAULT, parent)
    151
    152
    153 -def set_wait_cursor(parent):
    154      parent.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
    155
    156
    157 -def set_default_cursor(parent):
    158      parent.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
    159
    160
    161 -def set_focus(parent, name):
    162      for child in parent.GetChildren():
    163          if child.GetName() == name:
    164              child.SetFocus()
    165              break
    166
    167
    168 -def register_unlock_function(function):
    169      global unlock_function
    170      unlock_function = function
    171