| 720 | */ |
| 721 | |
| 722 | void KUndo2QStack::push(KUndo2Command *cmd) |
| 723 | { |
| 724 | cmd->redoMergedCommands(); |
| 725 | |
| 726 | if (cmd->endTime().isNull()) { |
| 727 | cmd->setEndTime(); |
| 728 | } |
| 729 | |
| 730 | bool macro = !m_macro_stack.isEmpty(); |
| 731 | |
| 732 | KUndo2Command *cur = 0; |
| 733 | if (macro) { |
| 734 | KUndo2Command *macro_cmd = m_macro_stack.last(); |
| 735 | if (!macro_cmd->d->child_list.isEmpty()) |
| 736 | cur = macro_cmd->d->child_list.last(); |
| 737 | } else { |
| 738 | if (m_index > 0) |
| 739 | cur = m_command_list.at(m_index - 1); |
| 740 | while (m_index < m_command_list.size()) |
| 741 | delete m_command_list.takeLast(); |
| 742 | if (m_clean_index > m_index) |
| 743 | m_clean_index = -1; // we've deleted the clean state |
| 744 | } |
| 745 | |
| 746 | bool try_merge = cur != 0 |
| 747 | && cur->id() != -1 |
| 748 | && cur->id() == cmd->id() |
| 749 | && (macro || m_index != m_clean_index); |
| 750 | |
| 751 | /** |
| 752 | * Here we are going to try to merge several commands together using the |
| 753 | * QVector field in the commands using 3 parameters. |
| 754 | * |
| 755 | * N : Number of commands that should remain individual at the top of the |
| 756 | * stack. |
| 757 | * |
| 758 | * T1 : Timeout for the commands to be outdated and merged |
| 759 | * |
| 760 | * T2 : Time separation between two commands to be considered as belonging |
| 761 | * to the same group |
| 762 | * |
| 763 | */ |
| 764 | |
| 765 | if (!macro && m_command_list.size() > 1 && cmd->timedId() != -1 && m_useCumulativeUndoRedo) { |
| 766 | KUndo2Command* lastcmd = m_command_list.last(); |
| 767 | |
| 768 | auto tryMergeBack = |
| 769 | [this] (auto revIt, bool *isMerged) { |
| 770 | /// we have a guarantee that std::next(revIt) exists, |
| 771 | /// so we can safely iterate back in the forward order |
| 772 | auto dst = std::prev(revIt.base()); |
| 773 | auto src = std::prev(dst); |
| 774 | |
| 775 | if ((*dst)->timedId() != -1 && |
| 776 | (*dst)->timedId() == (*src)->timedId() && |
| 777 | (*src)->time().msecsTo((*dst)->endTime()) <= m_cumulativeUndoData.maxGroupDuration && |
| 778 | (*src)->endTime().msecsTo((*dst)->time()) <= m_cumulativeUndoData.maxGroupSeparation && |
| 779 | (*dst)->timedMergeWith(*src)) { |