示例#1
0
void KUndo2QStack::setUndoLimit(int limit)
{
    if (!m_command_list.isEmpty()) {
        qWarning("KUndo2QStack::setUndoLimit(): an undo limit can only be set when the stack is empty");
        return;
    }

    if (limit == m_undo_limit)
        return;
    m_undo_limit = limit;
    checkUndoLimit();
}
示例#2
0
void KUndo2QStack::endMacro()
{
    if (m_macro_stack.isEmpty()) {
        qWarning("KUndo2QStack::endMacro(): no matching beginMacro()");
        return;
    }

    m_macro_stack.removeLast();

    if (m_macro_stack.isEmpty()) {
        checkUndoLimit();
        setIndex(m_index + 1, false);
    }
}
示例#3
0
void KUndo2QStack::push(KUndo2Command *cmd)
{
    cmd->redo();

    bool macro = !m_macro_stack.isEmpty();

    KUndo2Command *cur = 0;
    if (macro) {
        KUndo2Command *macro_cmd = m_macro_stack.last();
        if (!macro_cmd->d->child_list.isEmpty())
            cur = macro_cmd->d->child_list.last();
    } else {
        if (m_index > 0)
            cur = m_command_list.at(m_index - 1);
        while (m_index < m_command_list.size())
            delete m_command_list.takeLast();
        if (m_clean_index > m_index)
            m_clean_index = -1; // we've deleted the clean state
    }

    bool try_merge = cur != 0
                        && cur->id() != -1
                        && cur->id() == cmd->id()
                        && (macro || m_index != m_clean_index);

    if (try_merge && cur->mergeWith(cmd)) {
        delete cmd;
        if (!macro) {
            emit indexChanged(m_index);
            emit canUndoChanged(canUndo());
            emit undoTextChanged(undoText());
            emit canRedoChanged(canRedo());
            emit redoTextChanged(redoText());
        }
    } else {
        if (macro) {
            m_macro_stack.last()->d->child_list.append(cmd);
        } else {
            m_command_list.append(cmd);
            checkUndoLimit();
            setIndex(m_index + 1, false);
        }
    }
}
示例#4
0
bool KUndo2QStack::push(KUndo2Command *cmd)
{
    cmd->redoMergedCommands();
    cmd->setEndTime();

    bool macro = !m_macro_stack.isEmpty();

    KUndo2Command *cur = 0;
    if (macro) {
        KUndo2Command *macro_cmd = m_macro_stack.last();
        if (!macro_cmd->d->child_list.isEmpty())
            cur = macro_cmd->d->child_list.last();
    } else {
        if (m_index > 0)
            cur = m_command_list.at(m_index - 1);
        while (m_index < m_command_list.size())
            delete m_command_list.takeLast();
        if (m_clean_index > m_index)
            m_clean_index = -1; // we've deleted the clean state
    }

    bool try_merge = cur != 0
                     && cur->id() != -1
                     && cur->id() == cmd->id()
                     && (macro || m_index != m_clean_index);

    /*!
     *Here we are going to try to merge several commands together using the QVector field in the commands using
     *3 parameters. N : Number of commands that should remain individual at the top of the stack. T1 : Time lapsed between current command and previously merged command -- signal to
     *merge throughout the stack. T2 : Time lapsed between two commands signalling both commands belong to the same set
     *Whenever a KUndo2Command is initialized -- it consists of a start-time and when it is pushed --an end time.
     *Every time a command is pushed -- it checks whether the command pushed was pushed after T1 seconds of the last merged command
     *Then the merging begins with each group depending on the time in between each command (T2).
     *
     *@TODO : Currently it is not able to merge two merged commands together.
    */
    if (!macro && m_command_list.size() > 1 && cmd->timedId() != -1 && m_useCumulativeUndoRedo) {
        KUndo2Command* lastcmd = m_command_list.last();
        if (qAbs(cmd->time().msecsTo(lastcmd->endTime())) < m_timeT2 * 1000) {
            m_lastMergedSetCount++;
        } else {
            m_lastMergedSetCount = 0;
            m_lastMergedIndex = m_index-1;
        }
        if (lastcmd->timedId() == -1){
            m_lastMergedSetCount = 0;
            m_lastMergedIndex = m_index;
        }
        if (m_lastMergedSetCount > m_strokesN) { 
            KUndo2Command* toMerge = m_command_list.at(m_lastMergedIndex);
            if (toMerge && m_command_list.size() >= m_lastMergedIndex + 1 && m_command_list.at(m_lastMergedIndex + 1)) {
                if(toMerge->timedMergeWith(m_command_list.at(m_lastMergedIndex + 1))){
                    m_command_list.removeAt(m_lastMergedIndex + 1);
                }
                m_lastMergedSetCount--;
                m_lastMergedIndex = m_command_list.indexOf(toMerge);       
            }

        }
        m_index = m_command_list.size();
        if(m_lastMergedIndex<m_index){
            if (cmd->time().msecsTo(m_command_list.at(m_lastMergedIndex)->endTime()) < -m_timeT1 * 1000) { //T1 time elapsed
                QListIterator<KUndo2Command*> it(m_command_list);
                it.toBack();
                m_lastMergedSetCount = 1;

                while (it.hasPrevious()) {
                    KUndo2Command* curr = it.previous();
                    KUndo2Command* lastCmdInCurrent = curr;

                    if (!lastcmd->mergeCommandsVector().isEmpty()) {
                        if (qAbs(lastcmd->mergeCommandsVector().last()->time().msecsTo(lastCmdInCurrent->endTime())) < int(m_timeT2 * 1000) && lastcmd != lastCmdInCurrent && lastcmd != curr) {
                            if(lastcmd->timedMergeWith(curr)){
                                if (m_command_list.contains(curr)) {
                                    m_command_list.removeOne(curr);
                                }
                             }
                        } else {
                            lastcmd = curr; //end of a merge set
                        }
                    } else {
                        if (qAbs(lastcmd->time().msecsTo(lastCmdInCurrent->endTime())) < int(m_timeT2 * 1000) && lastcmd != lastCmdInCurrent &&lastcmd!=curr) {
                            if(lastcmd->timedMergeWith(curr)){
                                if (m_command_list.contains(curr)){
                                    m_command_list.removeOne(curr);
                                }
                            }
                        } else {
                            lastcmd = curr; //end of a merge set
                        }
                    }
                }
                m_lastMergedIndex = m_command_list.size()-1;
            }
        }
        m_index = m_command_list.size();
    }   
    if (try_merge && cur->mergeWith(cmd)) {
        delete cmd;
        cmd = 0;
        if (!macro) {
            emit indexChanged(m_index);
            emit canUndoChanged(canUndo());
            emit undoTextChanged(undoText());
            emit canRedoChanged(canRedo());
            emit redoTextChanged(redoText());
        }
    } else {
        if (macro) {
            m_macro_stack.last()->d->child_list.append(cmd);
        } else {
            m_command_list.append(cmd);
            if(checkUndoLimit())
            {
                m_lastMergedIndex = m_index - m_strokesN;
            }
            setIndex(m_index + 1, false);
        }
    }
    return cmd;
}