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

📄 qcontent.cpp

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

#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={0xEDEAD023};

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

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

//////////////////////////////////////////////////////////////////////////////////
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:
	TInt ListItemCount() const;
	const TDesC& ListItem(const TInt aIndex) const;
	void SetListItem(const TDesC& aBuf);

protected:
	// 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];
	};

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.
	{
	iListItems[iListItemsCount++]=aBuf;
	}

//////////////////////////////////////////////////////////////////////////////////
class CAppSpecificListView : public CQikViewBase
	{
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);
	
	// 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 EAppCmdAbout:
		break;
	default: // e.g. the back button...
		CQikViewBase::HandleCommandL(aCommand);
		break;
		}
	}

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

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

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

	// non owned handle to demonstrate usage of cloning
	CQikContent* savedIcon=NULL; 

	// Get the listbox model
	MQikListBoxModel& model(listbox->Model());
	model.ModelBeginUpdateLC();

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

		// construct CQikContents in various ways
		CQikContent* icon=NULL;
		switch (i)
			{

		case 0:
			{ // from a QIK_CONTENT_MBM type RSC, directly in the NewL()
			TResourceReader rr;
			HBufC8* q=iEikonEnv->AllocReadResourceAsDes8LC(R_COMMAND_ICON);
			rr.SetBuffer(q);
			icon=CQikContent::NewL(NULL,rr);
			CleanupStack::PopAndDestroy(q);
			break;
			}

		case 1:
			{ // from a QIK_CONTENT_MBM type RSC, but setting the content, after object construction
			TResourceReader rr;
			HBufC8* q=iEikonEnv->AllocReadResourceAsDes8LC(R_COMMAND2_ICON);
			rr.SetBuffer(q);
			icon=new(ELeave)CQikContent;
			CleanupStack::PushL(icon);
			icon->SetContentL(NULL,rr);
			CleanupStack::Pop(icon);
			CleanupStack::PopAndDestroy(q);
			break;
			}

		case 2:
			{ // from a QIK_CONTENT resource - which refers to a gif file, directly in the NewL()
			TResourceReader rr;
			HBufC8* q=iEikonEnv->AllocReadResourceAsDes8LC(R_QIK_CONTENT_ICON);
			rr.SetBuffer(q);
			icon=CQikContent::NewL(NULL,rr);
			CleanupStack::PopAndDestroy(q);
			break;
			}

		case 3:
			// load content directly from our MBM
			icon=CQikContent::NewL(NULL,KMbmFile,EMbmQcontentIcon2,EMbmQcontentIcon2mask);

			// save temp copy (non owned) for example clone in case 3
			savedIcon=icon;
			break;

		case 4:
			// create a clone of the icon instead of recreating from stored content
			icon=savedIcon->CloneL();
			break;

		case 5:
			{ // clone via a GulIcon (which we only create temporarily for this example app)
			CGulIcon* gi=iEikonEnv->CreateIconL(KMbmFile,EMbmQcontentIcon3,EMbmQcontentIcon3mask);
			CleanupStack::PushL(gi);

			// creating a CQikContent from a previously create CGilIcon
			icon=new(ELeave)CQikContent;
			CleanupStack::PushL(icon);
			icon->CloneL(gi);
			CleanupStack::Pop(icon);

			// normally for this example app purposes only
			CleanupStack::PopAndDestroy(gi);
			break;
			}

		default:
			// Copy the content of the ExampleFiles folder to your emulator c:\ folder for these to load
			// successfully.

			// load content from external file format - these all work in emulator, but we only want one.
			// NOTE THAT YOU SHOULD NOT HARD CODE PATHS IN COMMERICAL QUALITY APPS - e.g. user installs onto
			// different drive. This is for DEMO purposes only
			{
			TRAPD(err, 
//				icon=CQikContent::NewL(NULL,_L("c:\\Private\\EDEAD023\\Image.bmp"));
//				icon=CQikContent::NewL(NULL,_L("c:\\Private\\EDEAD023\\Image.gif"));
				icon=CQikContent::NewL(NULL,_L("c:\\Private\\EDEAD023\\Image.jpg"));
//				icon=CQikContent::NewL(NULL,_L("c:\\Private\\EDEAD023\\Image.png"));
//				icon=CQikContent::NewL(NULL,_L("c:\\Private\\EDEAD023\\Image.wbmp"));
//				icon=CQikContent::NewL(NULL,_L("c:\\Private\\EDEAD023\\Image.wmf"));
// UIQ 3.1 additionally supports SVG. (UIQ 3.0 does not support SVG)
				);
			if (err!=KErrNone)
				{
				_LIT(KMissingFile,"Missing Img, copy ExampleFiles to c:\\");
				iEikonEnv->InfoMsg(KMissingFile);
				}							
			break;
			}

			}
		CleanupStack::PushL(icon);
		lbData->AddIconL(icon,EQikListBoxSlotRightSmallIcon1);
		CleanupStack::Pop(icon); // since lbData now taken ownership

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

	iEngine=new(ELeave)CAppEngine;
	TBuf<KMaxListItemText>bb;
	const TInt KListView1Items=7;
	for (TInt i=0;i<KListView1Items;i++)
		{
		iEikonEnv->ReadResourceL(bb,R_STR_LIST_CONTENT_1+i);
		iEngine->SetListItem(bb);
		}

	// We create and add a list view.
	CAppSpecificListView* list=new(ELeave)CAppSpecificListView(*this,iEngine);
	CleanupStack::PushL(list);
	list->ConstructL();
	AddViewL(*list);	// takes ownership
	CleanupStack::Pop(list);

	SetDefaultViewL(*list);
	}

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