homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsmenuviewbuilder.cpp
branchRCL_3
changeset 82 5f0182e07bfb
equal deleted inserted replaced
79:f00a6757af32 82:5f0182e07bfb
       
     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: Menu View Builder
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <QActionGroup>
       
    20 #include <QGraphicsWidget>
       
    21 #include <HbAction>
       
    22 #include <HbGroupBox>
       
    23 #include <HbListView>
       
    24 #include <HbPushButton>
       
    25 #include <HbToolBar>
       
    26 #include <HbView>
       
    27 #include <HbWidget>
       
    28 #include <HbStaticVkbHost>
       
    29 #include <HbMainWindow>
       
    30 #include <HbInputMethod>
       
    31 #include <HbToolBarExtension>
       
    32 
       
    33 #include "hsmenuviewbuilder.h"
       
    34 #include "hsmenustates_global.h"
       
    35 
       
    36 static const char* DOCUMENT_BASE_NAME_MAP
       
    37         [InvalidStateContext][InvalidOperationalContext] =
       
    38     /*HsItemViewContext,    HsButtonContext,    HsEmptyLabelContext*/
       
    39 {
       
    40 /*HsAllAppsContext*/        {"listview", "listview", "listview"},
       
    41 /*HsAllCollectionsContext*/ {"listview", "listview", "listview"},
       
    42 /*HsInstalledAppsContext*/  {"labeledlistview", "labeledlistview",
       
    43         "emptylabeledview"},
       
    44 /*HsCollectionContext*/     {"labeledlistview", "addcontentlabeledview",
       
    45         "emptylabeledview"}
       
    46 };
       
    47 
       
    48 static const QString DOCUMENT_NAME_PREFIX(QLatin1String(":/xml/"));
       
    49 static const QString DOCUMENT_NAME_EXT(QLatin1String(".docml"));
       
    50 static const QString COMMON_OBJECTS_DOCUMENT_BASE_NAME(
       
    51         QLatin1String("common_objects"));
       
    52 static const QString VIEW_NAME(QLatin1String("view"));
       
    53 static const QString LIST_VIEW_NAME(QLatin1String("listView"));
       
    54 static const QString VIEW_LABEL_NAME(QLatin1String("label"));
       
    55 static const QString ADD_CONTENT_BUTTON_NAME(QLatin1String("addContentButton"));
       
    56 
       
    57 /*!
       
    58     \class HsMenuViewBuilder
       
    59     \ingroup group_hsmenustateplugin
       
    60 
       
    61     \brief Menu View Builder
       
    62 
       
    63     Reads UI object from Application Library docml
       
    64 
       
    65     \sa HsMenuView
       
    66 */
       
    67 
       
    68 
       
    69 
       
    70 
       
    71 /*!
       
    72  \return Pointer to the view resulting from last \a build call or NULL if
       
    73  the \a build has not yet been called.
       
    74  Memory ownership is not changed.
       
    75  */
       
    76 HbView *HsMenuViewBuilder::currentView()
       
    77 {
       
    78     HbView *const view =
       
    79         qobject_cast<HbView *>(currentLoader()->findWidget(VIEW_NAME));
       
    80 
       
    81     if (view != NULL && mStateContext != HsInstalledAppsContext) {
       
    82         view->setToolBar(mToolBar);
       
    83     }
       
    84     return view;
       
    85 }
       
    86 
       
    87 /*!
       
    88  \return Pointer to list view resulting from last \a build call or NULL if
       
    89  the \a build has not yet been called.
       
    90  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
    91  Memory ownership is not changed.
       
    92  */
       
    93 HbListView *HsMenuViewBuilder::currentListView()
       
    94 {
       
    95     HbListView *const listView =
       
    96         qobject_cast<HbListView *>(
       
    97                 currentLoader()->findWidget(LIST_VIEW_NAME));
       
    98 
       
    99     return listView;
       
   100 }
       
   101 
       
   102 /*!
       
   103  \return Pointer to the view label resulting from last \a build call. It is
       
   104  guaranteed to be not NULL if the \a build was called for the context
       
   105  related to view including label.
       
   106  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   107  Memory ownership is not changed.
       
   108  */
       
   109 HbGroupBox *HsMenuViewBuilder::currentViewLabel()
       
   110 {
       
   111     HbGroupBox *viewLabel =
       
   112             qobject_cast<HbGroupBox *>(
       
   113                     currentLoader()->findWidget(VIEW_LABEL_NAME));
       
   114 
       
   115     return viewLabel;
       
   116 }
       
   117 
       
   118 /*!
       
   119  \return Pointer to the 'Add content' button resulting from last
       
   120  \a build call. It is
       
   121  guaranteed to be not NULL if the \a build was called for the context
       
   122  related to view including label.
       
   123  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   124  Memory ownership is not changed.
       
   125  */
       
   126 HbPushButton *HsMenuViewBuilder::currentAddContentButton()
       
   127 {
       
   128     HbPushButton *pushButton =
       
   129             qobject_cast<HbPushButton *>(currentLoader()->findWidget(
       
   130                         ADD_CONTENT_BUTTON_NAME));
       
   131     return pushButton;
       
   132 }
       
   133 
       
   134 /*!
       
   135  \return Pointer to All Applications Action. Guaranteed to be not NULL.
       
   136  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   137  Memory ownership is not changed.
       
   138  */
       
   139 HbAction *HsMenuViewBuilder::allAppsAction() const
       
   140 {
       
   141     return qobject_cast<HbAction *>(mCommonObjectsLoader->findObject(
       
   142                                         ALL_APPS_ACTION_NAME));
       
   143 }
       
   144 
       
   145 /*!
       
   146  \return Pointer to All Collections Action. Guaranteed to be not NULL.
       
   147  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   148  Memory ownership is not changed.
       
   149  */
       
   150 HbAction *HsMenuViewBuilder::allCollectionsAction() const
       
   151 {
       
   152     return qobject_cast<HbAction *>(mCommonObjectsLoader->findObject(
       
   153                                         ALL_COLLECTIONS_ACTION_NAME));
       
   154 }
       
   155 
       
   156 /*!
       
   157  \return Pointer to Search Action. Guaranteed to be not NULL.
       
   158  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   159  Memory ownership is not changed.
       
   160  */
       
   161 HbAction *HsMenuViewBuilder::searchAction() const
       
   162 {
       
   163     return qobject_cast<HbAction *>(mCommonObjectsLoader->findObject(
       
   164                                         SEARCH_ACTION_NAME));
       
   165 }
       
   166 
       
   167 /*!
       
   168  \return Pointer to Ovi Store Action. Guaranteed to be not NULL.
       
   169  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   170  Memory ownership is not changed.
       
   171  */
       
   172 HbAction *HsMenuViewBuilder::oviStoreAction() const
       
   173 {
       
   174     return qobject_cast<HbAction *>(mCommonObjectsLoader->findObject(
       
   175                                         OVI_STORE_ACTION_NAME));
       
   176 }
       
   177 
       
   178 /*!
       
   179  \return Pointer to Operator Action. Guaranteed to be not NULL.
       
   180  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   181  Memory ownership is not changed.
       
   182  */
       
   183 HbAction *HsMenuViewBuilder::operatorAction() const
       
   184 {
       
   185     return qobject_cast<HbAction *>(mCommonObjectsLoader->findObject(
       
   186                                         OPERATOR_ACTION_NAME));
       
   187 }
       
   188 
       
   189 /*!
       
   190  \return Pointer to the main view toolbar.
       
   191  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   192  Memory ownership is not changed.
       
   193  */
       
   194 HbToolBar *HsMenuViewBuilder::toolBar() const
       
   195 {
       
   196     return mToolBar;
       
   197 }
       
   198 
       
   199 /*!
       
   200  \return Pointer to the toolbar extension.
       
   201  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   202  Memory ownership is not changed.
       
   203  */
       
   204 HbToolBarExtension *HsMenuViewBuilder::toolBarExtension() const
       
   205 {
       
   206     return mToolBarExtension;
       
   207 }
       
   208 
       
   209 /*!
       
   210  \return Action group for \a allAppsState and \a allCollectionsState action.
       
   211  */
       
   212 QActionGroup *HsMenuViewBuilder::toolBarActionGroup() const
       
   213 {
       
   214     return mToolBarActionGroup;
       
   215 }
       
   216 
       
   217 /*!
       
   218  Reads docml configuration corresponding to current context.
       
   219  \return Shared pointer to loader serving current context.
       
   220  */
       
   221 QSharedPointer<HbDocumentLoader> HsMenuViewBuilder::currentLoader()
       
   222 {
       
   223     if (!mLoaderMap.contains(context())) {
       
   224         QSharedPointer<HbDocumentLoader> loader =
       
   225                 readContextConfiguration();
       
   226         mLoaderMap.insert(context(), loader);
       
   227     }
       
   228 
       
   229     return mLoaderMap[context()];
       
   230 }
       
   231 
       
   232 
       
   233 /*!
       
   234   Loads non-context sensitive objects.
       
   235  */
       
   236 HsMenuViewBuilder::HsMenuViewBuilder():
       
   237     ALL_APPS_ACTION_NAME("allAppsAction"),
       
   238     ALL_COLLECTIONS_ACTION_NAME("allCollectionsAction"),
       
   239     SEARCH_ACTION_NAME("searchAction"),
       
   240     OVI_STORE_ACTION_NAME("oviStoreAction"),
       
   241     OPERATOR_ACTION_NAME("operatorAction"),
       
   242     mToolBar(new HbToolBar),
       
   243     mToolBarExtension(new HbToolBarExtension),
       
   244     mStateContext(HsAllAppsContext),
       
   245     mOperationalContext(HsItemViewContext)
       
   246 {
       
   247     mCommonObjectsLoader = parseDocument(COMMON_OBJECTS_DOCUMENT_BASE_NAME);
       
   248 
       
   249     mToolBar->addAction(allAppsAction());
       
   250     mToolBar->addAction(allCollectionsAction());
       
   251     mToolBar->addAction(searchAction());
       
   252 
       
   253     mToolBarActionGroup = new QActionGroup(allAppsAction());
       
   254 
       
   255     allAppsAction()->setActionGroup(mToolBarActionGroup);
       
   256     allCollectionsAction()->setActionGroup(mToolBarActionGroup);
       
   257 }
       
   258 
       
   259 /*!
       
   260  Deletes loaded objects.
       
   261  */
       
   262 HsMenuViewBuilder::~HsMenuViewBuilder()
       
   263 {
       
   264     delete mToolBarExtension;
       
   265     delete mToolBar;
       
   266     qDeleteAll(mLoadedObjects);
       
   267 }
       
   268 
       
   269 /*!
       
   270  Parses requested docml file section and reflects its contents in the
       
   271  object state.
       
   272  \param documentName Identifies document to parse.
       
   273  \return Document loader instance containing parsed
       
   274     objects for requested \documentName.
       
   275  */
       
   276 QSharedPointer<HbDocumentLoader> HsMenuViewBuilder::parseDocument(
       
   277         const QString &documentBaseName)
       
   278 {
       
   279     HSMENUTEST_FUNC_ENTRY("HsMenuViewBuilder::parseDocument");
       
   280 
       
   281     bool loadStatusOk = false;
       
   282 
       
   283     QSharedPointer<HbDocumentLoader> loader(new HbDocumentLoader());
       
   284 
       
   285     const QString documentFullName = DOCUMENT_NAME_PREFIX +
       
   286                                      documentBaseName +
       
   287                                      DOCUMENT_NAME_EXT;
       
   288     const QObjectList loadedObjects =
       
   289             loader->load(documentFullName, &loadStatusOk);
       
   290 
       
   291     mLoadedObjects |= loadedObjects.toSet();
       
   292 
       
   293     Q_ASSERT_X(loadStatusOk,
       
   294                documentFullName.toLatin1().data(),
       
   295                "Error while loading docml file.");
       
   296 
       
   297     HSMENUTEST_FUNC_EXIT("HsMenuViewBuilder::parseDocument");
       
   298 
       
   299     return loader;
       
   300 }
       
   301 
       
   302 /*!
       
   303  Sets view context. Not reflected in widgets returned by the builder
       
   304  until \a build is not run.
       
   305  */
       
   306 void HsMenuViewBuilder::setStateContext(HsStateContext stateContext)
       
   307 {
       
   308     mStateContext = stateContext;
       
   309 }
       
   310 
       
   311 /*!
       
   312  Sets operational context. Not reflected in widgets returned by the builder
       
   313  until \a build is not run.
       
   314  */
       
   315 void HsMenuViewBuilder::setOperationalContext(
       
   316     HsOperationalContext operationalContext)
       
   317 {
       
   318     mOperationalContext = operationalContext;
       
   319 }
       
   320 
       
   321 /*!
       
   322  Reads configuration for requested context and ensures search panel and corresponding
       
   323  vkb host are managed properly.
       
   324  \return Document loader with objects for current context.
       
   325  */
       
   326 QSharedPointer<HbDocumentLoader> HsMenuViewBuilder::readContextConfiguration()
       
   327 {
       
   328     const QLatin1String documentName(
       
   329             DOCUMENT_BASE_NAME_MAP[mStateContext][mOperationalContext]);
       
   330 
       
   331     QSharedPointer<HbDocumentLoader> loader =
       
   332             parseDocument(QString(documentName));
       
   333 
       
   334     return loader;
       
   335 }
       
   336 
       
   337 /*!
       
   338  \return Current context.
       
   339  */
       
   340 HsMenuViewBuilder::Context HsMenuViewBuilder::context() const
       
   341 {
       
   342     return Context(mStateContext, mOperationalContext);
       
   343 }