|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2019 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.faviconChanged.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 mouseReleaseEvent(self, evt): |
|
79 """ |
|
80 Protected method to handle mouse release events. |
|
81 |
|
82 @param evt reference to the mouse event (QMouseEvent) |
|
83 """ |
|
84 if evt.button() == Qt.LeftButton: |
|
85 self.__showPopup(evt.globalPos()) |
|
86 super(FavIconLabel, self).mouseReleaseEvent(evt) |
|
87 |
|
88 def mouseMoveEvent(self, evt): |
|
89 """ |
|
90 Protected method to handle mouse move events. |
|
91 |
|
92 @param evt reference to the mouse event (QMouseEvent) |
|
93 """ |
|
94 if evt.button() == Qt.LeftButton and \ |
|
95 (evt.pos() - self.__dragStartPos).manhattanLength() > \ |
|
96 QApplication.startDragDistance() and \ |
|
97 self.__browser is not None: |
|
98 drag = QDrag(self) |
|
99 mimeData = QMimeData() |
|
100 title = self.__browser.title() |
|
101 if title == "": |
|
102 title = str(self.__browser.url().toEncoded(), encoding="utf-8") |
|
103 mimeData.setText(title) |
|
104 mimeData.setUrls([self.__browser.url()]) |
|
105 p = self.pixmap() |
|
106 if p: |
|
107 drag.setPixmap(p) |
|
108 drag.setMimeData(mimeData) |
|
109 drag.exec_() |
|
110 |
|
111 def __showPopup(self, pos): |
|
112 """ |
|
113 Private method to show the site info popup. |
|
114 |
|
115 @param pos position the popup should be shown at |
|
116 @type QPoint |
|
117 """ |
|
118 if self.__browser is None: |
|
119 return |
|
120 |
|
121 url = self.__browser.url() |
|
122 if url.isValid() and url.scheme() not in [ |
|
123 "eric", "about", "data", "chrome"]: |
|
124 from ..SiteInfo.SiteInfoWidget import SiteInfoWidget |
|
125 info = SiteInfoWidget(self.__browser, self) |
|
126 info.showAt(pos) |