From f448aeebdac8c377beff5a0c6a567fc4883d0cae Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 13 May 2014 21:25:16 +0200 Subject: [PATCH] Use property to iterate over tabs --- TODO | 1 - qutebrowser/app.py | 4 ++-- qutebrowser/widgets/_tabbedbrowser.py | 28 ++++++++++++++++++--------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/TODO b/TODO index 39b8cafdc..ca9b7207b 100644 --- a/TODO +++ b/TODO @@ -92,7 +92,6 @@ Bugs Style ===== -- add generator which yields tab widgets instead doing range() foo - initialize completion models at some nicer place (not in widget) - move curcommand stuff to other places (e.g. current widget, etc.) maybe rename curcommand to commands or so? diff --git a/qutebrowser/app.py b/qutebrowser/app.py index b06a94430..e5b01f233 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -365,9 +365,9 @@ class QuteBrowser(QApplication): return pages if self.mainwindow.tabs is None: return pages - for tabidx in range(self.mainwindow.tabs.count()): + for tab in self.mainwindow.tabs.widgets: try: - url = self.mainwindow.tabs.widget(tabidx).url().toString() + url = tab.url().toString() if url: pages.append(url) except Exception: # pylint: disable=broad-except diff --git a/qutebrowser/widgets/_tabbedbrowser.py b/qutebrowser/widgets/_tabbedbrowser.py index 5a7458ee9..d2ce5d4e2 100644 --- a/qutebrowser/widgets/_tabbedbrowser.py +++ b/qutebrowser/widgets/_tabbedbrowser.py @@ -104,6 +104,17 @@ class TabbedBrowser(TabWidget): # FIXME adjust this to font size self.setIconSize(QSize(12, 12)) + @property + def widgets(self): + """Get a list of open tab widgets. + + We don't implement this as generator so we can delete tabs while + iterating over the list.""" + w = [] + for i in range(self.count()): + w.append(self.widget(i)) + return w + def _cb_tab_shutdown(self, tab): """Called after a tab has been shut down completely. @@ -153,7 +164,8 @@ class TabbedBrowser(TabWidget): """ idx = self.indexOf(tab) if idx == -1: - raise ValueError("tab is not contained in TabbedWidget!") + raise ValueError("tab {} is not contained in " + "TabbedWidget!".format(tab)) url = tab.url() if not url.isEmpty(): self._url_stack.append(url) @@ -252,9 +264,7 @@ class TabbedBrowser(TabWidget): logging.debug("No tabs -> shutdown complete") self.shutdown_complete.emit() return - for tabidx in range(tabcount): - logging.debug("Shutting down tab {}/{}".format(tabidx, tabcount)) - tab = self.widget(tabidx) + for tab in self.widgets: tab.shutdown(callback=partial(self._cb_tab_shutdown, tab)) @cmdutils.register(instance='mainwindow.tabs') @@ -284,10 +294,10 @@ class TabbedBrowser(TabWidget): @cmdutils.register(instance='mainwindow.tabs') def only(self): """Close all tabs except for the current one.""" - for i in range(self.count() - 1): - if i == self.currentIndex(): + for tab in self.widgets: + if tab is self.currentWidget(): continue - self._close_tab(self.widget(i)) + self._close_tab(tab) @cmdutils.register(instance='mainwindow.tabs', split=False, name='tabopen') def tabopen_cmd(self, url): @@ -463,9 +473,9 @@ class TabbedBrowser(TabWidget): tab.on_config_changed(section, option) if (section, option) == ('tabbar', 'show-favicons'): show = config.get('tabbar', 'show-favicons') - for i in range(self.count()): + for i, tab in enumerate(self.widgets): if show: - self.setTabIcon(i, self.widget(i).icon()) + self.setTabIcon(i, tab.icon()) else: self.setTabIcon(i, EmptyTabIcon())