shapedrawerlistviewcontainer.cpp

来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C++ 代码 · 共 271 行

CPP
271
字号
/**
 * 
 * @brief Definition of CShapesBarChartView
 *
 * Copyright (c) EMCC Software Ltd 2003
 * @version 1.0
 */

// INCLUDE FILES

// Class includes
#include "ShapeDrawerListViewContainer.h"

//System includes
#include <coemain.h>
#include <eikenv.h>
#include <StringLoader.h>    // StringLoader
#include <ShapeDrawer.rsg>

//User includes
#include "ShapeDrawer.pan"
#include "ShapeDrawerDocument.h"
#include "Circle.h"
#include "Rectangle.h"
#include "ShapeDrawerListView.h"
#include "ShapeDrawerAppUi.h"


using namespace NShapes;

//local definitions
static const TInt KTextMaxLength = 50;

// ================= MEMBER FUNCTIONS =======================    

/**
@function NewLC
@abstract Creates a CShapeDrawerListViewContainer object, which will draw itself to aRect
@param aRect A rectangle that defines the size and location of the displayable area
for the view
@param aDocument the document
**/
CShapeDrawerListViewContainer* CShapeDrawerListViewContainer::NewLC(const TRect& /*aRect*/, CAknView& aOwningView/*CShapeDrawerDocument* aDocument*/)
{
    CShapeDrawerListViewContainer* self = new (ELeave) CShapeDrawerListViewContainer(aOwningView);

    CleanupStack::PushL(self);
    return self;
}

/**
@function NewL
@abstract Creates a CShapeDrawerListViewContainer object, which will draw itself to aRect
@param aRect A rectangle that defines the size and location of the displayable area
for the view
@param aDocument the document
**/
CShapeDrawerListViewContainer* CShapeDrawerListViewContainer::NewL(const TRect& aRect, CAknView& aOwningView/*CShapeDrawerDocument* aDocument*/)
{
    CShapeDrawerListViewContainer* self = NewLC(aRect, aOwningView/*aDocument*/);

    CleanupStack::Pop(self);
    return self;
}


TInt CShapeDrawerListViewContainer::CMyDynamicArray::MdcaCount() const
{
    return iArray->Count();
}

TPtrC CShapeDrawerListViewContainer::CMyDynamicArray::MdcaPoint(TInt aIndex) const
{
    //we want to get the indexed item from the array
    //we then want to go return a pointer that is 4 bytes from
    //the beginning of that address

    //The first part of the array contains the address of the item.
    //So we need to skip over this when returning the item text.
    TInt size = sizeof(TUint16*);
    const TUint16* internalDataPtr = (*iArray)[aIndex].Ptr() + size;
    int itemLength = (*iArray)[aIndex].Length()-size;

    TPtrC retVal;
    retVal.Set(internalDataPtr, itemLength);
    
    return retVal;
}

void CShapeDrawerListViewContainer::CMyDynamicArray::SetArray(CDesCArray* aArray)
{
    iArray = aArray;
}

CDesCArray* CShapeDrawerListViewContainer::CMyDynamicArray::Array()
{
    return iArray;
}

CShapeDrawerListViewContainer::CMyDynamicArray::~CMyDynamicArray()
{
    delete iArray;
}


void CShapeDrawerListViewContainer::PopulateListBoxFromModelL()
{
    CMyDynamicArray* myArray = new CMyDynamicArray;
    CDesCArray* array = new (ELeave) CDesCArrayFlat(5);
    myArray->SetArray(array);
    CleanupStack::PushL(array);

    TShape* shape = GetModel()->GetNextShape();
    while (shape) 
    {

        TShapeType shapeType = shape->ShapeType();
        TBuf<KTextMaxLength> theShapeType;

        //insert the address of the object
        TShape** addrShape = reinterpret_cast<TShape**>(&shape);

        theShapeType.Append((TUint16*)addrShape, sizeof(CDesCArray*)); //append the address

        switch(shapeType)
    {
        case KCircle:
        {
            TBuf<KTextMaxLength> circleFormattedText;
            StringLoader::Load(circleFormattedText, R_SHAPEDRAWER_CIRCLE);
            theShapeType.Append(circleFormattedText);
        }
            break;
        case KRectangle:
        {
            TBuf<KTextMaxLength> rectangleFormattedText;
            StringLoader::Load(rectangleFormattedText, R_SHAPEDRAWER_RECTANGLE);
            theShapeType.Append(rectangleFormattedText);
        }
            break;
        default:
            break;
    }

        TBuf<KTextMaxLength> otherStuff;
        TPoint shapeCoords = shape->Coordinates();
        otherStuff.Format(theShapeType, shapeCoords.iX, shapeCoords.iY);
        myArray->Array()->AppendL(otherStuff);

        shape = GetModel()->GetNextShape();
    }
    CleanupStack::Pop(); //array
    iListBox->Model()->SetItemTextArray(myArray);
}

