|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a tool bar showing bookmarks. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from E4Gui.E4ModelToolBar import E4ModelToolBar |
|
14 |
|
15 import Helpviewer.HelpWindow |
|
16 |
|
17 from BookmarksModel import BookmarksModel |
|
18 from BookmarkNode import BookmarkNode |
|
19 from BookmarksMenu import BookmarksMenu |
|
20 from AddBookmarkDialog import AddBookmarkDialog |
|
21 |
|
22 import UI.PixmapCache |
|
23 |
|
24 class BookmarksToolBar(E4ModelToolBar): |
|
25 """ |
|
26 Class implementing a tool bar showing bookmarks. |
|
27 |
|
28 @signal openUrl(const QUrl&, const QString&) emitted to open a URL in the current |
|
29 tab |
|
30 @signal newUrl(const QUrl&, const QString&) emitted to open a URL in a new tab |
|
31 """ |
|
32 def __init__(self, model, parent = None): |
|
33 """ |
|
34 Constructor |
|
35 |
|
36 @param model reference to the bookmarks model (BookmarksModel) |
|
37 @param parent reference to the parent widget (QWidget) |
|
38 """ |
|
39 E4ModelToolBar.__init__(self, |
|
40 QApplication.translate("BookmarksToolBar", "Bookmarks"), parent) |
|
41 |
|
42 self.__bookmarksModel = model |
|
43 |
|
44 self.setModel(model) |
|
45 self.setRootIndex(model.nodeIndex( |
|
46 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().toolbar())) |
|
47 |
|
48 self.setContextMenuPolicy(Qt.CustomContextMenu) |
|
49 self.connect(self, SIGNAL("customContextMenuRequested(const QPoint &)"), |
|
50 self.__contextMenuRequested) |
|
51 self.connect(self, SIGNAL("activated(const QModelIndex &)"), |
|
52 self.__bookmarkActivated) |
|
53 |
|
54 self.setHidden(True) |
|
55 self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) |
|
56 |
|
57 self._build() |
|
58 |
|
59 def __contextMenuRequested(self, pos): |
|
60 """ |
|
61 Private slot to handle the context menu request. |
|
62 |
|
63 @param pos position the context menu shall be shown (QPoint) |
|
64 """ |
|
65 act = self.actionAt(pos) |
|
66 menu = QMenu() |
|
67 |
|
68 if act is not None: |
|
69 v = act.data() |
|
70 |
|
71 if act.menu() is None: |
|
72 menuAction = menu.addAction(self.trUtf8("&Open"), self.__openBookmark) |
|
73 menuAction.setData(v) |
|
74 |
|
75 menuAction = menu.addAction(self.trUtf8("Open in New &Tab\tCtrl+LMB"), |
|
76 self.__openBookmarkInNewTab) |
|
77 menuAction.setData(v) |
|
78 |
|
79 menu.addSeparator() |
|
80 |
|
81 menuAction = menu.addAction(self.trUtf8("&Remove"), self.__removeBookmark) |
|
82 menuAction.setData(v) |
|
83 |
|
84 menu.addSeparator() |
|
85 |
|
86 menu.addAction(self.trUtf8("Add &Bookmark..."), self.__newBookmark) |
|
87 menu.addAction(self.trUtf8("Add &Folder..."), self.__newFolder) |
|
88 |
|
89 menu.exec_(QCursor.pos()) |
|
90 |
|
91 def __bookmarkActivated(self, idx): |
|
92 """ |
|
93 Private slot handling the activation of a bookmark. |
|
94 |
|
95 @param idx index of the activated bookmark (QModelIndex) |
|
96 """ |
|
97 assert idx.isValid() |
|
98 |
|
99 if self._keyboardModifiers & Qt.ControlModifier: |
|
100 self.emit(SIGNAL("newUrl(const QUrl&, const QString&)"), |
|
101 idx.data(BookmarksModel.UrlRole).toUrl(), |
|
102 idx.data(Qt.DisplayRole).toString()) |
|
103 else: |
|
104 self.emit(SIGNAL("openUrl(const QUrl&, const QString&)"), |
|
105 idx.data(BookmarksModel.UrlRole).toUrl(), |
|
106 idx.data(Qt.DisplayRole).toString()) |
|
107 |
|
108 def __openToolBarBookmark(self): |
|
109 """ |
|
110 Private slot to open a bookmark in the current browser tab. |
|
111 """ |
|
112 idx = self.index(self.sender()) |
|
113 |
|
114 if self._keyboardModifiers & Qt.ControlModifier: |
|
115 self.emit(SIGNAL("newUrl(const QUrl&, const QString&)"), |
|
116 idx.data(BookmarksModel.UrlRole).toUrl(), |
|
117 idx.data(Qt.DisplayRole).toString()) |
|
118 else: |
|
119 self.emit(SIGNAL("openUrl(const QUrl&, const QString&)"), |
|
120 idx.data(BookmarksModel.UrlRole).toUrl(), |
|
121 idx.data(Qt.DisplayRole).toString()) |
|
122 self.resetFlags() |
|
123 |
|
124 def __openBookmark(self): |
|
125 """ |
|
126 Private slot to open a bookmark in the current browser tab. |
|
127 """ |
|
128 idx = self.index(self.sender()) |
|
129 |
|
130 self.emit(SIGNAL("openUrl(const QUrl&, const QString&)"), |
|
131 idx.data(BookmarksModel.UrlRole).toUrl(), |
|
132 idx.data(Qt.DisplayRole).toString()) |
|
133 |
|
134 def __openBookmarkInNewTab(self): |
|
135 """ |
|
136 Private slot to open a bookmark in a new browser tab. |
|
137 """ |
|
138 idx = self.index(self.sender()) |
|
139 |
|
140 self.emit(SIGNAL("newUrl(const QUrl&, const QString&)"), |
|
141 idx.data(BookmarksModel.UrlRole).toUrl(), |
|
142 idx.data(Qt.DisplayRole).toString()) |
|
143 |
|
144 def __removeBookmark(self): |
|
145 """ |
|
146 Private slot to remove a bookmark. |
|
147 """ |
|
148 idx = self.index(self.sender()) |
|
149 |
|
150 self.__bookmarksModel.removeRow(idx.row(), self.rootIndex()) |
|
151 |
|
152 def __newBookmark(self): |
|
153 """ |
|
154 Private slot to add a new bookmark. |
|
155 """ |
|
156 dlg = AddBookmarkDialog() |
|
157 dlg.setCurrentIndex(self.rootIndex()) |
|
158 dlg.exec_() |
|
159 |
|
160 def __newFolder(self): |
|
161 """ |
|
162 Private slot to add a new bookmarks folder. |
|
163 """ |
|
164 dlg = AddBookmarkDialog() |
|
165 dlg.setCurrentIndex(self.rootIndex()) |
|
166 dlg.setFolder(True) |
|
167 dlg.exec_() |
|
168 |
|
169 def _createMenu(self): |
|
170 """ |
|
171 Protected method to create the menu for a tool bar action. |
|
172 |
|
173 @return menu for a tool bar action (E4ModelMenu) |
|
174 """ |
|
175 menu = BookmarksMenu(self) |
|
176 self.connect(menu, SIGNAL("openUrl(const QUrl&, const QString&)"), |
|
177 self, SIGNAL("openUrl(const QUrl&, const QString&)")) |
|
178 self.connect(menu, SIGNAL("newUrl(const QUrl&, const QString&)"), |
|
179 self, SIGNAL("newUrl(const QUrl&, const QString&)")) |
|
180 return menu |