Helpviewer/Bookmarks/AddBookmarkDialog.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 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 PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12
13 import Helpviewer.HelpWindow
14
15 from BookmarkNode import BookmarkNode
16
17 from Ui_AddBookmarkDialog import Ui_AddBookmarkDialog
18
19 class AddBookmarkProxyModel(QSortFilterProxyModel):
20 """
21 Class implementing a proxy model used by the AddBookmarkDialog dialog.
22 """
23 def __init__(self, parent = None):
24 """
25 Constructor
26
27 @param parent reference to the parent object (QObject)
28 """
29 QSortFilterProxyModel.__init__(self, parent)
30
31 def columnCount(self, parent):
32 """
33 Public method to return the number of columns.
34
35 @param parent index of the parent (QModelIndex)
36 @return number of columns (integer)
37 """
38 return min(1, QSortFilterProxyModel.columnCount(self, parent))
39
40 def filterAcceptsRow(self, sourceRow, sourceParent):
41 """
42 Protected method to determine, if the row is acceptable.
43
44 @param sourceRow row number in the source model (integer)
45 @param sourceParent index of the source item (QModelIndex)
46 @return flag indicating acceptance (boolean)
47 """
48 idx = self.sourceModel().index(sourceRow, 0, sourceParent)
49 return self.sourceModel().hasChildren(idx)
50
51 def filterAcceptsColumn(self, sourceColumn, sourceParent):
52 """
53 Protected method to determine, if the column is acceptable.
54
55 @param sourceColumn column number in the source model (integer)
56 @param sourceParent index of the source item (QModelIndex)
57 @return flag indicating acceptance (boolean)
58 """
59 return sourceColumn == 0
60
61 def hasChildren(self, parent = QModelIndex()):
62 """
63 Public method to check, if a parent node has some children.
64
65 @param parent index of the parent node (QModelIndex)
66 @return flag indicating the presence of children (boolean)
67 """
68 sindex = self.mapToSource(parent)
69 return self.sourceModel().hasChildren(sindex)
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 QDialog.__init__(self, parent)
84 self.setupUi(self)
85
86 self.__bookmarksManager = None
87 self.__addedNode = None
88 self.__addFolder = False
89
90 if self.__bookmarksManager is None:
91 self.__bookmarksManager = Helpviewer.HelpWindow.HelpWindow.bookmarksManager()
92
93 self.__proxyModel = AddBookmarkProxyModel(self)
94 model = self.__bookmarksManager.bookmarksModel()
95 self.__proxyModel.setSourceModel(model)
96
97 self.__treeView = QTreeView(self)
98 self.__treeView.setModel(self.__proxyModel)
99 self.__treeView.expandAll()
100 self.__treeView.header().setStretchLastSection(True)
101 self.__treeView.header().hide()
102 self.__treeView.setItemsExpandable(False)
103 self.__treeView.setRootIsDecorated(False)
104 self.__treeView.setIndentation(10)
105 self.__treeView.show()
106
107 self.locationCombo.setModel(self.__proxyModel)
108 self.locationCombo.setView(self.__treeView)
109
110 self.addressEdit.setInactiveText(self.trUtf8("Url"))
111 self.nameEdit.setInactiveText(self.trUtf8("Title"))
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 setCurrentIndex(self, idx):
149 """
150 Public method to set the current index.
151
152 @param idx current index to be set (QModelIndex)
153 """
154 proxyIndex = self.__proxyModel.mapFromSource(idx)
155 self.__treeView.setCurrentIndex(proxyIndex)
156 self.locationCombo.setCurrentIndex(proxyIndex.row())
157
158 def currentIndex(self):
159 """
160 Public method to get the current index.
161
162 @return current index (QModelIndex)
163 """
164 idx = self.locationCombo.view().currentIndex()
165 idx = self.__proxyModel.mapToSource(idx)
166 return idx
167
168 def setFolder(self, folder):
169 """
170 Public method to set the dialog to "Add Folder" mode.
171
172 @param folder flag indicating "Add Folder" mode (boolean)
173 """
174 self.__addFolder = folder
175
176 if folder:
177 self.setWindowTitle(self.trUtf8("Add Folder"))
178 self.addressEdit.setVisible(False)
179 else:
180 self.setWindowTitle(self.trUtf8("Add Bookmark"))
181 self.addressEdit.setVisible(True)
182
183 self.resize(self.sizeHint())
184
185 def isFolder(self):
186 """
187 Public method to test, if the dialog is in "Add Folder" mode.
188
189 @return flag indicating "Add Folder" mode (boolean)
190 """
191 return self.__addFolder
192
193 def addedNode(self):
194 """
195 Public method to get a reference to the added bookmark node.
196
197 @return reference to the added bookmark node (BookmarkNode)
198 """
199 return self.__addedNode
200
201 def accept(self):
202 """
203 Public slot handling the acceptance of the dialog.
204 """
205 if (not self.__addFolder and not self.addressEdit.text()) or \
206 not self.nameEdit.text():
207 QDialog.accept(self)
208 return
209
210 idx = self.currentIndex()
211 if not idx.isValid():
212 idx = self.__bookmarksManager.bookmarksModel().index(0, 0)
213 parent = self.__bookmarksManager.bookmarksModel().node(idx)
214
215 if self.__addFolder:
216 type_ = BookmarkNode.Folder
217 else:
218 type_ = BookmarkNode.Bookmark
219 bookmark = BookmarkNode(type_)
220 bookmark.title = self.nameEdit.text()
221 if not self.__addFolder:
222 bookmark.url = self.addressEdit.text()
223
224 self.__bookmarksManager.addBookmark(parent, bookmark)
225 self.__addedNode = bookmark
226
227 QDialog.accept(self)

eric ide

mercurial