browsercore/appfw/Api/Views/BookmarksTreeView.cpp
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 2 bf4420e9fa4d
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     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 <QtGui/QHeaderView>
       
    20 #include <QtCore/QUrl>
       
    21 #include "BookmarksView_p.h"
       
    22 #include "BookmarksTreeView_p.h"
       
    23 #include "BookmarksTreeView.h"
       
    24 #include "BookmarksManager.h"
       
    25 #include "bookmarks.h"
       
    26 
       
    27 namespace WRT {
       
    28 
       
    29 class BookMarkTreeView : public QTreeView 
       
    30     {
       
    31     public:
       
    32         BookMarkTreeView(QWidget *parent = 0) ;
       
    33         bool viewportEvent(QEvent *event);
       
    34         void update();
       
    35 
       
    36     };
       
    37  
       
    38 class BookmarkProxyWidget : public QGraphicsProxyWidget
       
    39     {
       
    40     public:
       
    41          BookmarkProxyWidget(QTreeView* wid);
       
    42          void resizeEvent ( QGraphicsSceneResizeEvent * event );
       
    43     private:
       
    44         BookMarkTreeView* m_wid ;
       
    45     };
       
    46 
       
    47 
       
    48 BookMarkTreeView::BookMarkTreeView(QWidget *parent)
       
    49 	     :QTreeView(parent) 
       
    50 {
       
    51 }
       
    52 	
       
    53 bool BookMarkTreeView::viewportEvent(QEvent* event)
       
    54 {
       
    55     return QTreeView::viewportEvent(event);
       
    56 }
       
    57 
       
    58 void BookMarkTreeView::update()
       
    59 {
       
    60     updateGeometry(); 
       
    61 }
       
    62 
       
    63 BookmarkProxyWidget::BookmarkProxyWidget(QTreeView* wid)
       
    64 {
       
    65     m_wid = (BookMarkTreeView*)wid;
       
    66 }
       
    67 
       
    68 void BookmarkProxyWidget::resizeEvent ( QGraphicsSceneResizeEvent * event )
       
    69 {
       
    70     QRectF r = geometry();
       
    71     QRect r1(r.left(),r.top(),r.width(),r.height());
       
    72     if(m_wid != NULL){
       
    73         m_wid->setGeometry(r1);
       
    74         m_wid->update();
       
    75     }
       
    76 }
       
    77 
       
    78 BookmarksTreeViewPrivate::BookmarksTreeViewPrivate(QWidget *parent)
       
    79 {
       
    80     m_proxyWidget = NULL;
       
    81     // create the tree view widget
       
    82     m_treeView = new BookMarkTreeView(parent);
       
    83     
       
    84     m_actionBack = new QAction("Back", parent);
       
    85     
       
    86     m_treeView->setUniformRowHeights(true);
       
    87     m_treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
       
    88     m_treeView->setSelectionMode(QAbstractItemView::SingleSelection);
       
    89     m_treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
       
    90 
       
    91     m_treeView->setHeaderHidden(true);
       
    92     m_treeView->setAnimated(true);
       
    93     
       
    94 	//setting the style sheet for the tree view
       
    95 #ifndef QT_NO_STYLE_STYLESHEET
       
    96     m_treeView->setStyleSheet( " QTreeView { \
       
    97                                       background-color : white \
       
    98                                       } \
       
    99                                       QTreeView::branch { \
       
   100                                       border-image: none; image: none \
       
   101                                       } \
       
   102                                       QTreeView::item { \
       
   103                                       height: 28px; \
       
   104                                       } \
       
   105                                       QTreeView::branch:closed:has-children:has-siblings, \
       
   106                                       QTreeView::branch:has-children:!has-siblings:closed { \
       
   107                                       border-image: none;\
       
   108                                       image: url(:/icons/collapsed.png); \
       
   109                                       } \
       
   110                                       QTreeView::branch:open:has-children:has-siblings, \
       
   111                                       QTreeView::branch:open:has-children:!has-siblings { \
       
   112                                       border-image: none;\
       
   113                                       image: url(:/icons/expanded.png);\
       
   114                                       } \
       
   115                                    ");
       
   116 
       
   117 #endif
       
   118 }
       
   119 
       
   120 
       
   121 BookmarksTreeViewPrivate::~BookmarksTreeViewPrivate()
       
   122 {
       
   123     delete(m_treeView);
       
   124 }
       
   125 
       
   126 
       
   127 /*!
       
   128  * \class BookmarksTreeView
       
   129  *
       
   130  * \brief Derived class of BookmarksView. Implements tree view functionality
       
   131  *
       
   132  * This class is implements a tree view of bookmarks
       
   133  */
       
   134 
       
   135 /*!
       
   136  * Basic BookmarksTreeView constructor requires a BookmarksManager and 
       
   137  * a parent QWidget
       
   138  * @param  bm_mgr : BookmarksManager Handle
       
   139  * @param  parent : handle to parent widget
       
   140  * @see BookmarksManager
       
   141  */
       
   142 BookmarksTreeView::BookmarksTreeView(BookmarksManager* bm_mgr, QWidget *parent) :
       
   143     BookmarksView(bm_mgr, parent),
       
   144     dBookmarksTreeViewPrivate(new BookmarksTreeViewPrivate(parent))
       
   145 {
       
   146     dBookmarksTreeViewPrivate->m_bookmarksModel = bookmarksManager()->bookmarksModel();
       
   147     dBookmarksTreeViewPrivate->m_treeView->hide();
       
   148     m_jsObject = new BookmarksTreeViewJSObject(this, 0, type());
       
   149 }
       
   150 
       
   151 
       
   152 /*!
       
   153  * destructor 
       
   154  */
       
   155 BookmarksTreeView::~BookmarksTreeView()
       
   156 {
       
   157     delete dBookmarksTreeViewPrivate;
       
   158 }
       
   159 
       
   160 
       
   161 /*!
       
   162   Return the view's Back QAction
       
   163   @return Action*
       
   164 */
       
   165 QAction * BookmarksTreeView::getActionBack()
       
   166 {
       
   167     return dBookmarksTreeViewPrivate->m_actionBack;
       
   168 }
       
   169 
       
   170 // SLOTS
       
   171 
       
   172 /*!
       
   173   opens an item in the tree. If is is a bookmark it will emit
       
   174   the \sa openUrl signal, if it is a folder, it will expand the folder
       
   175   @param index : handle to modelIndex
       
   176 */
       
   177 void BookmarksTreeView::openItem(const QModelIndex &index)
       
   178 {
       
   179     const BookmarkNode *node = dBookmarksTreeViewPrivate->m_bookmarksModel->node(index);
       
   180        
       
   181     if (!node)
       
   182         {
       
   183         return;
       
   184         }
       
   185 
       
   186     if (node->isBookmark())
       
   187         {
       
   188         emit openUrl(QUrl(node->url));
       
   189         }
       
   190     else if (node->isFolder())
       
   191         {
       
   192         expandNodes(node);
       
   193         }
       
   194 }
       
   195 
       
   196 /*!
       
   197   opens whatever the current selected item is in the tree.
       
   198 */
       
   199 void BookmarksTreeView::openCurrentItem()
       
   200 {
       
   201     openItem(dBookmarksTreeViewPrivate->m_treeView->currentIndex());
       
   202 }
       
   203 
       
   204 /*! 
       
   205   activate the view's resources. Could be connected by client to view visibility
       
   206 */
       
   207 void BookmarksTreeView::activate()
       
   208 {
       
   209     Q_ASSERT(!d->m_isActive);
       
   210     
       
   211     dBookmarksTreeViewPrivate->m_treeView->setModel(bookmarksManager()->bookmarksModel());
       
   212 
       
   213     dBookmarksTreeViewPrivate->m_treeView->setExpanded(dBookmarksTreeViewPrivate->m_bookmarksModel->index(0, 0), true);
       
   214 
       
   215     expandNodes(bookmarksManager()->bookmarks());
       
   216 
       
   217     bookmarksManager()->setBookMarkView(dBookmarksTreeViewPrivate->m_treeView);
       
   218     
       
   219     connect(dBookmarksTreeViewPrivate->m_treeView, SIGNAL(activated(const QModelIndex &)),
       
   220             this, SLOT(openItem(const QModelIndex &)));
       
   221 
       
   222     connect(bookmarksManager(), SIGNAL(bookMarkEntryAdded(BookmarkNode *)),
       
   223                 this, SLOT(entryAdded(BookmarkNode *)));
       
   224 
       
   225     // connect the BookmarksTreeViews signals
       
   226     connect(this, SIGNAL(close()), this, SLOT(deactivate()));
       
   227 
       
   228     connect(dBookmarksTreeViewPrivate->m_treeView, SIGNAL(clicked(const QModelIndex &)),
       
   229             bookmarksManager(), SLOT(updateActions()));
       
   230 
       
   231     dBookmarksTreeViewPrivate->m_treeView->show();
       
   232 
       
   233     BookmarkNode *node = bookmarksManager()->bookMarksRoot();
       
   234     QModelIndex sourceIndex = bookmarksManager()->bookmarksModel()->index(node);
       
   235     
       
   236     //Set the root for treeview
       
   237     dBookmarksTreeViewPrivate->m_treeView->setRootIndex(sourceIndex);
       
   238 
       
   239 
       
   240     d->m_isActive = true;
       
   241     
       
   242     emit activated();
       
   243     
       
   244     dBookmarksTreeViewPrivate->m_treeView->setCurrentIndex ( bookmarksManager()->bookmarksModel()->index(0,0));
       
   245     dBookmarksTreeViewPrivate->m_treeView->setFocus();        
       
   246 }
       
   247     
       
   248 /*!
       
   249   deactivate the view's resources. Could be connected by client to view visibility
       
   250 */
       
   251 void BookmarksTreeView::deactivate()
       
   252 {
       
   253     Q_ASSERT(d->m_isActive);
       
   254 
       
   255     // disconnect signals
       
   256     disconnect(dBookmarksTreeViewPrivate->m_treeView, SIGNAL(activated(const QModelIndex &)),
       
   257             this, SLOT(openItem(const QModelIndex &)));
       
   258 
       
   259     disconnect(dBookmarksTreeViewPrivate->m_treeView, SIGNAL(clicked(const QModelIndex &)),
       
   260                bookmarksManager(), SLOT(updateActions()));
       
   261 
       
   262     disconnect(this, SIGNAL(close()), this, SLOT(deactivate()));
       
   263 
       
   264     bookmarksManager()->setBookMarkView(NULL);
       
   265 
       
   266     dBookmarksTreeViewPrivate->m_treeView->hide();
       
   267 
       
   268 
       
   269     d->m_isActive = false;
       
   270     
       
   271     emit deactivated();
       
   272 }
       
   273 
       
   274 /*!
       
   275   Return the widget handle of this view
       
   276 */
       
   277 QWidget* BookmarksTreeView::qWidget() const
       
   278 {
       
   279     return dBookmarksTreeViewPrivate->m_treeView;
       
   280 }
       
   281 
       
   282 /*! 
       
   283   Return the list of public QActions most relevant to the view's current context
       
   284   (most approptiate for contextual menus, etc.) (empty for now)
       
   285 */
       
   286 QList<QAction*> BookmarksTreeView::getContext()
       
   287 {
       
   288     QList<QAction*> contextList;
       
   289     return contextList;
       
   290 }
       
   291 
       
   292 void BookmarksTreeView::expandNodes(const BookmarkNode *node)
       
   293 {
       
   294     for (int i = 0; i < node->children().count(); ++i) 
       
   295         {
       
   296         BookmarkNode *childNode = node->children()[i];
       
   297         if (childNode->expanded) 
       
   298             {
       
   299             QModelIndex idx = dBookmarksTreeViewPrivate->m_bookmarksModel->index(childNode);
       
   300             dBookmarksTreeViewPrivate->m_treeView->setExpanded(idx, true);
       
   301             expandNodes(childNode);
       
   302             }
       
   303         }
       
   304 }
       
   305 
       
   306 
       
   307 ControllableView* BookmarksTreeView::createNew(QWidget *parent)
       
   308 {
       
   309     return new BookmarksTreeView(BookmarksManager::getSingleton(),parent);
       
   310 }
       
   311 
       
   312 
       
   313 void BookmarksTreeView::entryAdded(BookmarkNode *node)
       
   314  {
       
   315     QModelIndex index = bookmarksManager()->bookmarksModel()->index(node);
       
   316     dBookmarksTreeViewPrivate->m_treeView->setCurrentIndex ( index );
       
   317     
       
   318  }
       
   319 
       
   320  QGraphicsWidget* BookmarksTreeView::widget() const
       
   321 {
       
   322     assert(qWidget());
       
   323     if(!dBookmarksTreeViewPrivate->m_proxyWidget)
       
   324     {
       
   325         dBookmarksTreeViewPrivate->m_proxyWidget = new BookmarkProxyWidget(dBookmarksTreeViewPrivate->m_treeView);
       
   326         dBookmarksTreeViewPrivate->m_proxyWidget->setWidget(qWidget());
       
   327     }
       
   328 
       
   329     return dBookmarksTreeViewPrivate->m_proxyWidget;
       
   330 }
       
   331 
       
   332 BookmarksTreeViewJSObject::BookmarksTreeViewJSObject(BookmarksTreeView* view, QWebFrame* webFrame, const QString& objectName)
       
   333   : BookmarksViewJSObject(view, webFrame, objectName)
       
   334 {
       
   335     connect(view,SIGNAL(activated()),this,SIGNAL(activated()));
       
   336     connect(view,SIGNAL(deactivated()),this,SIGNAL(deactivated()));
       
   337     connect(view,SIGNAL(openUrl(const QUrl &)),this,SIGNAL(openUrl()));
       
   338 }
       
   339 
       
   340 BookmarksTreeViewJSObject::~BookmarksTreeViewJSObject()
       
   341 {
       
   342     BookmarksTreeView *  view =  static_cast<BookmarksTreeView *>(m_contentView);
       
   343     disconnect(view,SIGNAL(activated()),this,SIGNAL(activated()));
       
   344     disconnect(view,SIGNAL(deactivated()),this,SIGNAL(deactivated()));
       
   345     disconnect(view,SIGNAL(openUrl(const QUrl &)),this,SIGNAL(openUrl()));
       
   346 }
       
   347 
       
   348 
       
   349 } // namespace WRT
       
   350 
       
   351 
       
   352 
       
   353 
       
   354