Robot Framework Integrated Development Environment (RIDE)
actioninfo.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 re
17 
18 import wx
19 
20 from ..widgets import ImageProvider
21 from .shortcut import Shortcut
22 
23 
24 
106 def ActionInfoCollection(data, event_handler, container=None):
107 
108  menu = None
109  actions = []
110  for row in data.splitlines():
111  row = row.strip()
112  if not row:
113  continue
114  elif row.startswith('[') and row.endswith(']'):
115  menu = row[1:-1].strip()
116  else:
117  actions.append(_create_action_info(event_handler, menu, container, row))
118  return actions
119 
120 
121 def _create_action_info(eventhandler, menu, container, row):
122  if row.startswith('---'):
123  return SeparatorInfo(menu)
124  tokens = [t.strip() for t in row.split('|')]
125  tokens += [''] * (5-len(tokens))
126  name, doc, shortcut, icon, position = tokens
127  if name.startswith('!'):
128  name = name[1:]
129  container = None
130  eventhandler_name, name = _get_eventhandler_name_and_parsed_name(name)
131  action = getattr(eventhandler, eventhandler_name)
132  return ActionInfo(menu, name, action, container, shortcut, icon, doc, position)
133 
134 
136  eventhandler_name, name = _parse_shortcuts_from_name(name)
137  return 'On%s' % eventhandler_name.replace(' ', '').replace('&', ''), name
138 
139 
141  if '(' in name:
142  eventhandler_name, shortcuts = name.split('(', 1)
143  shortcuts = shortcuts.split(')')[0]
144  elements = shortcuts.split(' or ')
145  name = '%s (%s)' % (eventhandler_name, ' or '.join(Shortcut(e).printable for e in
146  elements))
147  return eventhandler_name, name
148  return name, name
149 
150 
151 
152 class MenuInfo():
153 
154  def __init__(self):
155  self.insertion_pointinsertion_point = _InsertionPoint()
156 
157  def is_separator(self):
158  return False
159 
160 
171  def set_menu_position(self, before=None, after=None):
172  self.insertion_pointinsertion_point = _InsertionPoint(before, after)
173 
174 
175 
177 
178 
219  def __init__(self, menu_name, name, action=None, container=None,
220  shortcut=None, icon=None, doc='', position=-1):
221  MenuInfo.__init__(self)
222  self.menu_namemenu_name = menu_name
223  self.namename = name
224  self.actionaction = action
225  self.containercontainer = container
226  self.shortcutshortcut = Shortcut(shortcut)
227  self._icon_icon = None
228  self._icon_source_icon_source = icon
229  self.docdoc = doc
230  self._position_position = position
231 
232  @property
233  icon = property
234 
235  def icon(self):
236  if not self._icon_icon:
237  self._icon_icon = self._get_icon_get_icon()
238  return self._icon_icon
239 
240  def _get_icon(self):
241  if not self._icon_source_icon_source:
242  return None
243  if isinstance(self._icon_source_icon_source, str):
244  if self._icon_source_icon_source.startswith("CUSTOM_"):
245  return ImageProvider().get_image_by_name(self._icon_source_icon_source[len("CUSTOM_"):])
246  return wx.ArtProvider.GetBitmap(getattr(wx, self._icon_source_icon_source),
247  wx.ART_TOOLBAR, (16, 16))
248  return self._icon_source_icon_source
249 
250  @property
251  position = property
252 
253  def position(self):
254  if isinstance(self._position_position, int):
255  return self._position_position
256  elif isinstance(self._position_position, str):
257  if len(self._position_position) > 0:
258  return int(self._position_position.split("POSITION-")[-1])
259  return -1
260 
261 
263 
264 
271  def __init__(self, menu_name):
272  MenuInfo.__init__(self)
273  self.menu_namemenu_name = menu_name
274 
275  def is_separator(self):
276  return True
277 
278 
280 
283  _shortcut_remover = re.compile(' {2,}\‍([^()]+\‍)$')
284 
285  def __init__(self, before=None, after=None):
286  self._item_item = before or after
287  self._insert_before_insert_before = before is not None
288 
289  def get_index(self, menu):
290  if not self._item_item:
291  return menu.GetMenuItemCount()
292  index = self._find_position_in_menu_find_position_in_menu(menu)
293  if not index:
294  return menu.GetMenuItemCount()
295  if not self._insert_before_insert_before:
296  index += 1
297  return index
298 
299  def _find_position_in_menu(self, menu):
300  for index in range(0, menu.GetMenuItemCount()):
301  item = menu.FindItemByPosition(index)
302  if self._get_menu_item_name_get_menu_item_name(item).lower() == self._item_item.lower():
303  return index
304  return None
305 
306  def _get_menu_item_name(self, item):
307  if wx.VERSION < (4, 1, 0):
308  return self._shortcut_remover_shortcut_remover.split(item.GetLabel())[0]
309  return self._shortcut_remover_shortcut_remover.split(item.GetItemLabel())[0]
Used to create menu entries, keyboard shortcuts and/or toolbar buttons.
Definition: actioninfo.py:176
def __init__(self, menu_name, name, action=None, container=None, shortcut=None, icon=None, doc='', position=-1)
Initializes information needed to create actions.
Definition: actioninfo.py:220
Base class for ActionInfo and SeparatorInfo.
Definition: actioninfo.py:152
def set_menu_position(self, before=None, after=None)
Sets the position of this menu entry.
Definition: actioninfo.py:171
Used to create separators to menus.
Definition: actioninfo.py:262
def __init__(self, menu_name)
Initializes information needed to add separators to menus.
Definition: actioninfo.py:271
def __init__(self, before=None, after=None)
Definition: actioninfo.py:285
def ActionInfoCollection(data, event_handler, container=None)
Parses the data into a list of ActionInfo and SeparatorInfo objects.
Definition: actioninfo.py:106
def _parse_shortcuts_from_name(name)
Definition: actioninfo.py:140
def _create_action_info(eventhandler, menu, container, row)
Definition: actioninfo.py:121
def _get_eventhandler_name_and_parsed_name(name)
Definition: actioninfo.py:135