2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2014-02-06 14:01:23 +01: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/>.
|
|
|
|
|
2014-06-04 14:52:12 +02:00
|
|
|
"""The main window of qutebrowser."""
|
2014-02-17 12:23:52 +01:00
|
|
|
|
2014-02-18 14:06:45 +01:00
|
|
|
import binascii
|
2014-08-26 19:10:14 +02:00
|
|
|
import base64
|
2014-02-18 14:06:45 +01:00
|
|
|
|
2014-09-23 19:22:55 +02:00
|
|
|
from PyQt5.QtCore import pyqtSlot, QRect, QPoint, QTimer
|
2014-05-26 15:13:39 +02:00
|
|
|
from PyQt5.QtWidgets import QWidget, QVBoxLayout
|
2014-01-21 17:54:21 +01:00
|
|
|
|
2014-08-26 20:48:39 +02:00
|
|
|
from qutebrowser.commands import cmdutils
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.config import config
|
2014-09-24 07:06:45 +02:00
|
|
|
from qutebrowser.utils import message, log, usertypes, qtutils, objreg
|
2014-08-26 19:10:14 +02:00
|
|
|
from qutebrowser.widgets import tabbedbrowser, completion, downloads
|
|
|
|
from qutebrowser.widgets.statusbar import bar
|
2014-01-28 23:04:02 +01:00
|
|
|
|
2014-02-18 14:21:39 +01: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
|
|
|
|
|
|
|
Adds all needed components to a vbox, initializes subwidgets and connects
|
|
|
|
signals.
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-02-18 16:38:13 +01:00
|
|
|
Attributes:
|
|
|
|
status: The StatusBar widget.
|
2014-09-24 22:17:53 +02:00
|
|
|
_downloadview: The DownloadView widget.
|
2014-09-23 23:05:55 +02:00
|
|
|
_tabbed_browser: The TabbedBrowser widget.
|
2014-02-18 16:38:13 +01:00
|
|
|
_vbox: The main QVBoxLayout.
|
|
|
|
"""
|
2014-01-20 15:58:49 +01:00
|
|
|
|
2014-06-17 07:17:21 +02:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
2014-01-21 09:05:31 +01:00
|
|
|
|
|
|
|
self.setWindowTitle('qutebrowser')
|
2014-09-24 07:06:45 +02:00
|
|
|
state_config = objreg.get('state-config')
|
2014-02-18 11:57:35 +01:00
|
|
|
try:
|
2014-09-23 23:05:55 +02:00
|
|
|
data = state_config['geometry']['mainwindow']
|
2014-08-26 19:10:14 +02:00
|
|
|
log.init.debug("Restoring mainwindow from {}".format(data))
|
|
|
|
geom = base64.b64decode(data, validate=True)
|
2014-09-16 08:20:19 +02:00
|
|
|
except KeyError:
|
|
|
|
# First start
|
2014-09-25 22:24:40 +02:00
|
|
|
self._set_default_geometry()
|
2014-09-16 08:20:19 +02:00
|
|
|
except binascii.Error:
|
|
|
|
log.init.exception("Error while reading geometry")
|
2014-02-18 14:06:45 +01:00
|
|
|
self._set_default_geometry()
|
2014-02-18 11:57:35 +01:00
|
|
|
else:
|
2014-02-18 14:06:45 +01:00
|
|
|
try:
|
|
|
|
ok = self.restoreGeometry(geom)
|
|
|
|
except KeyError:
|
2014-09-16 08:20:19 +02:00
|
|
|
log.init.exception("Error while restoring geometry.")
|
2014-02-18 14:06:45 +01:00
|
|
|
self._set_default_geometry()
|
|
|
|
if not ok:
|
2014-06-22 23:52:50 +02:00
|
|
|
log.init.warning("Error while restoring geometry.")
|
2014-02-18 14:06:45 +01:00
|
|
|
self._set_default_geometry()
|
2014-01-21 09:05:31 +01:00
|
|
|
|
2014-06-23 06:20:37 +02:00
|
|
|
log.init.debug("Initial mainwindow geometry: {}".format(
|
|
|
|
self.geometry()))
|
2014-02-18 16:38:13 +01:00
|
|
|
self._vbox = QVBoxLayout(self)
|
|
|
|
self._vbox.setContentsMargins(0, 0, 0, 0)
|
|
|
|
self._vbox.setSpacing(0)
|
2013-12-15 20:33:43 +01:00
|
|
|
|
2014-09-24 22:17:53 +02:00
|
|
|
self._downloadview = downloads.DownloadView()
|
|
|
|
self._vbox.addWidget(self._downloadview)
|
|
|
|
self._downloadview.show()
|
2014-06-12 08:02:44 +02:00
|
|
|
|
2014-09-23 23:05:55 +02:00
|
|
|
self._tabbed_browser = tabbedbrowser.TabbedBrowser()
|
|
|
|
self._tabbed_browser.title_changed.connect(self.setWindowTitle)
|
2014-09-24 07:06:45 +02:00
|
|
|
objreg.register('tabbed-browser', self._tabbed_browser)
|
2014-09-23 23:05:55 +02:00
|
|
|
self._vbox.addWidget(self._tabbed_browser)
|
2014-06-12 10:19:45 +02:00
|
|
|
|
2014-09-23 21:49:22 +02:00
|
|
|
self._completion = completion.CompletionView(self)
|
2014-01-27 21:35:12 +01:00
|
|
|
|
2014-08-26 19:10:14 +02:00
|
|
|
self.status = bar.StatusBar()
|
2014-02-18 16:38:13 +01:00
|
|
|
self._vbox.addWidget(self.status)
|
2013-12-15 20:33:43 +01:00
|
|
|
|
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.
|
2014-09-23 23:17:36 +02:00
|
|
|
QTimer.singleShot(0, self._connect_resize_completion)
|
2013-12-15 20:33:43 +01:00
|
|
|
#self.retranslateUi(MainWindow)
|
|
|
|
#self.tabWidget.setCurrentIndex(0)
|
|
|
|
#QtCore.QMetaObject.connectSlotsByName(MainWindow)
|
2014-02-18 14:06:45 +01:00
|
|
|
|
2014-06-17 06:37:56 +02:00
|
|
|
def __repr__(self):
|
2014-06-17 11:03:42 +02:00
|
|
|
return '<{}>'.format(self.__class__.__name__)
|
2014-06-17 06:37:56 +02:00
|
|
|
|
2014-09-23 23:17:36 +02:00
|
|
|
def _connect_resize_completion(self):
|
|
|
|
"""Connect the resize_completion signal and resize it once."""
|
|
|
|
self._completion.resize_completion.connect(self.resize_completion)
|
|
|
|
self.resize_completion()
|
|
|
|
|
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-04-17 11:39:25 +02:00
|
|
|
@pyqtSlot(str, str)
|
|
|
|
def on_config_changed(self, section, option):
|
2014-04-10 23:30:45 +02:00
|
|
|
"""Resize completion if config changed."""
|
2014-06-06 17:12:54 +02:00
|
|
|
if section == 'completion' and option in ('height', 'shrink'):
|
2014-04-10 23:30:45 +02:00
|
|
|
self.resize_completion()
|
|
|
|
|
2014-06-03 11:39:53 +02:00
|
|
|
@pyqtSlot()
|
2014-04-10 18:01:16 +02:00
|
|
|
def resize_completion(self):
|
|
|
|
"""Adjust completion according to config."""
|
2014-06-03 11:39:53 +02:00
|
|
|
# Get the configured height/percentage.
|
2014-04-27 21:21:14 +02:00
|
|
|
confheight = str(config.get('completion', 'height'))
|
2014-02-26 21:18:53 +01:00
|
|
|
if confheight.endswith('%'):
|
|
|
|
perc = int(confheight.rstrip('%'))
|
|
|
|
height = self.height() * perc / 100
|
|
|
|
else:
|
|
|
|
height = int(confheight)
|
2014-06-03 11:39:53 +02:00
|
|
|
# Shrink to content size if needed and shrinking is enabled
|
|
|
|
if config.get('completion', 'shrink'):
|
|
|
|
contents_height = (
|
2014-09-23 21:49:22 +02:00
|
|
|
self._completion.viewportSizeHint().height() +
|
|
|
|
self._completion.horizontalScrollBar().sizeHint().height())
|
2014-06-03 11:39:53 +02:00
|
|
|
if contents_height <= height:
|
|
|
|
height = contents_height
|
2014-06-22 23:52:50 +02:00
|
|
|
else:
|
|
|
|
contents_height = -1
|
2014-02-26 21:18:53 +01:00
|
|
|
# hpoint now would be the bottom-left edge of the widget if it was on
|
|
|
|
# the top of the main window.
|
2014-05-14 23:29:18 +02:00
|
|
|
topleft_y = self.height() - self.status.height() - height
|
2014-08-26 19:10:14 +02:00
|
|
|
topleft_y = qtutils.check_overflow(topleft_y, 'int', fatal=False)
|
2014-05-14 23:29:18 +02:00
|
|
|
topleft = QPoint(0, topleft_y)
|
2014-02-26 21:18:53 +01:00
|
|
|
bottomright = self.status.geometry().topRight()
|
2014-06-21 16:42:58 +02:00
|
|
|
rect = QRect(topleft, bottomright)
|
2014-08-07 14:42:22 +02:00
|
|
|
if rect.isValid():
|
2014-09-23 21:49:22 +02:00
|
|
|
self._completion.setGeometry(rect)
|
2014-04-10 18:01:16 +02:00
|
|
|
|
2014-09-23 23:05:55 +02:00
|
|
|
@cmdutils.register(instance='main-window', name=['quit', 'q'])
|
2014-05-26 16:51:57 +02:00
|
|
|
def close(self):
|
2014-07-16 20:09:41 +02:00
|
|
|
"""Quit qutebrowser.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
Extend close() so we can register it as a command.
|
|
|
|
"""
|
2014-05-26 16:51:57 +02:00
|
|
|
super().close()
|
|
|
|
|
2014-04-10 18:01:16 +02:00
|
|
|
def resizeEvent(self, e):
|
|
|
|
"""Extend resizewindow's resizeEvent to adjust completion.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
e: The QResizeEvent
|
|
|
|
"""
|
|
|
|
super().resizeEvent(e)
|
|
|
|
self.resize_completion()
|
2014-09-24 22:17:53 +02:00
|
|
|
self._downloadview.updateGeometry()
|
2014-09-23 23:05:55 +02:00
|
|
|
self._tabbed_browser.tabBar().refresh()
|
2014-05-26 16:51:57 +02:00
|
|
|
|
|
|
|
def closeEvent(self, e):
|
|
|
|
"""Override closeEvent to display a confirmation if needed."""
|
|
|
|
confirm_quit = config.get('ui', 'confirm-quit')
|
2014-09-23 23:05:55 +02:00
|
|
|
count = self._tabbed_browser.count()
|
2014-05-26 16:51:57 +02:00
|
|
|
if confirm_quit == 'never':
|
|
|
|
e.accept()
|
|
|
|
elif confirm_quit == 'multiple-tabs' and count <= 1:
|
|
|
|
e.accept()
|
|
|
|
else:
|
|
|
|
text = "Close {} {}?".format(
|
|
|
|
count, "tab" if count == 1 else "tabs")
|
2014-08-26 19:10:14 +02:00
|
|
|
confirmed = message.ask(text, usertypes.PromptMode.yesno,
|
|
|
|
default=True)
|
2014-05-26 16:51:57 +02:00
|
|
|
if confirmed:
|
|
|
|
e.accept()
|
|
|
|
else:
|
|
|
|
e.ignore()
|