183 lines
4.9 KiB
C++
183 lines
4.9 KiB
C++
/*
|
|
* SaveWorkflowAsImageCmd.cpp
|
|
*
|
|
* Created on: 2012-10-19
|
|
* Author: shenpenglin
|
|
*/
|
|
#include <QFileDialog>
|
|
#include <QPainter>
|
|
#include <QImageWriter>
|
|
|
|
#include "SaveWorkflowAsImageCmd.h"
|
|
#include "SaveHelper.h"
|
|
#include "PaiWorkflowDataModel.h"
|
|
#include "WorkflowWidget.h"
|
|
#include "ConsoleGUIService.h"
|
|
#include "WorkflowSceneManager.h"
|
|
#include "PaiFileDialog.h"
|
|
#include "PaiSettings.h"
|
|
#include "PaiMessageBox.h"
|
|
#include "TimeStampLogger.h"
|
|
|
|
using namespace pai::gui;
|
|
using namespace pai::objectmodel;
|
|
using namespace pai::graphics2d;
|
|
|
|
namespace pai
|
|
{
|
|
|
|
SaveWorkflowAsImageCmd::SaveWorkflowAsImageCmd(QUuid viewID, QUndoCommand *parent) :
|
|
QUndoCommand(parent),
|
|
m_ViewID(viewID)
|
|
{
|
|
}
|
|
|
|
SaveWorkflowAsImageCmd::~SaveWorkflowAsImageCmd()
|
|
{
|
|
}
|
|
|
|
void SaveWorkflowAsImageCmd::undo()
|
|
{
|
|
|
|
}
|
|
|
|
void SaveWorkflowAsImageCmd::redo()
|
|
{
|
|
pai::gui::TimeStampLogger::GetInstance().WriteGUIBenchMarkLog("Workflow", true, "Save workflow as image");
|
|
|
|
SaveWorkflowAsImageHelper();
|
|
|
|
pai::gui::TimeStampLogger::GetInstance().WriteGUIBenchMarkLog("Workflow", false, "Save workflow as image");
|
|
}
|
|
|
|
bool SaveWorkflowAsImageCmd::SaveWorkflowAsImageHelper()
|
|
{
|
|
// get active workflow name
|
|
QString workflowName;
|
|
if(SaveHelper::GetActiveWorkflowView())
|
|
{
|
|
PaiWorkflowDataModel * pWorkflow = SaveHelper::GetActiveWorkflowView()->GetWorkflow();
|
|
if (pWorkflow == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int moduleCount = pWorkflow->GetModuleCount();
|
|
|
|
if (moduleCount == 0)
|
|
{
|
|
return false; // if there are no modules return
|
|
}
|
|
|
|
workflowName = pWorkflow->GetName();
|
|
}
|
|
|
|
// get active workflow view
|
|
WorkflowWidget* pActiveWorkflowView = SaveHelper::GetActiveWorkflowView();
|
|
|
|
if (pActiveWorkflowView == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
QRectF rect = pActiveWorkflowView->GetScene()->sceneRect();
|
|
|
|
QPixmap pixmap(QSize(rect.width(), rect.height()));
|
|
if (pixmap.isNull())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
pixmap.fill(Qt::white);
|
|
QPainter painter(&pixmap);
|
|
|
|
pActiveWorkflowView->GetScene()->render(&painter);
|
|
|
|
PaiSettings settings;
|
|
QString path = settings.value("Save_File", QDir::homePath()).toString();
|
|
|
|
QStringList filters;
|
|
filters << "Images (*.png *.jpg *.xpm)"
|
|
<< "All Files (*)";
|
|
|
|
QString defaultName = workflowName;
|
|
|
|
PaiFileDialog saveAsPictureDlg((QWidget*)pActiveWorkflowView,
|
|
QObject::tr("Save File"), path,
|
|
"Images (*.png *.jpg *.xpm)",
|
|
QString("%1.png").arg(defaultName));
|
|
saveAsPictureDlg.SetNameFilters(filters);
|
|
saveAsPictureDlg.SetAcceptMode(AcceptSave);
|
|
saveAsPictureDlg.SetAutoClearFileName(false);
|
|
|
|
while (QDialog::Accepted == saveAsPictureDlg.exec())
|
|
{
|
|
QString fileName = saveAsPictureDlg.SelectedFiles().at(0);
|
|
settings.setValue("Save_File", saveAsPictureDlg.GetSelectedPath());
|
|
|
|
if (fileName.isEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
QList<QByteArray> lstImageEnds = QImageWriter::supportedImageFormats();
|
|
|
|
bool bFindWithEnd = false;
|
|
bool bCanSave = false;
|
|
for (int i = 0; i < lstImageEnds.size(); ++i)
|
|
{
|
|
bFindWithEnd = fileName.endsWith("." + lstImageEnds[i], Qt::CaseInsensitive);
|
|
if (bFindWithEnd == true)
|
|
{
|
|
bCanSave = true;
|
|
break;
|
|
}
|
|
}
|
|
if (bFindWithEnd == false)
|
|
{
|
|
fileName += ".png";
|
|
}
|
|
|
|
if (bCanSave == false)
|
|
{
|
|
bCanSave = true;
|
|
QFileInfo fi(fileName);
|
|
if (fi.exists() && (!fi.isWritable()))
|
|
{
|
|
PaiMessageBox::Critical(&saveAsPictureDlg, QObject::tr("Error"),
|
|
QObject::tr("You do not have the permission necessary to save the file. Please select another one."),
|
|
PaiMessageBox::Ok);
|
|
bCanSave = false;
|
|
}
|
|
else if (fi.exists())
|
|
{
|
|
int ret = PaiMessageBox::Warning(&saveAsPictureDlg, QObject::tr("Warning"), QObject::tr(
|
|
"The filename already exist! Do you want to overwrite the old one?"),
|
|
PaiMessageBox::Yes | PaiMessageBox::Cancel, PaiMessageBox::Yes);
|
|
if (PaiMessageBox::Yes != ret)
|
|
{
|
|
bCanSave = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//do nothing
|
|
}
|
|
}
|
|
// save the file
|
|
if (bCanSave == true)
|
|
{
|
|
if (!pixmap.save(fileName))
|
|
{
|
|
PaiMessageBox::Critical(&saveAsPictureDlg, QObject::tr("Error"),
|
|
QObject::tr("Save workflow as image failed, as it was not possible to write to %1.").arg(fileName)
|
|
, PaiMessageBox::Ok);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|