|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to add a bookmark or a bookmark folder. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import QModelIndex, QSortFilterProxyModel |
|
13 from PyQt5.QtWidgets import QDialog, QTreeView |
|
14 |
|
15 from .Ui_AddBookmarkDialog import Ui_AddBookmarkDialog |
|
16 |
|
17 |
|
18 class AddBookmarkProxyModel(QSortFilterProxyModel): |
|
19 """ |
|
20 Class implementing a proxy model used by the AddBookmarkDialog dialog. |
|
21 """ |
|
22 def __init__(self, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent object (QObject) |
|
27 """ |
|
28 super(AddBookmarkProxyModel, self).__init__(parent) |
|
29 |
|
30 def columnCount(self, parent): |
|
31 """ |
|
32 Public method to return the number of columns. |
|
33 |
|
34 @param parent index of the parent (QModelIndex) |
|
35 @return number of columns (integer) |
|
36 """ |
|
37 return min(1, QSortFilterProxyModel.columnCount(self, parent)) |
|
38 |
|
39 def filterAcceptsRow(self, sourceRow, sourceParent): |
|
40 """ |
|
41 Public method to determine, if the row is acceptable. |
|
42 |
|
43 @param sourceRow row number in the source model (integer) |
|
44 @param sourceParent index of the source item (QModelIndex) |
|
45 @return flag indicating acceptance (boolean) |
|
46 """ |
|
47 idx = self.sourceModel().index(sourceRow, 0, sourceParent) |
|
48 return self.sourceModel().hasChildren(idx) |
|
49 |
|
50 def filterAcceptsColumn(self, sourceColumn, sourceParent): |
|
51 """ |
|
52 Public method to determine, if the column is acceptable. |
|
53 |
|
54 @param sourceColumn column number in the source model (integer) |
|
55 @param sourceParent index of the source item (QModelIndex) |
|
56 @return flag indicating acceptance (boolean) |
|
57 """ |
|
58 return sourceColumn == 0 |
|
59 |
|
60 def hasChildren(self, parent=None): |
|
61 """ |
|
62 Public method to check, if a parent node has some children. |
|
63 |
|
64 @param parent index of the parent node (QModelIndex) |
|
65 @return flag indicating the presence of children (boolean) |
|
66 """ |
|
67 if parent is None: |
|
68 parent = QModelIndex() |
|
69 sindex = self.mapToSource(parent) |
|
70 return self.sourceModel().hasChildren(sindex) |
|
71 |
|
72 |
|
73 class AddBookmarkDialog(QDialog, Ui_AddBookmarkDialog): |
|
74 """ |
|
75 Class implementing a dialog to add a bookmark or a bookmark folder. |
|
76 """ |
|
77 def __init__(self, parent=None, bookmarksManager=None): |
|
78 """ |
|
79 Constructor |
|
80 |
|
81 @param parent reference to the parent widget (QWidget) |
|
82 @param bookmarksManager reference to the bookmarks manager |
|
83 object (BookmarksManager) |
|
84 """ |
|
85 super(AddBookmarkDialog, self).__init__(parent) |
|
86 self.setupUi(self) |
|
87 |
|
88 self.__bookmarksManager = bookmarksManager |
|
89 self.__addedNode = None |
|
90 self.__addFolder = False |
|
91 |
|
92 if self.__bookmarksManager is None: |
|
93 import WebBrowser.WebBrowserWindow |
|
94 self.__bookmarksManager = \ |
|
95 WebBrowser.WebBrowserWindow.WebBrowserWindow.bookmarksManager() |
|
96 |
|
97 self.__proxyModel = AddBookmarkProxyModel(self) |
|
98 model = self.__bookmarksManager.bookmarksModel() |
|
99 self.__proxyModel.setSourceModel(model) |
|
100 |
|
101 self.__treeView = QTreeView(self) |
|
102 self.__treeView.setModel(self.__proxyModel) |
|
103 self.__treeView.expandAll() |
|
104 self.__treeView.header().setStretchLastSection(True) |
|
105 self.__treeView.header().hide() |
|
106 self.__treeView.setItemsExpandable(False) |
|
107 self.__treeView.setRootIsDecorated(False) |
|
108 self.__treeView.setIndentation(10) |
|
109 self.__treeView.show() |
|
110 |
|
111 self.locationCombo.setModel(self.__proxyModel) |
|
112 self.locationCombo.setView(self.__treeView) |
|
113 |
|
114 self.addressEdit.setInactiveText(self.tr("Url")) |
|
115 self.nameEdit.setInactiveText(self.tr("Title")) |
|
116 |
|
117 self.resize(self.sizeHint()) |
|
118 |
|
119 def setUrl(self, url): |
|
120 """ |
|
121 Public slot to set the URL of the new bookmark. |
|
122 |
|
123 @param url URL of the bookmark (string) |
|
124 """ |
|
125 self.addressEdit.setText(url) |
|
126 self.resize(self.sizeHint()) |
|
127 |
|
128 def url(self): |
|
129 """ |
|
130 Public method to get the URL of the bookmark. |
|
131 |
|
132 @return URL of the bookmark (string) |
|
133 """ |
|
134 return self.addressEdit.text() |
|
135 |
|
136 def setTitle(self, title): |
|
137 """ |
|
138 Public method to set the title of the new bookmark. |
|
139 |
|
140 @param title title of the bookmark (string) |
|
141 """ |
|
142 self.nameEdit.setText(title) |
|
143 |
|
144 def title(self): |
|
145 """ |
|
146 Public method to get the title of the bookmark. |
|
147 |
|
148 @return title of the bookmark (string) |
|
149 """ |
|
150 return self.nameEdit.text() |
|
151 |
|
152 def setDescription(self, description): |
|
153 """ |
|
154 Public method to set the description of the new bookmark. |
|
155 |
|
156 @param description description of the bookamrk (string) |
|
157 """ |
|
158 self.descriptionEdit.setPlainText(description) |
|
159 |
|
160 def description(self): |
|
161 """ |
|
162 Public method to get the description of the bookmark. |
|
163 |
|
164 @return description of the bookamrk (string) |
|
165 """ |
|
166 return self.descriptionEdit.toPlainText() |
|
167 |
|
168 def setCurrentIndex(self, idx): |
|
169 """ |
|
170 Public method to set the current index. |
|
171 |
|
172 @param idx current index to be set (QModelIndex) |
|
173 """ |
|
174 proxyIndex = self.__proxyModel.mapFromSource(idx) |
|
175 self.__treeView.setCurrentIndex(proxyIndex) |
|
176 self.locationCombo.setCurrentIndex(proxyIndex.row()) |
|
177 |
|
178 def currentIndex(self): |
|
179 """ |
|
180 Public method to get the current index. |
|
181 |
|
182 @return current index (QModelIndex) |
|
183 """ |
|
184 idx = self.locationCombo.view().currentIndex() |
|
185 idx = self.__proxyModel.mapToSource(idx) |
|
186 return idx |
|
187 |
|
188 def setFolder(self, folder): |
|
189 """ |
|
190 Public method to set the dialog to "Add Folder" mode. |
|
191 |
|
192 @param folder flag indicating "Add Folder" mode (boolean) |
|
193 """ |
|
194 self.__addFolder = folder |
|
195 |
|
196 if folder: |
|
197 self.setWindowTitle(self.tr("Add Folder")) |
|
198 self.addressEdit.setVisible(False) |
|
199 else: |
|
200 self.setWindowTitle(self.tr("Add Bookmark")) |
|
201 self.addressEdit.setVisible(True) |
|
202 |
|
203 self.resize(self.sizeHint()) |
|
204 |
|
205 def isFolder(self): |
|
206 """ |
|
207 Public method to test, if the dialog is in "Add Folder" mode. |
|
208 |
|
209 @return flag indicating "Add Folder" mode (boolean) |
|
210 """ |
|
211 return self.__addFolder |
|
212 |
|
213 def addedNode(self): |
|
214 """ |
|
215 Public method to get a reference to the added bookmark node. |
|
216 |
|
217 @return reference to the added bookmark node (BookmarkNode) |
|
218 """ |
|
219 return self.__addedNode |
|
220 |
|
221 def accept(self): |
|
222 """ |
|
223 Public slot handling the acceptance of the dialog. |
|
224 """ |
|
225 if (not self.__addFolder and not self.addressEdit.text()) or \ |
|
226 not self.nameEdit.text(): |
|
227 super(AddBookmarkDialog, self).accept() |
|
228 return |
|
229 |
|
230 from .BookmarkNode import BookmarkNode |
|
231 |
|
232 idx = self.currentIndex() |
|
233 if not idx.isValid(): |
|
234 idx = self.__bookmarksManager.bookmarksModel().index(0, 0) |
|
235 parent = self.__bookmarksManager.bookmarksModel().node(idx) |
|
236 |
|
237 if self.__addFolder: |
|
238 type_ = BookmarkNode.Folder |
|
239 else: |
|
240 type_ = BookmarkNode.Bookmark |
|
241 bookmark = BookmarkNode(type_) |
|
242 bookmark.title = self.nameEdit.text() |
|
243 if not self.__addFolder: |
|
244 bookmark.url = self.addressEdit.text() |
|
245 bookmark.desc = self.descriptionEdit.toPlainText() |
|
246 |
|
247 self.__bookmarksManager.addBookmark(parent, bookmark) |
|
248 self.__addedNode = bookmark |
|
249 |
|
250 super(AddBookmarkDialog, self).accept() |