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

📄 listview2.cpp

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

#include <QikMultiPageViewBase.h>
#include <QikCommand.h>
#include <QikListBoxModel.h>
#include <QikListBox.h>
#include <MQikListBoxData.h>
#include <QikContent.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={0xEDEAD002};

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

_LIT(KMbmFile,"*"); // '*' is a shortcut to reference this app specific mbm (true for all apps)

//////////////////////////////////////////////////////////////////////////////////

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

class CAppEngine : public CBase
	{
protected:

public:
	CAppEngine(const TInt aZoomLevel);

	TInt ListViewZoomState() const;
	TBool SetListViewZoomState(const TInt aZoomLevel);
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;
	};

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

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

TBool 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(EFalse);
	iZoomLevel=aZoomLevel;
	return(ETrue);
	}

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

	// new methods
	void AddItemsToList1L();
	void AddItemsToList2L();
	void AddItemsToList3L();
	void AddItemsToList4L();
	void AddItemsToList5L();
	void AddItemsToList6L();
	void AddItemsToList7L();
	void AddItemsToList8L();
	void AddItemsToList9L();
	void AddItemsToList10L();
	void AddItemsToList11L();
	void AddItemsToList12L();
public:
	// new methods
	CAppSpecificListView(CAppSpecificUi& aAppUi,CAppEngine* aEngine);
	
protected:
	CAppEngine* iEngine;
	};

CAppSpecificListView::CAppSpecificListView(CAppSpecificUi& aAppUi,CAppEngine* aEngine) :
	CQikMultiPageViewBase(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 
			CQikMultiPageViewBase::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:
		{
		// Some code showing that you can use the ActivePageId() to deterime what page is 
		// currently selected if required by your application.
		CQikListBox* listbox=NULL;
		switch (ActivePageId())
			{
		case EAppSpecificListViewPageId1:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId1);
			break;
		case EAppSpecificListViewPageId2:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId2);
			break;
		case EAppSpecificListViewPageId3:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId3);
			break;
		case EAppSpecificListViewPageId4:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId4);
			break;
		case EAppSpecificListViewPageId5:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId5);
			break;
		case EAppSpecificListViewPageId6:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId6);
			break;
		case EAppSpecificListViewPageId7:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId7);
			break;
		case EAppSpecificListViewPageId8:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId8);
			break;
		case EAppSpecificListViewPageId9:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId9);
			break;
		case EAppSpecificListViewPageId10:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId10);
			break;
		case EAppSpecificListViewPageId11:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId11);
			break;
		case EAppSpecificListViewPageId12:
			listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId12);
			break;
		default:
			break;
			}
		if (aListBox!=listbox)		
			{
			_LIT(KListboxesDiffer,"Listboxes not as expected");
			iEikonEnv->InfoMsg(KListboxesDiffer);
			break;
			}

		// instead of obtaining the information to display from extracting item ids as we
		// did in ListView1 we can obtain information directly from the listbox..

		// you may wish to note there are slightly different UI behaviours with the different
		// listbox types. E.g. those that display 2 lines for the highlighted entry DO NOT
		// report EEventItemTapped when the highlight is move to those items. They only 
		// report EEventItemTapped when the highlighted item is selected. By comparison the
		// single list boxes report EEventItemTapped when highlight is moved.
		MQikListBoxData* lbData=aListBox->Model().RetrieveDataL(aItemIndex);
		CleanupClosePushL(*lbData); // its retrieved in an 'Open' state
		iEikonEnv->InfoMsg(lbData->Text(EQikListBoxSlotText1));
		CleanupStack::PopAndDestroy(lbData);
		break;
		}
	default:
		break;
		}
	}

const TInt KListView2Items=7;
void CAppSpecificListView::AddItemsToList1L()
//
// We are a single line list box. Add each of the items to the listbox for display
//
	{
	CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId1);
	MQikListBoxModel& model(listbox->Model());
	model.ModelBeginUpdateLC();
	TBuf<KMaxListItemText>bb;
	for (TInt i=0;i<KListView2Items;i++)
		{
		MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
		CleanupClosePushL(*lbData); // alternativly use NewDataLC() to save this line of code
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_CONTENT_1+i);
		lbData->AddTextL(bb,EQikListBoxSlotText1);
		CleanupStack::PopAndDestroy(lbData);

		// add some listbox row separators - first has a caption, second is plain line separator. 
		if (i==0)
			{	// Add a captioned separator between our 1st and 2nd entries
		    lbData=model.NewDataLC(MQikListBoxModel::EDataSeparator);
			iEikonEnv->ReadResourceL(bb,R_STR_LIST_SEPARATOR_1);
		    lbData->AddTextL(bb,MQikListBoxData::EDefaultSlot);
		    CleanupStack::PopAndDestroy(lbData);
			}
		if (i==3)
			{	// Add a separator with no caption between our 4th and 5th entries
		    lbData=model.NewDataLC(MQikListBoxModel::EDataSeparator);
		    CleanupStack::PopAndDestroy(lbData);
			}

		}
	model.ModelEndUpdateL();
	}