/**
@function ConstructL  
@abstract Performs the second phase construction, setting the bounding 
rectangle to aRect
@param aRect the display area for the view
**/
void CShapeDrawerListViewContainer::ConstructL(const TRect& aRect)
{
    // Create a window for this application view
    CreateWindowL();

    iListBox = new (ELeave) CAknSingleStyleListBox();
    iListBox->SetContainerWindowL(*this);
    iListBox->ConstructL(this, EAknListBoxSelectionList);
    iListBox->SetListBoxObserver(this);
    iListBox->SetMopParent(this);
    
    // Set the windows size
    SetRect(aRect);

    iListBox->SetRect(Rect());

    PopulateListBoxFromModelL();

    //Set up the listbox to use a scroll bar
    iListBox->CreateScrollBarFrameL(ETrue);
    iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);

    // Activate the window, which makes it ready to be drawn
    ActivateL();

    iListBox->HandleItemAdditionL();
 }


CShapeDrawerListViewContainer::CShapeDrawerListViewContainer(CAknView& aOwningView)
    :    iOwningView(aOwningView)
{
    // No implementation required
}

/**
@function    ~CShapeDrawerListViewContainer
@abstract Destroys the object
**/
CShapeDrawerListViewContainer::~CShapeDrawerListViewContainer()
{
    delete iListBox;
}

TInt CShapeDrawerListViewContainer::CountComponentControls() const
{
    return 1;
}

void CShapeDrawerListViewContainer::RemoveCurrentItemL()
{
    TInt currentSelectedItem = iListBox->CurrentItemIndex();
    if(currentSelectedItem < 0)
        return;
    //we need to get the underlying model from the listbox
    CMyDynamicArray* lbModel = static_cast<CMyDynamicArray*>(iListBox->Model()->ItemTextArray());
    CDesCArray* theArray = lbModel->Array();

    TShape** iShapePtr = (TShape**)(*theArray)[currentSelectedItem].Ptr();
    TShape* iShape = *iShapePtr;
    
    GetModel()->RemoveShapeL(iShape);

    lbModel->Array()->Delete(currentSelectedItem);

    iListBox->HandleItemRemovalL();
    
    iListBox->SetCurrentItemIndex(iListBox->TopItemIndex());
}

CCoeControl* CShapeDrawerListViewContainer::ComponentControl(TInt aIndex) const
{
    CCoeControl* returnedItem;
    switch(aIndex)
    {
        case 0:
            returnedItem = iListBox;
            break;
        default:
            returnedItem = NULL;
            break;
    }
    return returnedItem;
}


/**
@function OfferKeyEventL
@abstract Handle the user pressing a key
@param aKeyEvent Details of the event that has occured
@param aType The type of event that has occured
**/
TKeyResponse CShapeDrawerListViewContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{    
    return iListBox->OfferKeyEventL(aKeyEvent, aType);
}

CShapeListManager* CShapeDrawerListViewContainer::GetModel() const
{
    CShapeDrawerListView& aView = (CShapeDrawerListView&)iOwningView;
    CEikDocument* aEikDocument = aView.GetAppUi().Document();
    CShapeDrawerDocument* aShapeDrawerDocument = static_cast<CShapeDrawerDocument*>(aEikDocument);
    CShapeListManager* aModel = aShapeDrawerDocument->Model();
    return (aModel);
}

//End of File


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?