From c50ff53b329a10bddc1b951f0f0f3d299eb92784 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Fri, 19 Oct 2018 22:18:17 +0200 Subject: [PATCH] logic: Fixed history menu to show global activation history chronologically --- .../controller/UndoRedoController.cpp | 152 +++++++++--------- .../component/controller/UndoRedoController.h | 2 + src/lib_gui/qt/window/QtMainWindow.cpp | 4 +- 3 files changed, 84 insertions(+), 74 deletions(-) diff --git a/src/lib/component/controller/UndoRedoController.cpp b/src/lib/component/controller/UndoRedoController.cpp index 92aba497..5ec435da 100644 --- a/src/lib/component/controller/UndoRedoController.cpp +++ b/src/lib/component/controller/UndoRedoController.cpp @@ -30,6 +30,9 @@ void UndoRedoController::clear() m_iterator = m_list.begin(); m_historyOffset = 0; + m_history.clear(); + + updateHistoryMenu(nullptr); updateHistory(); getView()->setUndoButtonEnabled(false); @@ -534,57 +537,56 @@ void UndoRedoController::replayCommand(std::list::iterator it) void UndoRedoController::processCommand(Command command) { - if (command.message->isReplayed()) + if (!command.message->isReplayed()) { - return; - } - - if (command.order != Command::ORDER_ACTIVATE && m_iterator == m_list.begin()) - { - return; - } - - if (command.order == Command::ORDER_ACTIVATE && command.message->keepContent()) - { - command.order = Command::ORDER_ADAPT; - } - - if (command.order == Command::ORDER_ACTIVATE) - { - m_iterator = m_list.erase(m_iterator, m_list.end()); - } - else if (command.order == Command::ORDER_ADAPT) - { - std::list::iterator end = m_iterator; - while (end != m_list.end()) + if (command.order != Command::ORDER_ACTIVATE && m_iterator == m_list.begin()) { - if (end->order == Command::ORDER_ACTIVATE) + return; + } + + if (command.order == Command::ORDER_ACTIVATE && command.message->keepContent()) + { + command.order = Command::ORDER_ADAPT; + } + + if (command.order == Command::ORDER_ACTIVATE) + { + m_iterator = m_list.erase(m_iterator, m_list.end()); + } + else if (command.order == Command::ORDER_ADAPT) + { + std::list::iterator end = m_iterator; + while (end != m_list.end()) { - break; + if (end->order == Command::ORDER_ACTIVATE) + { + break; + } + std::advance(end, 1); } - std::advance(end, 1); + + m_iterator = m_list.erase(m_iterator, end); } - m_iterator = m_list.erase(m_iterator, end); - } + m_list.insert(m_iterator, command); - m_list.insert(m_iterator, command); - - if (command.order != Command::ORDER_VIEW) - { - if (m_list.begin() != std::prev(m_iterator)) + if (command.order != Command::ORDER_VIEW) { - getView()->setUndoButtonEnabled(true); - } + if (m_list.begin() != std::prev(m_iterator)) + { + getView()->setUndoButtonEnabled(true); + } - if (m_list.end() == m_iterator) - { - getView()->setRedoButtonEnabled(false); + if (m_list.end() == m_iterator) + { + getView()->setRedoButtonEnabled(false); + } } } if (command.order == Command::ORDER_ACTIVATE) { + updateHistoryMenu(command.message); updateHistory(); } } @@ -609,22 +611,47 @@ MessageBase* UndoRedoController::lastMessage() const return std::prev(m_iterator)->message.get(); } +void UndoRedoController::updateHistoryMenu(std::shared_ptr message) +{ + const size_t historyMenuSize = 20; + + if (message && dynamic_cast(message.get())) + { + std::vector matches = dynamic_cast(message.get())->getSearchMatches(); + if (matches.size() && !matches[0].text.empty()) + { + std::vector> history = { message }; + std::set uniqueMatches = { matches[0] }; + + for (std::shared_ptr m : m_history) + { + if (uniqueMatches.insert(dynamic_cast(m.get())->getSearchMatches()[0]).second) + { + history.push_back(m); + + if (history.size() >= historyMenuSize) + { + break; + } + } + } + + m_history = history; + } + } + + Application::getInstance()->updateHistoryMenu(m_history); +} + void UndoRedoController::updateHistory() { const size_t historyListSize = 50; - const size_t historyMenuSize = 20; - std::vector historyListMatches; - std::vector> historyMenuItems; - std::set uniqueMatches; size_t index = 0; int currentIndex = -1; m_historyOffset = 0; - bool historyMenuFull = false; - bool historyListFull = false; - for (std::list::const_reverse_iterator it = m_list.rbegin(); it != m_list.rend(); it++) { if (m_iterator == it.base()) @@ -636,42 +663,22 @@ void UndoRedoController::updateHistory() { index++; - std::vector m = dynamic_cast(it->message.get())->getSearchMatches(); - if (!m.size() || m[0].text.empty()) + std::vector matches = dynamic_cast(it->message.get())->getSearchMatches(); + if (!matches.size() || matches[0].text.empty()) { continue; } - SearchMatch match = m[0]; + historyListMatches.push_back(matches[0]); - if (!historyMenuFull && uniqueMatches.insert(match).second) + if (historyListMatches.size() > historyListSize) { - historyMenuItems.push_back(it->message); - - if (historyMenuItems.size() == historyMenuSize) - { - historyMenuFull = true; - } + historyListMatches.erase(historyListMatches.begin()); + m_historyOffset++; } - if (!historyListFull) - { - historyListMatches.push_back(match); - - if (historyListMatches.size() > historyListSize) - { - historyListMatches.erase(historyListMatches.begin()); - m_historyOffset++; - } - - if (historyListMatches.size() == historyListSize && - currentIndex != -1 && currentIndex - m_historyOffset != historyListSize - 1) - { - historyListFull = true; - } - } - - if (historyMenuFull && historyListFull) + if (historyListMatches.size() == historyListSize && + currentIndex != -1 && currentIndex - m_historyOffset != historyListSize - 1) { break; } @@ -679,7 +686,6 @@ void UndoRedoController::updateHistory() } getView()->updateHistory(historyListMatches, currentIndex - m_historyOffset); - Application::getInstance()->updateHistoryMenu(historyMenuItems); } void UndoRedoController::dump() const diff --git a/src/lib/component/controller/UndoRedoController.h b/src/lib/component/controller/UndoRedoController.h index c13425b6..d6e962ab 100644 --- a/src/lib/component/controller/UndoRedoController.h +++ b/src/lib/component/controller/UndoRedoController.h @@ -124,6 +124,7 @@ private: bool sameMessageTypeAsLast(MessageBase* message) const; MessageBase* lastMessage() const; + void updateHistoryMenu(std::shared_ptr message); void updateHistory(); void dump() const; @@ -133,6 +134,7 @@ private: std::list m_list; std::list::iterator m_iterator; + std::vector> m_history; size_t m_historyOffset; }; diff --git a/src/lib_gui/qt/window/QtMainWindow.cpp b/src/lib_gui/qt/window/QtMainWindow.cpp index 7f36e5f4..0fa45003 100644 --- a/src/lib_gui/qt/window/QtMainWindow.cpp +++ b/src/lib_gui/qt/window/QtMainWindow.cpp @@ -769,7 +769,9 @@ void QtMainWindow::openHistoryAction() QAction* action = qobject_cast(sender()); if (action) { - m_history[action->data().toInt()]->dispatch(); + std::shared_ptr m = m_history[action->data().toInt()]; + m->setIsReplayed(false); + m->dispatch(); } }