2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2014-05-13 07:10:50 +02:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""URL displayed in the statusbar."""
|
|
|
|
|
|
|
|
from PyQt5.QtCore import pyqtSlot, pyqtProperty, Qt
|
|
|
|
|
2014-05-15 19:02:20 +02:00
|
|
|
from qutebrowser.widgets.webview import LoadStatus
|
2014-06-03 14:57:57 +02:00
|
|
|
from qutebrowser.widgets.statusbar.textbase import TextBase
|
2014-05-13 07:10:50 +02:00
|
|
|
from qutebrowser.config.style import set_register_stylesheet, get_stylesheet
|
|
|
|
|
|
|
|
|
2014-06-20 17:40:36 +02:00
|
|
|
class UrlText(TextBase):
|
2014-05-13 07:10:50 +02:00
|
|
|
|
|
|
|
"""URL displayed in the statusbar.
|
|
|
|
|
|
|
|
Class attributes:
|
|
|
|
STYLESHEET: The stylesheet template.
|
|
|
|
|
|
|
|
Attributes:
|
2014-05-19 18:08:33 +02:00
|
|
|
normal_url: The normal URL to be displayed.
|
|
|
|
normal_url_type: The type of the normal URL.
|
|
|
|
hover_url: The URL we're currently hovering over.
|
2014-05-13 07:10:50 +02:00
|
|
|
_ssl_errors: Whether SSL errors occured while loading.
|
|
|
|
|
|
|
|
Class attributes:
|
2014-05-19 18:08:33 +02:00
|
|
|
_urltype: The URL type to show currently (normal/ok/error/warn/hover).
|
2014-05-13 07:10:50 +02:00
|
|
|
Accessed via the urltype property.
|
|
|
|
|
|
|
|
For some reason we need to have this as class attribute so
|
|
|
|
pyqtProperty works correctly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_urltype = None
|
|
|
|
|
|
|
|
STYLESHEET = """
|
2014-06-20 17:40:36 +02:00
|
|
|
QLabel#UrlText[urltype="normal"] {{
|
2014-05-13 07:10:50 +02:00
|
|
|
{color[statusbar.url.fg]}
|
|
|
|
}}
|
|
|
|
|
2014-06-20 17:40:36 +02:00
|
|
|
QLabel#UrlText[urltype="success"] {{
|
2014-05-13 07:10:50 +02:00
|
|
|
{color[statusbar.url.fg.success]}
|
|
|
|
}}
|
|
|
|
|
2014-06-20 17:40:36 +02:00
|
|
|
QLabel#UrlText[urltype="error"] {{
|
2014-05-13 07:10:50 +02:00
|
|
|
{color[statusbar.url.fg.error]}
|
|
|
|
}}
|
|
|
|
|
2014-06-20 17:40:36 +02:00
|
|
|
QLabel#UrlText[urltype="warn"] {{
|
2014-05-13 07:10:50 +02:00
|
|
|
{color[statusbar.url.fg.warn]}
|
|
|
|
}}
|
|
|
|
|
2014-06-20 17:40:36 +02:00
|
|
|
QLabel#UrlText[urltype="hover"] {{
|
2014-05-13 07:10:50 +02:00
|
|
|
{color[statusbar.url.fg.hover]}
|
|
|
|
}}
|
|
|
|
"""
|
|
|
|
|
2014-06-17 10:20:15 +02:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
"""Override TextBase.__init__ to elide in the middle by default."""
|
|
|
|
super().__init__(parent, Qt.ElideMiddle)
|
2014-05-13 07:10:50 +02:00
|
|
|
self.setObjectName(self.__class__.__name__)
|
|
|
|
set_register_stylesheet(self)
|
2014-05-19 18:08:33 +02:00
|
|
|
self._hover_url = None
|
|
|
|
self._normal_url = None
|
|
|
|
self._normal_url_type = 'normal'
|
2014-05-13 07:10:50 +02:00
|
|
|
|
|
|
|
@pyqtProperty(str)
|
|
|
|
def urltype(self):
|
|
|
|
"""Getter for self.urltype, so it can be used as Qt property."""
|
|
|
|
# pylint: disable=method-hidden
|
|
|
|
return self._urltype
|
|
|
|
|
|
|
|
@urltype.setter
|
|
|
|
def urltype(self, val):
|
2014-05-19 18:08:33 +02:00
|
|
|
"""Setter for self.urltype to update stylesheets after it is set."""
|
2014-05-13 07:10:50 +02:00
|
|
|
self._urltype = val
|
|
|
|
self.setStyleSheet(get_stylesheet(self.STYLESHEET))
|
|
|
|
|
2014-05-19 18:08:33 +02:00
|
|
|
@property
|
|
|
|
def hover_url(self):
|
|
|
|
"""Getter so we can define a setter."""
|
|
|
|
return self._hover_url
|
|
|
|
|
|
|
|
@hover_url.setter
|
|
|
|
def hover_url(self, val):
|
|
|
|
"""Setter to update displayed URL when hover_url was set."""
|
|
|
|
self._hover_url = val
|
|
|
|
self._update_url()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def normal_url(self):
|
|
|
|
"""Getter so we can define a setter."""
|
|
|
|
return self._normal_url
|
|
|
|
|
|
|
|
@normal_url.setter
|
|
|
|
def normal_url(self, val):
|
|
|
|
"""Setter to update displayed URL when normal_url was set."""
|
|
|
|
self._normal_url = val
|
|
|
|
self._update_url()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def normal_url_type(self):
|
|
|
|
"""Getter so we can define a setter."""
|
|
|
|
return self._normal_url_type
|
|
|
|
|
|
|
|
@normal_url_type.setter
|
|
|
|
def normal_url_type(self, val):
|
|
|
|
"""Setter to update displayed URL when normal_url_type was set."""
|
|
|
|
self._normal_url_type = val
|
|
|
|
self._update_url()
|
|
|
|
|
|
|
|
def _update_url(self):
|
|
|
|
"""Update the displayed URL if the url or the hover url changed."""
|
|
|
|
if self.hover_url is not None:
|
|
|
|
self.setText(self.hover_url)
|
|
|
|
self.urltype = 'hover'
|
|
|
|
elif self.normal_url is not None:
|
|
|
|
self.setText(self.normal_url)
|
|
|
|
self.urltype = self.normal_url_type
|
|
|
|
else:
|
|
|
|
self.setText('')
|
|
|
|
self.urltype = 'normal'
|
|
|
|
|
2014-05-15 17:57:08 +02:00
|
|
|
@pyqtSlot(str)
|
|
|
|
def on_load_status_changed(self, status):
|
|
|
|
"""Slot for load_status_changed. Sets URL color accordingly.
|
2014-05-13 07:10:50 +02:00
|
|
|
|
|
|
|
Args:
|
2014-05-15 17:57:08 +02:00
|
|
|
status: The LoadStatus as string.
|
2014-05-13 07:10:50 +02:00
|
|
|
"""
|
2014-06-06 17:12:54 +02:00
|
|
|
if status in ('success', 'error', 'warn'):
|
2014-05-19 18:08:33 +02:00
|
|
|
self.normal_url_type = status
|
2014-05-13 07:10:50 +02:00
|
|
|
else:
|
2014-05-19 18:08:33 +02:00
|
|
|
self.normal_url_type = 'normal'
|
2014-05-13 07:10:50 +02:00
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
def set_url(self, s):
|
|
|
|
"""Setter to be used as a Qt slot.
|
|
|
|
|
|
|
|
Args:
|
2014-05-15 22:31:01 +02:00
|
|
|
s: The URL to set as string.
|
2014-05-13 07:10:50 +02:00
|
|
|
"""
|
2014-05-19 18:08:33 +02:00
|
|
|
self.normal_url = s
|
|
|
|
self.normal_url_type = 'normal'
|
2014-05-13 07:10:50 +02:00
|
|
|
|
|
|
|
@pyqtSlot(str, str, str)
|
|
|
|
def set_hover_url(self, link, _title, _text):
|
|
|
|
"""Setter to be used as a Qt slot.
|
|
|
|
|
|
|
|
Saves old shown URL in self._old_url and restores it later if a link is
|
|
|
|
"un-hovered" when it gets called with empty parameters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
link: The link which was hovered (string)
|
|
|
|
_title: The title of the hovered link (string)
|
|
|
|
_text: The text of the hovered link (string)
|
|
|
|
"""
|
|
|
|
if link:
|
2014-05-19 18:08:33 +02:00
|
|
|
self.hover_url = link
|
2014-05-13 07:10:50 +02:00
|
|
|
else:
|
2014-05-19 18:08:33 +02:00
|
|
|
self.hover_url = None
|
2014-05-15 19:02:20 +02:00
|
|
|
|
|
|
|
@pyqtSlot(int)
|
2014-06-16 22:49:22 +02:00
|
|
|
def on_tab_changed(self, tab):
|
2014-05-15 19:02:20 +02:00
|
|
|
"""Update URL if the tab changed."""
|
2014-05-19 18:10:48 +02:00
|
|
|
self.hover_url = None
|
2014-05-19 18:08:33 +02:00
|
|
|
self.normal_url = tab.url_text
|
2014-05-15 19:02:20 +02:00
|
|
|
status = LoadStatus[tab.load_status]
|
2014-06-06 17:12:54 +02:00
|
|
|
if status in ('success', 'error', 'warn'):
|
2014-05-19 18:08:33 +02:00
|
|
|
self.normal_url_type = status
|
2014-05-15 19:02:20 +02:00
|
|
|
else:
|
2014-05-19 18:08:33 +02:00
|
|
|
self.normal_url_type = 'normal'
|