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

📄 pbmainview.cpp

📁 symbian中讲述的一个有关bookengine的简单例子
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CPBMainView from CAknView
*  Part of  : PBMain
*  Copyright (c) 2003 Nokia. All rights reserved.
* ============================================================================
*/

// INCLUDE FILES
#include <aknviewappui.h>
#include <avkon.hrh>
#include <akntitle.h>
#include <akncontext.h> 
#include <eikaufty.h>
#include <eikmenup.h>

#include "BookEngine.h"
#include <MyHello.mbg>
#include <MyHello.rsg>
#include "MyHello.hrh"
#include "PBMainView.h"
#include "PBMainContainer.h" 
#include "MyHelloAppUi.h"
#include "GridView.h"

#define MEM_FREE(p) if (NULL != p){delete p; p = NULL;}

// ================= MEMBER FUNCTIONS =======================

// C++ default constructor can NOT contain any code, that
// might leave.
//
CPBMainView* CPBMainView::NewL(/*MPBObserver* aPBObserver*/)
{
    CPBMainView* self = NewLC(/*aPBObserver*/);
    CleanupStack::Pop(self);
    return self;
}

CPBMainView* CPBMainView::NewLC(/*MPBObserver* aPBObserver*/)
{
    CPBMainView* self = new (ELeave) CPBMainView(/*aPBObserver*/);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

CPBMainView::CPBMainView(/*MPBObserver* aPBObserver*/)
{
	m_pBookEngine = NULL;
	// m_pBookObserver = aPBObserver;
}

// EPOC default constructor can leave.
void CPBMainView::ConstructL()
{
    BaseConstructL(R_PBMAIN_VIEW);
}

// Destructor
CPBMainView::~CPBMainView()
{
}

// ---------------------------------------------------------
// TUid CPBMainView::Id()
// Returns Id of the view.
// ---------------------------------------------------------
TUid CPBMainView::Id() const
{
    return KViewId8;
}

// ---------------------------------------------------------
// CPBMainView::HandleCommandL(TInt aCommand)
// Handles commands
// ---------------------------------------------------------
void CPBMainView::HandleCommandL(TInt aCommand)
{ 
	//	TInt a;
	switch(aCommand)
	{
	case EMyHelloCmdPBMainNew:
		{
			CreateNewContact();
		}
		break;
	case EMyHelloCmdPBMainDelete:
		{
			TInt nLstIndex = iContainer->GetSelectedIndexL();
			if (nLstIndex >= 0)
			{
				DeleteContact();
			}
		}
		break;
	case EMyHelloCmdPBMainBack:

	case EMyHelloCmdPBMainExit:
		AppUi()->ActivateLocalViewL(KViewId4);
		break;
	default:
		AppUi()->HandleCommandL(aCommand);
		break;
	}
}

// ---------------------------------------------------------
// CPBMainView::HandleClientRectChange()
// Handles client rect change.
// ---------------------------------------------------------
void CPBMainView::HandleClientRectChange()
{
    if (iContainer)
	{
        iContainer->SetRect(ClientRect());
	}
}

// ---------------------------------------------------------
// CPBMainView::DoActivateL(...)
// Creates the Container class object.
// ---------------------------------------------------------
void CPBMainView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
							  TUid /*aCustomMessageId*/,
							  const TDesC8& /*aCustomMessage*/)
{
	//Add a Icon
	TBuf<KMaxPath> pathMbm;
#ifdef __WINS__
	pathMbm.Copy(_L("z:\\system\\apps\\MyHello\\MyHello.mbm"));
#else
	CMyHelloAppUi* pApp = (CMyHelloAppUi*)CEikonEnv::Static()->AppUi();
	pApp->GetAppPath(pathMbm);
	pathMbm.Append(_L("MyHello.mbm"));
#endif
	
	TUid contextPaneUid;
	contextPaneUid.iUid = EEikStatusPaneUidContext;	
	
	//get the pointer of statuspane
	CEikStatusPane* statusPane = StatusPane();
	CAknContextPane* contextPane = (CAknContextPane*) statusPane->ControlL(contextPaneUid);
	CFbsBitmap* bitmap = iEikonEnv->CreateBitmapL(pathMbm, EMbmMyhelloSnoopy_1_icon);
	contextPane->SetPicture(bitmap);
	
	//modify Chinese title
	TBuf<32> sTmpTitle;//define a descriptor for read resource of title
	CEikonEnv::Static()->ReadResource(sTmpTitle, R_QTN_IM_PBMAIN_CAPTION);
	
	//get the pointer of titlepane
	CAknTitlePane* titlePane = (CAknTitlePane*) statusPane->ControlL(TUid::Uid(EEikStatusPaneUidTitle));
	
	//set title text
	titlePane->SetTextL(sTmpTitle);
	
	iContainer = new (ELeave) CPBMainContainer;
    iContainer->SetMopParent(this);
	
	//define a rectangle, the size and point is the same of clientrect;
	TRect rc;
	rc.SetRect(ClientRect().iTl, TSize(176, 144));
	
	// initialize container instance by the second phase construction
    iContainer->ConstructL(rc);
	m_pBookEngine = CBookEngine::NewL();
	
	//更新动态数组中的名片
	GetContactFromPB();
	iContainer->SetItemToListBox(m_pBookInfoArray);
	
	//Adds a control to the control stack
	AppUi()->AddToStackL(*this, iContainer);
}

