Helpviewer/Bookmarks/BookmarksManager.py

changeset 945
8cd4d08fa9f6
parent 882
34b86be88bf0
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
23 23
24 from Utilities.AutoSaver import AutoSaver 24 from Utilities.AutoSaver import AutoSaver
25 import Utilities 25 import Utilities
26 import Preferences 26 import Preferences
27 27
28 BOOKMARKBAR = QT_TRANSLATE_NOOP("BookmarksManager", "Bookmarks Bar") 28 BOOKMARKBAR = QT_TRANSLATE_NOOP("BookmarksManager", "Bookmarks Bar")
29 BOOKMARKMENU = QT_TRANSLATE_NOOP("BookmarksManager", "Bookmarks Menu") 29 BOOKMARKMENU = QT_TRANSLATE_NOOP("BookmarksManager", "Bookmarks Menu")
30 30
31 StartRoot = 0 31 StartRoot = 0
32 StartMenu = 1 32 StartMenu = 1
33 StartToolBar = 2 33 StartToolBar = 2
34 34
35 ########################################################################################## 35 ##########################################################################################
36 36
37 extract_js = r""" 37 extract_js = r"""
85 xbel; 85 xbel;
86 """ 86 """
87 87
88 ########################################################################################## 88 ##########################################################################################
89 89
90
90 class BookmarksManager(QObject): 91 class BookmarksManager(QObject):
91 """ 92 """
92 Class implementing the bookmarks manager. 93 Class implementing the bookmarks manager.
93 94
94 @signal entryAdded(BookmarkNode) emitted after a bookmark node has been added 95 @signal entryAdded(BookmarkNode) emitted after a bookmark node has been added
98 """ 99 """
99 entryAdded = pyqtSignal(BookmarkNode) 100 entryAdded = pyqtSignal(BookmarkNode)
100 entryRemoved = pyqtSignal(BookmarkNode, int, BookmarkNode) 101 entryRemoved = pyqtSignal(BookmarkNode, int, BookmarkNode)
101 entryChanged = pyqtSignal(BookmarkNode) 102 entryChanged = pyqtSignal(BookmarkNode)
102 103
103 def __init__(self, parent = None): 104 def __init__(self, parent=None):
104 """ 105 """
105 Constructor 106 Constructor
106 107
107 @param parent reference to the parent object (QObject) 108 @param parent reference to the parent object (QObject)
108 """ 109 """
160 if reader.error() != QXmlStreamReader.NoError: 161 if reader.error() != QXmlStreamReader.NoError:
161 E5MessageBox.warning(None, 162 E5MessageBox.warning(None,
162 self.trUtf8("Loading Bookmarks"), 163 self.trUtf8("Loading Bookmarks"),
163 self.trUtf8("""Error when loading bookmarks on line {0}, column {1}:\n""" 164 self.trUtf8("""Error when loading bookmarks on line {0}, column {1}:\n"""
164 """{2}""")\ 165 """{2}""")\
165 .format(reader.lineNumber(), 166 .format(reader.lineNumber(),
166 reader.columnNumber(), 167 reader.columnNumber(),
167 reader.errorString())) 168 reader.errorString()))
168 169
169 others = [] 170 others = []
170 for index in range(len(self.__bookmarkRootNode.children()) - 1, -1, -1): 171 for index in range(len(self.__bookmarkRootNode.children()) - 1, -1, -1):
171 node = self.__bookmarkRootNode.children()[index] 172 node = self.__bookmarkRootNode.children()[index]
226 227
227 # restore localized titles 228 # restore localized titles
228 self.__menu.title = self.trUtf8(BOOKMARKMENU) 229 self.__menu.title = self.trUtf8(BOOKMARKMENU)
229 self.__toolbar.title = self.trUtf8(BOOKMARKBAR) 230 self.__toolbar.title = self.trUtf8(BOOKMARKBAR)
230 231
231 def addBookmark(self, parent, node, row = -1): 232 def addBookmark(self, parent, node, row=-1):
232 """ 233 """
233 Public method to add a bookmark. 234 Public method to add a bookmark.
234 235
235 @param parent reference to the node to add to (BookmarkNode) 236 @param parent reference to the node to add to (BookmarkNode)
236 @param node reference to the node to add (BookmarkNode) 237 @param node reference to the node to add (BookmarkNode)
328 def importBookmarks(self): 329 def importBookmarks(self):
329 """ 330 """
330 Public method to import bookmarks. 331 Public method to import bookmarks.
331 """ 332 """
332 supportedFormats = [ 333 supportedFormats = [
333 self.trUtf8("XBEL bookmarks") + " (*.xbel *.xml)", 334 self.trUtf8("XBEL bookmarks") + " (*.xbel *.xml)",
334 self.trUtf8("HTML Netscape bookmarks") + " (*.html)" 335 self.trUtf8("HTML Netscape bookmarks") + " (*.html)"
335 ] 336 ]
336 337
337 fileName = E5FileDialog.getOpenFileName( 338 fileName = E5FileDialog.getOpenFileName(
338 None, 339 None,
366 if reader.error() != QXmlStreamReader.NoError: 367 if reader.error() != QXmlStreamReader.NoError:
367 E5MessageBox.warning(None, 368 E5MessageBox.warning(None,
368 self.trUtf8("Import Bookmarks"), 369 self.trUtf8("Import Bookmarks"),
369 self.trUtf8("""Error when importing bookmarks on""" 370 self.trUtf8("""Error when importing bookmarks on"""
370 """ line {0}, column [1}:\n{2}""")\ 371 """ line {0}, column [1}:\n{2}""")\
371 .format(reader.lineNumber(), 372 .format(reader.lineNumber(),
372 reader.columnNumber(), 373 reader.columnNumber(),
373 reader.errorString())) 374 reader.errorString()))
374 return 375 return
375 376
376 importRootNode.setType(BookmarkNode.Folder) 377 importRootNode.setType(BookmarkNode.Folder)
377 importRootNode.title = self.trUtf8("Imported {0}")\ 378 importRootNode.title = self.trUtf8("Imported {0}")\
436 url = url.toString() 437 url = url.toString()
437 nodes = self.bookmarksForUrl(url) 438 nodes = self.bookmarksForUrl(url)
438 for node in nodes: 439 for node in nodes:
439 self.bookmarksModel().entryChanged(node) 440 self.bookmarksModel().entryChanged(node)
440 441
441 def bookmarkForUrl(self, url, start = StartRoot): 442 def bookmarkForUrl(self, url, start=StartRoot):
442 """ 443 """
443 Public method to get a bookmark node for a given URL. 444 Public method to get a bookmark node for a given URL.
444 445
445 @param url URL of the bookmark to search for (QUrl or string) 446 @param url URL of the bookmark to search for (QUrl or string)
446 @keyparam start indicator for the start of the search 447 @keyparam start indicator for the start of the search
447 (StartRoot, StartMenu, StartToolBar) 448 (StartRoot, StartMenu, StartToolBar)
448 @return bookmark node for the given url (BookmarkNode) 449 @return bookmark node for the given url (BookmarkNode)
449 """ 450 """
450 if start == StartMenu: 451 if start == StartMenu:
451 startNode = self.__menu 452 startNode = self.__menu
478 bm = node 479 bm = node
479 if bm is not None: 480 if bm is not None:
480 return bm 481 return bm
481 return None 482 return None
482 483
483 def bookmarksForUrl(self, url, start = StartRoot): 484 def bookmarksForUrl(self, url, start=StartRoot):
484 """ 485 """
485 Public method to get a list of bookmark nodes for a given URL. 486 Public method to get a list of bookmark nodes for a given URL.
486 487
487 @param url URL of the bookmarks to search for (QUrl or string) 488 @param url URL of the bookmarks to search for (QUrl or string)
488 @keyparam start indicator for the start of the search 489 @keyparam start indicator for the start of the search
489 (StartRoot, StartMenu, StartToolBar) 490 (StartRoot, StartMenu, StartToolBar)
490 @return list of bookmark nodes for the given url (list of BookmarkNode) 491 @return list of bookmark nodes for the given url (list of BookmarkNode)
491 """ 492 """
492 if start == StartMenu: 493 if start == StartMenu:
493 startNode = self.__menu 494 startNode = self.__menu
518 elif node.type() == BookmarkNode.Bookmark: 519 elif node.type() == BookmarkNode.Bookmark:
519 if node.url == url: 520 if node.url == url:
520 bm.append(node) 521 bm.append(node)
521 return bm 522 return bm
522 523
524
523 class RemoveBookmarksCommand(QUndoCommand): 525 class RemoveBookmarksCommand(QUndoCommand):
524 """ 526 """
525 Class implementing the Remove undo command. 527 Class implementing the Remove undo command.
526 """ 528 """
527 def __init__(self, bookmarksManager, parent, row): 529 def __init__(self, bookmarksManager, parent, row):
530 532
531 @param bookmarksManager reference to the bookmarks manager (BookmarksManager) 533 @param bookmarksManager reference to the bookmarks manager (BookmarksManager)
532 @param parent reference to the parent node (BookmarkNode) 534 @param parent reference to the parent node (BookmarkNode)
533 @param row row number of bookmark (integer) 535 @param row row number of bookmark (integer)
534 """ 536 """
535 QUndoCommand.__init__(self, 537 QUndoCommand.__init__(self,
536 QApplication.translate("BookmarksManager", "Remove Bookmark")) 538 QApplication.translate("BookmarksManager", "Remove Bookmark"))
537 539
538 self._row = row 540 self._row = row
539 self._bookmarksManager = bookmarksManager 541 self._bookmarksManager = bookmarksManager
540 try: 542 try:
555 Public slot to perform the redo action. 557 Public slot to perform the redo action.
556 """ 558 """
557 self._parent.remove(self._node) 559 self._parent.remove(self._node)
558 self._bookmarksManager.entryRemoved.emit(self._parent, self._row, self._node) 560 self._bookmarksManager.entryRemoved.emit(self._parent, self._row, self._node)
559 561
562
560 class InsertBookmarksCommand(RemoveBookmarksCommand): 563 class InsertBookmarksCommand(RemoveBookmarksCommand):
561 """ 564 """
562 Class implementing the Insert undo command. 565 Class implementing the Insert undo command.
563 """ 566 """
564 def __init__(self, bookmarksManager, parent, node, row): 567 def __init__(self, bookmarksManager, parent, node, row):
584 """ 587 """
585 Public slot to perform the redo action. 588 Public slot to perform the redo action.
586 """ 589 """
587 RemoveBookmarksCommand.undo(self) 590 RemoveBookmarksCommand.undo(self)
588 591
592
589 class ChangeBookmarkCommand(QUndoCommand): 593 class ChangeBookmarkCommand(QUndoCommand):
590 """ 594 """
591 Class implementing the Insert undo command. 595 Class implementing the Insert undo command.
592 """ 596 """
593 def __init__(self, bookmarksManager, node, newValue, title): 597 def __init__(self, bookmarksManager, node, newValue, title):
595 Constructor 599 Constructor
596 600
597 @param bookmarksManager reference to the bookmarks manager (BookmarksManager) 601 @param bookmarksManager reference to the bookmarks manager (BookmarksManager)
598 @param node reference to the node to be changed (BookmarkNode) 602 @param node reference to the node to be changed (BookmarkNode)
599 @param newValue new value to be set (string) 603 @param newValue new value to be set (string)
600 @param title flag indicating a change of the title (True) or 604 @param title flag indicating a change of the title (True) or
601 the URL (False) (boolean) 605 the URL (False) (boolean)
602 """ 606 """
603 QUndoCommand.__init__(self) 607 QUndoCommand.__init__(self)
604 608
605 self._bookmarksManager = bookmarksManager 609 self._bookmarksManager = bookmarksManager

eric ide

mercurial