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

📄 imageconversionview.cpp

📁 《UIQ 3 The Complete Guide》书的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		cl->SetCurrentItem(0);
		}
	else
		{
		User::LeaveIfError(err);

		// Create list of sub-types to display to the user
		CDesCArrayFlat* items=new(ELeave)CDesCArrayFlat(4);
		CleanupStack::PushL(items);
		TInt count=iImageSubTypes.Count();
		for (TInt i=0;i<count;i++)
			items->AppendL(iImageSubTypes[i]->Description());
		cl->SetArrayL(items); // takes ownership
		CleanupStack::Pop(items);
		cl->SetCurrentItem(0);
		}
	}

void CImageSaverDialog::PreLayoutDynInitL()
//
// Create the two choice lists, one for types, other for sub-types of that type
//
	{
	// firstly the primary file types
	CImageEncoder::GetImageTypesL(iImageTypes);
	CEikChoiceList* cl=static_cast<CEikChoiceList*>(Control(2));
	CDesCArrayFlat* items=new(ELeave)CDesC16ArrayFlat(4);
	CleanupStack::PushL(items);
	TInt count=iImageTypes.Count();
	for (TInt i=0;i<count;i++)
		items->AppendL(iImageTypes[i]->Description());
	cl->SetArrayL(items);
	CleanupStack::Pop(items);
	cl->SetCurrentItem(0);

	// now create list of sub-types based on the primary type
	SetImageSubTypesL();
	}

void CImageSaverDialog::HandleControlStateChangeL(TInt aControlId)
// If the user chooses a different primary save file type, re-build the sub-types list
	{
	if (aControlId==2)
		SetImageSubTypesL();
	}

TBool CImageSaverDialog::OkToExitL(TInt aButtonId)
//
// User has suggested we start saving the file....
//
	{
	// user entered name for the file
	static_cast<CEikEdwin*>(Control(1))->GetText(iName);
	if (!iName.Length())
		iEikonEnv->LeaveWithInfoMsg(R_STR_NEW_IMAGE_NO_NAME);

	// the chosen type and any subType.
	CEikChoiceList* cl=static_cast<CEikChoiceList*>(Control(2));
	iType=iImageTypes[cl->CurrentItem()]->ImageType();
	iSubType=KNullUid;
	if (iImageSubTypes.Count()>0)
		{ // if the types list has a sub-types list - extract users chosen sub-type
		cl=static_cast<CEikChoiceList*>(Control(3));
		iSubType=iImageSubTypes[cl->CurrentItem()]->SubType();
		}
	return(ETrue);
	}

//////////////////////////////////////////////////////////////////////////////////////////
void CImageDisplayControl::Draw(const TRect& aRect) const
	{
	// in UIQ 3 we are no longer required to update our Rect() if we dont have anything
	// to display. We can require our parent control do this - so we support bg themes properly.
	if (iBitmap)
		{ // weve got a loaded bitmap
		CGraphicsContext& gc=SystemGc();
		gc.SetDrawMode(CGraphicsContext::EDrawModePEN);

		// Draw as much of the bitmap as will fit in the space we have been allocated. This
		// particular implementation simply crops the image if its too big. 
		TRect rect(Rect());
		TRect r2(0,0,rect.Width(),rect.Height());
		TSize size(iBitmap->SizeInPixels());
		if (size.iWidth<r2.iBr.iX)
			{
			r2.iBr.iX=size.iWidth;
			rect.iBr.iX=rect.iTl.iX+size.iWidth;
			}
		if (size.iHeight<r2.iBr.iY)
			{
			r2.iBr.iY=size.iHeight;
			rect.iBr.iY=rect.iTl.iY+size.iWidth;
			}
		gc.DrawBitmap(rect,iBitmap,r2);
		}
	else
		{
		// in this example application we deliberately draw a red rectangle. Whilst fast you can
		// visually see when we transfer ownership of the main iBitmap and draw 
		// Commerical applications probably dont need to draw anything here, the background will
		// be resolved correctly.
		CGraphicsContext& gc=SystemGc();
		gc.SetDrawMode(CGraphicsContext::EDrawModePEN);
		gc.SetPenStyle(CGraphicsContext::ENullPen);
	   	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
		gc.SetBrushColor(KRgbRed);
		gc.DrawRect(Rect());
		}
	}

