eric6/Helpviewer/UrlBar/FavIconLabel.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
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, QUrl, 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 import Helpviewer.HelpWindow
48 try:
49 url = QUrl()
50 if self.__browser:
51 url = self.__browser.url()
52 self.setPixmap(
53 Helpviewer.HelpWindow.HelpWindow.icon(url).pixmap(16, 16))
54 except RuntimeError:
55 pass
56
57 def __clearIcon(self):
58 """
59 Private slot to clear the icon.
60 """
61 self.setPixmap(QPixmap())
62
63 def setBrowser(self, browser):
64 """
65 Public method to set the browser connection.
66
67 @param browser reference to the browser widegt (HelpBrowser)
68 """
69 self.__browser = browser
70 self.__browser.loadFinished.connect(self.__browserIconChanged)
71 self.__browser.iconChanged.connect(self.__browserIconChanged)
72 self.__browser.loadStarted.connect(self.__clearIcon)
73
74 def mousePressEvent(self, evt):
75 """
76 Protected method to handle mouse press events.
77
78 @param evt reference to the mouse event (QMouseEvent)
79 """
80 if evt.button() == Qt.LeftButton:
81 self.__dragStartPos = evt.pos()
82 super(FavIconLabel, self).mousePressEvent(evt)
83
84 def mouseMoveEvent(self, evt):
85 """
86 Protected method to handle mouse move events.
87
88 @param evt reference to the mouse event (QMouseEvent)
89 """
90 if evt.button() == Qt.LeftButton and \
91 (evt.pos() - self.__dragStartPos).manhattanLength() > \
92 QApplication.startDragDistance() and \
93 self.__browser is not None:
94 drag = QDrag(self)
95 mimeData = QMimeData()
96 title = self.__browser.title()
97 if title == "":
98 title = str(self.__browser.url().toEncoded(), encoding="utf-8")
99 mimeData.setText(title)
100 mimeData.setUrls([self.__browser.url()])
101 p = self.pixmap()
102 if p:
103 drag.setPixmap(p)
104 drag.setMimeData(mimeData)
105 drag.exec_()

eric ide

mercurial