homescreenapp/stateplugins/hsmenuworkerstateplugin/src/hsdialogcontroller.cpp
changeset 90 3ac3aaebaee5
equal deleted inserted replaced
86:e4f038c420f7 90:3ac3aaebaee5
       
     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: Dialog controller
       
    15  *
       
    16  */
       
    17 
       
    18 #include <HbAction>
       
    19 #include <QString>
       
    20 #include <HbMessageBox>
       
    21 
       
    22 #include "hsdialogcontroller.h"
       
    23 
       
    24 /*!
       
    25  \class HsDialogController
       
    26  \ingroup group_hsworkerstateplugin
       
    27  \brief Dialog utility.
       
    28 
       
    29  Manages dialog lifetime depending on entry removal or
       
    30  signal arrival, forwards dialog originating actions to user specified callbacks.
       
    31  */
       
    32 
       
    33 /*!
       
    34     \fn void acceptActionTriggered(QAction* action)
       
    35     \param action Pointer to action which triggered the signal.
       
    36     Emitted when controlled  dialog closed with \a accept action.
       
    37 */
       
    38 
       
    39 
       
    40 /*!
       
    41     \fn rejectActionTriggered(QAction* action)
       
    42     \param action Pointer to action which triggered the signal.
       
    43     Emitted when controlled dialog is closed with \a reject action.
       
    44 */
       
    45 
       
    46 /*!
       
    47    \fn void dialogCompleted()
       
    48    Emitted whenever controlled dialog is closed.
       
    49 */
       
    50 
       
    51 /*!
       
    52   \param message Dialog's message.
       
    53   \param options Dialog type indicator.
       
    54   \see HsMenuDialogFactory
       
    55   Creates dialog controller instance with prediefinded dialog type.
       
    56   */
       
    57 HsDialogController::HsDialogController(
       
    58         const QString &message,
       
    59         HsMenuDialogFactory::Options options):
       
    60     mDialog(HsMenuDialogFactory().create(message, options)),
       
    61     mAcceptActionIndex(HsMenuDialogFactory::acceptActionIndex()),
       
    62     mRejectActionIndex(HsMenuDialogFactory::rejectActionIndex())
       
    63 {
       
    64 }
       
    65 
       
    66 /*!
       
    67   \param dialog Instance of the dialog to be displayed.
       
    68   Ownership of the dialog is taken by this object.
       
    69   \param acceptActionIndex Index of an \a accept action in a dialog action list
       
    70      Actions indices are 0 based. (\see QGraphicsWidget).
       
    71   \param rejectActionIndex Index of a \a reject action in a dialog action list.
       
    72   Actions indices are 0 based.(\see QGraphicsWidget).
       
    73   Creates dialog controller instance with custom dialog type.
       
    74 
       
    75   */
       
    76 HsDialogController::HsDialogController(HbDialog *dialog,
       
    77                                        int acceptActionIndex,
       
    78                                        int rejectActionIndex):
       
    79     mDialog(dialog),
       
    80     mAcceptActionIndex(acceptActionIndex),
       
    81     mRejectActionIndex(rejectActionIndex)
       
    82 {
       
    83     Q_ASSERT(mDialog != NULL);
       
    84     mDialog->setAttribute(Qt::WA_DeleteOnClose);
       
    85 }
       
    86 
       
    87 /*!
       
    88   Destroys controlled dialog.
       
    89   */
       
    90 HsDialogController::~HsDialogController()
       
    91 {
       
    92     QT_TRY {
       
    93         mDialog->deleteLater();
       
    94     } QT_CATCH (...) {
       
    95     }
       
    96 }
       
    97 
       
    98 
       
    99 /*!
       
   100   \param entryId Id of the entry which removal will close the dialog.
       
   101   Opens dialog.
       
   102  */
       
   103 void HsDialogController::openDialog(int entryId)
       
   104 {
       
   105     HsMenuEntryRemovedHandler *entryRemovedHandler =
       
   106         new HsMenuEntryRemovedHandler(entryId, mDialog, SLOT(close()));
       
   107 
       
   108     QT_TRY {
       
   109         // this object should be deleted when controlled dialog is deleted
       
   110         setParent(mDialog);
       
   111     } QT_CATCH (const std::bad_alloc &) {
       
   112         delete entryRemovedHandler;
       
   113         QT_RETHROW;
       
   114     }
       
   115 
       
   116     mDialog->open(this, SLOT(onDialogFinished(HbAction*)));
       
   117 }
       
   118 
       
   119 /*!
       
   120   This slot closes dialog.
       
   121   */
       
   122 void HsDialogController::dismissDialog()
       
   123 {
       
   124     mDialog->close();
       
   125 }
       
   126 
       
   127 /*!
       
   128   \param action Identifies dialog's action.
       
   129   Sends signals appropriate for \a action. Irrespective of the action,
       
   130   always sends \a dialogCompleted signal.
       
   131   */
       
   132 void HsDialogController::onDialogFinished(HbAction* action)
       
   133 {
       
   134     mDialog->disconnect();
       
   135 
       
   136     const int actionIndex = mDialog->actions().indexOf(action);
       
   137 
       
   138     if (actionIndex == mAcceptActionIndex)
       
   139     {
       
   140         emit acceptActionTriggered(action);
       
   141     } else if (actionIndex == mRejectActionIndex) {
       
   142         emit rejectActionTriggered(action);
       
   143     }
       
   144 
       
   145     emit dialogCompleted();
       
   146 
       
   147     mDialog->disconnect();
       
   148 }
       
   149 
       
   150