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

📄 multimediadlg.cpp

📁 Dream.exe soft source (Visual C++)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	/* Load picture in QT format */
	if (NewImage.loadFromData(&vecbyCurPict.vecbRawData[0], iPicSize))
	{
		/* Set new picture in source factory and set it in text control */
		QMimeSourceFactory::defaultFactory()->setImage("MOTSlideShowimage",
			NewImage.convertToImage());

		TextBrowser->setText("<center><img source=\"MOTSlideShowimage\">"
			"</center>");
	}
	else
	{
		/* Show text that tells the user of load failure */
		TextBrowser->setText("<br><br><center><b>" + tr("Image could not be "
			"loaded, ") +
			 QString(vecbyCurPict.strFormat.c_str()) +
			 tr("-format not supported by this version of QT!") +
			"</b><br><br><br>" + tr("If you want to view the image, "
			"save it to file and use an external viewer") + "</center>");
	}

	/* Add tool tip showing the name of the picture */
	if (vecRawImages[iCurImagePos].strName.length() != 0)
	{
		QToolTip::add(TextBrowser,
			QString(vecRawImages[iCurImagePos].strName.c_str()));
	}

	UpdateAccButtonsSlideShow();
}

void MultimediaDlg::UpdateAccButtonsSlideShow()
{
	/* Set enable menu entry for saving a picture */
	if (iCurImagePos < 0)
	{
		pFileMenu->setItemEnabled(0, FALSE);
		pFileMenu->setItemEnabled(1, FALSE);
		pFileMenu->setItemEnabled(2, FALSE);
	}
	else
	{
		pFileMenu->setItemEnabled(0, TRUE);
		pFileMenu->setItemEnabled(1, TRUE);
		pFileMenu->setItemEnabled(2, TRUE);
	}

	if (iCurImagePos <= 0)
	{
		/* We are already at the beginning */
		PushButtonStepBack->setEnabled(FALSE);
		PushButtonJumpBegin->setEnabled(FALSE);
	}
	else
	{
		PushButtonStepBack->setEnabled(TRUE);
		PushButtonJumpBegin->setEnabled(TRUE);
	}

	if (iCurImagePos == GetIDLastPicture())
	{
		/* We are already at the end */
		PushButtonStepForw->setEnabled(FALSE);
		PushButtonJumpEnd->setEnabled(FALSE);
	}
	else
	{
		PushButtonStepForw->setEnabled(TRUE);
		PushButtonJumpEnd->setEnabled(TRUE);
	}

	LabelCurPicNum->setText(QString().setNum(iCurImagePos + 1) + "/" +
		QString().setNum(GetIDLastPicture() + 1));

	/* If no picture was received, show the following text */
	if (iCurImagePos < 0)
	{
		/* Init text browser window */
		TextBrowser->setText("<center><h2>" +
			tr("MOT Slideshow Viewer") + "</h2></center>");
	}
}

void MultimediaDlg::OnSave()
{
	QString strFileName;
	QString strDefFileName;

	switch (eAppType)
	{
	case CDataDecoder::AT_MOTSLISHOW:
		/* Show "save file" dialog */
		/* Set file name */
		strDefFileName = vecRawImages[iCurImagePos].strName.c_str();

		/* Use default file name if no file name was transmitted */
		if (strDefFileName.length() == 0)
			strDefFileName = "RecPic";

		strFileName =
			QFileDialog::getSaveFileName(strDefFileName + "." +
			QString(vecRawImages[iCurImagePos].strFormat.c_str()),
			"*." + QString(vecRawImages[iCurImagePos].strFormat.c_str()), this);

		/* Check if user not hit the cancel button */
		if (!strFileName.isNull())
			SavePicture(iCurImagePos, strFileName);
		break;

	case CDataDecoder::AT_JOURNALINE:
		/* Save to file current journaline page */
		QString strTitle("");
		QString strItems("");

		/* TRUE = without html links */
		ExtractJournalineBody(iCurJourObjID, TRUE, strTitle, strItems);

		/* Prepare HTML page for storing the content (header, body tags, etc) */
		QString strJornalineText = "<html>\n<head>\n"
			"<meta http-equiv=\"content-Type\" "
			"content=\"text/html; charset=utf-8\">\n<title>" + strTitle +
			"</title>\n</head>\n\n<body>\n<table>\n"
			"<tr><th>" + strTitle + "</th></tr>\n"
			"<tr><td><ul type=\"square\">" + strItems + "</ul></td></tr>\n"
			"</table>\n</body>\n</html>";

		strFileName = QFileDialog::getSaveFileName(strTitle + ".html",
			"*.html", this);

		if (!strFileName.isNull())
		{
			/* Save Journaline page as a text stream */
			QFile FileObj(strFileName);

			if (FileObj.open(IO_WriteOnly))
			{
				QTextStream TextStream(&FileObj);
				TextStream << strJornalineText; /* Actual writing */
				FileObj.close();
			}
		}
		break;
	}
}

