Back to index

unity  6.0.0
panel.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: Marco Trevisan (TreviƱo)
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 import logging
00013 from time import sleep
00014 
00015 from autopilot.emulators.X11 import Mouse
00016 from autopilot.keybindings import KeybindingsHelper
00017 
00018 from unity.emulators import UnityIntrospectionObject
00019 logger = logging.getLogger(__name__)
00020 
00021 
00022 class PanelController(UnityIntrospectionObject):
00023     """The PanelController class."""
00024 
00025     def get_panel_for_monitor(self, monitor_num):
00026         """Return an instance of panel for the specified monitor, or None."""
00027         panels = self.get_children_by_type(UnityPanel, monitor=monitor_num)
00028         assert(len(panels) == 1)
00029         return panels[0]
00030 
00031     def get_active_panel(self):
00032         """Return the active panel, or None."""
00033         panels = self.get_children_by_type(UnityPanel, active=True)
00034         assert(len(panels) == 1)
00035         return panels[0]
00036 
00037     def get_active_indicator(self):
00038         for panel in self.get_panels:
00039             active = panel.get_active_indicator()
00040             if active:
00041                 return active
00042 
00043         return None
00044 
00045     @property
00046     def get_panels(self):
00047         """Return the available panels, or None."""
00048         return self.get_children_by_type(UnityPanel)
00049 
00050 
00051 class UnityPanel(UnityIntrospectionObject, KeybindingsHelper):
00052     """An individual panel for a monitor."""
00053 
00054     def __init__(self, *args, **kwargs):
00055         super(UnityPanel, self).__init__(*args, **kwargs)
00056         self._mouse = Mouse()
00057 
00058     def __get_menu_view(self):
00059         """Return the menu view."""
00060         menus = self.get_children_by_type(MenuView)
00061         assert(len(menus) == 1)
00062         return menus[0]
00063 
00064     def __get_window_buttons(self):
00065         """Return the window buttons view."""
00066         buttons = self.menus.get_children_by_type(WindowButtons)
00067         assert(len(buttons) == 1)
00068         return buttons[0]
00069 
00070     def __get_grab_area(self):
00071         """Return the panel grab area."""
00072         grab_areas = self.menus.get_children_by_type(GrabArea)
00073         assert(len(grab_areas) == 1)
00074         return grab_areas[0]
00075 
00076     def __get_indicators_view(self):
00077         """Return the menu view."""
00078         indicators = self.get_children_by_type(Indicators)
00079         assert(len(indicators) == 1)
00080         return indicators[0]
00081 
00082     def move_mouse_below_the_panel(self):
00083         """Places the mouse to bottom of this panel."""
00084         (x, y, w, h) = self.geometry
00085         target_x = x + w / 2
00086         target_y = y + h + 10
00087 
00088         logger.debug("Moving mouse away from panel.")
00089         self._mouse.move(target_x, target_y)
00090 
00091     def move_mouse_over_menus(self):
00092         """Move the mouse over the menu area for this panel."""
00093         (x, y, w, h) = self.menus.geometry
00094         target_x = x + w / 2
00095         target_y = y + h / 2
00096 
00097         # The menu view has bigger geometry than the real layout
00098         menu_entries = self.menus.get_entries()
00099         if len(menu_entries) > 0:
00100             first_x = menu_entries[0].x
00101             last_x = menu_entries[-1].x + menu_entries[-1].width / 2
00102 
00103             target_x = first_x + (last_x - first_x) / 2
00104 
00105         logger.debug("Moving mouse to center of menu area.")
00106         self._mouse.move(target_x, target_y)
00107 
00108     def move_mouse_over_grab_area(self):
00109         """Move the mouse over the grab area for this panel."""
00110         (x, y, w, h) = self.grab_area.geometry
00111         target_x = x + w / 2
00112         target_y = y + h / 2
00113 
00114         logger.debug("Moving mouse to center of grab area.")
00115         self._mouse.move(target_x, target_y)
00116 
00117     def move_mouse_over_window_buttons(self):
00118         """Move the mouse over the center of the window buttons area for this panel."""
00119         (x, y, w, h) = self.window_buttons.geometry
00120         target_x = x + w / 2
00121         target_y = y + h / 2
00122 
00123         logger.debug("Moving mouse to center of the window buttons.")
00124         self._mouse.move(target_x, target_y)
00125 
00126     def move_mouse_over_indicators(self):
00127         """Move the mouse over the center of the indicators area for this panel."""
00128         (x, y, w, h) = self.indicators.geometry
00129         target_x = x + w / 2
00130         target_y = y + h / 2
00131 
00132         logger.debug("Moving mouse to center of the indicators area.")
00133         self._mouse.move(target_x, target_y)
00134 
00135     def get_indicator_entries(self, visible_only=True, include_hidden_menus=False):
00136         """Returns a list of entries for this panel including both menus and indicators"""
00137         entries = []
00138         if include_hidden_menus or self.menus_shown:
00139             entries = self.menus.get_entries()
00140         entries += self.indicators.get_ordered_entries(visible_only)
00141         return entries
00142 
00143     def get_active_indicator(self):
00144         """Returns the indicator entry that is currently active"""
00145         entries = self.get_indicator_entries(False, True)
00146         entries = filter(lambda e: e.active == True, entries)
00147         assert(len(entries) <= 1)
00148         return entries[0] if entries else None
00149 
00150     def get_indicator_entry(self, entry_id):
00151         """Returns the indicator entry for the given ID or None"""
00152         entries = self.get_indicator_entries(False, True)
00153         entries = filter(lambda e: e.entry_id == entry_id, entries)
00154         assert(len(entries) <= 1)
00155         return entries[0] if entries else None
00156 
00157     @property
00158     def title(self):
00159         return self.menus.panel_title
00160 
00161     @property
00162     def desktop_is_active(self):
00163         return self.menus.desktop_active
00164 
00165     @property
00166     def menus_shown(self):
00167         return self.active and self.menus.draw_menus
00168 
00169     @property
00170     def window_buttons_shown(self):
00171         return self.menus.draw_window_buttons
00172 
00173     @property
00174     def window_buttons(self):
00175         return self.__get_window_buttons()
00176 
00177     @property
00178     def menus(self):
00179         return self.__get_menu_view()
00180 
00181     @property
00182     def grab_area(self):
00183         return self.__get_grab_area()
00184 
00185     @property
00186     def indicators(self):
00187         return self.__get_indicators_view()
00188 
00189     @property
00190     def geometry(self):
00191         """Returns a tuple of (x,y,w,h) for the current panel."""
00192         return (self.x, self.y, self.width, self.height)
00193 
00194 
00195 class MenuView(UnityIntrospectionObject):
00196     """The Menu View class."""
00197 
00198     def get_entries(self):
00199         """Return a list of menu entries"""
00200         return self.get_children_by_type(IndicatorEntry)
00201 
00202     def get_menu_by_label(self, entry_label):
00203         """Return the first indicator entry found with the given label"""
00204         indicators = self.get_children_by_type(IndicatorEntry, label=entry_label)
00205         return indicators[0] if indicators else None
00206 
00207     @property
00208     def geometry(self):
00209         """Returns a tuple of (x,y,w,h) for the current menu view."""
00210         return (self.x, self.y, self.width, self.height)
00211 
00212 
00213 class WindowButtons(UnityIntrospectionObject):
00214     """The window buttons class"""
00215 
00216     def get_buttons(self, visible_only=True):
00217         """Return a list of window buttons"""
00218         if visible_only:
00219             return self.get_children_by_type(WindowButton, visible=True)
00220         else:
00221             return self.get_children_by_type(WindowButton)
00222 
00223     def get_button(self, type):
00224         buttons = self.get_children_by_type(WindowButton, type=type)
00225         assert(len(buttons) == 1)
00226         return buttons[0]
00227 
00228     @property
00229     def visible(self):
00230         return len(self.get_buttons()) != 0
00231 
00232     @property
00233     def close(self):
00234         return self.get_button("Close")
00235 
00236     @property
00237     def minimize(self):
00238         return self.get_button("Minimize")
00239 
00240     @property
00241     def unmaximize(self):
00242         return self.get_button("Unmaximize")
00243 
00244     @property
00245     def maximize(self):
00246         return self.get_button("Maximize")
00247 
00248     @property
00249     def geometry(self):
00250         """Returns a tuple of (x,y,w,h) for the current panel."""
00251         return (self.x, self.y, self.width, self.height)
00252 
00253 
00254 class WindowButton(UnityIntrospectionObject):
00255     """The Window WindowButton class."""
00256 
00257     def __init__(self, *args, **kwargs):
00258         super(WindowButton, self).__init__(*args, **kwargs)
00259         self._mouse = Mouse()
00260 
00261     def mouse_move_to(self):
00262         target_x = self.x + self.width / 2
00263         target_y = self.y + self.height / 2
00264         self._mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
00265 
00266     def mouse_click(self):
00267         self.mouse_move_to()
00268         sleep(.2)
00269         self._mouse.click(press_duration=.1)
00270         sleep(.01)
00271 
00272     @property
00273     def geometry(self):
00274         """Returns a tuple of (x,y,w,h) for the window button."""
00275         return (self.x, self.y, self.width, self.height)
00276 
00277 
00278 class GrabArea(UnityIntrospectionObject):
00279     """The grab area class"""
00280 
00281     @property
00282     def geometry(self):
00283         """Returns a tuple of (x,y,w,h) for the grab area."""
00284         return (self.x, self.y, self.width, self.height)
00285 
00286 
00287 class Indicators(UnityIntrospectionObject):
00288     """The Indicators View class."""
00289 
00290     def get_ordered_entries(self, visible_only=True):
00291         """Return a list of indicators, ordered by their priority"""
00292 
00293         if visible_only:
00294             entries = self.get_children_by_type(IndicatorEntry, visible=True)
00295         else:
00296             entries = self.get_children_by_type(IndicatorEntry)
00297 
00298         return sorted(entries, key=lambda entry: entry.priority)
00299 
00300     def get_indicator_by_name_hint(self, name_hint):
00301         """Return the IndicatorEntry with the name_hint"""
00302         indicators = self.get_children_by_type(IndicatorEntry, name_hint=name_hint)
00303         assert(len(indicators) == 1)
00304         return indicators[0]
00305 
00306     @property
00307     def geometry(self):
00308         """Returns a tuple of (x,y,w,h) for the indicators area."""
00309         return (self.x, self.y, self.width, self.height)
00310 
00311 
00312 class IndicatorEntry(UnityIntrospectionObject):
00313     """The IndicatorEntry View class."""
00314 
00315     def __init__(self, *args, **kwargs):
00316         super(IndicatorEntry, self).__init__(*args, **kwargs)
00317         self._mouse = Mouse()
00318 
00319     def mouse_move_to(self):
00320         target_x = self.x + self.width / 2
00321         target_y = self.y + self.height / 2
00322         self._mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
00323 
00324     def mouse_click(self, button=1):
00325         self.mouse_move_to()
00326         sleep(.2)
00327         assert(self.visible)
00328         self._mouse.click(press_duration=.1)
00329         sleep(.01)
00330 
00331     @property
00332     def geometry(self):
00333         """Returns a tuple of (x,y,w,h) for the indicator entry."""
00334         return (self.x, self.y, self.width, self.height)
00335 
00336     @property
00337     def menu_geometry(self):
00338         """Returns a tuple of (x,y,w,h) for the opened menu geometry."""
00339         return (self.menu_x, self.menu_y, self.menu_width, self.menu_height)
00340