/* * PasteModuleCmd.cpp * * Created on: 2012-10-11 * Author: dev */ #include #include #include "PasteModuleCmd.h" #include "ModuleGraphicsItem.h" #include "ModuleInformation.h" #include "ModuleConnectGraphicsItem.h" #include "PaiWorkflowDataModel.h" #include "PaiSurvey.h" #include "WorkflowSceneManager.h" #include "PAIConst.h" #include "GlobalModuleBackup.h" #include "PaiMessageBox.h" #include "ConsoleGUIService.h" #include "WorkFlowFile.h" #include "GlobalWorkflowItems.h" #include "CModuleInfoAndStyle.h" #include "BreakLineCmd.h" #include "SaveHelper.h" //粘贴默认间隔 const int DEFAULT_INTERVAL = 60; using namespace pai; using namespace pai::graphics2d; using namespace pai::objectmodel; using namespace pai::gui; using namespace pai::workflow; namespace pai { namespace graphics2d { bool descend(int a, int b) { return a > b; } PasteModuleCmd::PasteModuleCmd(pai::graphics2d::WorkflowSceneManager *pSceneManager, pai::objectmodel::PaiWorkflowDataModel *pWorkflow, QList list, QPointF point, QRectF boundingRect, QUndoCommand *pParent) :QUndoCommand(pParent) { m_pSceneManager = pSceneManager; m_pWorkflow = pWorkflow; m_moduleInfoStyleList = GlobalWorkflowItems::GetInstance()->GetModuleInfoAndStyleList(list); m_connectionList = GlobalWorkflowItems::GetInstance()->GetConnectLineList(list); m_point = point; m_boundingRect = boundingRect; m_bPasteFromWorkflow = false; m_breakLine = NULL; } PasteModuleCmd::PasteModuleCmd(pai::graphics2d::WorkflowSceneManager *pSceneManager, pai::objectmodel::PaiWorkflowDataModel *pWorkflowDest, pai::objectmodel::PaiWorkflowDataModel *pWorkflowSrc, QPointF point, QRectF boundingRect, QUndoCommand *pParent) :QUndoCommand(pParent) { m_pSceneManager = pSceneManager; m_pWorkflow = pWorkflowDest; m_moduleInfoStyleList = GetModuleInfoAndStyleList(pWorkflowSrc); m_connectionList = GetConnectLineList(pWorkflowSrc); m_point = point; m_boundingRect = boundingRect; m_bPasteFromWorkflow = true; m_breakLine = NULL; } PasteModuleCmd::~PasteModuleCmd() { if(m_breakLine) { delete m_breakLine; m_breakLine = NULL; } qDeleteAll(m_connectionList); m_connectionList.clear(); qDeleteAll(m_moduleInfoStyleList); m_moduleInfoStyleList.clear(); } void PasteModuleCmd::undo() { if (m_pastedStepIdList.count() > 0) { qSort(m_pastedStepIdList.begin(), m_pastedStepIdList.end(), descend); } //删除连线 if(m_breakLine) { m_breakLine->redo(); } m_pSceneManager->WaitThreadForDone(); foreach (int stepId, m_pastedStepIdList) { GetGlobalModuleBackup()->RemoveBackupModule(m_pWorkflow->GetID(), m_pWorkflow->GetModuleByStepID(stepId)); m_pSceneManager->DeleteModule(stepId); m_pWorkflow->RemoveModule(m_pWorkflow->GetModuleByStepID(stepId)); GetGlobalModuleBackup()->UpdateBackupModuleStepIDs(m_pWorkflow->GetID()); } } void PasteModuleCmd::redo() { pasteGraphicItems(); } void PasteModuleCmd::pasteGraphicItems() { if (!m_pSceneManager) { return; } //m_pSceneManager->clear(); m_pasteLines.clear(); foreach(CModuleConnection* pConnection, m_connectionList) { CModuleConnection* m_pNewConnection = new CModuleConnection(); pConnection->CopyTo(*m_pNewConnection); m_pasteLines << m_pNewConnection; } m_pastedStepIdList.clear(); PasteModuleAndUpdateConnectLineModule(m_point); //添加连线 if(!m_breakLine) { m_breakLine = new pai::BreakLineCmd(m_pWorkflow,m_pSceneManager,m_pasteLines); } m_breakLine->undo(); } void PasteModuleCmd::PasteModuleAndUpdateConnectLineModule(QPointF point) { try { PasteModuleFromModuelInformation(point); } catch(pai::error::generic_error & e) { QString strMessage = QString("Paste module caught an exception. ").append(e.what()); PaiMessageBox::Critical(NULL, PAI_NAME, strMessage); pai::log::Error(_FLF(QObject::tr("QUndoCommand::ShowMessage ").toStdString()+strMessage.toStdString())); } } void PasteModuleCmd::PasteModuleFromModuelInformation(QPointF point) { foreach(CModuleInfoAndStyle* pInfoStyle, m_moduleInfoStyleList) { CModuleInformation* pInfOrignial = pInfoStyle->GetModuleInformation(); CModuleInformation* pInf = new CModuleInformation(); pInf->Clone(*pInfOrignial); ChangeModuleInformationIfNeeded(pInf); m_pWorkflow->AddModule(pInf); //在场景中添加模块或者连线 m_pSceneManager->AddModule(pInf,m_pWorkflow->GetModuleStyle(pInf->GetStepID())); ModuleGraphicsItem* pMod = GlobalWorkflowItems::GetInstance()->FindModule(m_pSceneManager, pInf->GetStepID()); assert(pMod != NULL); m_pastedStepIdList << pInf->GetStepID(); PaiModuleStyle* pStyle = pMod->GetModuleStyle(); int newId = pStyle->GetId(); PaiModuleStyle* pStyleOriginal = pInfoStyle->GetModuleStyle(); pStyle->Copy(*pStyleOriginal); pStyle->SetId(newId); if (m_bPasteFromWorkflow) { pStyle->SetPosition(point + pStyleOriginal->GetPosition(), pMod); } else if(point.x() < 0 || point.y() < 0) { QPointF pointCtrV(DEFAULT_INTERVAL, DEFAULT_INTERVAL); pStyle->SetPosition(pointCtrV + pStyleOriginal->GetPosition(), pMod); } else if (point.x() == 0 && point.y() == 0) { pStyle->SetPosition(pStyleOriginal->GetPosition(), pMod); }else { pStyle->SetPosition(point - m_boundingRect.topLeft() + pStyleOriginal->GetPosition(), pMod); } if(pMod && pMod->GetModule()) { m_pSceneManager->ExpandSceneRectIfNeeded(pMod->GetModule()->GetStepID()); } pMod->ensureVisible();//确保新加模块可见 if(m_pasteLines.count() != 0) { for (int i = 0; i< m_connectionList.count(); ++i) { if(m_connectionList.at(i)->GetSourceId() == pInfOrignial->GetId()) { m_pasteLines.at(i)->SetSourceId(pInf->GetId()); } else if(m_connectionList.at(i)->GetDestId() == pInfOrignial->GetId()) { m_pasteLines.at(i)->SetDestId(pInf->GetId()); } } } } } bool PasteModuleCmd::ChangeModuleInformationIfNeeded(CModuleInformation* pInf) { if (pInf->GetClassName() != "COutputModule") return false; CModuleParameter* pParam = pInf->GetModuleParameter(); PaiSurvey *pThisSurvey = SaveHelper::GetSurveyByWfDBID(m_pWorkflow->GetDBID()); if (pParam != NULL && pThisSurvey != NULL) { CParameterItem *pParaItem = pParam->GetParameterItem("files[0].filename"); if (pParaItem != NULL) { QString strCopyName = QString::fromStdString(pParaItem->GetStringValue()); int n = strCopyName.lastIndexOf('/'); QString strNewName = pThisSurvey->GetPhysicalFile(); if (strCopyName.isEmpty() == false && strCopyName.left(n) != strNewName) { strNewName.append(strCopyName.remove(0, n)); pParaItem->SetStringValue(strNewName.toStdString()); return true; } } } return false; } QList PasteModuleCmd::GetConnectLineList(pai::objectmodel::PaiWorkflowDataModel *pWorkflowSrc) { QList lineItemList; CWorkFlowFile* pWorkflowFile = pWorkflowSrc->GetWorkflowFile(); for (unsigned int i = 0; i < pWorkflowFile->GetModuleConnections()->size(); i++) { CModuleConnection* pConnection = new CModuleConnection(); pWorkflowFile->GetModuleConnections()->at(i)->CopyTo(*pConnection); lineItemList << pConnection; } return lineItemList; } QList PasteModuleCmd::GetModuleInfoAndStyleList(pai::objectmodel::PaiWorkflowDataModel *pWorkflowSrc) { QList moduleList; CWorkFlowFile* pWorkflowFile = pWorkflowSrc->GetWorkflowFile(); for (unsigned int j = 0; j < pWorkflowFile->GetModuleInfos()->size(); j++) { CModuleInformation *pInfo = new CModuleInformation(); pInfo->Clone(*(pWorkflowFile->GetModuleInfos()->at(j))); PaiModuleStyle *pStyle = new PaiModuleStyle(); for (unsigned int i = 0; i < pWorkflowSrc->GetModuleStyles()->size(); i++) { if (pInfo->GetStepID() == pWorkflowSrc->GetModuleStyles()->at(i)->GetId()) { pStyle->Copy(*(pWorkflowSrc->GetModuleStyles()->at(i))); } } CModuleInfoAndStyle* pModuleInfoStyle = new CModuleInfoAndStyle(); pModuleInfoStyle->SetModuleInformation(pInfo); pModuleInfoStyle->SetModuleStyle(pStyle); //pModuleInfoStyle->SetModulePos(pStyle->GetPosition()); moduleList << pModuleInfoStyle; } return moduleList; } } }