Back to index

unity  6.0.0
quicklist.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 import logging
00013 
00014 from autopilot.emulators.X11 import Mouse
00015 
00016 from unity.emulators import UnityIntrospectionObject
00017 
00018 logger = logging.getLogger(__name__)
00019 
00020 
00021 class Quicklist(UnityIntrospectionObject):
00022     """Represents a quicklist."""
00023 
00024     @property
00025     def items(self):
00026         """Individual items in the quicklist."""
00027         return self.get_children_by_type(QuicklistMenuItem, visible=True)
00028 
00029     @property
00030     def selectable_items(self):
00031         """Items that can be selected in the quicklist."""
00032         return self.get_children_by_type(QuicklistMenuItem, visible=True, selectable=True)
00033 
00034     def get_quicklist_item_by_text(self, text):
00035         """Returns a QuicklistMenuItemLabel object with the given text, or None."""
00036         if not self.active:
00037             raise RuntimeError("Cannot get quicklist items. Quicklist is inactive!")
00038 
00039         matches = filter(lambda i: i.text == text,
00040             self.get_children_by_type(QuicklistMenuItemLabel))
00041 
00042         return matches[0] if matches else None
00043 
00044     def get_quicklist_application_item(self, application_name):
00045         """Returns the QuicklistMenuItemLabel for the given application_name"""
00046         return self.get_quicklist_item_by_text("<b>"+application_name+"</b>")
00047 
00048     def click_item(self, item):
00049         """Click one of the quicklist items."""
00050         if not isinstance(item, QuicklistMenuItem):
00051             raise TypeError("Item must be a subclass of QuicklistMenuItem")
00052 
00053         item.mouse_click()
00054 
00055     def move_mouse_to_right(self):
00056         """Moves the mouse outside the quicklist"""
00057         logger.debug("Moving mouse outside the quicklist %r", self)
00058         target_x = self.x + self.width + 10
00059         target_y = self.y + self.height / 2
00060         Mouse().move(target_x, target_y, animate=False)
00061 
00062     @property
00063     def selected_item(self):
00064         items = self.get_children_by_type(QuicklistMenuItem, selected=True)
00065         assert(len(items) <= 1)
00066         return items[0] if items else None
00067 
00068     @property
00069     def geometry(self):
00070         """Returns a tuple of (x,y,w,h) for the quicklist."""
00071         return (self.x, self.y, self.width, self.height)
00072 
00073 
00074 class QuicklistMenuItem(UnityIntrospectionObject):
00075     """Represents a single item in a quicklist."""
00076 
00077     def __init__(self, *args, **kwargs):
00078         super(QuicklistMenuItem, self).__init__(*args, **kwargs)
00079         self._mouse = Mouse()
00080 
00081     @property
00082     def geometry(self):
00083         """Returns a tuple of (x,y,w,h) for the quicklist item."""
00084         return (self.x, self.y, self.width, self.height)
00085 
00086     def mouse_move_to(self):
00087         assert(self.visible)
00088         logger.debug("Moving mouse over quicklist item %r", self)
00089         target_x = self.x + self.width / 2
00090         target_y = self.y + self.height / 2
00091         self._mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
00092 
00093     def mouse_click(self, button=1):
00094         logger.debug("Clicking on quicklist item %r", self)
00095         self.mouse_move_to()
00096         self._mouse.click()
00097 
00098 
00099 class QuicklistMenuItemLabel(QuicklistMenuItem):
00100     """Represents a text label inside a quicklist."""
00101 
00102 
00103 class QuicklistMenuItemSeparator(QuicklistMenuItem):
00104     """Represents a separator in a quicklist."""