|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing an object to load web site icons. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject |
|
13 from PyQt5.QtGui import QIcon, QPixmap, QImage |
|
14 from PyQt5.QtNetwork import QNetworkRequest |
|
15 |
|
16 import WebBrowser.WebBrowserWindow |
|
17 |
|
18 |
|
19 class WebIconLoader(QObject): |
|
20 """ |
|
21 Class implementing a loader for web site icons. |
|
22 |
|
23 @signal iconLoaded(icon) emitted when the con has been loaded |
|
24 """ |
|
25 iconLoaded = pyqtSignal(QIcon) |
|
26 |
|
27 def __init__(self, url, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param url URL to fetch the icon from |
|
32 @type QUrl |
|
33 @param parent reference to the parent object |
|
34 @type QObject |
|
35 """ |
|
36 super(WebIconLoader, self).__init__(parent) |
|
37 |
|
38 networkManager = \ |
|
39 WebBrowser.WebBrowserWindow.WebBrowserWindow.networkManager() |
|
40 self.__reply = networkManager.get(QNetworkRequest(url)) |
|
41 self.__reply.finished.connect(self.__finished) |
|
42 |
|
43 @pyqtSlot() |
|
44 def __finished(self): |
|
45 """ |
|
46 Private slot handling the downloaded icon. |
|
47 """ |
|
48 # ignore any errors and emit an empty icon in this case |
|
49 data = self.__reply.readAll() |
|
50 icon = QIcon(QPixmap.fromImage(QImage.fromData(data))) |
|
51 self.iconLoaded.emit(icon) |
|
52 |
|
53 self.__reply.deleteLater() |
|
54 self.__reply = None |