CImageDisplayControl::~CImageDisplayControl()
// We no longer want to use the image.. free up those resources
	{
	CloseImage();
	delete(iImageLoader);
	delete(iImageRotator);
	delete(iImageSaver);
	}

CImageDisplayControl::CImageDisplayControl(CAppEngine* aEngine) :
	iEngine(aEngine)
	{}

void CImageDisplayControl::ConstructL()
// Construct the componet parts required to perform the image load/display
	{
	iImageLoader=new(ELeave)CImageLoader(*this);
	iImageRotator=new(ELeave)CImageRotator(*this);
	iImageSaver=new(ELeave)CImageSaver(*this);
	}

void CImageDisplayControl::NewImageSelectedL()
//
// Open the entry currently selected in the list view
//
	{
	// remove any currently running Image load request or loaded bitmap
	iImageLoader->Cancel();
	delete(iBitmap);
	iBitmap=NULL;
	DrawNow(); // remove image from display
 
	TFileName name;
	iEngine->EntryFullName(name);
	iImageLoader->StartLoadImageL(name);
	}

void CImageDisplayControl::LoadComplete(const TInt aErr,CFbsBitmap* aBitmap)
//
// Called back by the image loader when it completes its attept to load the image from
// a file. 
//
	{
	iBitmap=aBitmap; // take ownership, regardless of success of load
	if (aErr!=KErrNone)
		{
		// failed to load a bitmap file sucessfully, no real point in displaying nothingness
		delete(iBitmap);
		iBitmap=NULL;

		// report an error to the user		
		TBuf<128>bb;
		iEikonEnv->GetErrorText(bb,aErr);
		iEikonEnv->InfoMsg(bb);
		}
	else
		{ // update our display now an image been loaded
		DrawNow();
		}	
	}

void CImageDisplayControl::CloseImage()
// We no longer want to use the image.. free up those resources
	{
	delete(iBitmap);
	iBitmap=NULL;
	}

void CImageDisplayControl::RotateComplete(const TInt aErr,CFbsBitmap* aBitmap)
//
// Called back by the image rotation when it completes. 
//
	{
	iBitmap=aBitmap; // take ownership, regardless of success of rotate
	DrawNow(); // now update display with the rotated image.
	if (aErr!=KErrNone)
		{
		// report an error to the user		
		TBuf<128>bb;
		iEikonEnv->GetErrorText(bb,aErr);
		iEikonEnv->InfoMsg(bb);
		}
	}

void CImageDisplayControl::RotateImageL()
//
// Start rotation request, ensure got an image + not currently rotating or saving the image.
//
	{
	if (iBitmap && !iImageRotator->IsActive() && !iImageSaver->IsActive())
		{
		iImageRotator->StartRotateImageL(iBitmap);
		// iImageRotator takes ownership if does not leave. If leaves we dont get here
		iBitmap=NULL;
		DrawNow(); // bitmap removed from display, whilst being manipulated.
		}
	}

void CImageDisplayControl::SaveComplete(const TInt aErr)
//
// Called back by the image saving when it completes. 
//
	{
	// we have to manually cancel the Busy message we started
	iEikonEnv->BusyMsgCancel();

	// now report success/failure.
	TBuf<128>bb;
	TInt err=aErr;
	if (err==KErrNone)
		{
		TRAP(err,
			iEikonEnv->ReadResourceL(bb,R_STR_SAVED);
		
			// add to list of entries our engine is prepared to display
			iEngine->AddEntryL(iFileName,EAppCategoryImage);
			);
		}
	if (err!=KErrNone)
		{
		// report an error to the user		
		iEikonEnv->GetErrorText(bb,aErr);

		// remove any partially saved file
		iEikonEnv->FsSession().Delete(iFileName);
		}
	iEikonEnv->InfoMsg(bb);
	}

