Back to index

unity  6.0.0
hud.py
Go to the documentation of this file.
00001 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
00002 # Copyright 2012 Canonical
00003 # Author: Thomi Richards
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License version 3, as published
00007 # by the Free Software Foundation.
00008 #
00009 
00010 from __future__ import absolute_import
00011 
00012 from autopilot.emulators.X11 import Keyboard
00013 from autopilot.keybindings import KeybindingsHelper
00014 from HTMLParser import HTMLParser
00015 import re
00016 
00017 from unity.emulators import UnityIntrospectionObject
00018 from unity.emulators.dash import SearchBar
00019 from unity.emulators.icons import HudEmbeddedIcon, HudLauncherIcon
00020 
00021 
00022 class Hud(KeybindingsHelper):
00023     """An emulator class that makes it easier to iteract with unity hud."""
00024 
00025     def __init__(self):
00026         super (Hud, self).__init__()
00027         controllers = HudController.get_all_instances()
00028         assert(len(controllers) == 1)
00029         self.controller = controllers[0]
00030         self.keyboard = Keyboard()
00031 
00032     def ensure_hidden(self):
00033         """Hides the hud if it's not already hidden."""
00034         if self.visible:
00035             self.keyboard.press_and_release("Escape")
00036             self.visible.wait_for(False)
00037 
00038     def ensure_visible(self):
00039         """Shows the hud if it's not already showing."""
00040         if not self.visible:
00041             self.toggle_reveal()
00042             self.visible.wait_for(True)
00043 
00044     def toggle_reveal(self, tap_delay=0.1):
00045         """Tap the 'Alt' key to toggle the hud visibility."""
00046         old_state = self.visible
00047         self.keybinding("hud/reveal", tap_delay)
00048         self.visible.wait_for(not old_state)
00049 
00050     def get_embedded_icon(self):
00051         """Returns the HUD view embedded icon or None if is not shown."""
00052         view = self.view
00053         if (not view):
00054           return None
00055 
00056         icons = view.get_children_by_type(HudEmbeddedIcon)
00057         return icons[0] if icons else None
00058 
00059     def get_launcher_icon(self):
00060         """Returns the HUD launcher icon"""
00061         icons = HudLauncherIcon.get_all_instances()
00062         assert(len(icons) == 1)
00063         return icons[0]
00064 
00065     @property
00066     def icon(self):
00067         if self.is_locked_launcher:
00068             return self.get_launcher_icon()
00069         else:
00070             return self.get_embedded_icon()
00071 
00072     @property
00073     def view(self):
00074         """Returns the HudView."""
00075         return self.controller.get_hud_view()
00076 
00077     @property
00078     def visible(self):
00079         """Is the Hud visible?"""
00080         return self.controller.visible
00081 
00082     @property
00083     def searchbar(self):
00084         """Returns the searchbar attached to the hud."""
00085         return self.controller.get_hud_view().searchbar
00086 
00087     @property
00088     def search_string(self):
00089         """Returns the searchbars' search string."""
00090         return self.searchbar.search_string
00091 
00092     @property
00093     def is_locked_launcher(self):
00094         return self.controller.locked_to_launcher
00095 
00096     @property
00097     def monitor(self):
00098         return self.controller.hud_monitor
00099 
00100     @property
00101     def geometry(self):
00102         return (self.controller.x, self.controller.y, self.controller.width, self.controller.height)
00103 
00104     @property
00105     def selected_button(self):
00106         view = self.controller.get_hud_view()
00107         if view:
00108             return view.selected_button
00109         else:
00110             return 0
00111 
00112     @property
00113     def hud_buttons(self):
00114         """Returns a list of current HUD buttons."""
00115         return self.view.hud_buttons
00116 
00117     @property
00118     def selected_hud_button(self):
00119         try:
00120             [button] = filter(lambda x: x.focused, self.hud_buttons)
00121             return button
00122         except IndexError:
00123             raise RuntimeError("No HUD buttons found, is hud active?")
00124 
00125     @property
00126     def num_buttons(self):
00127         view = self.controller.get_hud_view()
00128         if view:
00129             return view.num_buttons
00130         else:
00131             return 0
00132 
00133 
00134 class HudView(UnityIntrospectionObject):
00135     """Proxy object for the hud view child of the controller."""
00136 
00137     @property
00138     def searchbar(self):
00139         """Get the search bar attached to this hud view."""
00140         return self.get_children_by_type(SearchBar)[0]
00141 
00142     @property
00143     def hud_buttons(self):
00144         return self.get_children_by_type(HudButton)
00145 
00146     @property
00147     def geometry(self):
00148         return (self.x, self.y, self.width, self.height)
00149 
00150 
00151 class HudController(UnityIntrospectionObject):
00152     """Proxy object for the Unity Hud Controller."""
00153 
00154     def get_hud_view(self):
00155         views = self.get_children_by_type(HudView)
00156         return views[0] if views else None
00157 
00158 class HudButton(UnityIntrospectionObject):
00159     """Proxy object for the hud buttons."""
00160 
00161     @property
00162     def label_no_formatting(self):
00163         """Returns the label text with the formatting removed."""
00164         htmlparser = HTMLParser()
00165         return htmlparser.unescape(re.sub("<[^>]*>", "", self.label))