WebBrowser/Bookmarks/AddBookmarkDialog.py

branch
QtWebEngine
changeset 4732
5ac4fc1dfc20
parent 4631
5c1a96925da4
child 5389
9b1c800daff3
equal deleted inserted replaced
4731:67d861d9e492 4732:5ac4fc1dfc20
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2016 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=QModelIndex()):
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 sindex = self.mapToSource(parent)
68 return self.sourceModel().hasChildren(sindex)
69
70
71 class AddBookmarkDialog(QDialog, Ui_AddBookmarkDialog):
72 """
73 Class implementing a dialog to add a bookmark or a bookmark folder.
74 """
75 def __init__(self, parent=None, bookmarksManager=None):
76 """
77 Constructor
78
79 @param parent reference to the parent widget (QWidget)
80 @param bookmarksManager reference to the bookmarks manager
81 object (BookmarksManager)
82 """
83 super(AddBookmarkDialog, self).__init__(parent)
84 self.setupUi(self)
85
86 self.__bookmarksManager = bookmarksManager
87 self.__addedNode = None
88 self.__addFolder = False
89
90 if self.__bookmarksManager is None:
91 import WebBrowser.WebBrowserWindow
92 self.__bookmarksManager = \
93 WebBrowser.WebBrowserWindow.WebBrowserWindow.bookmarksManager()
94
95 self.__proxyModel = AddBookmarkProxyModel(self)
96 model = self.__bookmarksManager.bookmarksModel()
97 self.__proxyModel.setSourceModel(model)
98
99 self.__treeView = QTreeView(self)
100 self.__treeView.setModel(self.__proxyModel)
101 self.__treeView.expandAll()
102 self.__treeView.header().setStretchLastSection(True)
103 self.__treeView.header().hide()
104 self.__treeView.setItemsExpandable(False)
105 self.__treeView.setRootIsDecorated(False)
106 self.__treeView.setIndentation(10)
107 self.__treeView.show()
108
109 self.locationCombo.setModel(self.__proxyModel)
110 self.locationCombo.setView(self.__treeView)
111
112 self.addressEdit.setInactiveText(self.tr("Url"))
113 self.nameEdit.setInactiveText(self.tr("Title"))
114
115 self.resize(self.sizeHint())
116
117 def setUrl(self, url):
118 """
119 Public slot to set the URL of the new bookmark.
120
121 @param url URL of the bookmark (string)
122 """
123 self.addressEdit.setText(url)
124 self.resize(self.sizeHint())
125
126 def url(self):
127 """
128 Public method to get the URL of the bookmark.
129
130 @return URL of the bookmark (string)
131 """
132 return self.addressEdit.text()
133
134 def setTitle(self, title):
135 """
136 Public method to set the title of the new bookmark.
137
138 @param title title of the bookmark (string)
139 """
140 self.nameEdit.setText(title)
141
142 def title(self):
143 """
144 Public method to get the title of the bookmark.
145
146 @return title of the bookmark (string)
147 """
148 return self.nameEdit.text()
149
150 def setDescription(self, description):
151 """
152 Public method to set the description of the new bookmark.
153
154 @param description description of the bookamrk (string)
155 """
156 self.descriptionEdit.setPlainText(description)
157
158 def description(self):
159 """
160 Public method to get the description of the bookmark.
161
162 @return description of the bookamrk (string)
163 """
164 return self.descriptionEdit.toPlainText()
165
166 def setCurrentIndex(self, idx):
167 """
168 Public method to set the current index.
169
170 @param idx current index to be set (QModelIndex)
171 """
172 proxyIndex = self.__proxyModel.mapFromSource(idx)
173 self.__treeView.setCurrentIndex(proxyIndex)
174 self.locationCombo.setCurrentIndex(proxyIndex.row())
175
176 def currentIndex(self):
177 """
178 Public method to get the current index.
179
180 @return current index (QModelIndex)
181 """
182 idx = self.locationCombo.view().currentIndex()
183 idx = self.__proxyModel.mapToSource(idx)
184 return idx
185
186 def setFolder(self, folder):
187 """
188 Public method to set the dialog to "Add Folder" mode.
189
190 @param folder flag indicating "Add Folder" mode (boolean)
191 """
192 self.__addFolder = folder
193
194 if folder:
195 self.setWindowTitle(self.tr("Add Folder"))
196 self.addressEdit.setVisible(False)
197 else:
198 self.setWindowTitle(self.tr("Add Bookmark"))
199 self.addressEdit.setVisible(True)
200
201 self.resize(self.sizeHint())
202
203 def isFolder(self):
204 """
205 Public method to test, if the dialog is in "Add Folder" mode.
206
207 @return flag indicating "Add Folder" mode (boolean)
208 """
209 return self.__addFolder
210
211 def addedNode(self):
212 """
213 Public method to get a reference to the added bookmark node.
214
215 @return reference to the added bookmark node (BookmarkNode)
216 """
217 return self.__addedNode
218
219 def accept(self):
220 """
221 Public slot handling the acceptance of the dialog.
222 """
223 if (not self.__addFolder and not self.addressEdit.text()) or \
224 not self.nameEdit.text():
225 super(AddBookmarkDialog, self).accept()
226 return
227
228 from .BookmarkNode import BookmarkNode
229
230 idx = self.currentIndex()
231 if not idx.isValid():
232 idx = self.__bookmarksManager.bookmarksModel().index(0, 0)
233 parent = self.__bookmarksManager.bookmarksModel().node(idx)
234
235 if self.__addFolder:
236 type_ = BookmarkNode.Folder
237 else:
238 type_ = BookmarkNode.Bookmark
239 bookmark = BookmarkNode(type_)
240 bookmark.title = self.nameEdit.text()
241 if not self.__addFolder:
242 bookmark.url = self.addressEdit.text()
243 bookmark.desc = self.descriptionEdit.toPlainText()
244
245 self.__bookmarksManager.addBookmark(parent, bookmark)
246 self.__addedNode = bookmark
247
248 super(AddBookmarkDialog, self).accept()

eric ide

mercurial