camerauis/cameraxui/cxengine/src/cxestatemachinebase.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 23 61bc0f252b2b
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
     1 /*
       
     2  * Copyright (c) 2009-2010 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 #include "cxestatemachinebase.h"
       
    19 #include "cxutils.h"
       
    20 #include "cxestate.h"
       
    21 
       
    22 CxeStateMachineBase::CxeStateMachineBase(const char *stateMachineName) :
       
    23     mStateBitsUsed(0), mStateId(0), mName(stateMachineName)
       
    24 {
       
    25 }
       
    26 
       
    27 CxeStateMachineBase::~CxeStateMachineBase()
       
    28 {
       
    29     qDeleteAll(mStates);
       
    30     mStates.clear();
       
    31 }
       
    32 
       
    33 bool CxeStateMachineBase::addState(CxeState *state)
       
    34 {
       
    35     bool success( state // non-null state object
       
    36                && state->stateId() // state id is not zero
       
    37                && !(mStateBitsUsed & state->stateId()) // no overlapping bits
       
    38                 );
       
    39 
       
    40     // if no errors
       
    41     if (success) {
       
    42         mStateBitsUsed |= state->stateId();
       
    43         mStates[state->stateId()] = state; // state is now owned by the state machine
       
    44     }
       
    45 
       
    46     return success;
       
    47 }
       
    48 
       
    49 bool CxeStateMachineBase::setState(int stateId, CxeError::Id error)
       
    50 {
       
    51     bool success = true;
       
    52 
       
    53     if (stateId == mStateId) {
       
    54         // No transition, nothing needs to be done
       
    55         return success;
       
    56     }
       
    57 
       
    58     if (mStates.contains(stateId)) {
       
    59         // New state name known, print it
       
    60         CX_DEBUG(("%s::setState(%s, %d)", mName.toAscii().constData(),
       
    61                 mStates[stateId]->name().toAscii().constData(), error));
       
    62     } else {
       
    63         // Unknown state...
       
    64         CX_DEBUG(("%s::setState(UNKNOWN[%d], %d)", mName.toAscii().constData(),
       
    65                 stateId, error));
       
    66     }
       
    67 
       
    68     // State state machine does not use the error parameter itself.
       
    69     // It is only forwarded to handleStateChanged to be passed
       
    70     // on as a parameter in the stateChanged() signal.
       
    71 
       
    72     // setStateId itself will never return an error.
       
    73     // If an illegal state or state change is detected, it will panic the application (because it is a design error).
       
    74 
       
    75     success = verifyStateChange(stateId);
       
    76 
       
    77     if (success) {
       
    78         mStateId = stateId;
       
    79         handleStateChanged(mStateId, error);
       
    80     }
       
    81 
       
    82     return success;
       
    83 }
       
    84 
       
    85 bool CxeStateMachineBase::setInitialState(int stateId)
       
    86 {
       
    87     bool success = mStates.contains(stateId);
       
    88 
       
    89     if (success) {
       
    90         // New state name known, print it
       
    91         CX_DEBUG(("%s::setInitialState(%s)", mName.toAscii().constData(),
       
    92                 mStates[stateId]->name().toAscii().constData()));
       
    93     } else {
       
    94         // Unknown state...
       
    95         CX_DEBUG(("%s::setInitialState(UNKNOWN[%d])", mName.toAscii().constData(),
       
    96                 stateId));
       
    97     }
       
    98 
       
    99     // check that state changes have not been made
       
   100     // and this is the one and only setInitialState call
       
   101     success = (success && mStateId == 0);
       
   102 
       
   103     if (success) {
       
   104         mStateId = stateId;
       
   105     } else {
       
   106         CX_DEBUG(("%s state has already been set", mName.toAscii().constData()));
       
   107     }
       
   108     return success;
       
   109 }
       
   110 
       
   111 int CxeStateMachineBase::stateId() const
       
   112 {
       
   113     return mStateId;
       
   114 }
       
   115 
       
   116 bool CxeStateMachineBase::verifyStateChange(int newStateId)
       
   117 {
       
   118     bool allowStateChange = false;
       
   119 
       
   120     // Check that current state can be found in the state chart
       
   121     if ( mStates.contains(stateId()) ) {
       
   122         // Check that current state allows change to the new state
       
   123         CxeState *currentState = mStates[stateId()];
       
   124         allowStateChange = (currentState->allowedNextStates() & newStateId);
       
   125     }
       
   126 
       
   127     return allowStateChange;
       
   128 }