Register tab registry under window registry with ID.

This commit is contained in:
Florian Bruhin 2014-10-06 07:41:05 +02:00
parent d3121034df
commit 85ead4273b
5 changed files with 54 additions and 23 deletions

View File

@ -101,6 +101,7 @@ class HintManager(QObject):
Attributes:
_context: The HintContext for the current invocation.
_win_id: The window ID this HintManager is associated with.
_tab_id: The tab ID this HintManager is associated with.
Signals:
mouse_event: Mouse event to be posted in the web view.
@ -138,10 +139,11 @@ class HintManager(QObject):
mouse_event = pyqtSignal('QMouseEvent')
set_open_target = pyqtSignal(str)
def __init__(self, win_id, parent=None):
def __init__(self, win_id, tab_id, parent=None):
"""Constructor."""
super().__init__(parent)
self._win_id = win_id
self._tab_id = tab_id
self._context = None
mode_manager = objreg.get('mode-manager', scope='window',
window=win_id)
@ -521,7 +523,9 @@ class HintManager(QObject):
window=self._win_id)
tabbed_browser.tabopen(url, background=False)
else:
objreg.get('webview', scope='tab').openurl(url)
webview = objreg.get('webview', scope='tab', window=self._win_id,
tab=self._tab_id)
webview.openurl(url)
@cmdutils.register(instance='hintmanager', scope='tab', name='hint')
def start(self, group=webelem.Group.all, target=Target.normal,

View File

@ -111,7 +111,8 @@ class HintKeyParser(keyparser.CommandKeyParser):
"""
log.keyboard.debug("Got special key 0x{:x} text {}".format(
e.key(), e.text()))
hintmanager = objreg.get('hintmanager', scope='tab')
hintmanager = objreg.get('hintmanager', scope='tab',
window=self._win_id, tab='current')
if e.key() == Qt.Key_Backspace:
log.keyboard.debug("Got backspace, mode {}, filtertext '{}', "
"keystring '{}'".format(self._last_press,
@ -166,7 +167,9 @@ class HintKeyParser(keyparser.CommandKeyParser):
if not isinstance(keytype, self.Type):
raise TypeError("Type {} is no Type member!".format(keytype))
if keytype == self.Type.chain:
objreg.get('hintmanager', scope='tab').fire(cmdstr)
hintmanager = objreg.get('hintmanager', scope='tab',
window=self._win_id, tab='current')
hintmanager.fire(cmdstr)
else:
# execute as command
super().execute(cmdstr, keytype, count)
@ -183,4 +186,6 @@ class HintKeyParser(keyparser.CommandKeyParser):
@pyqtSlot(str)
def on_keystring_updated(self, keystr):
"""Update hintmanager when the keystring was updated."""
objreg.get('hintmanager', scope='tab').handle_partial_key(keystr)
hintmanager = objreg.get('hintmanager', scope='tab',
window=self._win_id, tab='current')
hintmanager.handle_partial_key(keystr)

View File

@ -101,16 +101,28 @@ global_registry = ObjectRegistry()
window_registry = ObjectRegistry()
def _get_tab_registry():
def _get_tab_registry(win_id, tab_id):
"""Get the registry of a tab."""
app = get('app')
win = app.activeWindow()
tabbed_browser = get('tabbed-browser', scope='window', window=win)
widget = tabbed_browser.currentWidget()
if widget is None:
raise RegistryUnavailableError('tab')
if tab_id is None:
tab_id = 'current'
if tab_id == 'current' and win_id is None:
app = get('app')
window = app.activeWindow()
if window is None or not hasattr(window, 'win_id'):
raise RegistryUnavailableError('tab')
win_id = window.win_id
elif win_id is not None:
window = window_registry[win_id]
if tab_id == 'current':
tabbed_browser = get('tabbed-browser', scope='window', window=win_id)
tab = tabbed_browser.currentWidget()
if tab is None:
raise RegistryUnavailableError('window')
tab_id = tab.tab_id
tab_registry = get('tab-registry', scope='window', window=win_id)
try:
return widget.registry
return tab_registry[tab_id].registry
except AttributeError:
raise RegistryUnavailableError('tab')
@ -135,27 +147,29 @@ def _get_window_registry(window):
raise RegistryUnavailableError('window')
def _get_registry(scope, window):
def _get_registry(scope, window=None, tab=None):
"""Get the correct registry for a given scope."""
if window is not None and scope is not 'window':
if window is not None and scope not in ('window', 'tab'):
raise TypeError("window is set with scope {}".format(scope))
if tab is not None and scope != 'tab':
raise TypeError("tab is set with scope {}".format(scope))
if scope == 'global':
return global_registry
elif scope == 'tab':
return _get_tab_registry()
return _get_tab_registry(window, tab)
elif scope == 'window':
return _get_window_registry(window)
else:
raise ValueError("Invalid scope '{}'!".format(scope))
def get(name, default=_UNSET, scope='global', window=None):
def get(name, default=_UNSET, scope='global', window=None, tab=None):
"""Helper function to get an object.
Args:
default: A default to return if the object does not exist.
"""
reg = _get_registry(scope, window)
reg = _get_registry(scope, window, tab)
try:
return reg[name]
except KeyError:
@ -165,7 +179,8 @@ def get(name, default=_UNSET, scope='global', window=None):
raise
def register(name, obj, update=False, scope=None, registry=None, window=None):
def register(name, obj, update=False, scope=None, registry=None, window=None,
tab=None):
"""Helper function to register an object.
Args:
@ -181,16 +196,16 @@ def register(name, obj, update=False, scope=None, registry=None, window=None):
else:
if scope is None:
scope = 'global'
reg = _get_registry(scope, window)
reg = _get_registry(scope, window, tab)
if not update and name in reg:
raise KeyError("Object '{}' is already registered ({})!".format(
name, repr(reg[name])))
reg[name] = obj
def delete(name, scope='global', window=None):
def delete(name, scope='global', window=None, tab=None):
"""Helper function to unregister an object."""
reg = _get_registry(scope, window)
reg = _get_registry(scope, window, tab)
del reg[name]

View File

@ -78,6 +78,9 @@ class MainWindow(QWidget):
self.registry = objreg.ObjectRegistry()
objreg.window_registry[win_id] = self
objreg.register('main-window', self, scope='window', window=win_id)
tab_registry = objreg.ObjectRegistry()
objreg.register('tab-registry', tab_registry, scope='window',
window=win_id)
message_bridge = message.MessageBridge(self)
objreg.register('message-bridge', message_bridge, scope='window',

View File

@ -57,6 +57,7 @@ class WebView(QWebView):
viewing_source: Whether the webview is currently displaying source
code.
registry: The ObjectRegistry associated with this tab.
tab_id: The tab ID of the view.
_cur_url: The current URL (accessed via cur_url property).
_has_ssl_errors: Whether SSL errors occured during loading.
_zoom: A NeighborList with the zoom levels.
@ -101,10 +102,13 @@ class WebView(QWebView):
self.progress = 0
self.registry = objreg.ObjectRegistry()
self.tab_id = next(tab_id_gen)
tab_registry = objreg.get('tab-registry', scope='window',
window=win_id)
tab_registry[self.tab_id] = self
objreg.register('webview', self, registry=self.registry)
page = webpage.BrowserPage(win_id, self)
self.setPage(page)
hintmanager = hints.HintManager(win_id, self)
hintmanager = hints.HintManager(win_id, self.tab_id, self)
hintmanager.mouse_event.connect(self.on_mouse_event)
hintmanager.set_open_target.connect(self.set_force_open_target)
objreg.register('hintmanager', hintmanager, registry=self.registry)