listview3.cpp

来自「《UIQ 3 The Complete Guide》书的源代码」· C++ 代码 · 共 532 行

CPP
532
字号
//
// ListView3.cpp - Simple list view example
//
// Copyright (C) UIQ Technology AB, 2007
//
// This material is provided "as is" without any warranty to its performance or functionality. 
// In no event shall UIQ Technology be liable for any damages whatsoever arising out of the
// use or inabilty to use this material. 
//

#include "ListView3.h"
#include "ListView3.hrh"
#include <ListView3.rsg>

#include <QikViewBase.h>
#include <QikCommand.h>
#include <QikListBoxModel.h>
#include <QikListBox.h>
#include <MQikListBoxData.h>
#include <QikListBoxData.h>
#include <QikContent.h>
#include <eikstart.h>

//////////////////////////////////////////////////////////////////////////////
// The application Uid here MUST match UID3 defined in the MMP file 
// This is a development UID and must NOT be used in production type software
const TUid KAppSpecificUid={0xEDEAD021};

// internal secondary view id, must be unique amongst this applications views
const TUid KUidListView={0x00000001};

// views within applications are fully identified by the app Uid and secondary view id
#define KViewIdListView TVwsViewId(KAppSpecificUid,KUidListView)

//////////////////////////////////////////////////////////////////////////////////
// each element that exists within the list box is represented by one of these

const TInt KMaxListItemText=32; // this example app has fixed size text elements

class CAppSpecificListBoxElement : public CQikListBoxDataBase
	{
protected:
	// from MQikListBoxData
	void Open();
	void Close();
	void ClearHosts();
	void AddIconL(CQikContent* aIcon, TInt aSlotId);
	void AddThumbnailImageL(const TDesC& aFileName, TInt aSlotId);
	void AddTextL(HBufC* aText, TInt aSlotId);
	CQikContent* Icon(TInt aSlotId, TInt aIndex) const;
	TPtrC ThumbnailImage(TInt aSlotId, TInt aIndex) const;
	TPtrC Text(TInt aSlotId, TInt aIndex) const;
	void SetIconL(CQikContent* aIcon, TInt aSlotId, TInt aIndex);
	void SetThumbnailImageL(const TDesC& aFileName, TInt aSlotId, TInt aIndex);
	void SetTextL(HBufC* aText, TInt aSlotId, TInt aIndex);
	TInt DeleteText(TInt aSlotId, TInt aIndex);
	TInt DeleteIcon(TInt aSlotId, TInt aIndex);
	TInt DeleteThumbnailImage(TInt aSlotId, TInt aIndex);
	TInt LayoutId() const;
	void SetLayoutId(TInt aId);
	TInt IndexCountForSlot(TInt aSlotId) const;
	TInt CurrentIndexForSlot(TInt aSlotId) const;
	void SetCurrentIndexForSlotL(TInt aSlotId, TInt aIndex);
	TBool NextOrFirstSlotIndexL();
	TBool PreviousOrLastSlotIndexL();
	TBool NextOrFirstSlotIndexL(const RArray<TInt>& aSlotsToSwap);
	TBool PreviousOrLastSlotIndexL(const RArray<TInt>& aSlotsToSwap);
	TInt ItemId() const;
	void SetItemId(TInt aItemId);

public:
	// new methods
	CAppSpecificListBoxElement(const TDesC& aText);

protected:
	TInt iReferenceCounter;
	TInt iItemId;
	TBuf<KMaxListItemText> iText;
	};

CAppSpecificListBoxElement::CAppSpecificListBoxElement(const TDesC& aText) :
	iText(aText)
	{
	}

void CAppSpecificListBoxElement::Open()
	{
	iReferenceCounter++;
	}

void CAppSpecificListBoxElement::Close()
	{
	iReferenceCounter--;
	if (!iReferenceCounter)
		delete(this);
	}

void CAppSpecificListBoxElement::ClearHosts()
	{
	}

void CAppSpecificListBoxElement::AddIconL(CQikContent* aIcon,TInt aSlotId)
	{
	}

void CAppSpecificListBoxElement::AddThumbnailImageL(const TDesC& aFileName,TInt aSlotId)
	{
	}

void CAppSpecificListBoxElement::AddTextL(HBufC* aText,TInt aSlotId)
	{
	}

CQikContent* CAppSpecificListBoxElement::Icon(TInt aSlotId, TInt aIndex) const
	{
	return(NULL);
	}