void CImageDisplayControl::SaveImageL(const TFileName& aDrive)
// Prompt for image name and type. Start the save request..
	{
	if (iBitmap && !iImageSaver->IsActive() && !iImageRotator->IsActive())
		{
		// derive the drive and path for this applications private folder - which is 
		// where we will create the image. e.g. "c:\private\20000462\"
		TFileName installedDrive(aDrive);
		TFileName path;
		iEikonEnv->FsSession().PrivatePath(path); // does not supply a drive letter
		TParse parse;
		parse.Set(path,&installedDrive,NULL);	// add drive here
#ifdef __WINS__
		path=parse.FullName();
		path[0]='C';	// else emul points it at Z: 
		parse.Set(path,NULL,NULL);
#endif
		TUid type;
		TUid subType;
		if ((new(ELeave)CImageSaverDialog(path,type,subType))->ExecuteLD(R_IMAGE_SAVE_DIALOG))
			{
			// create full drive, path, file name
			installedDrive=parse.DriveAndPath();
			parse.Set(path,&installedDrive,NULL);

			// we probably ought to tidy up any file that we fail to save properly - so 
			// remember the file name we we can deal with issues in SaveComplete()
			iFileName=parse.FullName();

			// start the file saving. We choose to display a flashing busy msg to indicate
			// progress here, as opposed to any other visual feedback.
			iImageSaver->StartSaveImageL(parse.FullName(),type,subType,iBitmap);

			// only becomes visible if the operation takes several seconds.
			iEikonEnv->BusyMsgL(R_STR_SAVING_IMAGE);
			}
		}
	}


//////////////////////////////////////////////////////////////////////////////////////////
CImageConversionView::~CImageConversionView()
	{
	// dont really know why it does not take ownership. 
	// iContainer only set after the obj been constructed so its assumed the Control[] is
	// created successfully by then. 
	if (iContainer && iContainer->Controls().Count()>0)
		delete(iContainer->Controls().At(0).iControl);
	}

CImageConversionView::CImageConversionView(CQikAppUi& aAppUi,CAppEngine* aEngine) :
	CQikViewBase(aAppUi,KViewIdListView),iEngine(aEngine)
	{}

TVwsViewId CImageConversionView::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(KViewIdImageConversionView);
	}

void CImageConversionView::HandleCommandL(CQikCommand& aCommand)
//
// Handle the commands coming in from the controls that can deliver cmds..
//
	{
	switch (aCommand.Id())
		{
	case EAppCmdSaveAs:
		static_cast<CImageDisplayControl *>(iContainer->Controls().At(0).iControl)->SaveImageL(
												iQikAppUi.Application()->BitmapStoreName());
		break;

	case EAppCmdRotate:
		static_cast<CImageDisplayControl *>(iContainer->Controls().At(0).iControl)->RotateImageL();
		break;
	default: // e.g. the back button...
		CQikViewBase::HandleCommandL(aCommand);
		break;
		}
	}

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

	// A container type object is expected to exist. The layout manager - as defined in the
	// resource will drive the container, e.g. will call its SetRect() method. 
	CQikContainer* container = new(ELeave)CQikContainer();
	Controls().AppendLC(container);
	container->ConstructL(ETrue);
	container->SetRect(Rect());
	CleanupStack::Pop(container);
	iContainer=container;

	// we have an image display control - is is responsible for displaying any image that
	// has been loaded, handling commands etc.
	CImageDisplayControl *q=new(ELeave)CImageDisplayControl(iEngine);
	iContainer->AddControlLC(q);
	q->ConstructL();

	// we define a rather artificial region the images are allowed to exist within
	// images are simply cropped to this rect. In a more consumer friendly app this would 
	// probably want to change. 
	q->SetRect(TRect(15,20,225,180));
	CleanupStack::Pop(q);
	}

void CImageConversionView::ViewDeactivated()
//
// The view is being de-activated.
//
	{
	// we only have a single control added to the container - so we can get at it like this
	CImageDisplayControl *q=static_cast<CImageDisplayControl *>(iContainer->Controls().At(0).iControl);
	q->CloseImage();
	}

void CImageConversionView::ViewActivatedL(
//
// The view is being activated.
//
	const TVwsViewId& aPrevViewId,
	const TUid aCustomMessageId,
	const TDesC8& aCustomMessage)
	{
	// we only have a single control added to the container - so we can get at it like this
	CImageDisplayControl *q=static_cast<CImageDisplayControl *>(iContainer->Controls().At(0).iControl);
	q->NewImageSelectedL();
	}

⌨️ 快捷键说明

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