Module SearchScript
[hide private]
[frames] | no frames]

Source Code for Module SearchScript

  1  ''' 
  2  Defines a L{AEScript <AEScript.AEScript>} for searching through accessible tree. 
  3   
  4  @author: Scott Haeger 
  5  @organization: IBM Corporation 
  6  @copyright: Copyright (c) 2005, 2007 IBM Corporation 
  7   
  8  @author: Nicole Anacker 
  9  @organization: IT Science Center Ruegen gGmbH, Germany 
 10  @copyright: Copyright (c) 2007, 2008 ITSC Ruegen 
 11   
 12  @license: I{The BSD License} 
 13  All rights reserved. This program and the accompanying materials are made  
 14  available under the terms of the BSD license which accompanies 
 15  this distribution, and is available at 
 16  U{http://www.opensource.org/licenses/bsd-license.php} 
 17  ''' 
 18  # import useful modules for Scripts 
 19  from AccessEngine import AEScript, AccessEngineAPI 
 20  from AccessEngine import AEConstants 
 21  #from AccessEngine.AEPor import AEPor 
 22  from Tools.i18n import _ 
 23   
 24  __uie__ = dict(kind='script', all_tiers=True) 
 25   
26 -class SearchScript(AEScript.EventScript):
27 ''' 28 Manages the search dialog and performs search through accessible tree using 29 query input by the user. 30 It defines special hotkeys. 31 - I{(Alt+Shift+S)} - Open the search chooser. 32 - I{(Alt+Shift+W)} - Find previous element to be searched. 33 - I{(Alt+Shift+E)} - Find next element to be searched. 34 35 @ivar chooser: Chooser when exists or C{None}. 36 @type chooser: L{AEChooser} 37 '''
38 - def init(self):
39 ''' 40 Registers L{event task <AEScript.event_tasks>} to handle 41 L{chooser <AEEvent.ChooserChange>} change events. 42 Registers L{tasks <AEScript.registered_tasks>} that can be 43 mapped to L{AEInput.Gesture}s. 44 ''' 45 self.chooser = None 46 47 # Register for search specific tasks 48 self.registerTask('show search chooser', self.onChooserChange) 49 self.registerTask('find search next', self.findSearchNext) 50 self.registerTask('find search prev', self.findSearchPrev) 51 52 # get the Keyboard device and register modifiers and commands 53 kbd = AccessEngineAPI.getInputDevice(None, 'keyboard') 54 AccessEngineAPI.addInputModifiers(self, kbd, kbd.AEK_ALT_L, 55 kbd.AEK_SHIFT_L, kbd.AEK_ALT_R, 56 kbd.AEK_SHIFT_R) 57 58 # TODO: Move this line to a better, more central place for every script to 59 # just call pair maybe AEConstants? 60 # register commands for either left Alt-Shift or right Alt-Shift 61 pairs = [[kbd.AEK_ALT_L, kbd.AEK_SHIFT_L], [kbd.AEK_ALT_R, kbd.AEK_SHIFT_R]] 62 63 for pair in pairs: 64 self.registerCommand(kbd, 'show search chooser', _('show search chooser'), 65 False, pair+[kbd.AEK_S]) 66 self.registerCommand(kbd, 'find search prev', _('find search prev'), 67 False, pair+[kbd.AEK_W]) 68 self.registerCommand(kbd, 'find search next', _('find search next'), 69 False, pair+[kbd.AEK_E])
70
71 - def getName(self):
72 ''' 73 Provides the localized name of this L{AEScript <AEScript.AEScript>}. 74 75 @return: Human readable name of this script. 76 @rtype: string 77 ''' 78 return _('Search dialog')
79
80 - def getDescription(self):
81 ''' 82 Describe what this L{AEScript <AEScript.AEScript>} do. 83 84 @return: Human readable translated description of this script. 85 @rtype: string 86 ''' 87 return _('Searches the foreground window for user specified text')
88
89 - def findSearchNext(self, **kwargs):
90 ''' 91 Search through the accessible tree in the forward (down the tree) direction. 92 93 @param kwargs: Arbitrary keyword arguments to pass to the task 94 @type kwargs: dictionary 95 ''' 96 if self.chooser is None: 97 return False 98 query = self.chooser.getQuery() 99 if query is None or query == '': 100 return False 101 # define predicates 102 if self.chooser.isMatchCaseSet(): 103 def pred(acc): 104 return AccessEngineAPI.getAccName(por=acc) == query
105 else: 106 querylower = query.lower() 107 def pred(acc): 108 return AccessEngineAPI.getAccName(por=acc).lower() == querylower
109 # do search 110 for acc in AccessEngineAPI.iterNextItems(self.getVirtualPOR(), wrap=True): 111 if pred(acc): 112 self.doTask('pointer to por', 'ReviewScript', por=acc) 113 return 114 if self.chooser.isWrapSet(): 115 for acc in AccessEngineAPI.iterNextItems( 116 por=AccessEngineAPI.getRootAcc(self.getVirtualPOR()), 117 wrap=True): 118 if pred(acc): 119 self.doTask('pointer to por', 'ReviewScript', por=acc) 120 return 121 self.doTask('read message', 'BasicSpeechScript', 122 text=_('%s not found') %query) 123
124 - def findSearchPrev(self, **kwargs):
125 ''' 126 Search through the accessible tree in the backward (up the tree) direction. 127 128 @param kwargs: Arbitrary keyword arguments to pass to the task 129 @type kwargs: dictionary 130 ''' 131 if self.chooser is None: 132 return False 133 query = self.chooser.getQuery() 134 if query is None or query == '': 135 return False 136 # define predicates 137 if self.chooser.isMatchCaseSet(): 138 def pred(acc): 139 return AccessEngineAPI.getAccName(por=acc) == query
140 else: 141 querylower = query.lower() 142 def pred(acc): 143 return AccessEngineAPI.getAccName(por=acc).lower() == querylower 144 # do search 145 for acc in AccessEngineAPI.iterPrevItems(self.getVirtualPOR(), wrap=True): 146 if pred(acc): 147 self.doTask('pointer to por', 'ReviewScript', por=acc) 148 return 149 if self.chooser.isWrapSet(): 150 for acc in AccessEngineAPI.iterPrevItems( 151 por=AccessEngineAPI.getEndAcc(self.getVirtualPOR()), 152 wrap=True): 153 if pred(acc): 154 self.doTask('pointer to por', 'ReviewScript', por=acc) 155 return 156 self.doTask('read message', 'BasicSpeechScript', 157 text=_('%s not found') %query) 158
159 - def onChooserStart(self, timestamp=None, **kwargs):
160 ''' 161 Shows the Search chooser which allows a user to search the current view for 162 a given accessible. Runs when an input gesture is given to start the chooser. 163 164 @param timestamp: Time at which the event occurred 165 @type timestamp: float 166 @param kwargs: Arbitrary keyword arguments to pass to the task 167 @type kwargs: dictionary 168 ''' 169 # load the chooser if it is not loaded 170 if self.chooser is None: 171 self.chooser = AccessEngineAPI.loadChooser(self, 'SearchChooser', 172 app_name=AccessEngineAPI.getAppName(kwargs['por']), 173 timestamp=timestamp, **kwargs) 174 else: 175 # close the search dialog on second gesture 176 self.chooser.close() 177 self.onChooserEnd(self.chooser) 178 # announce that the dialog closed in the background 179 self.doTask('read message', 'BasicSpeechScript', 180 text=_('%s search dialog closed') % \ 181 AccessEngineAPI.getAppName(kwargs['por']))
182
183 - def onChooserEnd(self, **kwargs):
184 ''' 185 Runs when the L{chooser} is closed by a repeat search key press, or the 186 dialog is closed. 187 188 @param kwargs: Arbitrary keyword arguments to pass to the task 189 @type kwargs: dictionary 190 ''' 191 self.unregisterChooserTask(kwargs['chooser']) 192 self.chooser = None
193