Set window icon. Closes #325.

This commit is contained in:
Florian Bruhin 2014-12-28 14:35:28 +01:00
parent aef693805a
commit 2a4e884e1b
5 changed files with 34 additions and 3 deletions

View File

@ -32,7 +32,7 @@ import traceback
import faulthandler import faulthandler
from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox
from PyQt5.QtGui import QDesktopServices from PyQt5.QtGui import QDesktopServices, QPixmap, QIcon
from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl, from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl,
QStandardPaths, QObject, Qt) QStandardPaths, QObject, Qt)
@ -126,6 +126,7 @@ class Application(QApplication):
self.setOrganizationName("qutebrowser") self.setOrganizationName("qutebrowser")
self.setApplicationName("qutebrowser") self.setApplicationName("qutebrowser")
self.setApplicationVersion(qutebrowser.__version__) self.setApplicationVersion(qutebrowser.__version__)
self._init_icon()
utils.actute_warning() utils.actute_warning()
try: try:
self._init_modules() self._init_modules()
@ -197,6 +198,21 @@ class Application(QApplication):
main_window = objreg.get('main-window', scope='window', window=win_id) main_window = objreg.get('main-window', scope='window', window=win_id)
self.setActiveWindow(main_window) self.setActiveWindow(main_window)
def _init_icon(self):
"""Initialize the icon of qutebrowser."""
icon = QIcon()
for size in (16, 24, 32, 48, 64, 96, 128, 256, 512):
filename = 'icons/qutebrowser-{}x{}.png'.format(size, size)
data = utils.read_file(filename, binary=True, use_requirement=True)
pixmap = QPixmap()
ok = pixmap.loadFromData(data, 'PNG')
if not ok:
raise ValueError("Could not load icon!")
qtutils.ensure_not_null(pixmap)
icon.addPixmap(pixmap)
qtutils.ensure_not_null(icon)
self.setWindowIcon(icon)
def _handle_segfault(self): def _handle_segfault(self):
"""Handle a segfault from a previous run.""" """Handle a segfault from a previous run."""
path = standarddir.get(QStandardPaths.DataLocation) path = standarddir.get(QStandardPaths.DataLocation)

View File

@ -127,6 +127,12 @@ def ensure_valid(obj):
raise QtValueError(obj) raise QtValueError(obj)
def ensure_not_null(obj):
"""Ensure a Qt object with an .isNull() method is not null."""
if obj.isNull():
raise QtValueError(obj)
def _check_qdatastream(stream): def _check_qdatastream(stream):
"""Check the status of a QDataStream and raise OSError if it's not ok.""" """Check the status of a QDataStream and raise OSError if it's not ok."""
status_to_str = { status_to_str = {

View File

@ -64,13 +64,15 @@ def compact_text(text, elidelength=None):
return out return out
def read_file(filename, binary=False): def read_file(filename, binary=False, use_requirement=False):
"""Get the contents of a file contained with qutebrowser. """Get the contents of a file contained with qutebrowser.
Args: Args:
filename: The filename to open as string. filename: The filename to open as string.
binary: Whether to return a binary string. binary: Whether to return a binary string.
If False, the data is UTF-8-decoded. If False, the data is UTF-8-decoded.
use_requirement: Use a pkg_resources.Requirement object to get
non-package data.
Return: Return:
The file contents as string. The file contents as string.
@ -85,7 +87,11 @@ def read_file(filename, binary=False):
with open(fn, 'r', encoding='utf-8') as f: with open(fn, 'r', encoding='utf-8') as f:
return f.read() return f.read()
else: else:
data = pkg_resources.resource_string(qutebrowser.__name__, filename) if use_requirement:
target = pkg_resources.Requirement('qutebrowser', '', '')
else:
target = qutebrowser.__name__
data = pkg_resources.resource_string(target, filename)
if not binary: if not binary:
data = data.decode('UTF-8') data = data.decode('UTF-8')
return data return data

View File

@ -52,6 +52,7 @@ build_exe_options = {
('qutebrowser/html', 'html'), ('qutebrowser/html', 'html'),
('qutebrowser/html/doc', 'html/doc'), ('qutebrowser/html/doc', 'html/doc'),
('qutebrowser/git-commit-id', 'git-commit-id'), ('qutebrowser/git-commit-id', 'git-commit-id'),
('icons', 'icons'),
], ],
'include_msvcr': True, 'include_msvcr': True,
'excludes': ['tkinter'], 'excludes': ['tkinter'],

View File

@ -23,6 +23,7 @@
import os import os
import os.path import os.path
import glob
from scripts import setupcommon as common from scripts import setupcommon as common
@ -45,6 +46,7 @@ try:
test_suite='qutebrowser.test', test_suite='qutebrowser.test',
zip_safe=True, zip_safe=True,
install_requires=['pypeg2', 'jinja2', 'pygments'], install_requires=['pypeg2', 'jinja2', 'pygments'],
data_files=[('icons', glob.glob('icons/*'))],
**common.setupdata **common.setupdata
) )
finally: finally: