|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to manage the Favicons. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, Qt, QPoint |
|
11 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QMenu |
|
12 |
|
13 from .Ui_WebIconDialog import Ui_WebIconDialog |
|
14 |
|
15 |
|
16 class WebIconDialog(QDialog, Ui_WebIconDialog): |
|
17 """ |
|
18 Class implementing a dialog to manage the Favicons. |
|
19 """ |
|
20 def __init__(self, iconsDB, parent=None): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param iconsDB icons database |
|
25 @type dict |
|
26 @param parent reference to the parent widget |
|
27 @type QWidget |
|
28 """ |
|
29 super(WebIconDialog, self).__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 for url, icon in iconsDB.items(): |
|
33 itm = QTreeWidgetItem(self.iconsList, [url]) |
|
34 itm.setIcon(0, icon) |
|
35 self.iconsList.sortItems(0, Qt.AscendingOrder) |
|
36 |
|
37 self.__setRemoveButtons() |
|
38 |
|
39 def __setRemoveButtons(self): |
|
40 """ |
|
41 Private method to set the state of the 'remove' buttons. |
|
42 """ |
|
43 self.removeAllButton.setEnabled(self.iconsList.topLevelItemCount() > 0) |
|
44 self.removeButton.setEnabled(len(self.iconsList.selectedItems()) > 0) |
|
45 |
|
46 @pyqtSlot(QPoint) |
|
47 def on_iconsList_customContextMenuRequested(self, pos): |
|
48 """ |
|
49 Private slot to show the context menu. |
|
50 |
|
51 @param pos cursor position |
|
52 @type QPoint |
|
53 """ |
|
54 menu = QMenu() |
|
55 menu.addAction( |
|
56 self.tr("Remove Selected"), |
|
57 self.on_removeButton_clicked).setEnabled( |
|
58 len(self.iconsList.selectedItems()) > 0) |
|
59 menu.addAction( |
|
60 self.tr("Remove All"), |
|
61 self.on_removeAllButton_clicked).setEnabled( |
|
62 self.iconsList.topLevelItemCount() > 0) |
|
63 |
|
64 menu.exec_(self.iconsList.mapToGlobal(pos)) |
|
65 |
|
66 @pyqtSlot() |
|
67 def on_iconsList_itemSelectionChanged(self): |
|
68 """ |
|
69 Private slot handling the selection of entries. |
|
70 """ |
|
71 self.__setRemoveButtons() |
|
72 |
|
73 @pyqtSlot() |
|
74 def on_removeButton_clicked(self): |
|
75 """ |
|
76 Private slot to remove the selected items. |
|
77 """ |
|
78 for itm in self.iconsList.selectedItems(): |
|
79 index = self.iconsList.indexOfTopLevelItem(itm) |
|
80 self.iconsList.takeTopLevelItem(index) |
|
81 del itm |
|
82 |
|
83 @pyqtSlot() |
|
84 def on_removeAllButton_clicked(self): |
|
85 """ |
|
86 Private slot to remove all entries. |
|
87 """ |
|
88 self.iconsList.clear() |
|
89 |
|
90 def getUrls(self): |
|
91 """ |
|
92 Public method to get the list of URLs. |
|
93 |
|
94 @return list of URLs |
|
95 @rtype list of str |
|
96 """ |
|
97 urls = [] |
|
98 for index in range(self.iconsList.topLevelItemCount()): |
|
99 urls.append(self.iconsList.topLevelItem(index).text(0)) |
|
100 |
|
101 return urls |