www.pudn.com > UIQ_Code1.rar > CommandsViewModel.cpp


// CommandsViewModel.cpp 
// 
// © Symbian Software Ltd 2005. All rights reserved. 
// 
 
#include "CommandsViewModel.h" 
#include "CommandsHandlerPage2.h" 
#include "CommandsHandlerPage3.h" 
#include "CommandsHandlerPage4.h" 
#include "CommandsView.h" 
#include "Commands.hrh" 
 
#include  
#include  
 
/** 
Creates and constructs the view model. 
 
@param aCommandView Reference to the CCommandsView 
@return Pointer to a CCommandsViewModel object 
*/ 
CCommandsViewModel* CCommandsViewModel::NewL(CCommandsView& aCommandsView, CCoeEnv& aCoeEnv) 
	{ 
	CCommandsViewModel* self = new(ELeave) CCommandsViewModel(aCommandsView, aCoeEnv); 
	CleanupStack::PushL(self); 
	self->ConstructL(); 
	CleanupStack::Pop(self); 
	return self; 
	} 
	 
/** 
Constructor for the model. Adds the references to the command manager and  
command view.  
 
@param aCommandsView Reference to the CCommandsView 
*/ 
CCommandsViewModel::CCommandsViewModel(CCommandsView& aCommandsView, CCoeEnv& aCoeEnv) 
	: iCommandManager(CQikCommandManager::Static(aCoeEnv)), iCommandsView(aCommandsView), iConfigClient(CQUiConfigClient::Static(aCoeEnv)) 
	{ 
	} 
 
/** 
2nd stage construction of the model. 
Creates the command state monitor that initialized all states to 0. 
Changes the values for the state properties for applicationState1 and  
applicationState2. 
*/ 
void CCommandsViewModel::ConstructL() 
	{ 
	iCommandStateMonitor = CQikCommandStateMonitor::NewL(*this, ECommandsNumOfStates); 
	iCommandStateMonitor->SetApplicationState(ECommandsApplicationState1, ETrue); 
	iCommandStateMonitor->SetApplicationState(ECommandsApplicationState2, EFalse); 
	iConfigClient.AddUiConfigChangeObserverL(*this); 
	 
	// Create the objects that implements functionality corresponding to commands. 
	// Use one commandhandler per viewpage to break down code into comprehensible pieces. 
	// CCommandsViewModel implements functionality for commands that is available in all pages. 
	iCommandsHandlerPage2 = CCommandsHandlerPage2::NewL(iCommandsView, iCommandManager); 
	iCommandsHandlerPage3 = CCommandsHandlerPage3::NewL(iCommandsView, iCommandManager); 
	iCommandsHandlerPage4 = CCommandsHandlerPage4::NewL(iCommandsView, iCommandManager); 
	} 
 
/** 
Destructor for the model. Deletes the command state monitor. 
*/ 
CCommandsViewModel::~CCommandsViewModel() 
	{ 
	delete iCommandStateMonitor; 
	delete iCommandsHandlerPage2; 
	delete iCommandsHandlerPage3; 
	delete iCommandsHandlerPage4; 
	} 
 
/** 
Returns the object that is responsible for executing each Command  
(i.e. who's HandleCommandL that shall be called when the Command is activated). 
Called from the CommandManager for each command being added. 
 
@param aCommand The command that is added which do not already has its command  
handler. 
@return The command handler to set for the command. 
*/ 
MQikCommandHandler* CCommandsViewModel::MapCommandHandler(CQikCommand& aCommand) 
	{ 
	// Due to readablitity the handling of the commands is distributed  
	// according the area they appear. 
	const TInt commandId = aCommand.Id(); 
	if (commandId >= KCategoryMenuIdOffset) 
		{ 
		// Category commands are (when used) handled in CQikViewBase 
		return &iCommandsView; 
		} 
	else if (commandId >= ECommandsPage4Start) 
		{ 
		return iCommandsHandlerPage4; 
		} 
	else if (commandId >= ECommandsPage3Start) 
		{ 
		return iCommandsHandlerPage3; 
		} 
	else if (commandId >= ECommandsPage2Start) 
		{ 
		return iCommandsHandlerPage2; 
		} 
	else if (commandId >= ECommandsExtraStart) 
		{ 
		return this; 
		} 
	else 
		{ 
		// Forward unrecognized commands to the view base class 
		return &iCommandsView; 
		} 
	} 
	 
