widgets.webview: Add a cur_url property so the initial URL is available immediately.

This commit is contained in:
Florian Bruhin 2014-08-02 02:35:38 +02:00
parent f6b26f0d56
commit 157975b0d4
5 changed files with 28 additions and 27 deletions

View File

@ -15,9 +15,6 @@ Downloads
Webview Webview
------- -------
- Concept of "current URL" should work better before urlChanged is emitted,
e.g. loading a page and immediately yanking the (non-resolved) URL should
work.
- Hint positions wrong in wordpress admin interface - Hint positions wrong in wordpress admin interface
- Rightclick -> Copy link copies relative links - Rightclick -> Copy link copies relative links
- F on duckduckgo result page opens in current page - F on duckduckgo result page opens in current page

View File

@ -482,7 +482,8 @@ class Application(QApplication):
return pages return pages
for tab in self.mainwindow.tabs.widgets: for tab in self.mainwindow.tabs.widgets:
try: try:
url = tab.url().toString() # FIXME should that be encoded?
url = tab.cur_url.toString()
if url: if url:
pages.append(url) pages.append(url)
except Exception as e: # pylint: disable=broad-except except Exception as e: # pylint: disable=broad-except
@ -594,7 +595,8 @@ class Application(QApplication):
if pages is None: if pages is None:
pages = [] pages = []
for tab in self.mainwindow.tabs.widgets: for tab in self.mainwindow.tabs.widgets:
urlstr = tab.url().toString() # FIXME should that be encoded?
urlstr = tab.cur_url.toString()
if urlstr: if urlstr:
pages.append(urlstr) pages.append(urlstr)
log.destroy.debug("sys.executable: {}".format(sys.executable)) log.destroy.debug("sys.executable: {}".format(sys.executable))

View File

@ -201,5 +201,5 @@ class UrlText(TextBase):
def on_tab_changed(self, tab): def on_tab_changed(self, tab):
"""Update URL if the tab changed.""" """Update URL if the tab changed."""
self.hover_url = None self.hover_url = None
self.normal_url = tab.url_text self.normal_url = tab.cur_url.toDisplayString()
self.on_load_status_changed(tab.load_status.name) self.on_load_status_changed(tab.load_status.name)

View File

@ -200,7 +200,7 @@ class TabbedBrowser(TabWidget):
Raise: Raise:
CommandError if the current URL is invalid. CommandError if the current URL is invalid.
""" """
url = self.currentWidget().url() url = self.currentWidget().cur_url
try: try:
qt_ensure_valid(url) qt_ensure_valid(url)
except QtValueError as e: except QtValueError as e:
@ -257,10 +257,9 @@ class TabbedBrowser(TabWidget):
self._now_focused = None self._now_focused = None
if tab is self.last_focused: if tab is self.last_focused:
self.last_focused = None self.last_focused = None
url = tab.url() if not tab.cur_url.isEmpty():
if not url.isEmpty(): qt_ensure_valid(tab.cur_url)
qt_ensure_valid(url) self.url_stack.append(tab.cur_url)
self.url_stack.append(url)
tab.shutdown() tab.shutdown()
self._tabs.remove(tab) self._tabs.remove(tab)
self.removeTab(idx) self.removeTab(idx)

View File

@ -19,7 +19,7 @@
"""The main browser widgets.""" """The main browser widgets."""
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QTimer from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QTimer, QUrl
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebView, QWebPage from PyQt5.QtWebKitWidgets import QWebView, QWebPage
@ -58,8 +58,7 @@ class WebView(QWebView):
inspector: The QWebInspector used for this webview. inspector: The QWebInspector used for this webview.
open_target: Where to open the next tab ("normal", "tab", "tab_bg") open_target: Where to open the next tab ("normal", "tab", "tab_bg")
_page: The QWebPage behind the view _page: The QWebPage behind the view
_url_text: The current URL as string. _cur_url: The current URL (accessed via cur_url property).
Accessed via url_text property.
_load_status: loading status of this page (index into LoadStatus) _load_status: loading status of this page (index into LoadStatus)
Accessed via load_status property. Accessed via load_status property.
_has_ssl_errors: Whether SSL errors occured during loading. _has_ssl_errors: Whether SSL errors occured during loading.
@ -99,7 +98,8 @@ class WebView(QWebView):
self._zoom = None self._zoom = None
self._has_ssl_errors = False self._has_ssl_errors = False
self._init_neighborlist() self._init_neighborlist()
self._url_text = '' self._cur_url = None
self.cur_url = QUrl()
self.progress = 0 self.progress = 0
self._page = BrowserPage(self) self._page = BrowserPage(self)
self.setPage(self._page) self.setPage(self._page)
@ -153,19 +153,22 @@ class WebView(QWebView):
self.load_status_changed.emit(val.name) self.load_status_changed.emit(val.name)
@property @property
def url_text(self): def cur_url(self):
"""Getter for url_text.""" """Getter for cur_url so we can define a setter."""
return self._url_text return self._cur_url
@url_text.setter @cur_url.setter
def url_text(self, val): def cur_url(self, url):
"""Setter for url_text. """Setter for cur_url to emit a signal..
Arg:
url: The new URL as a QUrl.
Emit: Emit:
url_text_changed url_text_changed: Always emitted.
""" """
self._url_text = val self._cur_url = url
self.url_text_changed.emit(val) self.url_text_changed.emit(url.toDisplayString())
def _init_neighborlist(self): def _init_neighborlist(self):
"""Initialize the _zoom neighborlist.""" """Initialize the _zoom neighborlist."""
@ -300,7 +303,7 @@ class WebView(QWebView):
urlstr = url.toDisplayString() urlstr = url.toDisplayString()
log.webview.debug("New title: {}".format(urlstr)) log.webview.debug("New title: {}".format(urlstr))
self.titleChanged.emit(urlstr) self.titleChanged.emit(urlstr)
self.url_text = urlstr self.cur_url = url
return self.load(url) return self.load(url)
def zoom_perc(self, perc, fuzzyval=True): def zoom_perc(self, perc, fuzzyval=True):
@ -352,9 +355,9 @@ class WebView(QWebView):
@pyqtSlot('QUrl') @pyqtSlot('QUrl')
def on_url_changed(self, url): def on_url_changed(self, url):
"""Update url_text when URL has changed.""" """Update cur_url when URL has changed."""
qt_ensure_valid(url) qt_ensure_valid(url)
self.url_text = url.toDisplayString() self.cur_url = url
@pyqtSlot(str, str) @pyqtSlot(str, str)
def on_config_changed(self, section, option): def on_config_changed(self, section, option):