⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listview1.cpp

📁 《UIQ 3 The Complete Guide》书的源代码
💻 CPP
字号:
//
// ListView1.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 "ListView1.h"
#include "ListView1.hrh"
#include <ListView1.rsg>
#include <ListView1.mbg>

#include <QikViewBase.h>
#include <QikCommand.h>
#include <QikListBoxModel.h>
#include <QikListBox.h>
#include <MQikListBoxData.h>
#include <QikZoomDlg.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={0xEDEAD001};

// 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)

//////////////////////////////////////////////////////////////////////////////////
const TInt KMaxListItemText=32;	// we support a max of 32 chars for each text item to be displayed
const TInt KMaxListItems=8;		// we support upto 8 items

class CAppEngine : public CBase
	{
protected:

public:
	CAppEngine(const TInt aZoomLevel);

	TInt ListViewZoomState() const;
	TInt SetListViewZoomState(const TInt aZoomLevel);

	TInt ListItemCount() const;
	const TDesC& ListItem(const TInt aIndex) const;
	void SetListItem(const TDesC& aBuf);

protected:
	// The 'engine' stores 'UI' information since UI components come and go, yet we want to preserve
	// UI configuration information between invocations of the view.
	TInt iZoomLevel;

	// This incarnation of the engine deliberately uses a fixed size array and separate count
	// to demonstrate that this is possible. It may/maynot be appropriate for your situations
	TInt iListItemsCount;
	TBuf<KMaxListItemText> iListItems[KMaxListItems];
	};

CAppEngine::CAppEngine(const TInt aZoomLevel) : 
	iZoomLevel(aZoomLevel)
	{}

TInt CAppEngine::ListItemCount() const
// Report the number of items within the list
	{
	return(iListItemsCount);
	}

const TDesC& CAppEngine::ListItem(const TInt aIndex) const
// Report the 'n' item of text 
	{
	return(iListItems[aIndex]);
	}

void CAppEngine::SetListItem(const TDesC& aBuf)
// Add a new list item. By defining the param as a TDesC we can accept multiple descriptor types
	{
	// descriptor asignments will perform bounds checking as well as copy the data
	iListItems[iListItemsCount++]=aBuf;
	}

TInt CAppEngine::ListViewZoomState() const
// Report the currently stored zoom state
	{
	return(iZoomLevel);
	}

TInt CAppEngine::SetListViewZoomState(const TInt aZoomLevel)
//
// Update list view zoom state. 
// Return TRUE if zoom state set is different to current level
//
	{
	if (aZoomLevel==iZoomLevel)
		return(FALSE);
	iZoomLevel=aZoomLevel;
	return(TRUE);
	}

//////////////////////////////////////////////////////////////////////////////////
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);

	// new methods
	void AddItemsL();
public:
	// new methods
	CAppSpecificListView(CAppSpecificUi& aAppUi,CAppEngine* aEngine);
	
protected:
	CAppEngine* iEngine;
	};

CAppSpecificListView::CAppSpecificListView(CAppSpecificUi& aAppUi,CAppEngine* aEngine) :
	CQikViewBase(aAppUi,KNullViewId),iEngine(aEngine)
	{
	}

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())
		{
	case EAppCmdZoom:	// Launch the system std zoom dialog
		CQikZoomDialog::RunDlgLD(iEngine->ListViewZoomState(),*this);
		break;

	case EQikCmdZoomLevel1:	// delivered by the std system zoom dlg...
	case EQikCmdZoomLevel2:
	case EQikCmdZoomLevel3:
		if (iEngine->SetListViewZoomState(aCommand.Id()))
			{ // zoom state has changed 
			CQikViewBase::SetZoomFactorL(CQikAppUi::ZoomFactorL(aCommand.Id(),*iEikonEnv));	
			PerformLayout();
			}
		break;

	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:
		{
		const TInt id=aListBox->ItemIdL(aItemIndex);
		iEikonEnv->InfoMsg(iEngine->ListItem(id));
		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);

	// determine zoom level last used
	CQikViewBase::SetZoomFactorL(CQikAppUi::ZoomFactorL(iEngine->ListViewZoomState(),*iEikonEnv));	

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

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

/*
This sample code shows how you could add text,icon and progress bars into the app 
title bar regions - instead of the simple list title displayed by code above.

	TBuf<64>bb;
	iEikonEnv->ReadResource(bb,R_STR_LIST_TITLE);
	ViewContext()->AddTextL(1,bb.Left(4));

	_LIT(KMbmFile,"*");
	CFbsBitmap* bit=iEikonEnv->CreateBitmapL(KMbmFile,EMbmListview1Icon);
	CFbsBitmap* mask=iEikonEnv->CreateBitmapL(KMbmFile,EMbmListview1Iconmask);
	ViewContext()->AddIconL(2,bit,mask);
	ViewContext()->AddProgressInfoL(EEikProgressTextPercentage,100);
	ViewContext()->SetAndDrawProgressInfo(50);
*/
	}

void CAppSpecificListView::ViewDeactivated()
//
// Here we choose to remove all items from the list box. Apart from saving the memory used by
// list box items that are not displayed, in general things may change whilst not displaying
// within the list so we will have to rebuild the list when the view is activated..
//
	{
	CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);
	TRAPD(junk,listbox->RemoveAllItemsL());
	}

void CAppSpecificListView::AddItemsL()
//
// Add all items we have in the engine to the list box model = Duplicate the enties. 
//
	{
	CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId);

	// Get the listbox model
	MQikListBoxModel& model(listbox->Model());
	model.ModelBeginUpdateLC();
	
	// Will check for each item in the application model if there exist a 
	// match in the listbox, if not a new item will be added in the listbox
	const TInt count=iEngine->ListItemCount();
	for (TInt i=0;i<count;i++)
		{
		MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
		CleanupClosePushL(*lbData);

		lbData->AddTextL(iEngine->ListItem(i),EQikListBoxSlotText1);
		lbData->SetItemId(i);

		// Removes the listboxData from the stack and calls close on lbData
		CleanupStack::PopAndDestroy(lbData);
		}

	// weve now finished all updates so commit changes
	model.ModelEndUpdateL();
	}

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

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

	// Create an engine, 2 phase construction is not required since it does not need to allocate
	// anything. 
	iEngine=new(ELeave)CAppEngine(EQikCmdZoomLevel2);

	// 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& 
		iEngine->SetListItem(bb);
		}

	// All views within a UIQ app need to be created + registered here.
	CAppSpecificListView* q=new(ELeave)CAppSpecificListView(*this,iEngine);
	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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -