Skip to content

Commit

Permalink
Refactor TabbedBrowser from inheritance to composition
Browse files Browse the repository at this point in the history
  • Loading branch information
bttner authored and jberglinds committed Feb 19, 2018
1 parent ab0034f commit e169e21
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 155 deletions.
2 changes: 1 addition & 1 deletion qutebrowser/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def _open_startpage(win_id=None):
for cur_win_id in list(window_ids): # Copying as the dict could change
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=cur_win_id)
if tabbed_browser.count() == 0:
if tabbed_browser.widget.count() == 0:
log.init.debug("Opening start pages")
for url in config.val.url.start_pages:
tabbed_browser.tabopen(url)
Expand Down
51 changes: 26 additions & 25 deletions qutebrowser/browser/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ def _new_tabbed_browser(self, private):

def _count(self):
"""Convenience method to get the widget count."""
return self._tabbed_browser.count()
return self._tabbed_browser.widget.count()

def _set_current_index(self, idx):
"""Convenience method to set the current widget index."""
cmdutils.check_overflow(idx, 'int')
self._tabbed_browser.setCurrentIndex(idx)
self._tabbed_browser.widget.setCurrentIndex(idx)

def _current_index(self):
"""Convenience method to get the current widget index."""
return self._tabbed_browser.currentIndex()
return self._tabbed_browser.widget.currentIndex()

def _current_url(self):
"""Convenience method to get the current url."""
Expand All @@ -102,7 +102,7 @@ def _current_title(self):

def _current_widget(self):
"""Get the currently active widget from a command."""
widget = self._tabbed_browser.currentWidget()
widget = self._tabbed_browser.widget.currentWidget()
if widget is None:
raise cmdexc.CommandError("No WebView available yet!")
return widget
Expand Down Expand Up @@ -148,10 +148,10 @@ def _cntwidget(self, count=None):
None if no widget was found.
"""
if count is None:
return self._tabbed_browser.currentWidget()
return self._tabbed_browser.widget.currentWidget()
elif 1 <= count <= self._count():
cmdutils.check_overflow(count + 1, 'int')
return self._tabbed_browser.widget(count - 1)
return self._tabbed_browser.widget.widget(count - 1)
else:
return None

Expand All @@ -164,7 +164,7 @@ def _tab_focus_last(self, *, show_error=True):
if not show_error:
return
raise cmdexc.CommandError("No last focused tab!")
idx = self._tabbed_browser.indexOf(tab)
idx = self._tabbed_browser.widget.indexOf(tab)
if idx == -1:
raise cmdexc.CommandError("Last focused tab vanished!")
self._set_current_index(idx)
Expand Down Expand Up @@ -213,7 +213,7 @@ def _tab_close(self, tab, prev=False, next_=False, opposite=False):
what's configured in 'tabs.select_on_remove'.
count: The tab index to close, or None
"""
tabbar = self._tabbed_browser.tabBar()
tabbar = self._tabbed_browser.widget.tabBar()
selection_override = self._get_selection_override(prev, next_,
opposite)

Expand Down Expand Up @@ -265,7 +265,7 @@ def tab_pin(self, count=None):
return

to_pin = not tab.data.pinned
self._tabbed_browser.set_tab_pinned(tab, to_pin)
self._tabbed_browser.widget.set_tab_pinned(tab, to_pin)

