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