different qundocommands changing the qUndoStack order



  • im having two different QUndoCommands
    JobEntryCommand: which will be pushed to the QUndoStack on creating an item in the graphicsView. so its redo will create the item and redo will remove it from the graphicsview.

    JobEntryCommand::JobEntryCommand(JobFlowItem *item, JobContainerWidget *container, QUndoCommand *parent)
    : QUndoCommand(parent),m_pContainer(container)
    {
    m_JbItem = NULL;
    m_jobId = -1;

    if(item)
    {
        m_JbItem = item;
        m_structdata.JobName = m_JbItem->getParentJob()->GetJobName();
    
        m_structdata.level = m_JbItem->getItemLevel();
        m_structdata.ParentJobPath = m_JbItem->GetPath();
        m_structdata.ParentLevel = m_JbItem->getItemLevel()-1;
        m_structdata.ParentName = "";
        m_structdata.layoutIndex = m_pContainer->getItemIndex(m_JbItem) -1; //parent index
        m_structdata.JobId = m_JbItem->GetJobId();
    }
    

    }

    void JobEntryCommand::undo()
    {

    if(m_JbItem)
    {
    
        if(!m_pContainer->isItemPresentInContainer(m_JbItem))
        {
    
            m_JbItem = m_pContainer->getJobFromId(m_structdata.JobId);
    
    
        }
    
    
        m_structdata.JobName = m_JbItem->getParentJob()->GetJobName();
    
        m_structdata.level = m_JbItem->getItemLevel();
        m_structdata.ParentJobPath = m_JbItem->GetPath();
        m_structdata.ParentLevel = m_JbItem->getItemLevel()-1;
        m_structdata.ParentName = "";
        m_structdata.layoutIndex = m_pContainer->getItemIndex(m_JbItem) -1; //parent index
        m_structdata.JobId = m_JbItem->GetJobId();
    
        m_childList.clear();
        QList<JobFlowItem *> listItem = m_JbItem->getChildJobs();
    
        foreach (JobFlowItem *item, listItem) {
            m_childList << item->GetJobId();
    
        }
    
        if(!m_JbItem->isMacroGroup())
        {
    
            m_pContainer->removeJob(m_JbItem);
        }
        m_JbItem = NULL;
    }
    

    }

    void JobEntryCommand::redo()
    {

    QString jobPath = m_structdata.JobPath;
    QString jobName = m_structdata.JobName;
    int level = m_structdata.level;
    int layoutIndex = m_structdata.layoutIndex; //it will return  the parent index only for undo child
    QString name = m_structdata.ParentName;
    int jobId = m_structdata.JobId;
    
    
    
    
    if(level == 0)
        m_JbItem = m_pContainer->addJobToContainer(jobName,layoutIndex + 1,false,false,jobId,true);
    else
        m_JbItem = m_pContainer->createChildren(m_structdata.ParentJobPath,layoutIndex, m_structdata.ParentLevel,jobName,false,jobId,false,true);
    
    updateChildrens();
    

    }

    DeleteJobCommand : will be pushed when any item is deleted from the graphicsView. so its redo will remove the created item and undo will recreate the item.

    DeleteJobCommand::DeleteJobCommand( JobFlowItem *item, JobContainerWidget *container, int parentLevel, QUndoCommand *parent)
    : QUndoCommand(parent),m_ParentLevel(parentLevel)
    {

    m_pJobFlowItem = item;
    m_pContainer = container;
    m_key =0;
    
    
    
    m_structdata.JobName = m_pJobFlowItem->getParentJob()->GetJobName();
    
    m_structdata.level = m_pJobFlowItem->getItemLevel();
    m_structdata.ParentJobPath = m_pJobFlowItem->GetPath();
    m_structdata.ParentLevel = m_pJobFlowItem->getItemLevel()-1;
    m_structdata.ParentName = "";
    m_structdata.layoutIndex = m_pContainer->getItemIndex(m_pJobFlowItem) -1; //parent index
    m_structdata.JobId = m_pJobFlowItem->GetJobId();
    

    // m_childItemData.clear();

    m_macroGroup = m_pJobFlowItem->isMacroGroup();
    
    
    if(!m_macroGroup)
    {
        populateChildDetails(m_pJobFlowItem);
    
    }else
    {
        m_key = m_pContainer->getMacroGroupFromItem(m_pJobFlowItem);
    }
    

    }

    DeleteJobCommand::~DeleteJobCommand()
    {

    }

    void DeleteJobCommand::undo()
    {

    try
    {
        QString jobPath = m_structdata.JobPath;
        QString jobName = m_structdata.JobName;
        int level = m_structdata.level;
        int layoutIndex = m_structdata.layoutIndex; //it will return  the parent index only for undo child
        m_currentName = m_structdata.ParentName;
        int jobId = m_structdata.JobId;
    
    
        if(level == 0)
        {
            if(!m_macroGroup)
            {
                m_pJobFlowItem = m_pContainer->addJobToContainer(jobName,layoutIndex + 1,false,false,jobId,true);
    
            }else
            {
                m_pJobFlowItem = m_pContainer->createEntryInMacroTemplate(jobName, m_key,layoutIndex + 1);
                m_macroGroup = m_pJobFlowItem->isMacroGroup();
    
            }
    
    
    
        }
        else
        {
    
    
    
            // QModelIndex parentModal = m_pContainer->GetModelIndex(m_structdata.ParentJobPath);
    
            m_pJobFlowItem = m_pContainer->createChildren(m_structdata.ParentJobPath,layoutIndex, m_structdata.ParentLevel,jobName,false,jobId,false,true);
            m_currentName = m_pJobFlowItem->getParentJob()->GetJobName();
    
        }
    
    
        if(!m_macroGroup)
        {
            //Update the childrens after creation
            updateChildrens();
    
        }
        if(m_pJobFlowItem)
        {
    

    // qDebug()<<"On copy of the jobs";
    // m_pContainer->copyJobFiles(m_pJobFlowItem);
    }
    }
    catch(...)
    {
    qDebug("Crashed");
    }

    }

    void DeleteJobCommand::redo()
    {

    try
    {
        if(m_pJobFlowItem)
        {
            if(!m_pJobFlowItem->isMacroGroup())
            {
    
    
                if(!m_pContainer->isItemPresentInContainer(m_pJobFlowItem))
                {
    
                    m_pJobFlowItem = m_pContainer->getJobFromId(m_structdata.JobId);
    
    
                }
    
                m_structdata.JobName = m_pJobFlowItem->getParentJob()->GetJobName();
    
                m_structdata.level = m_pJobFlowItem->getItemLevel();
                m_structdata.ParentJobPath = m_pJobFlowItem->GetPath();
                m_structdata.ParentLevel = m_pJobFlowItem->getItemLevel()-1;
                m_structdata.ParentName = "";
                m_structdata.layoutIndex = m_pContainer->getItemIndex(m_pJobFlowItem) -1; //parent index
                m_structdata.JobId = m_pJobFlowItem->GetJobId();
    
                populateChildDetails(m_pJobFlowItem);
                m_pContainer->removeJob(m_pJobFlowItem);
            }else
                m_pContainer->removeMacroEntry(m_key,m_pJobFlowItem);
            m_pJobFlowItem = NULL;
        }
    }
    catch(...)
    {
        qDebug("Crashed");
    }
    

    }

    so when i start adding the item, the UndoStack is pushing the created item in the order what it created using JobEntryCommand. so redo on UndoStack work in the same order like

    On Create: Job1->Job2->Job3->Job4
    On Undo: Job4->Job3->Job2->Job1

    but when i deleted any item ex: Job4 the DeleteJobCommand is been pushed to the QUndoStack which in turn change the order of the stack the order will be

    ** On delete Job1->Job2->Job3 (Job4deleted)

    On redo: Job4->Job1->Job3->Job2**
    It is shuffling the items inside the stack in an unpredictable possibilities. it happens only if i add a DeleteJobCommand on a stack having JobEntryCommand.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you sure that it happens on the stack and not the structure you use to store the jobs ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.