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