|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the label to show the web site icon. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt, QPoint, QUrl, QMimeData |
|
11 from PyQt4.QtGui import QLabel, QApplication, QDrag |
|
12 |
|
13 import Helpviewer.HelpWindow |
|
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 QLabel.__init__(self, parent) |
|
26 |
|
27 self.__browser = None |
|
28 self.__dragStartPos = QPoint() |
|
29 |
|
30 self.setFocusPolicy(Qt.NoFocus) |
|
31 self.setCursor(Qt.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 url = QUrl() |
|
42 if self.__browser: |
|
43 url = self.__browser.url() |
|
44 self.setPixmap(Helpviewer.HelpWindow.HelpWindow.icon(url).pixmap(16, 16)) |
|
45 |
|
46 def setBrowser(self, browser): |
|
47 """ |
|
48 Public method to set the browser connection. |
|
49 |
|
50 @param browser reference to the browser widegt (HelpBrowser) |
|
51 """ |
|
52 self.__browser = browser |
|
53 self.__browser.loadFinished.connect(self.__browserIconChanged) |
|
54 self.__browser.iconChanged.connect(self.__browserIconChanged) |
|
55 |
|
56 def mousePressEvent(self, evt): |
|
57 """ |
|
58 Protected method to handle mouse press events. |
|
59 |
|
60 @param evt reference to the mouse event (QMouseEvent) |
|
61 """ |
|
62 if evt.button() == Qt.LeftButton: |
|
63 self.__dragStartPos = evt.pos() |
|
64 QLabel.mousePressEvent(self, evt) |
|
65 |
|
66 def mouseMoveEvent(self, evt): |
|
67 """ |
|
68 Protected method to handle mouse move events. |
|
69 |
|
70 @param evt reference to the mouse event (QMouseEvent) |
|
71 """ |
|
72 if evt.button() == Qt.LeftButton and \ |
|
73 (evt.pos() - self.__dragStartPos).manhattanLength() > \ |
|
74 QApplication.startDragDistance() and \ |
|
75 self.__browser is not None: |
|
76 drag = QDrag(self) |
|
77 mimeData = QMimeData() |
|
78 title = self.__browser.title() |
|
79 if title == "": |
|
80 title = str(self.__browser.url().toEncoded(), encoding = "utf-8") |
|
81 mimeData.setText(title) |
|
82 mimeData.setUrls([self.__browser.url()]) |
|
83 p = self.pixmap() |
|
84 if p: |
|
85 drag.setPixmap(p) |
|
86 drag.setMimeData(mimeData) |
|
87 drag.exec_() |