shapedrawergraphicviewcontainer.cpp

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

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

// INCLUDE FILES

// Class includes
#include "ShapeDrawerGraphicViewContainer.h"

// System Includes
#include <coemain.h>
#include <eikenv.h>

// User Includes
#include "ShapeDrawer.pan"
#include "ShapeDrawerDocument.h"
#include "Circle.h"
#include "Rectangle.h"
#include "ShapeDrawerGraphicView.h"
#include "ShapeDrawerAppUi.h"

using namespace NShapes;

// Variable definitions
static const TInt KBlackPointer = 1;
static const TInt KBrushRadius = 5;
static const TInt KBrushHeight = 10;
static const TInt KBrushWidth = 10;
static const TInt KCrosshairWidth = 10;
static const TInt KCrosshairHeight = 10;
static const TInt KInitialPositionX = 10;
static const TInt KInitialPositionY = 10;

_LIT(KShapeDrawerPanicName,    "ShapeDrawer");

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

/**
@function NewLC
@abstract Creates a CShapeDrawerGraphicViewContainer 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
**/
CShapeDrawerGraphicViewContainer* CShapeDrawerGraphicViewContainer::NewLC(const TRect& aRect, CAknView& aOwningView)
{
    CShapeDrawerGraphicViewContainer* self = new (ELeave) CShapeDrawerGraphicViewContainer(aOwningView);

    CleanupStack::PushL(self);
    self->ConstructL(aRect);

    return self;
}

/**
@function NewL
@abstract Creates a CShapeDrawerGraphicViewContainer 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
**/
CShapeDrawerGraphicViewContainer* CShapeDrawerGraphicViewContainer::NewL(const TRect& aRect, CAknView& aOwningView)
{
    CShapeDrawerGraphicViewContainer* self = NewLC(aRect, aOwningView);

    CleanupStack::Pop(self);

    return self;
}


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

    // Set the windows size
    SetRect(aRect);

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

    Window().SetPointerCursor(KBlackPointer);
}

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

/**
@function Draw
@abstract Draws the view
@param aRect the rectangle of this view that needs updating
**/
void CShapeDrawerGraphicViewContainer::Draw(const TRect& /*aRect*/) const
{
    CWindowGc& graphicsContext = SystemGc();

    // Clear the application view
    graphicsContext.Clear();

    // draw the 'cursor' crosshair. Size is KCrosshairWidth by KCrosshairHeight pixels
    graphicsContext.SetPenSize(TSize(1,1));
    graphicsContext.SetPenColor(KRgbBlack);

    graphicsContext.DrawLine(
        TPoint(iPosition.iX - KCrosshairWidth, iPosition.iY), 
        TPoint(iPosition.iX + KCrosshairWidth, iPosition.iY));

    graphicsContext.DrawLine(
        TPoint(iPosition.iX, iPosition.iY - KCrosshairHeight), 
        TPoint(iPosition.iX, iPosition.iY + KCrosshairHeight));

    // draw all the current shapes
    TShape* shape = GetModel()->GetNextShape();

    while (shape) 
    {
        shape->Draw(graphicsContext);
        shape = GetModel()->GetNextShape();
    }
}

/**
@function CShapeDrawerGraphicViewContainer
@abstract Performs the first stage construction
@param aOwningView the view that owns this container
**/
CShapeDrawerGraphicViewContainer::CShapeDrawerGraphicViewContainer(CAknView& aOwningView)
 :    iOwningView(aOwningView), 
    iBrushShapeType(ECircle),
    iPosition (KInitialPositionX, KInitialPositionY)    
{
    // No implementation required
}

CShapeDrawerGraphicViewContainer::~CShapeDrawerGraphicViewContainer()
{
}

/**
@function Clear
@abstract Clears the view / model
**/
void CShapeDrawerGraphicViewContainer::Clear()
{
    GetModel()->Clear();
    DrawNow();
}

/**
@function SetBrushShapeType
@abstract Set the type of shape that will be added to the list
@param aBrushShapeType the shape type of the brush
**/
void CShapeDrawerGraphicViewContainer::SetBrushShapeType(TBrushShapeType aShapeType)
{
    iBrushShapeType = aShapeType;
}

/**
@function BrushShapeType
@abstract Gets the type of shape that will be added to the list
@result Returns the type of shape that will be added.
**/
CShapeDrawerGraphicViewContainer::TBrushShapeType CShapeDrawerGraphicViewContainer::BrushShapeType() const
{
    return iBrushShapeType;
}

/**
@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 CShapeDrawerGraphicViewContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{    
    if (aType != EEventKey)
    {
        return EKeyWasNotConsumed;
    }

    // move left
    if (aKeyEvent.iScanCode == EStdKeyLeftArrow)
    {
        if (iPosition.iX >  (Rect().iTl.iX + KCrosshairWidth))
        {
            --iPosition.iX;
            DrawNow();
        }
        return EKeyWasConsumed;
    }

    // move right
    else if (aKeyEvent.iScanCode == EStdKeyRightArrow)
    {
        if (iPosition.iX < (Rect().iBr.iX - KCrosshairWidth))
        {
            ++iPosition.iX;
            DrawNow();
        }
        return EKeyWasConsumed;
    }

    // move up
    else if (aKeyEvent.iScanCode == EStdKeyUpArrow)
    {
        if (iPosition.iY > (Rect().iTl.iY + KCrosshairHeight))
        {
            --iPosition.iY;
            DrawNow();
        }
        return EKeyWasConsumed;
    }

    // move down
    else if (aKeyEvent.iScanCode == EStdKeyDownArrow)
    {
        if (iPosition.iY < (Rect().iBr.iY - KCrosshairHeight))
        {
            ++iPosition.iY;
            DrawNow();
        }
        return EKeyWasConsumed;
    }


    // place a shape
    else if (aKeyEvent.iScanCode == EStdKeyDevice3)
    {
        TShape* newShape = NULL;
        // Update the co-ordinates in the model to the position at which the
        // pointer event occurred.

        switch (iBrushShapeType) 
        {
            case ECircle :
                newShape = new (ELeave) TCircle(iPosition, KBrushRadius);
                GetModel()->AddShapeL(newShape);        //    Takes ownership
                break;
            case ERectangle :
                newShape = new (ELeave) TRectangle(iPosition, KBrushHeight, KBrushWidth);
                GetModel()->AddShapeL(newShape);        //    Takes ownership
                break;
            default :
                User::Panic(KShapeDrawerPanicName, EShapeDrawerInvalidBrushType);
        }
        DrawNow();
        return EKeyWasConsumed;
    }

    return EKeyWasNotConsumed;
}

//End Of File


⌨️ 快捷键说明

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