void CAppSpecificListView::AddItemsToList2L()
//
// The highlighted entry is displayed as a two line list box entry. 
//	standard_normal_layout = EQikListBoxLineIcon;
//	standard_highlight_layout = EQikListBoxTwoLines;
//
	{
	CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId2);
	MQikListBoxModel& model(listbox->Model());
	model.ModelBeginUpdateLC();
	TBuf<KMaxListItemText>bb;
	for (TInt i=0;i<KListView2Items;i++)
		{
		MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
		CleanupClosePushL(*lbData);

		// This listbox has 2 slots, the second is only displayed when the current item
		// is highlighted.
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_CONTENT_1+i);
		lbData->AddTextL(bb,EQikListBoxSlotText1);

		// we have an icon to right of the single line of text
		CQikContent* icon=CQikContent::NewL(NULL,KMbmFile,EMbmListview2Icon0,EMbmListview2Icon0mask);
		CleanupStack::PushL(icon);
		lbData->AddIconL(icon,EQikListBoxSlotRightSmallIcon1);
		CleanupStack::Pop(icon); // since lbData now taken ownership

		// displayed in highlighted item
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_DETAILS_1+i);
		lbData->AddTextL(bb,EQikListBoxSlotText2);

		// entries within a list box can be given a 32-bit id. This enables listbox entries
		// to maintain a link with any underlying data structure. We dont happen to have such
		// a structure in this example, however more often than not an application will. So
		// whilst SetItemId() is not strictly necessary in this app, its left here with this comment
		lbData->SetItemId(i);

		CleanupStack::PopAndDestroy(lbData);
		}
	model.ModelEndUpdateL();
	}


void CAppSpecificListView::AddItemsToList3L()
//
// We are a small icon to left list box.
// The highlighted entry is displayed as a two line list box entry with large icon. 
//	standard_normal_layout = EQikListBoxIconLine;
//	standard_highlight_layout = EQikListBoxMediumIconTwoLines;
//
	{
	CQikListBox* listbox=LocateControlByUniqueHandle<CQikListBox>(EAppSpecificListViewListId3);
	MQikListBoxModel& model(listbox->Model());
	model.ModelBeginUpdateLC();
	TBuf<KMaxListItemText>bb;
	for (TInt i=0;i<KListView2Items;i++)
		{
		MQikListBoxData* lbData=model.NewDataL(MQikListBoxModel::EDataNormal);
		CleanupClosePushL(*lbData);

		// This listbox has 2 slots, the second is only displayed when the current item
		// is highlighted. Also has Icon to the left.
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_CONTENT_1+i);
		lbData->AddTextL(bb,EQikListBoxSlotText1);
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_DETAILS_1+i);
		lbData->AddTextL(bb,EQikListBoxSlotText2);

		// Creates a CQikContent that loads the CFbsBitmap from our mbm file and set the
		// listbox slot for small icons with the loaded image. 
		// In general apps will do something more intelligent about assigning images to 
		// rows in the list box than what row they are in. For the purposes of the example
		// we dont.
		CQikContent* icon=NULL;
		switch (i)
			{
		case 0:
			icon=CQikContent::NewL(NULL,KMbmFile,EMbmListview2Icon0,EMbmListview2Icon0mask);
			break;
		case 1:
		case 5:
			icon=CQikContent::NewL(NULL,KMbmFile,EMbmListview2Icon1,EMbmListview2Icon1mask);
			break;
		case 2:
		case 6:
			icon=CQikContent::NewL(NULL,KMbmFile,EMbmListview2Icon2,EMbmListview2Icon2mask);
			break;
		case 3:
			icon=CQikContent::NewL(NULL,KMbmFile,EMbmListview2Icon3,EMbmListview2Icon3mask);
			break;
		// the remaining items (case 4) dont have images... so show its entirely feasible
		// to have rows with no image.
		default:
			break;
			}
		if (icon)
			{
			CleanupStack::PushL(icon);
			lbData->AddIconL(icon,EQikListBoxSlotLeftSmallIcon1);
			CleanupStack::Pop(icon); // since lbData now taken ownership
			}

		// Now the medium icon
		// Note that combining the two switch statements requires us to change our current
		// object ownership/error handling strategy so we dont cause alloc heaven should
		// loading one icon fail.Whilst entirely feasible, we have chosen not to do that here.
		icon=NULL;
		switch (i)
			{
		case 0:
			icon=CQikContent::NewL(NULL,KMbmFile,EMbmListview2Largeicon0,EMbmListview2Largeicon0mask);
			break;

⌨️ 快捷键说明

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