@cmdutils.register(instance='command-dispatcher', name='open',
maxsplit=0, scope='window')
Expand Down Expand Up @@ -484,7 +484,8 @@ def tab_clone(self, bg=False, window=False):
"""
cmdutils.check_exclusive((bg, window), 'bw')
curtab = self._current_widget()
cur_title = self._tabbed_browser.page_title(self._current_index())
cur_title = self._tabbed_browser.widget.page_title(
self._current_index())
try:
history = curtab.history.serialize()
except browsertab.WebTabError as e:
Expand All @@ -500,18 +501,18 @@ def tab_clone(self, bg=False, window=False):
newtab = new_tabbed_browser.tabopen(background=bg)
new_tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=newtab.win_id)
idx = new_tabbed_browser.indexOf(newtab)
idx = new_tabbed_browser.widget.indexOf(newtab)

new_tabbed_browser.set_page_title(idx, cur_title)
new_tabbed_browser.widget.set_page_title(idx, cur_title)
if config.val.tabs.favicons.show:
new_tabbed_browser.setTabIcon(idx, curtab.icon())
new_tabbed_browser.widget.setTabIcon(idx, curtab.icon())
if config.val.tabs.tabs_are_windows:
new_tabbed_browser.window().setWindowIcon(curtab.icon())
new_tabbed_browser.widget.window().setWindowIcon(curtab.icon())

newtab.data.keep_icon = True
newtab.history.deserialize(history)
newtab.zoom.set_factor(curtab.zoom.factor())
new_tabbed_browser.set_tab_pinned(newtab, curtab.data.pinned)
new_tabbed_browser.widget.set_tab_pinned(newtab, curtab.data.pinned)
return newtab

@cmdutils.register(instance='command-dispatcher', scope='window')
Expand Down Expand Up @@ -847,7 +848,7 @@ def yank(self, what='url', sel=False, keep=False):
keep: Stay in visual mode after yanking the selection.
"""
if what == 'title':
s = self._tabbed_browser.page_title(self._current_index())
s = self._tabbed_browser.widget.page_title(self._current_index())
elif what == 'domain':
port = self._current_url().port()
s = '{}://{}{}'.format(self._current_url().scheme(),
Expand Down Expand Up @@ -959,7 +960,7 @@ def tab_only(self, prev=False, next_=False, force=False):
force: Avoid confirmation for pinned tabs.
"""
cmdutils.check_exclusive((prev, next_), 'pn')
cur_idx = self._tabbed_browser.currentIndex()
cur_idx = self._tabbed_browser.widget.currentIndex()
assert cur_idx != -1

def _to_close(i):
Expand Down Expand Up @@ -1076,11 +1077,11 @@ def _resolve_buffer_index(self, index):

tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=win_id)
if not 0 < idx <= tabbed_browser.count():
if not 0 < idx <= tabbed_browser.widget.count():
raise cmdexc.CommandError(
"There's no tab with index {}!".format(idx))

return (tabbed_browser, tabbed_browser.widget(idx-1))
return (tabbed_browser, tabbed_browser.widget.widget(idx-1))

@cmdutils.register(instance='command-dispatcher', scope='window',
maxsplit=0)
Expand Down Expand Up @@ -1108,10 +1109,10 @@ def buffer(self, index=None, count=None):

tabbed_browser, tab = self._resolve_buffer_index(index)

window = tabbed_browser.window()
window = tabbed_browser.widget.window()
window.activateWindow()
window.raise_()
tabbed_browser.setCurrentWidget(tab)
tabbed_browser.widget.setCurrentWidget(tab)

@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('index', choices=['last'])
Expand Down Expand Up @@ -1195,7 +1196,7 @@ def tab_move(self, index: typing.Union[str, int] = None, count=None):
cur_idx = self._current_index()
cmdutils.check_overflow(cur_idx, 'int')
cmdutils.check_overflow(new_idx, 'int')
self._tabbed_browser.tabBar().moveTab(cur_idx, new_idx)
self._tabbed_browser.widget.tabBar().moveTab(cur_idx, new_idx)

@cmdutils.register(instance='command-dispatcher', scope='window',
maxsplit=0, no_replace_variables=True)
Expand Down Expand Up @@ -1279,10 +1280,10 @@ def _run_userscript(self, selection, cmd, args, verbose):

idx = self._current_index()
if idx != -1:
env['QUTE_TITLE'] = self._tabbed_browser.page_title(idx)
env['QUTE_TITLE'] = self._tabbed_browser.widget.page_title(idx)

# FIXME:qtwebengine: If tab is None, run_async will fail!
tab = self._tabbed_browser.currentWidget()
tab = self._tabbed_browser.widget.currentWidget()

try:
url = self._tabbed_browser.current_url()
Expand Down Expand Up @@ -2220,5 +2221,5 @@ def fullscreen(self, leave=False):
pass
return

window = self._tabbed_browser.window()
window = self._tabbed_browser.widget.window()
window.setWindowState(window.windowState() ^ Qt.WindowFullScreen)
2 changes: 1 addition & 1 deletion qutebrowser/browser/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def start(self, # pylint: disable=keyword-arg-before-vararg
"""
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=self._win_id)
tab = tabbed_browser.currentWidget()
tab = tabbed_browser.widget.currentWidget()
if tab is None:
raise cmdexc.CommandError("No WebView available yet!")

