homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsallcollectionsstate.cpp
changeset 35 f9ce957a272c
child 36 cdae8c6c3876
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 All Collections state.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <qstatemachine.h>
       
    19 #include <hbview.h>
       
    20 #include <hbmainwindow.h>
       
    21 #include <hbmenu.h>
       
    22 #include <hbaction.h>
       
    23 #include <hbabstractviewitem.h>
       
    24 #include <hsmenueventfactory.h>
       
    25 #include <hbinstance.h>
       
    26 #include <hbsearchpanel.h>
       
    27 
       
    28 #include "hsmenuview.h"
       
    29 #include "hsmenuitemmodel.h"
       
    30 #include "cadefs.h"
       
    31 #include "hsallcollectionsstate.h"
       
    32 #include "hsaddappstocollectionstate.h"
       
    33 #include "hsmenumodetransition.h"
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 HsAllCollectionsState::HsAllCollectionsState(
       
    39     HsMenuView &menuView, QState *parent) :
       
    40     QState(parent), mSortAttribute(CustomHsSortAttribute),
       
    41     mMenuView(menuView), mAllCollectionsModel(0)
       
    42 {
       
    43     construct();
       
    44 }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 void HsAllCollectionsState::construct()
       
    50 {
       
    51     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::construct");
       
    52 
       
    53     QState *initialState = new QState(this);
       
    54     setInitialState(initialState);
       
    55 
       
    56     QState *addModeState = new QState(this);
       
    57     connect(addModeState, SIGNAL(entered()),SLOT(addModeEntered()));
       
    58 
       
    59     QState *normalModeState = new QState(this);
       
    60     connect(normalModeState, SIGNAL(entered()),SLOT(normalModeEntered()));
       
    61     connect(normalModeState, SIGNAL(exited()),SLOT(normalModeExited()));
       
    62 
       
    63     initialState->addTransition(new HsMenuModeTransition(
       
    64                                     mMenuView, NormalHsMenuMode, normalModeState));
       
    65     initialState->addTransition(new HsMenuModeTransition(
       
    66                                     mMenuView, AddHsMenuMode, addModeState));
       
    67 
       
    68     setObjectName(this->parent()->objectName() + "/allcollectionsstate");
       
    69     connect(this, SIGNAL(entered()),SLOT(stateEntered()));
       
    70     connect(this, SIGNAL(exited()),SLOT(stateExited()));
       
    71     mAllCollectionsModel = HsMenuService::getAllCollectionsModel(
       
    72                                mSortAttribute);
       
    73     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::construct");
       
    74 }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void HsAllCollectionsState::setMenuOptions()
       
    80 {
       
    81     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::setMenuOptions");
       
    82     HbMenu *const options = new HbMenu();
       
    83     options->addAction(hbTrId("txt_applib_opt_new_collection"),
       
    84                        this, SLOT(createNewCollection()));
       
    85 
       
    86     HbMenu *const sortMenu = options->addMenu(
       
    87                                  hbTrId("txt_applib_opt_sort_by"));
       
    88 
       
    89     //Grouped options are exclusive by default.
       
    90     QActionGroup *sortGroup = new QActionGroup(options);
       
    91     sortGroup->addAction(sortMenu->addAction(
       
    92                              hbTrId("txt_applib_opt_sub_custom"),
       
    93                              this, SLOT(customMenuAction())));
       
    94     sortGroup->addAction(sortMenu->addAction(
       
    95                              hbTrId("txt_applib_opt_sub_ascending"),
       
    96                              this, SLOT(ascendingMenuAction())));
       
    97     sortGroup->addAction(sortMenu->addAction(
       
    98                              hbTrId("txt_applib_opt_sub_descending"),
       
    99                              this, SLOT(descendingMenuAction())));
       
   100 
       
   101     if (mSortAttribute == CustomHsSortAttribute) {
       
   102         options->addAction(hbTrId("txt_applib_opt_arrange"),
       
   103                            this, SLOT(createArrangeCollection()));
       
   104     }
       
   105     options->setParent(this);
       
   106 
       
   107     foreach(QAction *action, sortMenu->actions()) {
       
   108         action->setCheckable(true);
       
   109     }
       
   110 
       
   111     switch (mSortAttribute) {
       
   112     case AscendingNameHsSortAttribute:
       
   113         sortGroup->actions().at(1)->setChecked(true);
       
   114         break;
       
   115     case DescendingNameHsSortAttribute:
       
   116         sortGroup->actions().at(2)->setChecked(true);
       
   117         break;
       
   118     case CustomHsSortAttribute:
       
   119     default:
       
   120         sortGroup->actions().at(0)->setChecked(true);
       
   121         break;
       
   122     }
       
   123 
       
   124     mMenuView.view()->setMenu(options);
       
   125 
       
   126 
       
   127     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::setMenuOptions");
       
   128 }
       
   129 // ---------------------------------------------------------------------------
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 HsAllCollectionsState::~HsAllCollectionsState()
       
   133 {
       
   134     delete mAllCollectionsModel;
       
   135 }
       
   136 
       
   137 // ---------------------------------------------------------------------------
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 void HsAllCollectionsState::scrollToBeginning()
       
   141 {
       
   142     mBookmark = mAllCollectionsModel->index(0);
       
   143 }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void HsAllCollectionsState::stateEntered()
       
   149 {
       
   150     qDebug("AllCollectionsState::stateEntered()");
       
   151     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::stateEntered");
       
   152 
       
   153     mMenuView.setModel(mAllCollectionsModel);
       
   154     mMenuView.scrollTo(mBookmark, HbAbstractItemView::PositionAtTop);
       
   155 
       
   156     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::stateEntered");
       
   157 }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 void HsAllCollectionsState::normalModeEntered()
       
   163 {
       
   164     setMenuOptions();
       
   165 
       
   166     connect(&mMenuView, SIGNAL(activated(QModelIndex)),
       
   167             SLOT(listItemActivated(QModelIndex)));
       
   168     connect(&mMenuView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   169             SLOT(listItemLongPressed(HbAbstractViewItem *, QPointF)));
       
   170 }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void HsAllCollectionsState::normalModeExited()
       
   176 {
       
   177     mMenuView.view()->setMenu(NULL);
       
   178 }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void HsAllCollectionsState::addModeEntered()
       
   184 {
       
   185     connect(&mMenuView, SIGNAL(activated(QModelIndex)),
       
   186             SLOT(addActivated(QModelIndex)));
       
   187     connect(&mMenuView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   188             SLOT(addLongPressed(HbAbstractViewItem *, QPointF)));
       
   189 }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void HsAllCollectionsState::stateExited()
       
   195 {
       
   196     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::stateExited");
       
   197 
       
   198     mMenuView.disconnect(this);
       
   199 
       
   200     mBookmark = mMenuView.firstVisibleItemIndex();
       
   201 
       
   202     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::stateExited");
       
   203     qDebug("AllCollectionsState::stateExited()");
       
   204 }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 void HsAllCollectionsState::listItemActivated(const QModelIndex &index)
       
   210 {
       
   211     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::listItemActivated");
       
   212     QVariant data = mAllCollectionsModel->data(index, CaItemModel::IdRole);
       
   213     int id = data.toInt();
       
   214     QString collectionType =
       
   215         mAllCollectionsModel->data(index, CaItemModel::TypeRole).toString();
       
   216     qDebug("AllCollectionsState::listItemActivated - MCS ID: %d",
       
   217            data.toInt());
       
   218 
       
   219     mMenuView.setSearchPanelVisible(false);
       
   220 
       
   221     machine()->postEvent(HsMenuEventFactory::createOpenCollectionEvent(id,
       
   222                          collectionType));
       
   223     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::listItemActivated");
       
   224 }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void HsAllCollectionsState::addActivated(const QModelIndex &index)
       
   230 {
       
   231     mMenuView.setSearchPanelVisible(false);
       
   232     const int itemId = index.data(CaItemModel::IdRole).toInt();
       
   233     machine()->postEvent(
       
   234         HsMenuEventFactory::createAddToHomeScreenEvent(itemId));
       
   235     machine()->postEvent(
       
   236         HsMenuEventFactory::createOpenHomeScreenEvent());
       
   237 }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void HsAllCollectionsState::addLongPressed(HbAbstractViewItem *item,
       
   243         const QPointF &coords)
       
   244 {
       
   245     Q_UNUSED(coords);
       
   246     mMenuView.setSearchPanelVisible(false);
       
   247     const int itemId = item->modelIndex().data(CaItemModel::IdRole).toInt();
       
   248     machine()->postEvent(
       
   249         HsMenuEventFactory::createAddToHomeScreenEvent(itemId));
       
   250     machine()->postEvent(
       
   251         HsMenuEventFactory::createOpenHomeScreenEvent());
       
   252 }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void HsAllCollectionsState::listItemLongPressed(HbAbstractViewItem *item,
       
   258         const QPointF &coords)
       
   259 {
       
   260     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::listItemLongPressed");
       
   261 
       
   262     mMenuView.setSearchPanelVisible(false);
       
   263 
       
   264     const int itemId = item->modelIndex().data(CaItemModel::IdRole).toInt();
       
   265     HbMenu *menu = new HbMenu();
       
   266 
       
   267     // create context menu
       
   268     HbAction *addShortcutAction = menu->addAction(hbTrId(
       
   269                                       "txt_applib_menu_add_to_home_screen"));
       
   270     HbAction *renameAction = NULL;
       
   271     HbAction *deleteAction = NULL;
       
   272 
       
   273     EntryFlags flags = item->modelIndex().data(CaItemModel::FlagsRole).value<
       
   274                        EntryFlags> ();
       
   275 
       
   276     if ((flags & RemovableEntryFlag)) {
       
   277         renameAction = menu->addAction(
       
   278                            hbTrId("txt_common_menu_rename_item"));
       
   279         deleteAction = menu->addAction(hbTrId("txt_common_menu_delete"));
       
   280     }
       
   281 
       
   282     // choose proper action
       
   283     if (HbAction *selectedAction = menu->exec(coords)) {
       
   284         if (selectedAction == addShortcutAction) {
       
   285             machine()->postEvent(
       
   286                 HsMenuEventFactory::createAddToHomeScreenEvent(itemId));
       
   287         } else if (renameAction && selectedAction == renameAction) {
       
   288             machine()->postEvent(
       
   289                 HsMenuEventFactory::createRenameCollectionEvent(itemId));
       
   290         } else if (deleteAction && selectedAction == deleteAction) {
       
   291             machine()->postEvent(
       
   292                 HsMenuEventFactory::createDeleteCollectionEvent(itemId));
       
   293         }
       
   294     }
       
   295     delete menu;
       
   296     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::listItemLongPressed");
       
   297 }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 void HsAllCollectionsState::createNewCollection()
       
   303 {
       
   304     // Adding a new collection via the Collections view
       
   305     machine()->postEvent(HsMenuEventFactory::createNewCollectionEvent());
       
   306 }
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // ---------------------------------------------------------------------------
       
   310 //
       
   311 void HsAllCollectionsState::createArrangeCollection()
       
   312 {
       
   313     // Arrange collection via the Arrange view
       
   314     QModelIndex idx = mMenuView.firstVisibleItemIndex();
       
   315     int topItemId = idx.data(CaItemModel::IdRole).toInt();
       
   316     machine()->postEvent(
       
   317         HsMenuEventFactory::createArrangeCollectionEvent(topItemId));
       
   318 }
       
   319 
       
   320 // ---------------------------------------------------------------------------
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void HsAllCollectionsState::customMenuAction()
       
   324 {
       
   325     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::customMenuAction");
       
   326     mSortAttribute = CustomHsSortAttribute;
       
   327     setMenuOptions();
       
   328     mAllCollectionsModel->setSort(mSortAttribute);
       
   329     emit sortOrderChanged(mSortAttribute);
       
   330     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::customMenuAction");
       
   331 }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void HsAllCollectionsState::ascendingMenuAction()
       
   337 {
       
   338     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::ascendingMenuAction");
       
   339     mSortAttribute = AscendingNameHsSortAttribute;
       
   340     setMenuOptions();
       
   341     mAllCollectionsModel->setSort(mSortAttribute);
       
   342     emit sortOrderChanged(mSortAttribute);
       
   343     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::ascendingMenuAction");
       
   344 }
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // ---------------------------------------------------------------------------
       
   348 //
       
   349 void HsAllCollectionsState::descendingMenuAction()
       
   350 {
       
   351     HSMENUTEST_FUNC_ENTRY("HsAllCollectionsState::descendingMenuAction");
       
   352     mSortAttribute = DescendingNameHsSortAttribute;
       
   353     setMenuOptions();
       
   354     mAllCollectionsModel->setSort(mSortAttribute);
       
   355     emit sortOrderChanged(mSortAttribute);
       
   356     HSMENUTEST_FUNC_EXIT("HsAllCollectionsState::descendingMenuAction");
       
   357 }