TPtrC CAppSpecificListBoxElement::ThumbnailImage(TInt aSlotId,TInt aIndex) const
	{
	return(TPtrC(KNullDesC)); // GCC gags on return(KNullDesC);
	}

TPtrC CAppSpecificListBoxElement::Text(TInt aSlotId,TInt aIndex) const
	{
	return(iText);
	}

void CAppSpecificListBoxElement::SetIconL(CQikContent* aIcon,TInt aSlotId,TInt aIndex)
	{
	}

void CAppSpecificListBoxElement::SetThumbnailImageL(const TDesC& aFileName,TInt aSlotId,TInt aIndex)
	{
	}

void CAppSpecificListBoxElement::SetTextL(HBufC* aText,TInt aSlotId,TInt aIndex)
	{
	}

TInt CAppSpecificListBoxElement::DeleteText(TInt aSlotId,TInt aIndex)
	{
	return(KErrNotSupported);
	}

TInt CAppSpecificListBoxElement::DeleteIcon(TInt aSlotId, TInt aIndex)
	{
	return(KErrNotSupported);
	}

TInt CAppSpecificListBoxElement::DeleteThumbnailImage(TInt aSlotId,TInt aIndex)
	{
	return(KErrNotSupported);
	}

TInt CAppSpecificListBoxElement::LayoutId() const
	{
	return(0);
	}

void CAppSpecificListBoxElement::SetLayoutId(TInt aId)
	{
	}

TInt CAppSpecificListBoxElement::IndexCountForSlot(TInt aSlotId) const
	{
	return(KErrNotSupported);
	}

TInt CAppSpecificListBoxElement::CurrentIndexForSlot(TInt aSlotId) const
	{
	return(KErrNotSupported);
	}

void CAppSpecificListBoxElement::SetCurrentIndexForSlotL(TInt aSlotId, TInt aIndex)
	{
	}

TBool CAppSpecificListBoxElement::NextOrFirstSlotIndexL()
	{
	return(EFalse);
	}

TBool CAppSpecificListBoxElement::PreviousOrLastSlotIndexL()
	{
	return(EFalse);
	}

TBool CAppSpecificListBoxElement::NextOrFirstSlotIndexL(const RArray<TInt>& aSlotsToSwap)
	{
	return(EFalse);
	}

TBool CAppSpecificListBoxElement::PreviousOrLastSlotIndexL(const RArray<TInt>& aSlotsToSwap)
	{
	return(EFalse);
	}

TInt CAppSpecificListBoxElement::ItemId() const
	{
	return(iItemId);
	}

void CAppSpecificListBoxElement::SetItemId(TInt aItemId)
	{
	iItemId=aItemId;
	}


//////////////////////////////////////////////////////////////////////////////////
class CAppSpecificListBoxModel : public CBase, public MQikListBoxModel
	{
protected:
	// From MQikListBoxModel
	void ReleaseModel();
	TInt Count() const;
	MQikListBoxData* NewDataL(MQikListBoxModelDataType aDataType);
	MQikListBoxData* NewDataL(MQikListBoxModelDataType aDataType, TInt aItemIndex);
	MQikListBoxData* NewDataLC(MQikListBoxModelDataType aDataType);
	MQikListBoxData* NewDataLC(MQikListBoxModelDataType aDataType, TInt aItemIndex);
	MQikListBoxData* RetrieveDataL(TInt aItemIndex);
	void RemoveDataL(TInt aItemIndex);
	void RemoveAllDataL();
	void Sort(TLinearOrder<MQikListBoxData> aOrder);
	TInt FindMatchIndexL(const TDesC& aMatchBuffer, TInt aStartIndex, TInt aIncrementalMatchSlot) const;
	TInt ItemIdIndex(TInt aItemId) const;

public:
	~CAppSpecificListBoxModel();
	void AddEntryL(const TDesC& aText);
protected:
	RPointerArray<CAppSpecificListBoxElement> iEntryList;
	};

void CAppSpecificListBoxModel::ReleaseModel()
	{
	}

TInt CAppSpecificListBoxModel::Count() const
// report the number of entries 
	{
	return(iEntryList.Count());
	}

MQikListBoxData* CAppSpecificListBoxModel::NewDataL(MQikListBoxModelDataType aDataType)
// We handle all the data internally in the listbox so we don't need to implement these
	{
	return(NULL);
	}

