qutebrowser/qutebrowser/mainwindow/mainwindow.py

531 lines
20 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-02-06 14:01:23 +01:00
#
# 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/>.
2014-06-04 14:52:12 +02:00
"""The main window of qutebrowser."""
2014-02-17 12:23:52 +01:00
import binascii
2014-08-26 19:10:14 +02:00
import base64
2014-09-28 22:13:14 +02:00
import itertools
import functools
2014-09-28 22:13:14 +02:00
from PyQt5.QtCore import pyqtSlot, QRect, QPoint, QTimer, Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QSizePolicy
2014-01-21 17:54:21 +01:00
2014-09-28 22:13:14 +02:00
from qutebrowser.commands import runners, cmdutils
2014-08-26 19:10:14 +02:00
from qutebrowser.config import config
2014-09-26 15:48:24 +02:00
from qutebrowser.utils import message, log, usertypes, qtutils, objreg, utils
2016-09-23 06:18:48 +02:00
from qutebrowser.mainwindow import tabbedbrowser, messageview, prompt
from qutebrowser.mainwindow.statusbar import bar
from qutebrowser.completion import completionwidget, completer
2014-09-28 22:13:14 +02:00
from qutebrowser.keyinput import modeman
from qutebrowser.browser import commands, downloadview, hints
from qutebrowser.browser.webkit import downloads
from qutebrowser.misc import crashsignal, keyhintwidget
2014-09-28 22:13:14 +02:00
win_id_gen = itertools.count(0)
2014-01-28 23:04:02 +01:00
2014-02-18 14:21:39 +01:00
2015-10-06 23:18:04 +02:00
def get_window(via_ipc, force_window=False, force_tab=False,
force_target=None):
2015-04-30 07:37:25 +02:00
"""Helper function for app.py to get a window id.
Args:
via_ipc: Whether the request was made via IPC.
force_window: Whether to force opening in a window.
force_tab: Whether to force opening in a tab.
2015-10-06 22:59:49 +02:00
force_target: Override the new-instance-open-target config
2015-04-30 07:37:25 +02:00
"""
if force_window and force_tab:
raise ValueError("force_window and force_tab are mutually exclusive!")
2015-04-30 07:37:25 +02:00
if not via_ipc:
# Initial main window
return 0
open_target = config.get('general', 'new-instance-open-target')
# Apply any target overrides, ordered by precedence
2015-10-06 22:59:49 +02:00
if force_target is not None:
open_target = force_target
if force_window:
open_target = 'window'
if force_tab and open_target == 'window':
# Command sent via IPC
open_target = 'tab-silent'
window = None
raise_window = False
# Try to find the existing tab target if opening in a tab
if open_target != 'window':
window = get_target_window()
raise_window = open_target not in ['tab-silent', 'tab-bg-silent']
# Otherwise, or if no window was found, create a new one
if window is None:
2015-04-30 07:37:25 +02:00
window = MainWindow()
window.show()
raise_window = True
if raise_window:
window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
window.setWindowState(window.windowState() | Qt.WindowActive)
window.raise_()
window.activateWindow()
QApplication.instance().alert(window)
return window.win_id
def get_target_window():
"""Get the target window for new tabs, or None if none exist."""
try:
win_mode = config.get('general', 'new-instance-open-target.window')
if win_mode == 'last-focused':
return objreg.last_focused_window()
elif win_mode == 'first-opened':
return objreg.window_by_index(0)
elif win_mode == 'last-opened':
return objreg.window_by_index(-1)
elif win_mode == 'last-visible':
return objreg.last_visible_window()
else:
raise ValueError("Invalid win_mode {}".format(win_mode))
except objreg.NoWindow:
return None
2015-04-30 07:37:25 +02:00
2014-02-12 20:51:50 +01:00
class MainWindow(QWidget):
2014-02-07 20:21:50 +01:00
2014-06-04 14:52:12 +02:00
"""The main window of qutebrowser.
2014-01-29 15:30:19 +01:00
2015-03-31 20:49:29 +02:00
Adds all needed components to a vbox, initializes sub-widgets and connects
2014-01-29 15:30:19 +01:00
signals.
2014-02-07 20:21:50 +01:00
2014-02-18 16:38:13 +01:00
Attributes:
status: The StatusBar widget.
tabbed_browser: The TabbedBrowser widget.
_downloadview: The DownloadView widget.
2014-02-18 16:38:13 +01:00
_vbox: The main QVBoxLayout.
2014-09-28 22:13:14 +02:00
_commandrunner: The main CommandRunner instance.
_overlays: Widgets shown as overlay for the current webpage.
2014-02-18 16:38:13 +01:00
"""
def __init__(self, geometry=None, parent=None):
"""Create a new main window.
Args:
geometry: The geometry to load, as a bytes-object (or None).
parent: The parent the window should get.
"""
2014-06-17 07:17:21 +02:00
super().__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose)
2014-09-28 22:13:14 +02:00
self._commandrunner = None
self._overlays = []
self.win_id = next(win_id_gen)
self.registry = objreg.ObjectRegistry()
objreg.window_registry[self.win_id] = self
objreg.register('main-window', self, scope='window',
window=self.win_id)
tab_registry = objreg.ObjectRegistry()
objreg.register('tab-registry', tab_registry, scope='window',
window=self.win_id)
2014-09-28 22:13:14 +02:00
message_bridge = message.MessageBridge(self)
objreg.register('message-bridge', message_bridge, scope='window',
window=self.win_id)
2014-01-21 09:05:31 +01:00
self.setWindowTitle('qutebrowser')
2014-02-18 16:38:13 +01:00
self._vbox = QVBoxLayout(self)
self._vbox.setContentsMargins(0, 0, 0, 0)
self._vbox.setSpacing(0)
self._init_downloadmanager()
self._downloadview = downloadview.DownloadView(self.win_id)
self.tabbed_browser = tabbedbrowser.TabbedBrowser(self.win_id)
objreg.register('tabbed-browser', self.tabbed_browser, scope='window',
window=self.win_id)
self._init_command_dispatcher()
2014-06-12 10:19:45 +02:00
# We need to set an explicit parent for StatusBar because it does some
# show/hide magic immediately which would mean it'd show up as a
# window.
self.status = bar.StatusBar(self.win_id, parent=self)
self._add_widgets()
self._downloadview.show()
2014-09-28 22:13:14 +02:00
self._init_completion()
self._commandrunner = runners.CommandRunner(self.win_id,
partial_match=True)
2014-09-28 22:13:14 +02:00
self._keyhint = keyhintwidget.KeyHintView(self.win_id, self)
2016-09-23 06:18:48 +02:00
self._add_overlay(self._keyhint, self._keyhint.update_geometry)
2016-09-14 20:52:32 +02:00
self._messageview = messageview.MessageView(parent=self)
2016-09-23 06:18:48 +02:00
self._add_overlay(self._messageview, self._messageview.update_geometry)
2016-10-03 18:39:53 +02:00
self._prompt_container = prompt.PromptContainer(self.win_id, self)
self._add_overlay(self._prompt_container,
self._prompt_container.update_geometry,
2016-10-26 21:25:47 +02:00
centered=True, padding=10)
objreg.register('prompt-container', self._prompt_container,
scope='window', window=self.win_id)
self._prompt_container.hide()
2014-09-28 22:13:14 +02:00
log.init.debug("Initializing modes...")
2014-10-07 23:10:58 +02:00
modeman.init(self.win_id, self)
2014-09-28 22:13:14 +02:00
if geometry is not None:
self._load_geometry(geometry)
elif self.win_id == 0:
self._load_state_geometry()
else:
self._set_default_geometry()
log.init.debug("Initial main window geometry: {}".format(
self.geometry()))
2014-09-28 22:13:14 +02:00
self._connect_signals()
2014-06-23 06:23:33 +02:00
# When we're here the statusbar might not even really exist yet, so
# resizing will fail. Therefore, we use singleShot QTimers to make sure
# we defer this until everything else is initialized.
QTimer.singleShot(0, self._connect_overlay_signals)
objreg.get('config').changed.connect(self.on_config_changed)
2015-04-09 19:51:50 +02:00
objreg.get("app").new_window.emit(self)
2016-10-26 21:25:47 +02:00
def _add_overlay(self, widget, signal, *, centered=False, padding=0):
self._overlays.append((widget, signal, centered, padding))
2016-09-23 06:18:48 +02:00
2016-10-26 21:25:47 +02:00
def _update_overlay_geometry(self, widget=None, centered=None, padding=0):
"""Reposition/resize the given overlay.
If no widget is given, reposition/resize all overlays.
"""
if widget is None:
2016-10-26 21:25:47 +02:00
for w, _signal, centered, padding in self._overlays:
self._update_overlay_geometry(w, centered, padding)
return
2016-09-23 06:18:48 +02:00
assert centered is not None
if not widget.isVisible():
return
size_hint = widget.sizeHint()
if widget.sizePolicy().horizontalPolicy() == QSizePolicy.Expanding:
2016-10-26 21:25:47 +02:00
width = self.width() - 2 * padding
left = padding
else:
width = size_hint.width()
2016-09-23 06:18:48 +02:00
left = (self.width() - size_hint.width()) / 2 if centered else 0
status_position = config.get('ui', 'status-position')
if status_position == 'bottom':
top = self.height() - self.status.height() - size_hint.height()
top = qtutils.check_overflow(top, 'int', fatal=False)
2016-09-23 06:18:48 +02:00
topleft = QPoint(left, top)
bottomright = QPoint(left + width, self.status.geometry().top())
elif status_position == 'top':
2016-09-23 06:18:48 +02:00
topleft = QPoint(left, self.status.geometry().bottom())
2016-09-22 17:34:30 +02:00
bottom = self.status.height() + size_hint.height()
bottom = qtutils.check_overflow(bottom, 'int', fatal=False)
2016-09-22 17:34:30 +02:00
bottomright = QPoint(width, bottom)
else:
raise ValueError("Invalid position {}!".format(status_position))
rect = QRect(topleft, bottomright)
log.misc.debug('new geometry for {!r}: {}'.format(widget, rect))
if rect.isValid():
widget.setGeometry(rect)
def _init_downloadmanager(self):
log.init.debug("Initializing downloads...")
download_manager = downloads.DownloadManager(self.win_id, self)
objreg.register('download-manager', download_manager, scope='window',
window=self.win_id)
download_model = downloads.DownloadModel(download_manager)
objreg.register('download-model', download_model, scope='window',
window=self.win_id)
def _init_completion(self):
self._completion = completionwidget.CompletionView(self.win_id, self)
cmd = objreg.get('status-command', scope='window', window=self.win_id)
completer_obj = completer.Completer(cmd, self.win_id, self._completion)
self._completion.selection_changed.connect(
completer_obj.on_selection_changed)
objreg.register('completion', self._completion, scope='window',
window=self.win_id)
2016-09-23 06:18:48 +02:00
self._add_overlay(self._completion, self._completion.update_geometry)
def _init_command_dispatcher(self):
dispatcher = commands.CommandDispatcher(self.win_id,
self.tabbed_browser)
objreg.register('command-dispatcher', dispatcher, scope='window',
window=self.win_id)
self.tabbed_browser.destroyed.connect(
functools.partial(objreg.delete, 'command-dispatcher',
scope='window', window=self.win_id))
def __repr__(self):
2014-09-26 15:48:24 +02:00
return utils.get_repr(self)
@pyqtSlot(str, str)
def on_config_changed(self, section, option):
"""Resize the completion if related config options changed."""
if section != 'ui':
return
if option == 'statusbar-padding':
self._update_overlay_geometry()
elif option == 'downloads-position':
self._add_widgets()
elif option == 'status-position':
self._add_widgets()
self._update_overlay_geometry()
def _add_widgets(self):
2015-04-09 12:49:32 +02:00
"""Add or readd all widgets to the VBox."""
self._vbox.removeWidget(self.tabbed_browser)
self._vbox.removeWidget(self._downloadview)
self._vbox.removeWidget(self.status)
downloads_position = config.get('ui', 'downloads-position')
status_position = config.get('ui', 'status-position')
widgets = [self.tabbed_browser]
if downloads_position == 'top':
widgets.insert(0, self._downloadview)
elif downloads_position == 'bottom':
widgets.append(self._downloadview)
else:
raise ValueError("Invalid position {}!".format(downloads_position))
if status_position == 'top':
widgets.insert(0, self.status)
elif status_position == 'bottom':
widgets.append(self.status)
else:
raise ValueError("Invalid position {}!".format(status_position))
for widget in widgets:
self._vbox.addWidget(widget)
def _load_state_geometry(self):
2014-09-28 22:13:14 +02:00
"""Load the geometry from the state file."""
state_config = objreg.get('state-config')
try:
data = state_config['geometry']['mainwindow']
geom = base64.b64decode(data, validate=True)
except KeyError:
# First start
self._set_default_geometry()
except binascii.Error:
log.init.exception("Error while reading geometry")
self._set_default_geometry()
else:
self._load_geometry(geom)
2015-04-30 07:37:25 +02:00
def _save_geometry(self):
"""Save the window geometry to the state config."""
state_config = objreg.get('state-config')
data = bytes(self.saveGeometry())
geom = base64.b64encode(data).decode('ASCII')
state_config['geometry']['mainwindow'] = geom
def _load_geometry(self, geom):
"""Load geometry from a bytes object.
If loading fails, loads default geometry.
"""
log.init.debug("Loading mainwindow from {!r}".format(geom))
ok = self.restoreGeometry(geom)
if not ok:
log.init.warning("Error while loading geometry.")
self._set_default_geometry()
2014-09-28 22:13:14 +02:00
def _connect_overlay_signals(self):
2016-09-14 20:52:32 +02:00
"""Connect the resize signal and resize everything once."""
2016-10-26 21:25:47 +02:00
for widget, signal, centered, padding in self._overlays:
signal.connect(
2016-09-23 06:18:48 +02:00
functools.partial(self._update_overlay_geometry, widget,
2016-10-26 21:25:47 +02:00
centered, padding))
2016-09-23 06:18:48 +02:00
self._update_overlay_geometry(widget, centered)
2014-04-17 17:44:27 +02:00
def _set_default_geometry(self):
"""Set some sensible default geometry."""
self.setGeometry(QRect(50, 50, 800, 600))
2014-09-28 22:13:14 +02:00
def _get_object(self, name):
"""Get an object for this window in the object registry."""
return objreg.get(name, scope='window', window=self.win_id)
def _connect_signals(self):
"""Connect all mainwindow signals."""
key_config = objreg.get('key-config')
status = self._get_object('statusbar')
keyparsers = self._get_object('keyparsers')
2014-09-28 22:23:37 +02:00
completion_obj = self._get_object('completion')
2014-09-28 22:13:14 +02:00
tabs = self._get_object('tabbed-browser')
cmd = self._get_object('status-command')
message_bridge = self._get_object('message-bridge')
mode_manager = self._get_object('mode-manager')
# misc
self.tabbed_browser.close_window.connect(self.close)
2014-09-28 22:13:14 +02:00
mode_manager.entered.connect(hints.on_mode_entered)
# status bar
mode_manager.entered.connect(status.on_mode_entered)
mode_manager.left.connect(status.on_mode_left)
mode_manager.left.connect(cmd.on_mode_left)
mode_manager.left.connect(prompt.prompt_queue.on_mode_left)
2014-09-28 22:13:14 +02:00
# commands
keyparsers[usertypes.KeyMode.normal].keystring_updated.connect(
status.keystring.setText)
cmd.got_cmd.connect(self._commandrunner.run_safely)
cmd.returnPressed.connect(tabs.on_cmd_return_pressed)
2014-09-28 22:13:14 +02:00
# key hint popup
for mode, parser in keyparsers.items():
parser.keystring_updated.connect(functools.partial(
self._keyhint.update_keyhint, mode.name))
2014-09-28 22:13:14 +02:00
# config
for obj in keyparsers.values():
key_config.changed.connect(obj.on_keyconfig_changed)
# messages
2016-09-14 20:52:32 +02:00
message.global_bridge.show_message.connect(
self._messageview.show_message)
2014-09-28 22:13:14 +02:00
message_bridge.s_set_text.connect(status.set_text)
message_bridge.s_maybe_reset_text.connect(status.txt.maybe_reset_text)
# statusbar
tabs.current_tab_changed.connect(status.prog.on_tab_changed)
tabs.cur_progress.connect(status.prog.setValue)
tabs.cur_load_finished.connect(status.prog.hide)
tabs.cur_load_started.connect(status.prog.on_load_started)
tabs.current_tab_changed.connect(status.percentage.on_tab_changed)
tabs.cur_scroll_perc_changed.connect(status.percentage.set_perc)
tabs.tab_index_changed.connect(status.tabindex.on_tab_index_changed)
2014-09-28 22:13:14 +02:00
tabs.current_tab_changed.connect(status.url.on_tab_changed)
tabs.cur_url_changed.connect(status.url.set_url)
2014-09-28 22:13:14 +02:00
tabs.cur_link_hovered.connect(status.url.set_hover_url)
tabs.cur_load_status_changed.connect(status.url.on_load_status_changed)
# command input / completion
mode_manager.left.connect(tabs.on_mode_left)
cmd.clear_completion_selection.connect(
2014-09-28 22:23:37 +02:00
completion_obj.on_clear_completion_selection)
cmd.hide_completion.connect(completion_obj.hide)
2014-09-28 22:13:14 +02:00
@cmdutils.register(instance='main-window', scope='window')
@pyqtSlot()
def close(self):
2014-09-28 22:13:14 +02:00
"""Close the current window.
//
Extend close() so we can register it as a command.
"""
super().close()
2014-12-22 22:56:52 +01:00
@cmdutils.register(instance='main-window', scope='window')
def fullscreen(self):
"""Toggle fullscreen mode."""
if self.isFullScreen():
self.showNormal()
else:
self.showFullScreen()
def resizeEvent(self, e):
"""Extend resizewindow's resizeEvent to adjust completion.
Args:
e: The QResizeEvent
"""
super().resizeEvent(e)
self._update_overlay_geometry()
self._downloadview.updateGeometry()
self.tabbed_browser.tabBar().refresh()
def showEvent(self, e):
"""Extend showEvent to register us as the last-visible-main-window.
Args:
e: The QShowEvent
"""
super().showEvent(e)
objreg.register('last-visible-main-window', self, update=True)
def _do_close(self):
"""Helper function for closeEvent."""
try:
last_visible = objreg.get('last-visible-main-window')
if self is last_visible:
objreg.delete('last-visible-main-window')
except KeyError:
2016-09-11 16:03:03 +02:00
pass
objreg.get('session-manager').save_last_window_session()
self._save_geometry()
log.destroy.debug("Closing window {}".format(self.win_id))
self.tabbed_browser.shutdown()
def closeEvent(self, e):
"""Override closeEvent to display a confirmation if needed."""
if crashsignal.is_crashing:
e.accept()
return
confirm_quit = config.get('ui', 'confirm-quit')
tab_count = self.tabbed_browser.count()
download_model = objreg.get('download-model', scope='window',
window=self.win_id)
download_count = download_model.running_downloads()
2015-01-05 08:01:22 +01:00
quit_texts = []
2015-01-02 05:32:39 +01:00
# Ask if multiple-tabs are open
if 'multiple-tabs' in confirm_quit and tab_count > 1:
2015-01-05 08:01:22 +01:00
quit_texts.append("{} {} open.".format(
tab_count, "tab is" if tab_count == 1 else "tabs are"))
2015-01-02 05:32:39 +01:00
# Ask if multiple downloads running
if 'downloads' in confirm_quit and download_count > 0:
2015-01-05 08:01:22 +01:00
quit_texts.append("{} {} running.".format(
download_count,
"download is" if download_count == 1 else "downloads are"))
2015-01-02 05:32:39 +01:00
# Process all quit messages that user must confirm
2015-01-05 08:01:22 +01:00
if quit_texts or 'always' in confirm_quit:
text = '\n'.join(['Really quit?'] + quit_texts)
confirmed = message.ask(text, mode=usertypes.PromptMode.yesno,
2015-01-05 08:01:22 +01:00
default=True)
# Stop asking if the user cancels
if not confirmed:
2015-01-06 11:03:21 +01:00
log.destroy.debug("Cancelling closing of window {}".format(
2015-01-05 08:01:22 +01:00
self.win_id))
e.ignore()
return
2014-10-05 23:09:04 +02:00
e.accept()
self._do_close()