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
=====
- 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?

View File

@ -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

View File

@ -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())