MQikListBoxData* CAppSpecificListBoxModel::NewDataL(MQikListBoxModelDataType aDataType,TInt aItemIndex)
// We handle all the data internally in the listbox so we don't need to implement these
	{
	return(NULL);
	}

MQikListBoxData* CAppSpecificListBoxModel::NewDataLC(MQikListBoxModelDataType aDataType)
// We handle all the data internally in the listbox so we don't need to implement these
	{
	return(NULL);
	}

MQikListBoxData* CAppSpecificListBoxModel::NewDataLC(MQikListBoxModelDataType aDataType,TInt aItemIndex)
// We handle all the data internally in the listbox so we don't need to implement these
	{
	return(NULL);
	}

MQikListBoxData* CAppSpecificListBoxModel::RetrieveDataL(TInt aItemIndex)
// This requires all elements implement the MQikListBoxData i/f 
	{
	return(iEntryList[aItemIndex]);
	}

void CAppSpecificListBoxModel::RemoveDataL(TInt aItemIndex)
	{
	delete(iEntryList[aItemIndex]);
	iEntryList.Remove(aItemIndex);

	// inform any observers of the change
	ReportModelEventL(MQikListBoxModelObserver::EQikListBoxDataRemoved,EQikListBoxParamNotApplicable,EQikListBoxParamNotApplicable);
	}

void CAppSpecificListBoxModel::RemoveAllDataL()
	{
	const TInt count=Count();
	for (TInt i=0;i<count;i++)
		RetrieveDataL(i)->Close();
	iEntryList.Reset();

	// inform any observers of the change
	ReportModelEventL(MQikListBoxModelObserver::EQikListBoxAllDataRemoved,EQikListBoxParamNotApplicable,EQikListBoxParamNotApplicable);
	}

void CAppSpecificListBoxModel::Sort(TLinearOrder<MQikListBoxData> aOrder)
	{
	}

TInt CAppSpecificListBoxModel::FindMatchIndexL(const TDesC& aMatchBuffer,TInt aStartIndex,TInt aIncrementalMatchSlot) const
	{
	return(KErrNotFound);
	}

TInt CAppSpecificListBoxModel::ItemIdIndex(TInt aItemId) const
	{
	const TInt count=Count();
	for (TInt i=0;i<count;i++)
		{
		MQikListBoxData* entry=iEntryList[i];
		if (entry->ItemId()==aItemId)
			return(i);
		}
	return(KErrNotFound);
	}
	
CAppSpecificListBoxModel::~CAppSpecificListBoxModel()
	{
	iEntryList.ResetAndDestroy();
	iEntryList.Close();
	}

void CAppSpecificListBoxModel::AddEntryL(const TDesC& aText)
// Add a new list box entry to the model.
	{
	CAppSpecificListBoxElement* element=new(ELeave)CAppSpecificListBoxElement(aText);
	CleanupStack::PushL(element);
	iEntryList.AppendL(element);
	CleanupStack::Pop(element);
	}

//////////////////////////////////////////////////////////////////////////////////
// The application view
class CAppSpecificListView : public CQikViewBase, public MQikListBoxObserver
	{
protected: 
	// from CQikViewBase
	TVwsViewId ViewId() const;
	void HandleCommandL(CQikCommand& aCommand);
	void ViewConstructL();
	void ViewDeactivated();
	void ViewActivatedL(const TVwsViewId& aPrevViewId,const TUid aCustomMessageId,const TDesC8& aCustomMessage);
	
	// from MQikListBoxObserver
	void HandleListBoxEventL(CQikListBox* aListBox,TQikListBoxEvent aEventType,TInt aItemIndex,TInt aSlotId);

public:
	// new methods
	CAppSpecificListView(CAppSpecificUi& aAppUi,CAppSpecificListBoxModel* aModel);
	
protected:
	CAppSpecificListBoxModel* iListBoxModel;
	};

CAppSpecificListView::CAppSpecificListView(CAppSpecificUi& aAppUi,CAppSpecificListBoxModel* aModel) :
	CQikViewBase(aAppUi,KNullViewId),iListBoxModel(aModel)
	{
	}

TVwsViewId CAppSpecificListView::ViewId() const
//
// All views are uniquely identified within the entire system. A TVwsViewId consists of 
// the application uid (uid3) and app specific view uid
//
	{
	return(KViewIdListView);
	}

