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   
 22   
 23  # Border, in pixels, between controls in a window (should always be used when 
 24  # border is needed) 
 25  BORDER = 5 
 26  unlock_function = None 
 27   
 28   
29 -class WildcardHelper(object):
30
31 - def __init__(self, name, extensions):
32 self.name = name 33 self.ext_data = {} 34 self.ext_names = [] 35 self._extract_ext_info(extensions)
36
37 - def wildcard_string(self):
38 return "%s (%s)|%s" % ( 39 self.name, 40 ", ".join(["*." + e for e in self.ext_names]), 41 ";".join(["*." + e for e in self.ext_names]))
42
43 - def get_path(self, dialog):
44 path = dialog.GetPath() 45 for ext_name in self.ext_names: 46 if path.endswith("." + ext_name): 47 return path 48 return "%s.%s" % (path, self.ext_names[0])
49
50 - def get_extension_data(self, path):
51 split_path = path.split(".") 52 if len(split_path) > 1: 53 ext_name = split_path[-1] 54 return self.ext_data.get(ext_name, None) 55 return None
56
57 - def _extract_ext_info(self, extensions):
58 for ext in extensions: 59 if isinstance(ext, tuple): 60 name, data = ext 61 self.ext_data[name] = data 62 self.ext_names.append(name) 63 else: 64 self.ext_names.append(ext)
65 66
67 -class PopupTextWindow(wx.PopupTransientWindow):
68
69 - def __init__(self, parent, text, color="#D3F4B8", timeout=1200, pos=None):
70 self.timeout = timeout 71 wx.PopupTransientWindow.__init__(self, parent, wx.NO_BORDER) 72 self.SetBackgroundColour(color) 73 st = wx.StaticText(self, wx.ID_ANY, text, pos=(10, 10)) 74 sz = st.GetBestSize() 75 self.SetSize((sz.width + 20, sz.height + 20)) 76 if pos: 77 self.Position(pos, (-1, -1)) 78 self.Popup()
79
80 - def ProcessLeftDown(self, evt):
81 return False
82
83 - def Popup(self):
84 super(PopupTextWindow, self).Popup() 85 wx.CallLater(self.timeout, self.Dismiss)
86 87
88 -def _set_focus_and_select(ctrl):
89 ctrl.SetFocus() 90 if hasattr(ctrl, "SelectAll"): 91 ctrl.SelectAll()
92 93
94 -def display_error_message(message, parent=None):
95 """Display an error message in a modal dialog box""" 96 dial = wx.MessageDialog(parent, message, _("Error"), wx.OK | wx.ICON_ERROR) 97 dial.ShowModal()
98 99
100 -def display_warning_message(message, parent=None):
101 dial = wx.MessageDialog(parent, message, _("Warning"), wx.OK | wx.ICON_WARNING) 102 dial.ShowModal()
103 104
105 -def display_information_message(caption, message, parent=None):
106 dialog = wx.MessageDialog(parent, message, caption, 107 wx.OK | wx.ICON_INFORMATION) 108 dialog.ShowModal() 109 dialog.Destroy()
110 111
112 -def get_user_ack(question, parent=None):
113 return wx.MessageBox(question, _("Question"), 114 wx.YES_NO | wx.CENTRE | wx.NO_DEFAULT, parent) == wx.YES
115 116
117 -def _ask_question(question, parent=None):
118 """Ask a yes/no question and return the reply.""" 119 return wx.MessageBox(question, _("Question"), 120 wx.YES_NO | wx.CENTRE | wx.NO_DEFAULT, parent)
121 122
123 -def set_wait_cursor(parent):
124 parent.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
125 126
127 -def set_default_cursor(parent):
128 parent.SetCursor(wx.Cursor(wx.CURSOR_DEFAULT))
129 130
131 -def set_focus(parent, name):
132 for child in parent.GetChildren(): 133 if child.GetName() == name: 134 child.SetFocus() 135 break
136 137
138 -def register_unlock_function(function):
139 global unlock_function 140 unlock_function = function
141