|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the label to show the web site icon. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt, QPoint, QMimeData |
|
11 from PyQt6.QtGui import QDrag, QPixmap |
|
12 from PyQt6.QtWidgets import QLabel, QApplication |
|
13 |
|
14 |
|
15 class FavIconLabel(QLabel): |
|
16 """ |
|
17 Class implementing the label to show the web site icon. |
|
18 """ |
|
19 def __init__(self, parent=None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param parent reference to the parent widget (QWidget) |
|
24 """ |
|
25 super().__init__(parent) |
|
26 |
|
27 self.__browser = None |
|
28 self.__dragStartPos = QPoint() |
|
29 |
|
30 self.setFocusPolicy(Qt.FocusPolicy.NoFocus) |
|
31 self.setCursor(Qt.CursorShape.ArrowCursor) |
|
32 self.setMinimumSize(16, 16) |
|
33 self.resize(16, 16) |
|
34 |
|
35 self.__browserIconChanged() |
|
36 |
|
37 def __browserIconChanged(self): |
|
38 """ |
|
39 Private slot to set the icon. |
|
40 """ |
|
41 if self.__browser: |
|
42 self.setPixmap( |
|
43 self.__browser.icon().pixmap(16, 16)) |
|
44 |
|
45 def __clearIcon(self): |
|
46 """ |
|
47 Private slot to clear the icon. |
|
48 """ |
|
49 self.setPixmap(QPixmap()) |
|
50 |
|
51 def setBrowser(self, browser): |
|
52 """ |
|
53 Public method to set the browser connection. |
|
54 |
|
55 @param browser reference to the browser widegt (HelpBrowser) |
|
56 """ |
|
57 self.__browser = browser |
|
58 self.__browser.loadFinished.connect(self.__browserIconChanged) |
|
59 self.__browser.faviconChanged.connect(self.__browserIconChanged) |
|
60 self.__browser.loadStarted.connect(self.__clearIcon) |
|
61 |
|
62 def mousePressEvent(self, evt): |
|
63 """ |
|
64 Protected method to handle mouse press events. |
|
65 |
|
66 @param evt reference to the mouse event (QMouseEvent) |
|
67 """ |
|
68 if evt.button() == Qt.MouseButton.LeftButton: |
|
69 self.__dragStartPos = evt.position().toPoint() |
|
70 super().mousePressEvent(evt) |
|
71 |
|
72 def mouseReleaseEvent(self, evt): |
|
73 """ |
|
74 Protected method to handle mouse release events. |
|
75 |
|
76 @param evt reference to the mouse event (QMouseEvent) |
|
77 """ |
|
78 if evt.button() == Qt.MouseButton.LeftButton: |
|
79 self.__showPopup(evt.globalPosition().toPoint()) |
|
80 super().mouseReleaseEvent(evt) |
|
81 |
|
82 def mouseMoveEvent(self, evt): |
|
83 """ |
|
84 Protected method to handle mouse move events. |
|
85 |
|
86 @param evt reference to the mouse event (QMouseEvent) |
|
87 """ |
|
88 if ( |
|
89 evt.button() == Qt.MouseButton.LeftButton and ( |
|
90 (evt.position().toPoint() - |
|
91 self.__dragStartPos).manhattanLength() > |
|
92 QApplication.startDragDistance() |
|
93 ) and self.__browser is not None |
|
94 ): |
|
95 drag = QDrag(self) |
|
96 mimeData = QMimeData() |
|
97 title = self.__browser.title() |
|
98 if title == "": |
|
99 title = str(self.__browser.url().toEncoded(), encoding="utf-8") |
|
100 mimeData.setText(title) |
|
101 mimeData.setUrls([self.__browser.url()]) |
|
102 p = self.pixmap() |
|
103 if p: |
|
104 drag.setPixmap(p) |
|
105 drag.setMimeData(mimeData) |
|
106 drag.exec() |
|
107 |
|
108 def __showPopup(self, pos): |
|
109 """ |
|
110 Private method to show the site info popup. |
|
111 |
|
112 @param pos position the popup should be shown at |
|
113 @type QPoint |
|
114 """ |
|
115 if self.__browser is None: |
|
116 return |
|
117 |
|
118 url = self.__browser.url() |
|
119 if url.isValid() and url.scheme() not in [ |
|
120 "eric", "about", "data", "chrome"]: |
|
121 from ..SiteInfo.SiteInfoWidget import SiteInfoWidget |
|
122 info = SiteInfoWidget(self.__browser, self) |
|
123 info.showAt(pos) |