src/eric7/WebBrowser/Bookmarks/AddBookmarkDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2022 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 PyQt6.QtCore import QModelIndex, QSortFilterProxyModel
11 from PyQt6.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.resize(self.sizeHint())
114
115 def setUrl(self, url):
116 """
117 Public slot to set the URL of the new bookmark.
118
119 @param url URL of the bookmark (string)
120 """
121 self.addressEdit.setText(url)
122 self.resize(self.sizeHint())
123
124 def url(self):
125 """
126 Public method to get the URL of the bookmark.
127
128 @return URL of the bookmark (string)
129 """
130 return self.addressEdit.text()
131
132 def setTitle(self, title):
133 """
134 Public method to set the title of the new bookmark.
135
136 @param title title of the bookmark (string)
137 """
138 self.nameEdit.setText(title)
139
140 def title(self):
141 """
142 Public method to get the title of the bookmark.
143
144 @return title of the bookmark (string)
145 """
146 return self.nameEdit.text()
147
148 def setDescription(self, description):
149 """
150 Public method to set the description of the new bookmark.
151
152 @param description description of the bookamrk (string)
153 """
154 self.descriptionEdit.setPlainText(description)
155
156 def description(self):
157 """
158 Public method to get the description of the bookmark.
159
160 @return description of the bookamrk (string)
161 """
162 return self.descriptionEdit.toPlainText()
163
164 def setCurrentIndex(self, idx):
165 """
166 Public method to set the current index.
167
168 @param idx current index to be set (QModelIndex)
169 """
170 proxyIndex = self.__proxyModel.mapFromSource(idx)
171 self.__treeView.setCurrentIndex(proxyIndex)
172 self.locationCombo.setCurrentIndex(proxyIndex.row())
173
174 def currentIndex(self):
175 """
176 Public method to get the current index.
177
178 @return current index (QModelIndex)
179 """
180 idx = self.locationCombo.view().currentIndex()
181 idx = self.__proxyModel.mapToSource(idx)
182 return idx
183
184 def setFolder(self, folder):
185 """
186 Public method to set the dialog to "Add Folder" mode.
187
188 @param folder flag indicating "Add Folder" mode (boolean)
189 """
190 self.__addFolder = folder
191
192 if folder:
193 self.setWindowTitle(self.tr("Add Folder"))
194 self.addressEdit.setVisible(False)
195 self.addressLabel.setVisible(False)
196 else:
197 self.setWindowTitle(self.tr("Add Bookmark"))
198 self.addressEdit.setVisible(True)
199 self.addressLabel.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 (
224 (not self.__addFolder and not self.addressEdit.text()) or
225 not self.nameEdit.text()
226 ):
227 super().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 type_ = (BookmarkNode.Folder if self.__addFolder
238 else 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().accept()

eric ide

mercurial