void CAppSpecificListView::HandleCommandL(CQikCommand& aCommand)
//
// Handle the commands coming in from the controls that can deliver cmds..
//
	{
	switch (aCommand.Id())
		{
	default: // e.g. the back button...
		CQikViewBase::HandleCommandL(aCommand);
		break;
		}
	}

void CAppSpecificListView::HandleListBoxEventL(
//
// List box event occured - its reporting back what that might be.
//
	CQikListBox* aListBox,
	TQikListBoxEvent aEventType,
	TInt aItemIndex,
	TInt aSlotId)
	{
	switch (aEventType)
		{
	case EEventItemConfirmed:
	case EEventItemTapped:
		break;
	default:
		break;
		}
	}

void CAppSpecificListView::ViewConstructL()
	{
	// Loads information about the UI configurations this view supports
	// together with definition of each view.	
	ViewConstructFromResourceL(R_LIST_VIEW_CONFIGURATIONS);

	// we want to HandleListBoxEventL() - so observe the listbox
	CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);
	listbox->SetListBoxObserver(this);

	// we also supply the list box with the model which it displays
	listbox->SetModelL(iListBoxModel);

	// set the line below app name to be "List title"
	TBuf<64>bb;
	iEikonEnv->ReadResourceL(bb,R_STR_LIST_TITLE);
	ViewContext()->AddTextL(1,bb); 
	}

void CAppSpecificListView::ViewDeactivated()
//
// The view is being deactivated
//
	{
	}

void CAppSpecificListView::ViewActivatedL(
//
// The view is being activated.
//
	const TVwsViewId& aPrevViewId,
	const TUid aCustomMessageId,
	const TDesC8& aCustomMessage)
	{
	}
	
//////////////////////////////////////////////////////////////////////////////
CAppSpecificUi::~CAppSpecificUi()
	{
	delete(iListBoxModel);
	}

void CAppSpecificUi::ConstructL()
//
// Normal primary entry point to a Symbian App
//
	{
	CQikAppUi::ConstructL();

	iListBoxModel=new(ELeave)CAppSpecificListBoxModel;

	// declared outside the loop means its constructor not called every time we go round the loop
	TBuf<KMaxListItemText>bb;

	// we have a fixed number of items in this simple example
	const TInt KListView1Items=7;
	for (TInt i=0;i<KListView1Items;i++)
		{
		// this code uses the fact that resource Ids are always generated sequentially
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_CONTENT_1+i);

		// uses standard C++ to pass a TBuf<> to a function declared as accepting TDesC& 
		iListBoxModel->AddEntryL(bb);
		}

	// All views within a UIQ app need to be created + registered here.
	CAppSpecificListView* q=new(ELeave)CAppSpecificListView(*this,iListBoxModel);
	CleanupStack::PushL(q);
	q->ConstructL();
	AddViewL(*q);	// takes ownership
	CleanupStack::Pop(q);
	}

/////////////////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificDocument : public CQikDocument
	{
protected:
	CQikAppUi* CreateAppUiL();
public:
    CAppSpecificDocument(CQikApplication& aApp);
	static CAppSpecificDocument* NewL(CQikApplication& aApp);
protected:
	};

CAppSpecificDocument::CAppSpecificDocument(CQikApplication& aApp) :
	CQikDocument(aApp)
	{
	__DECLARE_NAME(_S("CAppSpecificDocument"));
	}

CAppSpecificDocument* CAppSpecificDocument::NewL(CQikApplication& aApp)
	{
	return(new(ELeave)CAppSpecificDocument(aApp));
	}

CQikAppUi* CAppSpecificDocument::CreateAppUiL()
	{
	return(new(ELeave)CAppSpecificUi);
	}

//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application framework code when creating an application
class CAppSpecificApplication : public CQikApplication
	{
protected:
	TUid AppDllUid() const;
	CApaDocument* CreateDocumentL();
	}; 

TUid CAppSpecificApplication::AppDllUid() const
    {
    return(KAppSpecificUid);
    }

CApaDocument* CAppSpecificApplication::CreateDocumentL()
    {
    return(CAppSpecificDocument::NewL(*this));
    }

//////////////////////////////////////////////////////////////////////////////////
// Standard Symbian application start up code
LOCAL_C CApaApplication* NewApplication()
    {
    return(new CAppSpecificApplication);
    }

GLDEF_C TInt E32Main()
	{
	return(EikStart::RunApplication(NewApplication));
	}

⌨️ 快捷键说明

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