📄 numericeditorcontainer.cpp
字号:
/**
*
* @brief Definition of CNumericEditorContainer
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
// INCLUDE FILES
// Class include
#include "NumericEditorContainer.h"
// System includes
#include <aknlists.h> // CAknSingleHeadingStyleListBox
#include <barsread.h> // TResourceReader
#include <NumericEditor.rsg> //R_NUMERICEDITOR_FORM, R_NUMERICEDITOR_EMPLOYEES_LISTBOX
// User includes
#include "NumericEditorForm.h"
// CONSTANTS
// N.B. #define'd as DLL cannot contain writeable static data
#define KListPosition TPoint(0,0)
// ================= MEMBER FUNCTIONS =======================
/**
* Symbian OS 2nd phase constructor. Creates a Window for the controls, which it contains.
* Constructs a list and adds it to the window, which it then activates.
* @param aRect The rectangle for this window
*/
void CNumericEditorContainer::ConstructL(const TRect& aRect)
{
CreateWindowL();
iEmployeesList = new (ELeave) CAknSingleHeadingStyleListBox;
iEmployeesList->SetContainerWindowL(*this);
// Second Phase Construction
TResourceReader reader;
CEikonEnv::Static()->CreateResourceReaderLC(reader, R_NUMERICEDITOR_EMPLOYEES_LISTBOX);
iEmployeesList->ConstructFromResourceL(reader);
CleanupStack::PopAndDestroy(); // reader
iEmployeesList->CreateScrollBarFrameL(ETrue);
iEmployeesList->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
SetRect(aRect);
ActivateL();
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CNumericEditorContainer using the NewLC method, popping
* the constructed object from the CleanupStack before returning it.
*
* @param aRect The rectangle for this window
* @return The newly constructed CNumericEditorContainer
*/
CNumericEditorContainer* CNumericEditorContainer::NewL(const TRect& aRect)
{
CNumericEditorContainer* self = CNumericEditorContainer::NewLC(aRect);
CleanupStack::Pop(self);
return self;
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CNumericEditorContainer using the constructor and ConstructL
* method, leaving the constructed object on the CleanupStack before returning it.
*
* @param aRect The rectangle for this window
* @return The newly constructed CNumericEditorContainer
*/
CNumericEditorContainer* CNumericEditorContainer::NewLC(const TRect& aRect)
{
CNumericEditorContainer* self = new (ELeave) CNumericEditorContainer;
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
/**
* Destructor. Frees up memory for the list and the array of employees.
*/
CNumericEditorContainer::~CNumericEditorContainer()
{
delete iEmployeesList;
iEmployees.Close();
}
/**
*
* Called by framework when the view size is changed. Resizes the
* iEmployeesList accordingly.
*
*/
void CNumericEditorContainer::SizeChanged()
{
iEmployeesList->SetExtent (KListPosition, iEmployeesList->MinimumSize());
}
/**
* Called by the framework in compound controls
* @return The number of controls in this CNumericEditorContainer
*/
TInt CNumericEditorContainer::CountComponentControls() const
{
return 1; // return number of controls inside this container
}
/**
* Called by the framework in compound controls
* @param The index of the control to return
* @return The control for aIndex
*/
CCoeControl* CNumericEditorContainer::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0:
return iEmployeesList;
default:
return NULL;
}
}
/**
* Called by the framework to draw this control. Clears the area in
* aRect.
* @param aRect in which to draw
*/
void CNumericEditorContainer::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
gc.Clear(aRect);
}
/**
* Called by the framework whenever a key event occurs.
* Passes the key event to the employees list if it is not null, otherwise returns
* EKeyWasNotConsumed
* @param aKeyEvent the Key event which occured, e.g. select key pressed
* @param aType the type of Key event which occurred, e.g. key up, key down
* @return TKeyResponse EKeyWasNotConsumed if the key was not processed, EKeyWasConsumed if it was
*/
TKeyResponse CNumericEditorContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (iEmployeesList)
return iEmployeesList->OfferKeyEventL (aKeyEvent, aType);
else
return EKeyWasNotConsumed;
}
/**
* Invokes the form in editable state, with a new opponenet, so that the details can be entered.
* Adds the Employee to the array of iEmployees and the list
*/
void CNumericEditorContainer::NewEmployeeL()
{
TNumericEditorEmployee Employee;
TBool saveState = EFalse;
CNumericEditorForm* form = CNumericEditorForm::NewL(Employee, saveState);
form->ExecuteLD(R_NUMERICEDITOR_FORM_DIALOG);
if (saveState)
{
AddEmployeeToListL(Employee);
}
else
{
iEmployeesList->DrawNow();
}
}
/**
* Invokes the form in the view state, with the currently selected Employee so that the details
* can be viewed and edited.
* Updates the Employee in the array of iEmployees and the list
*/
void CNumericEditorContainer::OpenEmployeeL()
{
TNumericEditorEmployee& Employee = iEmployees[iEmployeesList->CurrentItemIndex()];
TBool saveState = EFalse;
CNumericEditorForm* form = CNumericEditorForm::NewL(Employee, saveState);
form->ExecuteLD(R_NUMERICEDITOR_FORM_DIALOG);
if (saveState)
{
UpdateEmployeeInListL (Employee);
}
else
{
iEmployeesList->DrawNow();
}
}
/**
* If the Employee is valid, adds it to the array of iEmployees and updates the list
* of employees
* @param aEmployee reference to the Employee which should be added
*/
void CNumericEditorContainer::AddEmployeeToListL (const TNumericEditorEmployee& aEmployee)
{
iEmployees.Append(aEmployee);
CTextListBoxModel* model = iEmployeesList->Model(); // not taking ownership
model->SetOwnershipType (ELbmOwnsItemArray);
CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
itemArray->AppendL (ListStringFromEmployeeL(aEmployee));
iEmployeesList->HandleItemAdditionL();
}
/**
* Extracts the information from aEmployee, for display in the list.
* @param aEmployee the Employee to get the information from
*/
TBuf<50> CNumericEditorContainer::ListStringFromEmployeeL (const TNumericEditorEmployee& aEmployee) const
{
TBuf<50> string;
_LIT (KStringHeader, "%d\t%S");
HBufC* name = aEmployee.Name().AllocLC();
string.Format(KStringHeader(), aEmployee.Age(),name);
CleanupStack::PopAndDestroy(name);
return string;
}
/**
* Updates the Employee in the array of iEmployees and updates the list
* of employees
* @param aEmployee reference to the employee which should be updated
*/
void CNumericEditorContainer::UpdateEmployeeInListL (TNumericEditorEmployee& aEmployee)
{
CTextListBoxModel* model = iEmployeesList->Model(); // not taking ownership
model->SetOwnershipType (ELbmOwnsItemArray);
CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
TInt index = iEmployeesList->CurrentItemIndex();
TPtrC item = (*itemArray)[index];
item.Set (ListStringFromEmployeeL(aEmployee));
itemArray->Delete(index);
itemArray->InsertL(index, item);
iEmployeesList->DrawNow();
}
/**
* Deletes the currently selected Employee in the array of iEmployees and updates the list of employees
*/
void CNumericEditorContainer::DeleteEmployeeL()
{
CTextListBoxModel* model = iEmployeesList->Model(); // not taking ownership
model->SetOwnershipType (ELbmOwnsItemArray);
CDesCArray* itemArray = STATIC_CAST(CDesCArray*, model->ItemTextArray());
TInt index = iEmployeesList->CurrentItemIndex();
iEmployees.Remove(index);
itemArray->Delete(index);
iEmployeesList->SetCurrentItemIndex(iEmployeesList->TopItemIndex());
iEmployeesList->DrawNow();
}
/**
* Returns the number of employees currently in the list
*/
TInt CNumericEditorContainer::NumberOfEmployees()
{
TInt numberOfEmployees = 0;
if (iEmployeesList)
numberOfEmployees = iEmployees.Count();
return numberOfEmployees;
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -