1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <QtCore/QUrl> |
|
20 #include "BookMarksHistoryView.h" |
|
21 #include "BookmarksManager.h" |
|
22 #include "bookmarks.h" |
|
23 #include "BookmarksView_p.h" |
|
24 |
|
25 #define COLUMN0_EXPANDED_WIDTH 165 |
|
26 #define COLUMN0_NORMAL_WIDTH 145 |
|
27 |
|
28 namespace WRT { |
|
29 |
|
30 class HistoryTreeView : public QTreeView |
|
31 { |
|
32 public: |
|
33 HistoryTreeView(QWidget *parent = 0); |
|
34 bool viewportEvent(QEvent *event); |
|
35 void update(); |
|
36 }; |
|
37 |
|
38 |
|
39 class HistoryProxyWidget : public QGraphicsProxyWidget |
|
40 { |
|
41 public: |
|
42 HistoryProxyWidget(QTreeView* wid); |
|
43 void resizeEvent ( QGraphicsSceneResizeEvent * event ); |
|
44 private: |
|
45 HistoryTreeView* m_wid ; |
|
46 }; |
|
47 |
|
48 |
|
49 class BookmarksHistoryViewPrivate |
|
50 { |
|
51 public: |
|
52 BookmarksHistoryViewPrivate(QWidget *parent); |
|
53 ~BookmarksHistoryViewPrivate(); |
|
54 QTreeView *m_treeView; |
|
55 HistoryModel *m_historyModel; //not owned |
|
56 QGraphicsProxyWidget* m_proxyWidget; |
|
57 |
|
58 }; |
|
59 |
|
60 HistoryTreeView::HistoryTreeView(QWidget *parent) |
|
61 :QTreeView(parent) |
|
62 { |
|
63 } |
|
64 bool HistoryTreeView::viewportEvent(QEvent* event) |
|
65 { |
|
66 return QTreeView::viewportEvent(event); |
|
67 |
|
68 } |
|
69 void HistoryTreeView::update() |
|
70 { |
|
71 updateGeometry(); |
|
72 } |
|
73 |
|
74 HistoryProxyWidget::HistoryProxyWidget(QTreeView* wid) |
|
75 { |
|
76 m_wid = (HistoryTreeView*)wid; |
|
77 } |
|
78 |
|
79 void HistoryProxyWidget::resizeEvent ( QGraphicsSceneResizeEvent * event ) |
|
80 { |
|
81 QRectF r = geometry(); |
|
82 QRect r1(r.left(),r.top(),r.width(),r.height()); |
|
83 if(m_wid != NULL){ |
|
84 m_wid->setGeometry(r1); |
|
85 m_wid->update(); |
|
86 } |
|
87 } |
|
88 |
|
89 BookmarksHistoryViewPrivate::BookmarksHistoryViewPrivate(QWidget *parent) |
|
90 { |
|
91 |
|
92 // create the tree view widget |
|
93 m_treeView = new HistoryTreeView(parent); |
|
94 m_treeView->setHeaderHidden(true); //To keep the headers for columns hidden |
|
95 m_treeView->setAllColumnsShowFocus(true); //To highlight the row completely when user focus on a row |
|
96 m_treeView->setAnimated(true); //To animate when expands and collapses the folder |
|
97 m_proxyWidget = NULL; |
|
98 |
|
99 //setting the style sheet for the tree view |
|
100 #ifndef QT_NO_STYLE_STYLESHEET |
|
101 m_treeView->setStyleSheet( " QTreeView { \ |
|
102 background-color : white \ |
|
103 } \ |
|
104 QTreeView::branch { \ |
|
105 border-image: none; image: none \ |
|
106 } \ |
|
107 QTreeView::item { \ |
|
108 height: 28px; \ |
|
109 } \ |
|
110 QTreeView::branch:closed:has-children:has-siblings, \ |
|
111 QTreeView::branch:has-children:!has-siblings:closed { \ |
|
112 border-image: none;\ |
|
113 image: url(:/icons/collapsed.png); \ |
|
114 } \ |
|
115 QTreeView::branch:open:has-children:has-siblings, \ |
|
116 QTreeView::branch:open:has-children:!has-siblings { \ |
|
117 border-image: none;\ |
|
118 image: url(:/icons/expanded.png);\ |
|
119 } \ |
|
120 "); |
|
121 #endif |
|
122 } |
|
123 |
|
124 BookmarksHistoryViewPrivate::~BookmarksHistoryViewPrivate() |
|
125 { |
|
126 delete m_treeView; |
|
127 } |
|
128 |
|
129 /*! |
|
130 * \class BookmarksHistoryView |
|
131 * |
|
132 * \brief Derived class of BookmarksView. Implements tree view functionality |
|
133 * |
|
134 * This class is implements a tree view of history items |
|
135 */ |
|
136 |
|
137 /*! |
|
138 * Basic BookmarksHistoryView constructor requires a BookmarksManager and |
|
139 * a parent QWidget |
|
140 * @param bm_mgr : BookmarksManager Handle |
|
141 * @param parent : handle to parent widget |
|
142 * @see BookmarksManager |
|
143 */ |
|
144 BookmarksHistoryView::BookmarksHistoryView(WRT::BookmarksManager* bm_mgr, QWidget *parent) |
|
145 :BookmarksView(bm_mgr, parent) |
|
146 ,dBookmarksHistoryViewPrivate(new BookmarksHistoryViewPrivate(parent)) |
|
147 { |
|
148 dBookmarksHistoryViewPrivate->m_historyModel = bookmarksManager()->historyModel(); //getting the history model |
|
149 dBookmarksHistoryViewPrivate->m_treeView->hide(); |
|
150 m_jsObject = new BookmarksHistoryViewJSObject(this, 0, type()); |
|
151 } |
|
152 |
|
153 /*! |
|
154 * destructor |
|
155 */ |
|
156 BookmarksHistoryView::~BookmarksHistoryView() |
|
157 { |
|
158 delete dBookmarksHistoryViewPrivate; |
|
159 } |
|
160 |
|
161 // SLOTS |
|
162 |
|
163 /*! |
|
164 * opens an item in the tree. If it is a history item it will emit |
|
165 * the openUrl signal. |
|
166 * @param index : handle to modelIndex |
|
167 */ |
|
168 void BookmarksHistoryView::openItem(const QModelIndex &index) |
|
169 { |
|
170 |
|
171 BookmarkNode *itemNode = dBookmarksHistoryViewPrivate->m_historyModel->node(index); |
|
172 if (!itemNode) |
|
173 return; |
|
174 |
|
175 //open url for the history item |
|
176 if (itemNode->isBookmark()) |
|
177 { |
|
178 emit openUrl(QUrl(itemNode->url)); |
|
179 } |
|
180 } |
|
181 |
|
182 /*! |
|
183 * Handles when the folder is expanded |
|
184 * |
|
185 * @param index : handle to modelIndex |
|
186 */ |
|
187 void BookmarksHistoryView::itemExpanded(const QModelIndex &index) |
|
188 { |
|
189 BookmarkNode *itemNode = dBookmarksHistoryViewPrivate->m_historyModel->node(index); |
|
190 if (!itemNode) |
|
191 return; |
|
192 |
|
193 BookmarkNode *parentNode = itemNode->parent(); |
|
194 |
|
195 if (!parentNode) |
|
196 return; |
|
197 |
|
198 if (parentNode->parent()){ |
|
199 dBookmarksHistoryViewPrivate->m_treeView->setColumnWidth(0,COLUMN0_EXPANDED_WIDTH); |
|
200 } |
|
201 else{ |
|
202 dBookmarksHistoryViewPrivate->m_treeView->setColumnWidth(0,COLUMN0_NORMAL_WIDTH); |
|
203 } |
|
204 |
|
205 for (int i = 0; i < parentNode->children().count(); ++i){ |
|
206 BookmarkNode *childNode = parentNode->children()[i]; |
|
207 if (childNode->expanded && childNode != itemNode ){ |
|
208 itemNode->expanded = false; |
|
209 dBookmarksHistoryViewPrivate->m_treeView->collapse(dBookmarksHistoryViewPrivate->m_historyModel->index(childNode)); |
|
210 } |
|
211 } |
|
212 |
|
213 itemNode->expanded = true; |
|
214 } |
|
215 |
|
216 /*! |
|
217 * Handles when the folder is collapsed |
|
218 * |
|
219 * @param index : handle to modelIndex |
|
220 */ |
|
221 void BookmarksHistoryView::itemCollapsed(const QModelIndex &index) |
|
222 { |
|
223 |
|
224 BookmarkNode *itemNode = dBookmarksHistoryViewPrivate->m_historyModel->node(index); |
|
225 if (!itemNode) |
|
226 return; |
|
227 |
|
228 //set expanded false for the item which is collapsed |
|
229 itemNode->expanded = false; |
|
230 } |
|
231 |
|
232 /*! |
|
233 activate the view's resources. Could be connected by client to view visibility |
|
234 */ |
|
235 void BookmarksHistoryView::activate() |
|
236 { |
|
237 Q_ASSERT(!d->m_isActive); |
|
238 |
|
239 bookmarksManager()->updateHistory(); |
|
240 dBookmarksHistoryViewPrivate->m_historyModel = bookmarksManager()->historyModel(); |
|
241 dBookmarksHistoryViewPrivate->m_treeView->setModel(dBookmarksHistoryViewPrivate->m_historyModel); //set model for the view |
|
242 dBookmarksHistoryViewPrivate->m_treeView->setColumnWidth(0,COLUMN0_NORMAL_WIDTH); //set the 0th column's width |
|
243 |
|
244 //Connect the signals |
|
245 connect(dBookmarksHistoryViewPrivate->m_treeView, SIGNAL(activated(const QModelIndex &)), |
|
246 this, SLOT(openItem(const QModelIndex &))); |
|
247 connect(dBookmarksHistoryViewPrivate->m_treeView, SIGNAL(expanded(const QModelIndex &)), |
|
248 this, SLOT(itemExpanded(const QModelIndex &))); |
|
249 connect(dBookmarksHistoryViewPrivate->m_treeView, SIGNAL(collapsed(const QModelIndex &)), |
|
250 this, SLOT(itemCollapsed(const QModelIndex &))); |
|
251 |
|
252 connect(this, SIGNAL(close()), this, SLOT(deactivate())); |
|
253 |
|
254 BookmarkNode *historyRootNode = bookmarksManager()->history(); |
|
255 |
|
256 for (int i = 0; i < historyRootNode->children().count(); ++i){ |
|
257 BookmarkNode *node = historyRootNode->children()[i]; |
|
258 |
|
259 //For folder set the 1st column spanned throughout |
|
260 if(node->isFolder()) { |
|
261 dBookmarksHistoryViewPrivate->m_treeView->setFirstColumnSpanned(i, QModelIndex(),true); |
|
262 //expand "Today" folder when activating the view itself |
|
263 |
|
264 if(QDateTime::currentDateTime().date().toString() == node->date.toString()) { |
|
265 dBookmarksHistoryViewPrivate->m_treeView->expand(dBookmarksHistoryViewPrivate->m_historyModel->index(node)); |
|
266 } |
|
267 |
|
268 for (int j = 0; j < node->children().count(); ++j){ |
|
269 BookmarkNode *subNode = node->children()[j]; |
|
270 |
|
271 if(subNode->isFolder()) { |
|
272 dBookmarksHistoryViewPrivate->m_treeView->setFirstColumnSpanned(j, |
|
273 dBookmarksHistoryViewPrivate->m_historyModel->index(node),true); |
|
274 } |
|
275 |
|
276 } |
|
277 } |
|
278 } |
|
279 |
|
280 dBookmarksHistoryViewPrivate->m_treeView->show(); //show the view |
|
281 d->m_isActive = true; |
|
282 |
|
283 emit activated(); |
|
284 |
|
285 dBookmarksHistoryViewPrivate->m_treeView->setCurrentIndex ( bookmarksManager()->historyModel()->index(0,0)); |
|
286 dBookmarksHistoryViewPrivate->m_treeView->setFocus(); |
|
287 } |
|
288 |
|
289 /*! |
|
290 deactivate the view's resources. Could be connected by client to view visibility |
|
291 */ |
|
292 void BookmarksHistoryView::deactivate() |
|
293 { |
|
294 Q_ASSERT(d->m_isActive); |
|
295 |
|
296 // disconnect signals |
|
297 disconnect(dBookmarksHistoryViewPrivate->m_treeView, SIGNAL(activated(const QModelIndex &)), |
|
298 this, SLOT(openItem(const QModelIndex &))); |
|
299 disconnect(dBookmarksHistoryViewPrivate->m_treeView, SIGNAL(expanded(const QModelIndex &)), |
|
300 this, SLOT(itemExpanded(const QModelIndex &))); |
|
301 disconnect(dBookmarksHistoryViewPrivate->m_treeView, SIGNAL(collapsed(const QModelIndex &)), |
|
302 this, SLOT(itemCollided(const QModelIndex &))); |
|
303 |
|
304 disconnect(this, SIGNAL(close()), this, SLOT(deactivate())); |
|
305 |
|
306 bookmarksManager()->setHistoryView(NULL); |
|
307 dBookmarksHistoryViewPrivate->m_treeView->hide(); //hide the view |
|
308 d->m_isActive = false; |
|
309 |
|
310 emit deactivated(); |
|
311 } |
|
312 |
|
313 QGraphicsWidget* BookmarksHistoryView::widget() const |
|
314 { |
|
315 assert(qWidget()); |
|
316 if(!dBookmarksHistoryViewPrivate->m_proxyWidget){ |
|
317 dBookmarksHistoryViewPrivate->m_proxyWidget = new HistoryProxyWidget(dBookmarksHistoryViewPrivate->m_treeView); |
|
318 dBookmarksHistoryViewPrivate->m_proxyWidget->setWidget(qWidget()); |
|
319 } |
|
320 |
|
321 return dBookmarksHistoryViewPrivate->m_proxyWidget; |
|
322 } |
|
323 |
|
324 |
|
325 /*! |
|
326 Return the widget handle of this view |
|
327 */ |
|
328 QWidget* BookmarksHistoryView::qWidget() const |
|
329 { |
|
330 return dBookmarksHistoryViewPrivate->m_treeView; |
|
331 } |
|
332 |
|
333 /*! |
|
334 Return the title of this view for display |
|
335 @return Title string |
|
336 */ |
|
337 QString BookmarksHistoryView::title() |
|
338 { |
|
339 return tr("Recent Urls"); |
|
340 } |
|
341 |
|
342 /*! |
|
343 Return the list of public QActions most relevant to the view's current context |
|
344 (most approptiate for contextual menus, etc.) (empty for now) |
|
345 */ |
|
346 QList<QAction*> BookmarksHistoryView::getContext() |
|
347 { |
|
348 QList<QAction*> contextList; |
|
349 return contextList; |
|
350 } |
|
351 |
|
352 |
|
353 ControllableView* BookmarksHistoryView::createNew(QWidget *parent) |
|
354 { |
|
355 return new BookmarksHistoryView(BookmarksManager::getSingleton(),parent); |
|
356 } |
|
357 |
|
358 BookmarksHistoryViewJSObject::BookmarksHistoryViewJSObject(BookmarksHistoryView* view, QWebFrame* webFrame, const QString& objectName) |
|
359 : ControllableViewJSObject(view, webFrame, objectName) |
|
360 { |
|
361 connect(view,SIGNAL(activated()),this,SIGNAL(activated())); |
|
362 connect(view,SIGNAL(deactivated()),this,SIGNAL(deactivated())); |
|
363 connect(view,SIGNAL(openUrl(const QUrl &)),this,SIGNAL(openUrl())); |
|
364 } |
|
365 |
|
366 BookmarksHistoryViewJSObject::~BookmarksHistoryViewJSObject() |
|
367 { |
|
368 disconnect(static_cast<BookmarksHistoryView*>(m_contentView),SIGNAL(activated()),this,SIGNAL(activated())); |
|
369 disconnect(static_cast<BookmarksHistoryView*>(m_contentView),SIGNAL(deactivated()),this,SIGNAL(deactivated())); |
|
370 disconnect(static_cast<BookmarksHistoryView*>(m_contentView),SIGNAL(openUrl(const QUrl &)),this,SIGNAL(openUrl())); |
|
371 } |
|
372 |
|
373 } |
|