|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the label to show the web site icon. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode |
|
13 except NameError: |
|
14 pass |
|
15 |
|
16 from PyQt5.QtCore import Qt, QPoint, QMimeData |
|
17 from PyQt5.QtGui import QDrag, QPixmap |
|
18 from PyQt5.QtWidgets import QLabel, QApplication |
|
19 |
|
20 |
|
21 class FavIconLabel(QLabel): |
|
22 """ |
|
23 Class implementing the label to show the web site icon. |
|
24 """ |
|
25 def __init__(self, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent widget (QWidget) |
|
30 """ |
|
31 super(FavIconLabel, self).__init__(parent) |
|
32 |
|
33 self.__browser = None |
|
34 self.__dragStartPos = QPoint() |
|
35 |
|
36 self.setFocusPolicy(Qt.NoFocus) |
|
37 self.setCursor(Qt.ArrowCursor) |
|
38 self.setMinimumSize(16, 16) |
|
39 self.resize(16, 16) |
|
40 |
|
41 self.__browserIconChanged() |
|
42 |
|
43 def __browserIconChanged(self): |
|
44 """ |
|
45 Private slot to set the icon. |
|
46 """ |
|
47 if self.__browser: |
|
48 self.setPixmap( |
|
49 self.__browser.icon().pixmap(16, 16)) |
|
50 |
|
51 def __clearIcon(self): |
|
52 """ |
|
53 Private slot to clear the icon. |
|
54 """ |
|
55 self.setPixmap(QPixmap()) |
|
56 |
|
57 def setBrowser(self, browser): |
|
58 """ |
|
59 Public method to set the browser connection. |
|
60 |
|
61 @param browser reference to the browser widegt (HelpBrowser) |
|
62 """ |
|
63 self.__browser = browser |
|
64 self.__browser.loadFinished.connect(self.__browserIconChanged) |
|
65 self.__browser.iconChanged.connect(self.__browserIconChanged) |
|
66 self.__browser.loadStarted.connect(self.__clearIcon) |
|
67 |
|
68 def mousePressEvent(self, evt): |
|
69 """ |
|
70 Protected method to handle mouse press events. |
|
71 |
|
72 @param evt reference to the mouse event (QMouseEvent) |
|
73 """ |
|
74 if evt.button() == Qt.LeftButton: |
|
75 self.__dragStartPos = evt.pos() |
|
76 super(FavIconLabel, self).mousePressEvent(evt) |
|
77 |
|
78 def mouseMoveEvent(self, evt): |
|
79 """ |
|
80 Protected method to handle mouse move events. |
|
81 |
|
82 @param evt reference to the mouse event (QMouseEvent) |
|
83 """ |
|
84 if evt.button() == Qt.LeftButton and \ |
|
85 (evt.pos() - self.__dragStartPos).manhattanLength() > \ |
|
86 QApplication.startDragDistance() and \ |
|
87 self.__browser is not None: |
|
88 drag = QDrag(self) |
|
89 mimeData = QMimeData() |
|
90 title = self.__browser.title() |
|
91 if title == "": |
|
92 title = str(self.__browser.url().toEncoded(), encoding="utf-8") |
|
93 mimeData.setText(title) |
|
94 mimeData.setUrls([self.__browser.url()]) |
|
95 p = self.pixmap() |
|
96 if p: |
|
97 drag.setPixmap(p) |
|
98 drag.setMimeData(mimeData) |
|
99 drag.exec_() |