eric7/WebBrowser/Bookmarks/AddBookmarkDialog.py

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

eric ide

mercurial