From c94b5f34a33f79d578d033f7b1a39eee7539fd00 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Mon, 15 May 2017 00:45:50 +0200 Subject: [PATCH] ui: Create bookmarks from graph context menu for node under mouse cursor (issue #373) bug id = 373 --- .../controller/BookmarkController.cpp | 65 ++++++++----------- .../component/controller/BookmarkController.h | 5 +- src/lib/component/view/BookmarkView.cpp | 29 +++++++-- src/lib/component/view/BookmarkView.h | 2 +- .../messaging/type/MessageCreateBookmark.h | 4 +- .../type/MessageDisplayBookmarkCreator.h | 8 +++ .../messaging/type/MessageEditBookmark.h | 4 +- src/lib_gui/qt/graphics/QtGraphicsView.cpp | 19 ++++++ src/lib_gui/qt/graphics/QtGraphicsView.h | 5 ++ src/lib_gui/qt/view/QtBookmarkView.cpp | 5 +- src/lib_gui/qt/view/QtBookmarkView.h | 2 +- src/lib_gui/qt/window/QtBookmarkCreator.cpp | 14 ++-- src/lib_gui/qt/window/QtBookmarkCreator.h | 5 +- 13 files changed, 103 insertions(+), 64 deletions(-) diff --git a/src/lib/component/controller/BookmarkController.cpp b/src/lib/component/controller/BookmarkController.cpp index 8e330afb..f5e224c5 100644 --- a/src/lib/component/controller/BookmarkController.cpp +++ b/src/lib/component/controller/BookmarkController.cpp @@ -22,7 +22,6 @@ const std::string BookmarkController::s_defaultCategoryName = "default"; BookmarkController::BookmarkController(StorageAccess* storageAccess) : m_storageAccess(storageAccess) , m_bookmarkCache(storageAccess) - , m_hasBookmarkForActiveToken(false) { } @@ -35,8 +34,6 @@ void BookmarkController::clear() m_activeNodeIds.clear(); m_activeEdgeIds.clear(); - m_hasBookmarkForActiveToken = false; - getView()->setCreateButtonState(BookmarkView::CreateButtonState::CANNOT_CREATE); } @@ -66,16 +63,16 @@ std::vector BookmarkController::getActiveTokenDisplayNames() const } } +std::vector BookmarkController::getDisplayNamesForNodeId(Id nodeId) const +{ + return std::vector({ getNodeDisplayName(nodeId) }); +} + std::vector BookmarkController::getAllBookmarkCategories() const { return m_storageAccess->getAllBookmarkCategories(); } -bool BookmarkController::hasBookmarkForActiveToken() const -{ - return m_hasBookmarkForActiveToken; -} - std::shared_ptr BookmarkController::getBookmarkForActiveToken() const { if (!m_activeEdgeIds.empty()) @@ -103,6 +100,19 @@ std::shared_ptr BookmarkController::getBookmarkForActiveToken() const return std::shared_ptr(); } +std::shared_ptr BookmarkController::getBookmarkForNodeId(Id nodeId) const +{ + for (std::shared_ptr nodeBookmark: getAllNodeBookmarks()) + { + if (nodeBookmark->getNodeIds().size() == 1 && nodeBookmark->getNodeIds()[0] == nodeId) + { + return std::make_shared(*(nodeBookmark.get())); + } + } + + return std::shared_ptr(); +} + bool BookmarkController::canCreateBookmark() const { return m_activeNodeIds.size() || m_activeEdgeIds.size(); @@ -191,7 +201,7 @@ void BookmarkController::handleMessage(MessageActivateBookmark* message) for (Id nodeId: bookmark->getNodeIds()) { - activateNodes.addNode(nodeId, NameHierarchy()); + activateNodes.addNode(nodeId, m_storageAccess->getNameHierarchyForNodeId(nodeId)); } activateNodes.dispatch(); @@ -210,12 +220,10 @@ void BookmarkController::handleMessage(MessageActivateTokens* message) if (getBookmarkForActiveToken()) { - m_hasBookmarkForActiveToken = true; getView()->setCreateButtonState(BookmarkView::CreateButtonState::ALREADY_CREATED); } else { - m_hasBookmarkForActiveToken = false; getView()->setCreateButtonState(BookmarkView::CreateButtonState::CAN_CREATE); } } @@ -227,12 +235,10 @@ void BookmarkController::handleMessage(MessageActivateTokens* message) if (getBookmarkForActiveToken()) { - m_hasBookmarkForActiveToken = true; getView()->setCreateButtonState(BookmarkView::CreateButtonState::ALREADY_CREATED); } else { - m_hasBookmarkForActiveToken = false; getView()->setCreateButtonState(BookmarkView::CreateButtonState::CAN_CREATE); } } @@ -248,17 +254,7 @@ void BookmarkController::handleMessage(MessageCreateBookmark* message) { LOG_INFO_STREAM(<< "Creating Edge Bookmark"); - std::string displayName = message->displayName; - if (displayName.empty()) - { - std::vector activeEdgeDisplayNames = getActiveEdgeDisplayNames(); - if (!activeEdgeDisplayNames.empty()) - { - displayName = activeEdgeDisplayNames.front(); - } - } - - EdgeBookmark bookmark(0, displayName, message->comment, TimePoint::now(), category); + EdgeBookmark bookmark(0, message->displayName, message->comment, TimePoint::now(), category); bookmark.setEdgeIds(m_activeEdgeIds); if (!m_activeNodeIds.empty()) @@ -276,24 +272,20 @@ void BookmarkController::handleMessage(MessageCreateBookmark* message) { LOG_INFO_STREAM(<< "Creating Node Bookmark"); - std::string displayName = message->displayName; - if (displayName.empty()) + NodeBookmark bookmark(0, message->displayName, message->comment, TimePoint::now(), category); + if (message->nodeId) { - std::vector activeNodeDisplayNames = getActiveNodeDisplayNames(); - if (!activeNodeDisplayNames.empty()) - { - displayName = activeNodeDisplayNames.front(); - } + bookmark.addNodeId(message->nodeId); + } + else + { + bookmark.setNodeIds(m_activeNodeIds); } - - NodeBookmark bookmark(0, displayName, message->comment, TimePoint::now(), category); - bookmark.setNodeIds(m_activeNodeIds); m_storageAccess->addNodeBookmark(bookmark); } m_bookmarkCache.clear(); - m_hasBookmarkForActiveToken = true; getView()->setCreateButtonState(BookmarkView::CreateButtonState::ALREADY_CREATED); getView()->update(); } @@ -315,7 +307,6 @@ void BookmarkController::handleMessage(MessageDeleteBookmark* message) if (!getBookmarkForActiveToken()) { - m_hasBookmarkForActiveToken = false; getView()->setCreateButtonState(BookmarkView::CreateButtonState::CAN_CREATE); } @@ -330,7 +321,6 @@ void BookmarkController::handleMessage(MessageDeleteBookmarkCategory* message) if (!getBookmarkForActiveToken()) { - m_hasBookmarkForActiveToken = false; getView()->setCreateButtonState(BookmarkView::CreateButtonState::CAN_CREATE); } @@ -347,7 +337,6 @@ void BookmarkController::handleMessage(MessageDeleteBookmarkForActiveTokens* mes cleanBookmarkCategories(); - m_hasBookmarkForActiveToken = false; getView()->setCreateButtonState(BookmarkView::CreateButtonState::CAN_CREATE); getView()->update(); } diff --git a/src/lib/component/controller/BookmarkController.h b/src/lib/component/controller/BookmarkController.h index a6bd8b8a..08f9cec3 100644 --- a/src/lib/component/controller/BookmarkController.h +++ b/src/lib/component/controller/BookmarkController.h @@ -50,10 +50,12 @@ public: const MessageDisplayBookmarks::BookmarkFilter& filter, const MessageDisplayBookmarks::BookmarkOrder& order) const; std::vector getActiveTokenDisplayNames() const; + std::vector getDisplayNamesForNodeId(Id nodeId) const; + std::vector getAllBookmarkCategories() const; - bool hasBookmarkForActiveToken() const; std::shared_ptr getBookmarkForActiveToken() const; + std::shared_ptr getBookmarkForNodeId(Id nodeId) const; bool canCreateBookmark() const; @@ -118,7 +120,6 @@ private: std::vector m_activeNodeIds; std::vector m_activeEdgeIds; - bool m_hasBookmarkForActiveToken; }; #endif // BOOKMARK_CONTROLLER_H diff --git a/src/lib/component/view/BookmarkView.cpp b/src/lib/component/view/BookmarkView.cpp index 3f6e0707..ee79e2b8 100644 --- a/src/lib/component/view/BookmarkView.cpp +++ b/src/lib/component/view/BookmarkView.cpp @@ -46,18 +46,39 @@ void BookmarkView::handleMessage(MessageDisplayBookmarks* message) void BookmarkView::handleMessage(MessageDisplayBookmarkCreator* message) { - if (!getController()->canCreateBookmark()) + if (!getController()->canCreateBookmark() && !message->nodeId) { return; } - if (getController()->hasBookmarkForActiveToken()) + if (message->nodeId) { - displayBookmarkEditor(getController()->getBookmarkForActiveToken(), getController()->getAllBookmarkCategories()); + if (getController()->getBookmarkForNodeId(message->nodeId) != nullptr) + { + displayBookmarkEditor( + getController()->getBookmarkForNodeId(message->nodeId), + getController()->getAllBookmarkCategories() + ); + } + else + { + displayBookmarkCreator( + getController()->getDisplayNamesForNodeId(message->nodeId), + getController()->getAllBookmarkCategories(), + message->nodeId + ); + } } else { - displayBookmarkCreator(getController()->getActiveTokenDisplayNames(), getController()->getAllBookmarkCategories()); + if (getController()->getBookmarkForActiveToken() != nullptr) + { + displayBookmarkEditor(getController()->getBookmarkForActiveToken(), getController()->getAllBookmarkCategories()); + } + else + { + displayBookmarkCreator(getController()->getActiveTokenDisplayNames(), getController()->getAllBookmarkCategories(), 0); + } } } diff --git a/src/lib/component/view/BookmarkView.h b/src/lib/component/view/BookmarkView.h index c4dda329..d43fc502 100644 --- a/src/lib/component/view/BookmarkView.h +++ b/src/lib/component/view/BookmarkView.h @@ -46,7 +46,7 @@ private: virtual void handleMessage(MessageDisplayBookmarkEditor* message); virtual void displayBookmarks(const std::vector>& bookmarks) = 0; - virtual void displayBookmarkCreator(const std::vector& names, const std::vector& categories) = 0; + virtual void displayBookmarkCreator(const std::vector& names, const std::vector& categories, Id nodeId) = 0; virtual void displayBookmarkEditor(std::shared_ptr bookmark, const std::vector& categories) = 0; MessageDisplayBookmarks::BookmarkFilter m_filter; diff --git a/src/lib/utility/messaging/type/MessageCreateBookmark.h b/src/lib/utility/messaging/type/MessageCreateBookmark.h index a595cbd0..b6277bed 100644 --- a/src/lib/utility/messaging/type/MessageCreateBookmark.h +++ b/src/lib/utility/messaging/type/MessageCreateBookmark.h @@ -7,10 +7,11 @@ class MessageCreateBookmark : public Message { public: - MessageCreateBookmark(const std::string& comment, const std::string& displayName, const std::string& categoryName) + MessageCreateBookmark(const std::string& comment, const std::string& displayName, const std::string& categoryName, Id nodeId) : comment(comment) , displayName(displayName) , categoryName(categoryName) + , nodeId(nodeId) { } @@ -26,6 +27,7 @@ public: const std::string comment; const std::string displayName; const std::string categoryName; + const Id nodeId; }; #endif // MESSAGE_CREATE_BOOKMARK_H \ No newline at end of file diff --git a/src/lib/utility/messaging/type/MessageDisplayBookmarkCreator.h b/src/lib/utility/messaging/type/MessageDisplayBookmarkCreator.h index 0fbe325b..8ac0a7aa 100644 --- a/src/lib/utility/messaging/type/MessageDisplayBookmarkCreator.h +++ b/src/lib/utility/messaging/type/MessageDisplayBookmarkCreator.h @@ -8,6 +8,12 @@ class MessageDisplayBookmarkCreator { public: MessageDisplayBookmarkCreator() + : nodeId(0) + { + } + + MessageDisplayBookmarkCreator(Id nodeId) + : nodeId(nodeId) { } @@ -15,6 +21,8 @@ public: { return "MessageDisplayBookmarkCreator"; } + + Id nodeId; }; #endif // MESSAGE_DISPLAY_BOOKMARK_CREATOR_H \ No newline at end of file diff --git a/src/lib/utility/messaging/type/MessageEditBookmark.h b/src/lib/utility/messaging/type/MessageEditBookmark.h index 9801c2c7..7fbef812 100644 --- a/src/lib/utility/messaging/type/MessageEditBookmark.h +++ b/src/lib/utility/messaging/type/MessageEditBookmark.h @@ -8,12 +8,11 @@ class MessageEditBookmark : public Message { public: - MessageEditBookmark(const Id id, const std::string& comment, const std::string& displayName, const std::string& categoryName, const bool isEdge) + MessageEditBookmark(Id id, const std::string& comment, const std::string& displayName, const std::string& categoryName) : bookmarkId(id) , comment(comment) , displayName(displayName) , categoryName(categoryName) - , isEdge(isEdge) { } @@ -30,7 +29,6 @@ public: const std::string comment; const std::string displayName; const std::string categoryName; - const bool isEdge; }; #endif // MESSAGE_EDIT_BOOKMARK_H \ No newline at end of file diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.cpp b/src/lib_gui/qt/graphics/QtGraphicsView.cpp index 4c819d63..7fbc21f9 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.cpp +++ b/src/lib_gui/qt/graphics/QtGraphicsView.cpp @@ -17,6 +17,7 @@ #include "qt/utility/QtContextMenu.h" #include "qt/utility/utilityQt.h" #include "settings/ApplicationSettings.h" +#include "utility/messaging/type/MessageDisplayBookmarkCreator.h" #include "utility/ResourcePaths.h" QtGraphicsView::QtGraphicsView(QWidget* parent) @@ -52,6 +53,11 @@ QtGraphicsView::QtGraphicsView(QWidget* parent) m_copyNodeNameAction->setToolTip(tr("Copies the name of this node to the clipboard")); connect(m_copyNodeNameAction, SIGNAL(triggered()), this, SLOT(copyNodeName())); + m_bookmarkNodeAction = new QAction(tr("Bookmark Node"), this); + m_bookmarkNodeAction->setStatusTip(tr("Create a bookmark for this node")); + m_bookmarkNodeAction->setToolTip(tr("Create a bookmark for this node")); + connect(m_bookmarkNodeAction, SIGNAL(triggered()), this, SLOT(bookmarkNode())); + m_zoomInButton = new QPushButton(this); m_zoomInButton->setObjectName("zoom_in_button"); m_zoomInButton->setAutoRepeat(true); @@ -277,6 +283,7 @@ void QtGraphicsView::wheelEvent(QWheelEvent* event) void QtGraphicsView::contextMenuEvent(QContextMenuEvent* event) { m_clipboardNodeName = ""; + m_bookmarkNodeId = 0; FilePath clipboardFilePath; QtGraphNode* node = getNodeAtCursorPosition(); @@ -286,6 +293,7 @@ void QtGraphicsView::contextMenuEvent(QContextMenuEvent* event) if (dataNode) { m_clipboardNodeName = dataNode->getName(); + m_bookmarkNodeId = dataNode->getTokenId(); clipboardFilePath = dataNode->getFilePath(); } else if (dynamic_cast(node)) @@ -300,6 +308,12 @@ void QtGraphicsView::contextMenuEvent(QContextMenuEvent* event) menu.addSeparator(); menu.addAction(m_exportGraphAction); + if (m_bookmarkNodeId) + { + menu.addSeparator(); + menu.addAction(m_bookmarkNodeAction); + } + if (!m_clipboardNodeName.empty() || !clipboardFilePath.empty()) { menu.addSeparator(); @@ -455,6 +469,11 @@ void QtGraphicsView::copyNodeName() QApplication::clipboard()->setText(m_clipboardNodeName.c_str()); } +void QtGraphicsView::bookmarkNode() +{ + MessageDisplayBookmarkCreator(m_bookmarkNodeId).dispatch(); +} + void QtGraphicsView::zoomInPressed() { updateZoom(m_zoomInButtonSpeed); diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index 13dd2f0d..0ae3b2db 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.h +++ b/src/lib_gui/qt/graphics/QtGraphicsView.h @@ -6,6 +6,8 @@ #include #include +#include "utility/types.h" + class QTimer; class QtGraphNode; @@ -51,6 +53,7 @@ private slots: void exportGraph(); void copyNodeName(); + void bookmarkNode(); void zoomInPressed(); void zoomOutPressed(); @@ -72,12 +75,14 @@ private: bool m_shift; std::string m_clipboardNodeName; + Id m_bookmarkNodeId; std::shared_ptr m_timer; std::shared_ptr m_timerStopper; QAction* m_exportGraphAction; QAction* m_copyNodeNameAction; + QAction* m_bookmarkNodeAction; QPushButton* m_zoomInButton; QPushButton* m_zoomOutButton; diff --git a/src/lib_gui/qt/view/QtBookmarkView.cpp b/src/lib_gui/qt/view/QtBookmarkView.cpp index 64441a46..96ed662e 100644 --- a/src/lib_gui/qt/view/QtBookmarkView.cpp +++ b/src/lib_gui/qt/view/QtBookmarkView.cpp @@ -191,7 +191,7 @@ void QtBookmarkView::displayBookmarks(const std::vector& names, const std::vector& categories) +void QtBookmarkView::displayBookmarkCreator(const std::vector& names, const std::vector& categories, Id nodeId) { m_onQtThread( [=]() @@ -213,6 +213,8 @@ void QtBookmarkView::displayBookmarkCreator(const std::vector& name bookmarkCreator->setDisplayName(displayName); bookmarkCreator->setBookmarkCategories(categories); + bookmarkCreator->setNodeId(nodeId); + bookmarkCreator->show(); bookmarkCreator->raise(); } @@ -230,7 +232,6 @@ void QtBookmarkView::displayBookmarkEditor(std::shared_ptr bookmark, c bookmarkCreator->setComment(bookmark->getComment()); bookmarkCreator->setBookmarkCategories(categories); bookmarkCreator->setCurrentBookmarkCategory(bookmark->getCategory()); - bookmarkCreator->setIsEdge((dynamic_cast(bookmark.get()) != nullptr)); bookmarkCreator->show(); bookmarkCreator->raise(); diff --git a/src/lib_gui/qt/view/QtBookmarkView.h b/src/lib_gui/qt/view/QtBookmarkView.h index f7876c46..e8946f6f 100644 --- a/src/lib_gui/qt/view/QtBookmarkView.h +++ b/src/lib_gui/qt/view/QtBookmarkView.h @@ -35,7 +35,7 @@ private slots: private: virtual void displayBookmarks(const std::vector>& bookmarks); - virtual void displayBookmarkCreator(const std::vector& names, const std::vector& categories); + virtual void displayBookmarkCreator(const std::vector& names, const std::vector& categories, Id nodeId); virtual void displayBookmarkEditor(std::shared_ptr bookmark, const std::vector& categories); void setStyleSheet(); diff --git a/src/lib_gui/qt/window/QtBookmarkCreator.cpp b/src/lib_gui/qt/window/QtBookmarkCreator.cpp index 19b45af2..ac0adca4 100644 --- a/src/lib_gui/qt/window/QtBookmarkCreator.cpp +++ b/src/lib_gui/qt/window/QtBookmarkCreator.cpp @@ -19,6 +19,7 @@ QtBookmarkCreator::QtBookmarkCreator(QWidget* parent, bool edit, Id id) , m_edit(edit) , m_bookmarkId(id) , m_categoryCount(0) + , m_nodeId(0) { } @@ -151,14 +152,9 @@ void QtBookmarkCreator::setCurrentBookmarkCategory(const BookmarkCategory& categ } } -bool QtBookmarkCreator::getIsEdge() const +void QtBookmarkCreator::setNodeId(Id nodeId) { - return m_isEdge; -} - -void QtBookmarkCreator::setIsEdge(const bool isEdge) -{ - m_isEdge = isEdge; + m_nodeId = nodeId; } void QtBookmarkCreator::resizeEvent(QResizeEvent* event) @@ -177,11 +173,11 @@ void QtBookmarkCreator::handleNext() if (m_edit) { MessageEditBookmark( - m_bookmarkId, qComment.toStdString(), qDisplayName.toStdString(), qCategory.toStdString(), m_isEdge).dispatch(); + m_bookmarkId, qComment.toStdString(), qDisplayName.toStdString(), qCategory.toStdString()).dispatch(); } else { - MessageCreateBookmark(qComment.toStdString(), qDisplayName.toStdString(), qCategory.toStdString()).dispatch(); + MessageCreateBookmark(qComment.toStdString(), qDisplayName.toStdString(), qCategory.toStdString(), m_nodeId).dispatch(); } MessageStatus("Creating Bookmark for active Token").dispatch(); diff --git a/src/lib_gui/qt/window/QtBookmarkCreator.h b/src/lib_gui/qt/window/QtBookmarkCreator.h index a393474d..25138a18 100644 --- a/src/lib_gui/qt/window/QtBookmarkCreator.h +++ b/src/lib_gui/qt/window/QtBookmarkCreator.h @@ -31,8 +31,7 @@ public: void setBookmarkCategories(const std::vector& categories); void setCurrentBookmarkCategory(const BookmarkCategory& category); - bool getIsEdge() const; - void setIsEdge(const bool isEdge); + void setNodeId(Id nodeId); protected: virtual void resizeEvent(QResizeEvent* event) Q_DECL_OVERRIDE; @@ -54,7 +53,7 @@ private: int m_categoryCount; - bool m_isEdge; + Id m_nodeId; QWidget* m_headerBackground; };