categories.cpp

来自「《UIQ 3 The Complete Guide》书的源代码」· C++ 代码 · 共 623 行 · 第 1/2 页

CPP
623
字号
//
// Categories.cpp - Categories 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 "Categories.h"
#include "Categories.hrh"
#include <Categories.rsg>
#include <Categories.mbg>

#include <QikViewBase.h>
#include <QikCommand.h>
#include <QikCommandManager.h>
#include <QikListBoxModel.h>
#include <QikListBox.h>
#include <MQikListBoxData.h>
#include <QikListBoxData.h>
#include <QikCategoryModel.h>
#include <QikEditCategoryObserver.h>
#include <QikEditCategoriesDlg.h>
#include <QikContent.h>
#include <QikZoomDlg.h>
#include <QikSimpleDialog.h>
#include <eikstart.h>
#include <eiklabel.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={0xEDEAD00C};

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

// a class representing each of the possible categories
class TAppCategoryEntry
	{
public:
	TInt iCategoryId;					// Id assigned to the category name
	TBuf<EQikCategoryNameMaxLength> iCategoryName;		// textual name of the category
	};

// a class representing each of the items within our list
const TInt KMaxListItemText=32;
class TAppListEntry
	{
public:
	TInt iCategoryId;					// category we belong to
	TBuf<KMaxListItemText> iName;		// textual name of our entry
	};

class CAppEngine : public CBase
	{
protected:
	TInt CategoryListCount() const;
	TAppCategoryEntry& CategoryListAt(const TInt aIndex);

public:
	~CAppEngine();
	void ConstructL();

	TInt EntryListCount() const;
	TAppListEntry& EntryListAt(const TInt aIndex);

	void AddCategoryL(const TInt aHandle,const TDesC& aName);
	void UpdateCategoryName(const TInt aHandle,const TDesC& aNewName);
	void DeleteCategory(const TInt aHandle);

protected:
	CArrayFixFlat<TAppCategoryEntry>* iCategoryList;	// list of categories we have
	CArrayFixFlat<TAppListEntry>* iEntryList;			// list of entries we have
	};

CAppEngine::~CAppEngine()
	{
	delete(iCategoryList);
	delete(iEntryList);
	}

// We have 5 categories defined in this application
const TInt KMaxDefaultCategories=5;

// We have 7 entries defined in this application
const TInt KMaxDefaultEntries=7;

void CAppEngine::ConstructL()
	{
	TInt i;
	CEikonEnv* env=CEikonEnv::Static();

	// create a variable array to contain the TAppCategoryEntry objects. Set its granularlity to
	// KMaxDefaultCategories, i.e. every time it reallocats it does to to hold the
	// next set of KMaxDefaultCategories elements. By doing this we get a single alloc when 
	// building our default list. Worse case scenario is we will waste KMaxDefaultCategories-1 = 4 
	// slots in the array. 
	iCategoryList=new(ELeave)CArrayFixFlat<TAppCategoryEntry>(KMaxDefaultCategories);

	// Now create a TAppCategoryEntry for each of our default categories and add to list
	// of categories we know about
	TAppCategoryEntry category;
	for (i=0;i<KMaxDefaultCategories;i++)
		{
		// we have arranged the pre-defined TAppCategoryIds and resources to be in same order
		// so we can optimise the code and perform the following:
		category.iCategoryId=i;
		env->ReadResourceL(category.iCategoryName,R_STR_CATEGORY_NAME_1+i);
		iCategoryList->AppendL(category);
		}


	// Create a varaible array to contain our list entries.
	// We have defined this to have a granularity of 4 items. We have 7 items in the default list so
	// when we build the list it will be re-allocated during the process. There is an application 
	// specific trade off between the number of times a re-alloc occurs an the number of enpty slots 
	// in the variable array. In this example we will have 1 empty slot, we have 7 predefined entries
	// and the array is allocated every 4 entries.
	iEntryList=new(ELeave)CArrayFixFlat<TAppListEntry>(4);

	TAppListEntry entry;
	for (i=0;i<KMaxDefaultEntries;i++)
		{
		// we have not specially arranged the list content name resources and categories
		// they belong to so we cant optimise the code as much as we did for the Category list.
		env->ReadResourceL(entry.iName,R_STR_LIST_CONTENT_1+i);
		switch (i)
			{
		case 0:	// South Africa
			entry.iCategoryId=EAppCategoryUnfiled;
			break;
		case 1:	// Australia
		case 4:	// new Zealand
			entry.iCategoryId=EAppCategoryAustralasian;
			break;
		case 2: // Sri Lanka
		case 3:	// India
			entry.iCategoryId=EAppCategoryAsian;
			break;
		case 5: // West Indies
			entry.iCategoryId=EAppCategoryUnfiled;
			break;
		case 6: // England
			entry.iCategoryId=EAppCategoryEuropean;
			break;
			}
		iEntryList->AppendL(entry);
		}

	}

TInt CAppEngine::CategoryListCount() const
// Report the number of items we have in the category list
	{
	return(iCategoryList->Count());
	}

TAppCategoryEntry& CAppEngine::CategoryListAt(const TInt aIndex)
// Report the aIndex'th item in the category list
	{
	return(iCategoryList->At(aIndex));
	}

TInt CAppEngine::EntryListCount() const
// Report the number of items we have in the entry list
	{
	return(iEntryList->Count());
	}

TAppListEntry& CAppEngine::EntryListAt(const TInt aIndex)
// Report the aIndex'th item in the entry list
	{
	return(iEntryList->At(aIndex));
	}

void CAppEngine::UpdateCategoryName(const TInt aHandle,const TDesC& aNewName)
// Update the textual name of the category identified by the aHandle value.
	{
	TInt count=CategoryListCount();
	for (TInt i=0;i<count;i++)
		{
		TAppCategoryEntry& category=CategoryListAt(i);
		if (category.iCategoryId==aHandle)
			{ // found the category the user has updated, store the new name
			category.iCategoryName=aNewName;
			break;
			}
		}
	}

void CAppEngine::AddCategoryL(const TInt aHandle,const TDesC& aName)
// Add a new category.
	{
	TAppCategoryEntry category;
	category.iCategoryId=aHandle;
	category.iCategoryName=aName;
	iCategoryList->AppendL(category);
	}

void CAppEngine::DeleteCategory(const TInt aHandle)
//
// Remove the category indicated. Any of our entries that belong to that category need to be
// moved into the unfiled category
//
	{
	TInt i;
	TInt count=CategoryListCount();
	for (i=0;i<count;i++)
		{
		TAppCategoryEntry& category=CategoryListAt(i);
		if (category.iCategoryId==aHandle)
			{ // found the category the user has deleted remove, it
			iCategoryList->Delete(i);
			break;
			}
		}

	// update the list entries so none refer to the deleted category
	count=EntryListCount();
	for (i=0;i<count;i++)
		{
		TAppListEntry& entry=EntryListAt(i);
		if (entry.iCategoryId==aHandle)
			entry.iCategoryId=EAppCategoryUnfiled;
		}
	}

////////////////////////////////////////////////////////////////////////////////////////////
class CAboutDialog : public CQikSimpleDialog
    {
protected:
	void PreLayoutDynInitL();
	};

// These would normally be derived from your in-house version control, this example just
// defines them here to remove such complexity from the example.
const TInt KAppMajorVersion=1;
const TInt KAppMinorVersion=0;
const TInt KAppBuild=2;

void CAboutDialog::PreLayoutDynInitL()
//
// Generate the version text - e.g. Version 1.00 (02) and set the label
//
	{
	TBuf<128>bb;
	iEikonEnv->Format128(bb,R_STR_VERSION,KAppMajorVersion,KAppMinorVersion,KAppBuild);
	CEikLabel* lbl=LocateControlByUniqueHandle<CEikLabel>(EAppLabel1);
	lbl->SetTextL(bb);	
	}

//////////////////////////////////////////////////////////////////////////////////
class CAppSpecificListView : public CQikViewBase, 
									public MQikListBoxObserver, 
									public MQikEditCategoryObserver
	{
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);

	//from MQikEditCategoryObserver
	TBool OkToAddCategory() const;
	TBool DoAddCategoryL(TInt& aHandle);
	TBool OkToRenameCategory(TInt aHandle, const TDesC& aNewName) const;
	TBool DoRenameCategoryL(TInt aHandle, const TDesC& aNewName);
	TBool OkToMergeCategories(TInt aSourceHandle, TInt aTargetHandle) const;
	TBool DoMergeCategoriesL(TInt aSourceHandle, TInt aTargetHandle);
	TBool OkToDeleteCategory(TInt aHandle) const;
	TBool DoDeleteCategoryL(TInt aHandle);
	TBool IsCategoryEmpty(TInt aHandle) const;

	void UpdateListBoxL();
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..

⌨️ 快捷键说明

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