/** 
For every command inserted to a command list, the client will receive the  
callback where a command can be initialized or removed 
 
@param aCommand The command to edit. 
@param aControlAddingCommands A reference to a control. 
@return the aCommand after approval and any update of appearance. 
*/ 
CQikCommand* CCommandsViewModel::DynInitOrDeleteCommandL(CQikCommand* aCommand, const CCoeControl& /*aControlAddingCommands*/) 
	{ 
	// Call this method to put a command under supervision. The return value  
	// from a call to your DoUpdateCommandStateL() will determine if the  
	// command will be put under supervision or not, i.e. your  
	// DoUpdateCommandStateL() must return ETrue for aCommand otherwise it  
	// will be ignored. 
	iCommandStateMonitor->SuperviseCommandL(*aCommand); 
 
	switch(aCommand->Id()) 
		{ 
		case ECommandsExtraEnableState1Cmd: 
			{ 
			// Defines if the checkbox should be marked or not, depends on the  
			// state of the application state 1 in the command state monitor. 
			aCommand->SetChecked((*iCommandStateMonitor)[ECommandsApplicationState1]); 
			break; 
			} 
		case ECommandsExtraEnableState2Cmd: 
			{ 
			// Defines if the checkbox should be marked or not, depends on the  
			// state of the application state 2 in the command state monitor. 
			aCommand->SetChecked((*iCommandStateMonitor)[ECommandsApplicationState2]); 
			break; 
			} 
		default: 
			break; 
		} 
 
	return aCommand; 
	} 
 
/** 
The CQikCommandStateMonitor will use this callback to ask you to set state of  
each command under supervision. The CQikCommand implementation will compare  
the new state against current and only update the user interface when needed. 
 
@param aMonitor The CQikCommandStateMonitor 
@param aCommand The command to set state on 
@param aUiConfig The Ui configuration to use when deciding what state to set. 
*/	 
TBool CCommandsViewModel::DoUpdateCommandStateL(CQikCommandStateMonitor& aMonitor, CQikCommand& aCommand, const TQikUiConfig& /*aUiConfig*/) const 
	{ 
	TBool recognized = EFalse; 
	switch (aCommand.Id()) 
		{ 
		case ECommandsExtraCmdAState1Cmd: 
			{ 
			// Sets the ExtraState1 commands availability according to the  
			// state of ApplicationState1 
			aCommand.SetAvailable(aMonitor[ECommandsApplicationState1]); 
			recognized = ETrue; 
			break; 
			} 
		case ECommandsExtraCmdBState1Cmd: 
			{ 
			// Sets the ExtraState1 commands availability according to the  
			// state of ApplicationState1 
			aCommand.SetAvailable(aMonitor[ECommandsApplicationState1]); 
			recognized = ETrue; 
			break; 
			} 
		case ECommandsExtraCmdCState2Cmd: 
			{ 
			// Sets the ExtraState2 commands availability according to the  
			// state of ApplicationState2 
			aCommand.SetAvailable(aMonitor[ECommandsApplicationState2]); 
			recognized = ETrue; 
			break; 
			} 
		case ECommandsExtraCmdDState1And2Cmd: 
			{ 
			// To set ExtraState1And2 command is only available when the both  
			// state of ApplicationState1 and ApplicationState2 is active. 
			const TBool state = aMonitor[ECommandsApplicationState1] && aMonitor[ECommandsApplicationState2]; 
			aCommand.SetAvailable(state); 
			recognized = ETrue; 
			break; 
			} 
		default: 
			recognized = EFalse; 
			break; 
		} 
	return recognized; 
	} 
 
/** 
Called from UI configuration server when the UI configuration changes.  
Need to refresh the commands that only change in the specific UI configuration. 
*/ 
void CCommandsViewModel::HandleUiConfigChangedL() 
	{ 
	// Changes the text for the command that make it possible to expand or  
	// contract commands 
	CQikCommand* command = iCommandManager.Command(iCommandsView, ECommandsExtraCmd); 
	const TInt commandText = iExtraCommandsState ? R_COMMANDS_CMD_EXTRA_CONTRACT_COMMANDS : R_COMMANDS_CMD_EXTRA_EXPAND_COMMANDS; 
	command->SetTextL(commandText); 
	 
	// Need to update the new inserted commands with the state from the command state monitor 
	iCommandStateMonitor->UpdateCommandsL(iCommandsView); 
	} 
	 
