Use property to iterate over tabs

This commit is contained in:
Florian Bruhin 2014-05-13 21:25:16 +02:00
parent 6b2d2713f7
commit f448aeebda
3 changed files with 21 additions and 12 deletions

1
TODO
View File

@ -92,7 +92,6 @@ Bugs
Style Style
===== =====
- add generator which yields tab widgets instead doing range() foo
- initialize completion models at some nicer place (not in widget) - initialize completion models at some nicer place (not in widget)
- move curcommand stuff to other places (e.g. current widget, etc.) - move curcommand stuff to other places (e.g. current widget, etc.)
maybe rename curcommand to commands or so? maybe rename curcommand to commands or so?

View File

@ -365,9 +365,9 @@ class QuteBrowser(QApplication):
return pages return pages
if self.mainwindow.tabs is None: if self.mainwindow.tabs is None:
return pages return pages
for tabidx in range(self.mainwindow.tabs.count()): for tab in self.mainwindow.tabs.widgets:
try: try:
url = self.mainwindow.tabs.widget(tabidx).url().toString() url = tab.url().toString()
if url: if url:
pages.append(url) pages.append(url)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except

View File

@ -104,6 +104,17 @@ class TabbedBrowser(TabWidget):
# FIXME adjust this to font size # FIXME adjust this to font size
self.setIconSize(QSize(12, 12)) 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): def _cb_tab_shutdown(self, tab):
"""Called after a tab has been shut down completely. """Called after a tab has been shut down completely.
@ -153,7 +164,8 @@ class TabbedBrowser(TabWidget):
""" """
idx = self.indexOf(tab) idx = self.indexOf(tab)
if idx == -1: if idx == -1:
raise ValueError("tab is not contained in TabbedWidget!") raise ValueError("tab {} is not contained in "
"TabbedWidget!".format(tab))
url = tab.url() url = tab.url()
if not url.isEmpty(): if not url.isEmpty():
self._url_stack.append(url) self._url_stack.append(url)
@ -252,9 +264,7 @@ class TabbedBrowser(TabWidget):
logging.debug("No tabs -> shutdown complete") logging.debug("No tabs -> shutdown complete")
self.shutdown_complete.emit() self.shutdown_complete.emit()
return return
for tabidx in range(tabcount): for tab in self.widgets:
logging.debug("Shutting down tab {}/{}".format(tabidx, tabcount))
tab = self.widget(tabidx)
tab.shutdown(callback=partial(self._cb_tab_shutdown, tab)) tab.shutdown(callback=partial(self._cb_tab_shutdown, tab))
@cmdutils.register(instance='mainwindow.tabs') @cmdutils.register(instance='mainwindow.tabs')
@ -284,10 +294,10 @@ class TabbedBrowser(TabWidget):
@cmdutils.register(instance='mainwindow.tabs') @cmdutils.register(instance='mainwindow.tabs')
def only(self): def only(self):
"""Close all tabs except for the current one.""" """Close all tabs except for the current one."""
for i in range(self.count() - 1): for tab in self.widgets:
if i == self.currentIndex(): if tab is self.currentWidget():
continue continue
self._close_tab(self.widget(i)) self._close_tab(tab)
@cmdutils.register(instance='mainwindow.tabs', split=False, name='tabopen') @cmdutils.register(instance='mainwindow.tabs', split=False, name='tabopen')
def tabopen_cmd(self, url): def tabopen_cmd(self, url):
@ -463,9 +473,9 @@ class TabbedBrowser(TabWidget):
tab.on_config_changed(section, option) tab.on_config_changed(section, option)
if (section, option) == ('tabbar', 'show-favicons'): if (section, option) == ('tabbar', 'show-favicons'):
show = config.get('tabbar', 'show-favicons') show = config.get('tabbar', 'show-favicons')
for i in range(self.count()): for i, tab in enumerate(self.widgets):
if show: if show:
self.setTabIcon(i, self.widget(i).icon()) self.setTabIcon(i, tab.icon())
else: else:
self.setTabIcon(i, EmptyTabIcon()) self.setTabIcon(i, EmptyTabIcon())