Merge remote-tracking branch 'origin/pr/6448'

This commit is contained in:
Florian Bruhin 2022-06-13 13:15:27 +02:00
commit ce27831740
5 changed files with 106 additions and 34 deletions

View File

@ -4131,6 +4131,7 @@ Valid values:
* +keypress+: Display pressed keys when composing a vi command.
* +progress+: Progress bar for the current page loading.
* +text:foo+: Display the static text after the colon, `foo` in the example.
* +clock+: Display current time. Format can be changed by adding like `clock:$format`. For format strings see link:https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior[the python datetime library].
Default:

View File

@ -2062,6 +2062,9 @@ statusbar.widgets:
- keypress: "Display pressed keys when composing a vi command."
- progress: "Progress bar for the current page loading."
- 'text:foo': "Display the static text after the colon, `foo` in the example."
- clock: "Display current time. Format can be changed by adding like
`clock:$format`. For format strings see
link:https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior[the python datetime library]."
none_ok: true
default: ['keypress', 'url', 'scroll', 'history', 'tabs', 'progress']
desc: "List of widgets displayed in the statusbar."

View File

@ -2015,6 +2015,6 @@ class StatusbarWidget(String):
"""
def _validate_valid_values(self, value: str) -> None:
if value.startswith("text:"):
if value.startswith("text:") or value.startswith("clock:"):
return
super()._validate_valid_values(value)

View File

@ -31,7 +31,7 @@ from qutebrowser.keyinput import modeman
from qutebrowser.utils import usertypes, log, objreg, utils
from qutebrowser.mainwindow.statusbar import (backforward, command, progress,
keystring, percentage, url,
tabindex, textbase)
tabindex, textbase, clock)
@dataclasses.dataclass
@ -199,6 +199,7 @@ class StatusBar(QWidget):
self.tabindex = tabindex.TabIndex()
self.keystring = keystring.KeyString()
self.prog = progress.Progress(self)
self.clock = clock.Clock()
self._text_widgets = []
self._draw_widgets()
@ -208,6 +209,31 @@ class StatusBar(QWidget):
def __repr__(self):
return utils.get_repr(self)
def _get_widget_from_config(self, key):
"""Return the widget that fits with config string key."""
if key == 'url':
return self.url
elif key == 'scroll':
return self.percentage
elif key == 'scroll_raw':
return self.percentage
elif key == 'history':
return self.backforward
elif key == 'tabs':
return self.tabindex
elif key == 'keypress':
return self.keystring
elif key == 'progress':
return self.prog
elif key.startswith('text:'):
new_text_widget = textbase.TextBase()
self._text_widgets.append(new_text_widget)
return new_text_widget
elif key.startswith('clock:') or key == 'clock':
return self.clock
else:
raise utils.Unreachable(key)
@pyqtSlot(str)
def _on_config_changed(self, option):
if option == 'statusbar.show':
@ -225,47 +251,35 @@ class StatusBar(QWidget):
# Read the list and set widgets accordingly
for segment in config.val.statusbar.widgets:
if segment == 'url':
self._hbox.addWidget(self.url)
self.url.show()
elif segment == 'scroll':
self._hbox.addWidget(self.percentage)
self.percentage.show()
elif segment == 'scroll_raw':
self._hbox.addWidget(self.percentage)
self.percentage.set_raw()
self.percentage.show()
elif segment == 'history':
self._hbox.addWidget(self.backforward)
self.backforward.enabled = True
cur_widget = self._get_widget_from_config(segment)
self._hbox.addWidget(cur_widget)
if segment == 'scroll_raw':
cur_widget.set_raw()
elif segment in ('history', 'progress'):
cur_widget.enabled = True
if tab:
self.backforward.on_tab_changed(tab)
elif segment == 'tabs':
self._hbox.addWidget(self.tabindex)
self.tabindex.show()
elif segment == 'keypress':
self._hbox.addWidget(self.keystring)
self.keystring.show()
elif segment == 'progress':
self._hbox.addWidget(self.prog)
self.prog.enabled = True
if tab:
self.prog.on_tab_changed(tab)
cur_widget.on_tab_changed(tab)
# Do not call .show() for these widgets.
return
elif segment.startswith('text:'):
cur_widget = textbase.TextBase()
self._text_widgets.append(cur_widget)
cur_widget.setText(segment.split(':', maxsplit=1)[1])
self._hbox.addWidget(cur_widget)
cur_widget.show()
else:
raise utils.Unreachable(segment)
elif segment.startswith('clock:') or segment == 'clock':
split_segment = segment.split(':', maxsplit=1)
if len(split_segment) == 2 and split_segment[1]:
cur_widget.format = split_segment[1]
else:
cur_widget.format = '%X'
cur_widget.show()
def _clear_widgets(self):
"""Clear widgets before redrawing them."""
# Start with widgets hidden and show them when needed
for widget in [self.url, self.percentage,
self.backforward, self.tabindex,
self.keystring, self.prog, *self._text_widgets]:
self.keystring, self.prog, self.clock, *self._text_widgets]:
assert isinstance(widget, QWidget)
widget.hide()
self._hbox.removeWidget(widget)

View File

@ -0,0 +1,54 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014-2021 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 <https://www.gnu.org/licenses/>.
"""Clock displayed in the statusbar."""
from datetime import datetime
from PyQt5.QtCore import Qt, QTimer
from qutebrowser.mainwindow.statusbar import textbase
class Clock(textbase.TextBase):
"""Shows current time and date in the statusbar."""
UPDATE_DELAY = 500 # ms
def __init__(self, parent=None):
super().__init__(parent, elidemode=Qt.ElideNone)
self.format = ""
self.timer = QTimer(self)
self.timer.timeout.connect(self._show_time)
def _show_time(self):
"""Set text to current time, using self.format as format-string."""
self.setText(datetime.now().strftime(self.format))
def hideEvent(self, event):
"""Stop timer when widget is hidden."""
self.timer.stop()
super().hideEvent(event)
def showEvent(self, event):
"""Override showEvent to show time and start self.timer for updating."""
self.timer.start(Clock.UPDATE_DELAY)
self._show_time()
super().showEvent(event)