void MultimediaDlg::OnSaveAll()
{
	/* Let the user choose a directory */
	QString strDirName =
		QFileDialog::getExistingDirectory(NULL, this);

	if (!strDirName.isNull())
	{
		/* Loop over all pictures received yet */
		for (int j = 0; j < GetIDLastPicture() + 1; j++)
		{
			QString strFileName = vecRawImages[j].strName.c_str();

			if (strFileName.length() == 0)
			{
				/* Construct file name from date and picture number (default) */
				strFileName = "Dream_" + QDate().currentDate().toString() +
					"_#" + QString().setNum(j);
			}

			/* Add directory and ending */
			strFileName = strDirName + strFileName + "." +
				QString(vecRawImages[j].strFormat.c_str());

			SavePicture(j, strFileName);
		}
	}
}

void MultimediaDlg::SavePicture(const int iPicID, const QString& strFileName)
{
	/* Get picture size */
	const int iPicSize = vecRawImages[iPicID].vecbRawData.Size();

	/* Open file */
	FILE* pFiBody = fopen(strFileName.latin1(), "wb");

	if (pFiBody != NULL)
	{
		for (int i = 0; i < iPicSize; i++)
		{
			fwrite((void*) &vecRawImages[iPicID].vecbRawData[i],
				size_t(1), size_t(1), pFiBody);
		}

		/* Close the file afterwards */
		fclose(pFiBody);
	}
}

void MultimediaDlg::ClearAllSlideShow()
{
	/* Init vector which will store the received images with zero size */
	vecRawImages.Init(0);

	/* Init current image position */
	iCurImagePos = -1;

	/* Update GUI */
	UpdateAccButtonsSlideShow();

	/* Remove tool tips */
	QToolTip::remove(TextBrowser);
}

void MultimediaDlg::InitNotSupported()
{
	/* Hide all controls, disable menu items */
	pFileMenu->setItemEnabled(0, FALSE);
	pFileMenu->setItemEnabled(1, FALSE);
	pFileMenu->setItemEnabled(2, FALSE);
	PushButtonStepForw->hide();
	PushButtonJumpBegin->hide();
	PushButtonJumpEnd->hide();
	LabelCurPicNum->hide();
	PushButtonStepBack->hide();
	QToolTip::remove(TextBrowser);

	/* Show that application is not supported */
	TextBrowser->setText("<center><h2>" + tr("No data service or data service "
		"not supported.") + "</h2></center>");
}

void MultimediaDlg::InitMOTSlideShow()
{
	/* Make all browse buttons visible */
	PushButtonStepBack->show();
	PushButtonStepForw->show();
	PushButtonJumpBegin->show();
	PushButtonJumpEnd->show();
	LabelCurPicNum->show();

	/* Set current image position to the last picture and display it (if at
	   least one picture is available) */
	iCurImagePos = GetIDLastPicture();
	if (iCurImagePos >= 0)
		SetSlideShowPicture();
	else
	{
		/* Remove tool tips */
		QToolTip::remove(TextBrowser);
	}

	/* Update buttons and menu */
	UpdateAccButtonsSlideShow();
}

