Robot Framework Integrated Development Environment (RIDE)
dialog.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import webbrowser
17 
18 import wx
19 from wx import html, Colour
20 
21 from . import sizers, ButtonWithHandler
22 
23 
24 # TODO: Make this colour configurable
25 #HTML_BACKGROUND = (240, 242, 80) # (200, 222, 40)
26 # _settings = RideSettings()
27 # general_settings = _settings['General']
28 # HTML_BACKGROUND = _settings.get('background help', HTML_BACKGROUND)
29 # HTML_BACKGROUND = general_settings['background help']
30 # Workaround for circular import
31 
32 # general_settings = {'background help': HTML_BACKGROUND, 'foreground help': HTML_FOREGROUND,
33 # 'font face': '', 'font size': 11}
34 
35 
36 class HtmlWindow(html.HtmlWindow):
37 
38  def __init__(self, parent, size=wx.DefaultSize, text=None, color_background=None, color_foreground=None):
39  html.HtmlWindow.__init__(self, parent, size=size, style=html.HW_DEFAULT_STYLE)
40  from ..preferences import RideSettings
41 
44  _settings = RideSettings()
45  self.general_settingsgeneral_settings = _settings['General']
46  self.color_background_helpcolor_background_help = color_background if color_background else self.general_settingsgeneral_settings['background help']
47  self.color_foreground_textcolor_foreground_text = color_foreground if color_foreground else self.general_settingsgeneral_settings['foreground text']
48  # HTML_FONT_FACE = _settings.get('font face', '')
49  # HTML_FONT_SIZE = _settings.get('font size', 11)
50  self.SetBorders(2)
51  self.SetStandardFonts(size=9)
52  if text:
53  self.set_contentset_content(text)
54  self.SetHTMLBackgroundColour(Colour(self.color_background_helpcolor_background_help))
55  # print(f"DEBUG: HTML init Dialog color: {Colour(self.general_settings['background help'])}")
56  self.SetForegroundColour(Colour(self.color_foreground_textcolor_foreground_text))
57  self.fontfont = self.GetFont()
58  self.fontfont.SetFaceName(self.general_settingsgeneral_settings['font face'])
59  self.fontfont.SetPointSize(self.general_settingsgeneral_settings['font size'])
60  self.SetFont(self.fontfont)
61  self.Refresh(True)
62  self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDownOnKeyDown)
63  self.Bind(wx.EVT_CLOSE, self.closeclose)
64 
65  def set_content(self, content):
66  bkgcolor = self.color_background_helpcolor_background_help
67  # print(f"DEBUG: HTML text Dialog color: {bkgcolor}")
68  try:
69  color = ''.join(hex(item)[2:] for item in tuple(bkgcolor))
70 
73  _content = '<body bgcolor=#%s>%s</body>' % (color, content)
74  except TypeError:
75 
78  _content = '<body bgcolor=%s>%s</body>' % (bkgcolor, content)
79  self.SetPage(_content)
80 
81  def OnKeyDown(self, event):
82  if self._is_copy_is_copy(event):
83  self._add_selection_to_clipboard_add_selection_to_clipboard()
84  self.Parent.OnKey(event)
85  event.Skip()
86 
87  @staticmethod
88  def _is_copy(event):
89  return event.GetKeyCode() == ord('C') and event.CmdDown()
90 
92  wx.TheClipboard.Open()
93  wx.TheClipboard.SetData(wx.TextDataObject(self.SelectionToText()))
94  wx.TheClipboard.Close()
95 
96  def OnLinkClicked(self, link):
97  webbrowser.open(link.Href)
98 
99  def close(self):
100  self.Show(False)
101  self.Destroy()
102 
103  def clear(self):
104  self.SetPage('')
105 
106 
107 class RIDEDialog(wx.Dialog):
108 
109  def __init__(self, title='', parent=None, size=None, style=None, message=None):
110  size = size or (-1, -1)
111  style = style or (wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
112  wx.Dialog.__init__(self, parent=parent, title=title, size=size, style=style)
113  # set Left to Right direction (while we don't have localization)
114  self.SetLayoutDirection(wx.Layout_LeftToRight)
115  self.messagemessage = message
116  from ..preferences import RideSettings
117 
120  _settings = RideSettings()
121  self.general_settingsgeneral_settings = _settings['General']
122  self.font_facefont_face = self.general_settingsgeneral_settings['font face']
123  self.font_sizefont_size = self.general_settingsgeneral_settings['font size']
124  self.color_backgroundcolor_background = self.general_settingsgeneral_settings['background']
125  self.color_foregroundcolor_foreground = self.general_settingsgeneral_settings['foreground']
126  self.color_secondary_backgroundcolor_secondary_background = self.general_settingsgeneral_settings['secondary background']
127  self.color_secondary_foregroundcolor_secondary_foreground = self.general_settingsgeneral_settings['secondary foreground']
128  self.color_background_helpcolor_background_help = self.general_settingsgeneral_settings['background help']
129  self.color_foreground_textcolor_foreground_text = self.general_settingsgeneral_settings['foreground text']
130  self.SetBackgroundColour(Colour(self.color_backgroundcolor_background))
131  self.SetForegroundColour(Colour(self.color_foregroundcolor_foreground))
132  if self.messagemessage:
133  sizer = wx.BoxSizer(wx.VERTICAL)
134  self.SetSizer(sizer)
135  content = wx.StaticText(self, -1, self.messagemessage)
136  button = wx.Button(self, wx.ID_OK, '', style=style)
137  content.SetBackgroundColour(Colour(self.color_backgroundcolor_background))
138  content.SetForegroundColour(Colour(self.color_foregroundcolor_foreground))
139  button.SetBackgroundColour(Colour(self.color_secondary_backgroundcolor_secondary_background))
140  button.SetForegroundColour(Colour(self.color_secondary_foregroundcolor_secondary_foreground))
141  sizer.Add(content, 0, wx.ALL | wx.EXPAND, 3)
142  sizer.Add(wx.StaticText(self, -1, "\n\n"), 0, wx.ALL, 3)
143  sizer.Add(button, 0, wx.ALIGN_CENTER | wx.BOTTOM, 5)
144  self.CenterOnParent()
145 
146  def _create_buttons(self, sizer):
147  buttons = self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL)
148  self.SetBackgroundColour(Colour(self.color_backgroundcolor_background))
149  self.SetForegroundColour(Colour(self.color_foregroundcolor_foreground))
150  for item in self.GetChildren():
151  if isinstance(item, (wx.Button, wx.BitmapButton, ButtonWithHandler)):
152  item.SetBackgroundColour(Colour(self.color_secondary_backgroundcolor_secondary_background))
153  item.SetOwnBackgroundColour(Colour(self.color_secondary_backgroundcolor_secondary_background))
154  item.SetForegroundColour(Colour(self.color_secondary_foregroundcolor_secondary_foreground))
155  item.SetOwnForegroundColour(Colour(self.color_secondary_foregroundcolor_secondary_foreground))
156  sizer.Add(buttons, flag=wx.ALIGN_CENTER | wx.ALL, border=5)
157 
158  def _create_horizontal_line(self, sizer):
159  line = wx.StaticLine(self, size=(20, -1), style=wx.LI_HORIZONTAL)
160  if wx.VERSION < (4, 1, 0):
161  sizer.Add(line, border=5, flag=wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.TOP)
162  else:
163  sizer.Add(line, border=5, flag=wx.GROW | wx.RIGHT | wx.TOP)
164 
165  def execute(self):
166  retval = None
167  if self.ShowModal() == wx.ID_OK:
168  retval = self._execute_execute()
169  self.Destroy()
170  return retval
171 
172  def _execute(self):
173  raise NotImplementedError(self.__class__.__name__)
174 
175 
177 
178  def __init__(self, title, content, padding=0, font_size=-1):
179  RIDEDialog.__init__(self, title)
180  # set Left to Right direction (while we don't have localization)
181  self.SetLayoutDirection(wx.Layout_LeftToRight)
182  szr = sizers.VerticalSizer()
183  self.html_wndhtml_wnd = HtmlWindow(self, text=content)
184  self.html_wndhtml_wnd.SetStandardFonts(size=font_size)
185  szr.add_expanding(self.html_wndhtml_wnd, padding=padding)
186  self.SetSizer(szr)
187 
188  def OnKey(self, event):
189  pass
def __init__(self, title, content, padding=0, font_size=-1)
Definition: dialog.py:178
def OnLinkClicked(self, link)
Definition: dialog.py:96
def set_content(self, content)
Definition: dialog.py:65
def OnKeyDown(self, event)
Definition: dialog.py:81
def __init__(self, parent, size=wx.DefaultSize, text=None, color_background=None, color_foreground=None)
Definition: dialog.py:38
def _create_horizontal_line(self, sizer)
Definition: dialog.py:158
def _create_buttons(self, sizer)
Definition: dialog.py:146
def __init__(self, title='', parent=None, size=None, style=None, message=None)
Definition: dialog.py:109