Expand Down
4 changes: 2 additions & 2 deletions qutebrowser/browser/signalfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ def _filter_signals(self, signal, tab, *args):
tabbed_browser = objreg.get('tabbed-browser', scope='window',
window=self._win_id)
try:
tabidx = tabbed_browser.indexOf(tab)
tabidx = tabbed_browser.widget.indexOf(tab)
except RuntimeError:
# The tab has been deleted already
return
if tabidx == tabbed_browser.currentIndex():
if tabidx == tabbed_browser.widget.currentIndex():
if log_signal:
log.signals.debug("emitting: {} (tab {})".format(
debug.dbg_signal(signal, args), tabidx))
Expand Down
6 changes: 3 additions & 3 deletions qutebrowser/completion/models/miscmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def delete_buffer(data):
if tabbed_browser.shutting_down:
continue
tabs = []
for idx in range(tabbed_browser.count()):
tab = tabbed_browser.widget(idx)
for idx in range(tabbed_browser.widget.count()):
tab = tabbed_browser.widget.widget(idx)
tabs.append(("{}/{}".format(win_id, idx + 1),
tab.url().toDisplayString(),
tabbed_browser.page_title(idx)))
tabbed_browser.widget.page_title(idx)))
cat = listcategory.ListCategory("{}".format(win_id), tabs,
delete_func=delete_buffer)
model.add_category(cat)
Expand Down
12 changes: 6 additions & 6 deletions qutebrowser/mainwindow/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def _init_command_dispatcher(self):
self.tabbed_browser)
objreg.register('command-dispatcher', dispatcher, scope='window',
window=self.win_id)
self.tabbed_browser.destroyed.connect(
self.tabbed_browser.widget.destroyed.connect(
functools.partial(objreg.delete, 'command-dispatcher',
scope='window', window=self.win_id))

Expand All @@ -347,10 +347,10 @@ def _on_config_changed(self, option):

def _add_widgets(self):
"""Add or readd all widgets to the VBox."""
self._vbox.removeWidget(self.tabbed_browser)
self._vbox.removeWidget(self.tabbed_browser.widget)
self._vbox.removeWidget(self._downloadview)
self._vbox.removeWidget(self.status)
widgets = [self.tabbed_browser]
widgets = [self.tabbed_browser.widget]

downloads_position = config.val.downloads.position
if downloads_position == 'top':
Expand Down Expand Up @@ -469,7 +469,7 @@ def _connect_signals(self):

self.tabbed_browser.cur_scroll_perc_changed.connect(
status.percentage.set_perc)
self.tabbed_browser.tab_index_changed.connect(
self.tabbed_browser.widget.tab_index_changed.connect(
status.tabindex.on_tab_index_changed)

self.tabbed_browser.cur_url_changed.connect(status.url.set_url)
Expand Down Expand Up @@ -517,7 +517,7 @@ def resizeEvent(self, e):
super().resizeEvent(e)
self._update_overlay_geometries()
self._downloadview.updateGeometry()
self.tabbed_browser.tabBar().refresh()
self.tabbed_browser.widget.tabBar().refresh()

def showEvent(self, e):
"""Extend showEvent to register us as the last-visible-main-window.
Expand Down Expand Up @@ -546,7 +546,7 @@ def closeEvent(self, e):
if crashsignal.is_crashing:
e.accept()
return
tab_count = self.tabbed_browser.count()
tab_count = self.tabbed_browser.widget.count()
download_model = objreg.get('download-model', scope='window',
window=self.win_id)
download_count = download_model.running_downloads()
Expand Down
2 changes: 1 addition & 1 deletion qutebrowser/mainwindow/statusbar/backforward.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, parent=None):

def on_tab_cur_url_changed(self, tabs):
"""Called on URL changes."""
tab = tabs.currentWidget()
tab = tabs.widget.currentWidget()
if tab is None: # pragma: no cover
self.setText('')
self.hide()
Expand Down
2 changes: 1 addition & 1 deletion qutebrowser/mainwindow/statusbar/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _current_tab(self):
"""Get the currently displayed tab."""
window = objreg.get('tabbed-browser', scope='window',
window=self._win_id)
return window.currentWidget()
return window.widget.currentWidget()

def set_mode_active(self, mode, val):
"""Setter for self.{insert,command,caret}_active.
Expand Down
Loading

0 comments on commit e169e21

Please sign in to comment.