1# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2
3# Copyright 2015-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
4#
5# This file is part of qutebrowser.
6#
7# qutebrowser is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# qutebrowser is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.
19
20"""Convenience functions to show message boxes."""
21
22from PyQt5.QtCore import Qt
23from PyQt5.QtWidgets import QMessageBox
24
25from qutebrowser.misc import objects
26from qutebrowser.utils import log
27
28
29class DummyBox:
30
31    """A dummy QMessageBox returned when --no-err-windows is used."""
32
33    def exec(self):
34        pass
35
36
37def msgbox(parent, title, text, *, icon, buttons=QMessageBox.Ok,
38           on_finished=None, plain_text=None):
39    """Display a QMessageBox with the given icon.
40
41    Args:
42        parent: The parent to set for the message box.
43        title: The title to set.
44        text: The text to set.
45        buttons: The buttons to set (QMessageBox::StandardButtons)
46        on_finished: A slot to connect to the 'finished' signal.
47        plain_text: Whether to force plain text (True) or rich text (False).
48                    None (the default) uses Qt's auto detection.
49
50    Return:
51        A new QMessageBox.
52    """
53    if objects.args.no_err_windows:
54        log.misc.info(f'{title}\n\n{text}')
55        return DummyBox()
56
57    box = QMessageBox(parent)
58    box.setAttribute(Qt.WA_DeleteOnClose)
59    box.setIcon(icon)
60    box.setStandardButtons(buttons)
61    if on_finished is not None:
62        box.finished.connect(on_finished)
63    if plain_text:
64        box.setTextFormat(Qt.PlainText)
65    elif plain_text is not None:
66        box.setTextFormat(Qt.RichText)
67    box.setWindowTitle(title)
68    box.setText(text)
69    box.show()
70    return box
71
72
73def information(*args, **kwargs):
74    """Display an information box.
75
76    Args:
77        *args: Passed to msgbox.
78        **kwargs: Passed to msgbox.
79
80    Return:
81        A new QMessageBox.
82    """
83    return msgbox(*args, icon=QMessageBox.Information, **kwargs)
84