void MultimediaDlg::InitJournaline()
{
	/* Disable "clear all" menu item */
	pFileMenu->setItemEnabled(0, FALSE);

	/* Disable "save" menu items */
	pFileMenu->setItemEnabled(1, FALSE);
	pFileMenu->setItemEnabled(2, FALSE);

	/* Only one back button is visible and enabled */
	PushButtonStepForw->hide();
	PushButtonJumpBegin->hide();
	PushButtonJumpEnd->hide();
	LabelCurPicNum->hide();

	/* Show back button and disable it because we always start at the root
	   object */
	PushButtonStepBack->show();
	PushButtonStepBack->setEnabled(FALSE);

	/* Init text browser window */
	iCurJourObjID = 0;
	SetJournalineText();

	/* Remove tool tips */
	QToolTip::remove(TextBrowser);

	NewIDHistory.Reset();
}

void MultimediaDlg::JpgToPng(CMOTObject& NewPic)
{
#ifdef HAVE_LIBFREEIMAGE
	/* This class is needed for FreeImage load and save from memory. This code
	   is based on an example code shipped with FreeImage library */
	class MemIO : public FreeImageIO
	{
	public :
		/* Assign function pointers in constructor */
		MemIO(CVector<_BYTE> vecNewData) : vecbyData(vecNewData), iPos(0)
			{read_proc  = _ReadProc; write_proc = _WriteProc;
			tell_proc = _TellProc; seek_proc = _SeekProc;}
		CVector<_BYTE>& GetData() {return vecbyData;}
		void Reset() {iPos = 0;}

		static long DLL_CALLCONV _TellProc(fi_handle handle)
			{return ((MemIO*) handle)->iPos;} /* Return current position */

		static unsigned DLL_CALLCONV _ReadProc(void* buffer, unsigned size,
			unsigned count, fi_handle handle)
		{
			MemIO* memIO = (MemIO*) handle;
			_BYTE* tmpBuf = (_BYTE*) buffer;

			/* Copy new data in internal storage vector. Write at current iPos
			   and increment position. Check for out-of-range, too */
			for (unsigned int c = 0; c < count; c++)
			{
				for (unsigned int i = 0; i < size; i++)
				{
					if (memIO->iPos < memIO->vecbyData.Size())
						*tmpBuf++ = memIO->vecbyData[memIO->iPos++];
				}
			}

			return count;
		}

		static unsigned DLL_CALLCONV _WriteProc(void* buffer, unsigned size,
			unsigned count, fi_handle handle)
		{
			MemIO* memIO = (MemIO*) handle;
			_BYTE* tmpBuf = (_BYTE*) buffer;

			/* Make sure, enough space is available */
			const long int iSpaceLeft =
				memIO->vecbyData.Size() - (memIO->iPos + size * count);

			if (iSpaceLeft < 0)
				memIO->vecbyData.Enlarge(-iSpaceLeft);

			/* Copy data */
			for (unsigned int c = 0; c < count; c++)
			{
				for (unsigned int i = 0; i < size; i++)
					memIO->vecbyData[memIO->iPos++] = *tmpBuf++;
			}

			return count;
		}

		static int DLL_CALLCONV _SeekProc(fi_handle handle, long offset,
			int origin)
		{
			if (origin == SEEK_SET)
				((MemIO*) handle)->iPos = offset; /* From beginning */
			else
				((MemIO*) handle)->iPos += offset; /* From current position */

			return 0;
		}

	private:
		CVector<_BYTE>	vecbyData;
		long int		iPos;
	};

	/* Only jpeg images are converted here */
	if (NewPic.strFormat.compare("jpeg") != 0)
		return;

	/* If we use freeimage as a static library, we need to initialize it first */
	FreeImage_Initialise();

	/* Put input data in a new IO object */
	MemIO memIO(NewPic.vecbRawData);

	/* Load data from memory */
	FIBITMAP* fbmp =
		FreeImage_LoadFromHandle(FIF_JPEG, &memIO, (fi_handle) &memIO);

	/* After the reading functions, the IO must be reset for the writing */
	memIO.Reset();

	/* Actual conversion */
	if (FreeImage_SaveToHandle(FIF_PNG, fbmp, &memIO, (fi_handle) &memIO))
	{
		/* Get converted data and set new format string */
		NewPic.vecbRawData.Init(memIO.GetData().Size()); /* Size has certainly
															changed */
		NewPic.vecbRawData = memIO.GetData(); /* Actual copying */
		NewPic.strFormat = "png"; /* New format string */
	}
#endif
}

⌨️ 快捷键说明

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