homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsmenuviewbuilder.cpp
changeset 35 f9ce957a272c
child 39 4e8ebe173323
equal deleted inserted replaced
5:c743ef5928ba 35:f9ce957a272c
       
     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 <QGraphicsWidget>
       
    20 #include <HbAction>
       
    21 #include <HbGroupBox>
       
    22 #include <HbListView>
       
    23 #include <HbLineEdit>
       
    24 #include <HbSearchPanel>
       
    25 #include <HbToolBar>
       
    26 #include <HbView>
       
    27 #include <HbWidget>
       
    28 
       
    29 #include "hsmenuviewbuilder.h"
       
    30 #include "hsmenustates_global.h"
       
    31 
       
    32 /*!
       
    33     \class HsMenuViewBuilder
       
    34     \ingroup group_hsmenustateplugin
       
    35 
       
    36     \brief Menu View Builder
       
    37 
       
    38     Reads UI object from Application Library docml
       
    39 
       
    40     \sa HsMenuView
       
    41 */
       
    42 
       
    43 /*!
       
    44   Sets view label visible or hidden.
       
    45   \param visible When true label is set visible, invisible otherwise.
       
    46   \return \a true on success, \a false otherwise.
       
    47  */
       
    48 bool HsMenuViewBuilder::setLabelVisible(bool visible)
       
    49 {
       
    50     bool result(true);
       
    51 
       
    52     if (visible != mViewOptions.testFlag(Label)) {
       
    53         mViewOptions ^= ViewOptions(Label);
       
    54         result = parseSection(mSections[mViewOptions]);
       
    55     }
       
    56     return result;
       
    57 }
       
    58 
       
    59 
       
    60 /*!
       
    61   Sets search panel visible or hidden.
       
    62   \param visible When true search panel is set visible,
       
    63   invisible otherwise.
       
    64   \return \a true on success, \a false otherwise.
       
    65  */
       
    66 bool HsMenuViewBuilder::setSearchPanelVisible(bool visible)
       
    67 {
       
    68     bool result(true);
       
    69 
       
    70     if (visible != mViewOptions.testFlag(Search)) {
       
    71         mViewOptions ^= ViewOptions(Search);
       
    72         result = parseSection(mSections[mViewOptions]);
       
    73 
       
    74         if (visible) {
       
    75             HbLineEdit *const lineEdit(searchPanelLineEdit());
       
    76 
       
    77             lineEdit->setText("");
       
    78         }
       
    79     }
       
    80 
       
    81     return result;
       
    82 }
       
    83 
       
    84 /*!
       
    85  \return \a true when label is visible, \a false otherwise.
       
    86  */
       
    87 bool HsMenuViewBuilder::isLabelVisible() const
       
    88 {
       
    89     return mViewOptions.testFlag(Label);
       
    90 }
       
    91 
       
    92 /*!
       
    93  \return \a true when search panel is visible, \a false otherwise.
       
    94  */
       
    95 bool HsMenuViewBuilder::isSearchPanelVisible() const
       
    96 {
       
    97     return mViewOptions.testFlag(Search);
       
    98 }
       
    99 
       
   100 /*!
       
   101  \return pointer to the main view.
       
   102  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   103  Memory ownership is not changed.
       
   104  */
       
   105 HbView *HsMenuViewBuilder::view() const
       
   106 {
       
   107     return qobject_cast<HbView *>(mDocumentLoader.findWidget(VIEW_NAME));
       
   108 }
       
   109 
       
   110 /*!
       
   111  \return pointer to the list item view.
       
   112  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   113  Memory ownership is not changed.
       
   114  */
       
   115 HbListView *HsMenuViewBuilder::listView() const
       
   116 {
       
   117     return qobject_cast<HbListView *>(
       
   118                mDocumentLoader.findWidget(LIST_VIEW_NAME));
       
   119 }
       
   120 
       
   121 /*!
       
   122  \return pointer to the label.
       
   123  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   124  Memory ownership is not changed.
       
   125  */
       
   126 HbGroupBox *HsMenuViewBuilder::label() const
       
   127 {
       
   128     return qobject_cast<HbGroupBox *>(
       
   129                mDocumentLoader.findWidget(LABEL_NAME));
       
   130 }
       
   131 
       
   132 /*!
       
   133  \return pointer to the searchPanel.
       
   134  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   135  Memory ownership is not changed.
       
   136  */
       
   137 HbSearchPanel *HsMenuViewBuilder::searchPanel() const
       
   138 {
       
   139     return qobject_cast<HbSearchPanel *>(mDocumentLoader.findWidget(
       
   140             SEARCH_PANEL_NAME));
       
   141 }
       
   142 
       
   143 /*!
       
   144  \return pointer to All Applications Action.
       
   145  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   146  Memory ownership is not changed.
       
   147  */
       
   148 HbAction *HsMenuViewBuilder::allAppsAction() const
       
   149 {
       
   150     return qobject_cast<HbAction *>(mDocumentLoader.findObject(
       
   151                                         ALL_APPS_ACTION_NAME));
       
   152 }
       
   153 
       
   154 /*!
       
   155  \return pointer to All Collections Action.
       
   156  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   157  Memory ownership is not changed.
       
   158  */
       
   159 HbAction *HsMenuViewBuilder::allCollectionsAction() const
       
   160 {
       
   161     return qobject_cast<HbAction *>(mDocumentLoader.findObject(
       
   162                                         ALL_COLLECTIONS_ACTION_NAME));
       
   163 }
       
   164 
       
   165 /*!
       
   166  \return pointer to Search Action.
       
   167  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   168  Memory ownership is not changed.
       
   169  */
       
   170 HbAction *HsMenuViewBuilder::searchAction() const
       
   171 {
       
   172     return qobject_cast<HbAction *>(mDocumentLoader.findObject(
       
   173                                         SEARCH_ACTION_NAME));
       
   174 }
       
   175 
       
   176 /*!
       
   177  \return pointer to Ovi Store Action.
       
   178  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   179  Memory ownership is not changed.
       
   180  */
       
   181 HbAction *HsMenuViewBuilder::oviStoreAction() const
       
   182 {
       
   183     return qobject_cast<HbAction *>(mDocumentLoader.findObject(
       
   184                                         OVI_STORE_ACTION_NAME));
       
   185 }
       
   186 
       
   187 /*!
       
   188  \return pointer to the main view toolbar.
       
   189  The pointer is valid until the HsMenuViewBuilder instance is destroyed.
       
   190  Memory ownership is not changed.
       
   191  */
       
   192 HbToolBar *HsMenuViewBuilder::toolBar() const
       
   193 {
       
   194     return qobject_cast<HbToolBar *>(
       
   195                mDocumentLoader.findWidget(TOOL_BAR_NAME));
       
   196 }
       
   197 
       
   198 /*!
       
   199  Constructor.
       
   200  Makes object be initialized with with hidden but existing
       
   201  search panel and view label.
       
   202  */
       
   203 HsMenuViewBuilder::HsMenuViewBuilder():
       
   204     mViewOptions(0),
       
   205     DOCUMENT_FILE_NAME(":/xml/applibrary.docml"),
       
   206     ALL_APPS_ACTION_NAME("allAppsAction"),
       
   207     ALL_COLLECTIONS_ACTION_NAME("allCollectionsAction"),
       
   208     SEARCH_ACTION_NAME("searchAction"),
       
   209     OVI_STORE_ACTION_NAME("oviStoreAction"),
       
   210     VIEW_NAME("view"),
       
   211     CONTAINER_NAME("container"),
       
   212     LIST_VIEW_NAME("listView"),
       
   213     TOOL_BAR_NAME("toolBar"),
       
   214     SEARCH_PANEL_NAME("searchPanel"),
       
   215     LABEL_NAME("label"),
       
   216     LIST_VIEW_SECTION_NAME("list_view"),
       
   217     LIST_LABELED_VIEW_SECTION_NAME("list_labeled_view"),
       
   218     LIST_SEARCH_VIEW_SECTION_NAME("list_search_view"),
       
   219     LIST_SEARCH_LABELED_VIEW_SECTION_NAME("list_search_labeled_view")
       
   220 {
       
   221     buildSectionKeyMap();
       
   222 
       
   223     // parse common section and the one specified by view options
       
   224     bool result = parseSection("") && parseSection(mSections[mViewOptions]);
       
   225 
       
   226     Q_ASSERT_X(result,
       
   227                "HsMenuViewBuilder::HsMenuViewBuilder()",
       
   228                "construction failed");
       
   229 }
       
   230 
       
   231 /*!
       
   232  Destructor.
       
   233  Deletes list view, search panel, view label, All Applications
       
   234  Action, All Collections Action, Search Action, Ovi Store Action, container
       
   235  widget, main view toolbar and the main view.
       
   236 
       
   237  The destructor needs to be checked agains any change in related docml file.
       
   238  */
       
   239 HsMenuViewBuilder::~HsMenuViewBuilder()
       
   240 {
       
   241     QObjectList objectList;
       
   242 
       
   243     objectList << listView();
       
   244 
       
   245     objectList << searchPanel();
       
   246 
       
   247     objectList << label();
       
   248 
       
   249     objectList << allAppsAction();
       
   250     objectList << allCollectionsAction();
       
   251     objectList << searchAction();
       
   252     objectList << mDocumentLoader.findWidget(CONTAINER_NAME);
       
   253     objectList << toolBar();
       
   254     objectList << view();
       
   255 
       
   256     foreach(QObject *obj, objectList) {
       
   257         obj->deleteLater();
       
   258     }
       
   259 }
       
   260 
       
   261 /*!
       
   262  Parses requested docml file section and reflects its contents in the
       
   263  object state.
       
   264  \param sectionName Name of the section to parse.
       
   265  \retval \a true on success, \a false otherwise.
       
   266  */
       
   267 bool HsMenuViewBuilder::parseSection(const QString &sectionName)
       
   268 {
       
   269     HSMENUTEST_FUNC_ENTRY("HsMenuViewBuilder::parseSection");
       
   270 
       
   271     bool loadStatusOk = false;
       
   272 
       
   273     QObjectList loadedObjects = mDocumentLoader.load(DOCUMENT_FILE_NAME,
       
   274                                 sectionName,
       
   275                                 &loadStatusOk);
       
   276 
       
   277     Q_ASSERT_X(loadStatusOk,
       
   278                DOCUMENT_FILE_NAME.toLatin1().data(),
       
   279                "Error while loading docml file.");
       
   280     HSMENUTEST_FUNC_EXIT("HsMenuViewBuilder::parseSection");
       
   281 
       
   282     return loadStatusOk;
       
   283 }
       
   284 
       
   285 /*!
       
   286  \retval Line edit of the searchPanel on success, 0 otherwise.
       
   287  */
       
   288 HbLineEdit *HsMenuViewBuilder::searchPanelLineEdit() const
       
   289 {
       
   290     HSMENUTEST_FUNC_ENTRY("HsMenuViewBuilder::searchPanelLineEdit");
       
   291 
       
   292     HbLineEdit *result(0);
       
   293 
       
   294     foreach(QGraphicsItem *obj, searchPanel()->childItems()) {
       
   295 
       
   296         QGraphicsWidget *const widget =
       
   297             static_cast<QGraphicsWidget *>(obj);
       
   298 
       
   299         if (widget != NULL) {
       
   300 
       
   301             HbLineEdit *const lineEdit = qobject_cast<HbLineEdit *>(widget);
       
   302 
       
   303             if (lineEdit != NULL) {
       
   304                 result = lineEdit;
       
   305                 break;
       
   306             }
       
   307         }
       
   308     }
       
   309     HSMENUTEST_FUNC_EXIT("HsMenuViewBuilder::searchPanelLineEdit");
       
   310 
       
   311     return result;
       
   312 }
       
   313 
       
   314 /*!
       
   315     Sets up mapping between ViewOptions and section names.
       
   316  */
       
   317 void HsMenuViewBuilder::buildSectionKeyMap()
       
   318 {
       
   319     mSections[ViewOptions()]  =
       
   320         LIST_VIEW_SECTION_NAME;
       
   321 
       
   322     mSections[ViewOptions(Search)]   =
       
   323         LIST_SEARCH_VIEW_SECTION_NAME;
       
   324 
       
   325     mSections[ViewOptions(Label)]  =
       
   326         LIST_LABELED_VIEW_SECTION_NAME;
       
   327 
       
   328     mSections[ViewOptions(Label | Search)]   =
       
   329         LIST_SEARCH_LABELED_VIEW_SECTION_NAME;
       
   330 }