// ---------------------------------------------------------
// CPBMainView::DoDeactivate()
// Deletes the Container class object.
// ---------------------------------------------------------
void CPBMainView::DoDeactivate()
{
    if (iContainer)
    {
		AppUi()->RemoveFromStack(iContainer);
		MEM_FREE(iContainer);
    }
	
	MEM_FREE(m_pBookEngine);
	//动态数组重置并销毁
	m_pBookInfoArray.ResetAndDestroy();
}

//更新动态数组中的名片
void CPBMainView::GetContactFromPB()
{
	m_pBookInfoArray.Reset();	//删除后重置
	m_pBookEngine->UpdatePhoneBook(0,m_pBookInfoArray);
}

//创建新名片
void CPBMainView::CreateNewContact()
{
	TBuf<16> firstname;
	TBuf<16> lastname;
	TBuf<16> phonenumber;
	
	// 	iContainer->GetFirstName(firstname);
	// 	iContainer->GetLastName(lastname);
	// 	iContainer->GetPhoneNumber(phonenumber);
	firstname.Copy(_L("Good"));
	lastname.Copy(_L("Mao"));
	phonenumber.Copy(_L("13812341234"));
	
	
	if ((firstname.Length()==0) && (lastname.Length()==0) && (phonenumber.Length())==0)
	{
		//AppUi()->ActivateLocalViewL(TUid::Uid(8));
		return;
	}
	
	CBookEngine* pEngine = CBookEngine::NewL();
	RPointerArray<CBookInfo> szBookInfoArray;
	CBookInfo* pBookInfo = CBookInfo::NewL();
	
	pBookInfo->SetFirstName(lastname);
	pBookInfo->SetLastName(firstname);
	pBookInfo->SetTelephone(phonenumber);
	pBookInfo->SetItemID(-1);
 			
	szBookInfoArray.Append(pBookInfo);
	
	pEngine->UpdatePhoneBook(1, szBookInfoArray);
	
	MEM_FREE(pEngine);
	for (TInt i=0; i<szBookInfoArray.Count(); i++)
	{
		delete szBookInfoArray[i];
	}
	szBookInfoArray.Close();

	//更新动态数组中的名片
	GetContactFromPB();
	//刷新显示内容
	RefreshListBox(0);
}

//删除名片
void CPBMainView::DeleteContact()
{
	if (m_pBookInfoArray.Count() <= 0) 
	{
		return;
	}

	TInt nResult;
	iContainer->GetDeleteResult(nResult);

	if (nResult) 
	{
		TInt nIndex = iContainer->m_pListBox->CurrentItemIndex();
		
		if (nIndex >= 0)
		{
			//从数据库中删除一个名片
			m_pBookEngine->DeleteUserFromBook(m_pBookInfoArray[nIndex]->GetItemID());
			//更新动态数组中的名片
			GetContactFromPB();
			//刷新显示内容
			RefreshListBox(nIndex);
		}
	}
}

//刷新显示内容
void CPBMainView::RefreshListBox(TInt aIndex)
{
	if (m_pBookInfoArray.Count() != 0)
	{		
		iContainer->SetItemToListBox(m_pBookInfoArray);
		
		if (aIndex != 0)
		{
			iContainer->m_pListBox->SetCurrentItemIndexAndDraw(aIndex-1);
		}
		else
		{
			iContainer->m_pListBox->SetCurrentItemIndexAndDraw(0);
			iContainer->ShowNoCard();
		}
	}
	else
	{
		iContainer->SetItemToListBox(m_pBookInfoArray);
	}
	iContainer->m_pListBox->DrawNow();
}

void CPBMainView::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
{
}
	// End of File
	

⌨️ 快捷键说明

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