/** 
The purpose of extra commands is to show how to add/delete command list 
during runtime, change command text and usage of command state monitor. 
 
When the state of a command is depending on 3 or more different  
variables in your application, it becomes difficult to make sure that  
the command always is in correct state.  
 
CQikCommandStateMonitor / MQikCommandStateController has been developed 
to handle these cases. CQikCommandStateMonitor is an optional class  
for keeping track of different states in your application model and  
update the commands' state accordingly.  
 
@param aCommand The command to be executed 
*/ 
void CCommandsViewModel::HandleCommandL(CQikCommand& aCommand) 
	{ 
	// Launch an infoprint with the command text  
	iCommandsView.InfoprintCmdL(aCommand); 
		 
	switch(aCommand.Id()) 
		{ 
		case ECommandsExtraCmd: 
			{ 
			// Change iExtraCommandsState that indicate if the command list  
			// should be expanded or contract with commands 
			iExtraCommandsState = !iExtraCommandsState; 
			 
			// Changes the text for the command that make it possible to expand 
			// or contract commands 
			const TInt commandText = iExtraCommandsState ? R_COMMANDS_CMD_EXTRA_CONTRACT_COMMANDS : R_COMMANDS_CMD_EXTRA_EXPAND_COMMANDS; 
			aCommand.SetTextL(commandText); 
 
			if(iExtraCommandsState) 
				{ 
				// Populate the command list from resource into the commandlist 
				iCommandManager.InsertIntoCommandListL(iCommandsView, iCommandsView, R_COMMANDS_EXTRA_COMMANDS); 
 
				// Need to update the new inserted commands with the state from the command state monitor 
				iCommandStateMonitor->UpdateCommandsL(iCommandsView); 
				} 
			else 
				{ 
				// Delete those commands originating from command resource  
				iCommandManager.DeleteFromCommandList(iCommandsView, R_COMMANDS_EXTRA_COMMANDS); 
				} 
 
			break; 
			} 
		case ECommandsExtraEnableState1Cmd: 
			{ 
			// Get the checkbox state 
			const TBool checked = aCommand.Checked(); 
			// Set the applicationState1 in the command state monitor to the  
			// checkbox state 
			iCommandStateMonitor->SetApplicationState(ECommandsApplicationState1, checked); 
			// Call this method when your commands need to be updated, call it  
			// only once after your have finished change states. 
			iCommandStateMonitor->UpdateCommandsL(iCommandsView); 
			break; 
			} 
		case ECommandsExtraEnableState2Cmd: 
			{ 
			// Get the checkbox state 
			const TBool checked = aCommand.Checked(); 
			// Set the applicationState2 in the command state monitor to the  
			// checkbox state 
			iCommandStateMonitor->SetApplicationState(ECommandsApplicationState2, checked); 
			// Call this method when there is a chance your commands need to be updated.  
			iCommandStateMonitor->UpdateCommandsL(iCommandsView);		 
			break; 
			} 
		case ECommandsExtraCmdAState1Cmd: 
		case ECommandsExtraCmdBState1Cmd: 
		case ECommandsExtraCmdCState2Cmd: 
		case ECommandsExtraCmdDState1And2Cmd: 
			{ 
			// Do nothing. In a normal application something should happen  
			// here when the command has been activated.  
			// If commands are not handle by the view they will be propageted  
			// to the appUi.  
			break; 
			} 
		case ECommandsChangeUiModeCmd: 
			{ 
			// Get the current interaction style 
			TQikInteractionStyle interactionStyle = iConfigClient.CurrentConfig().InteractionStyle(); 
			 
			// Change the current config style, toggles between pen based and softkey style. 
			const TInt uiConfig = ( interactionStyle == EQikInteractionStyleSoftkey) ? KQikPenStyleTouchPortrait : KQikSoftkeyStylePortrait; 
			iConfigClient.SetCurrentConfigL(uiConfig); 
			break; 
			} 
		default: 
			{ 
			iCommandsView.HandleCommandL(aCommand); 
			break; 
			} 
		} 
	}