From 11a8a9f9ad36ed150f2df089b1efd08d27a8bd01 Mon Sep 17 00:00:00 2001 From: miltolstoy Date: Mon, 2 Nov 2020 20:12:26 +0200 Subject: [PATCH 1/2] feat: menu action for graph 'save as image' --- .../messaging/type/MessageSaveAsImage.h | 20 +++++++++++++++++++ src/lib_gui/qt/graphics/QtGraphicsView.cpp | 13 +++++++++++- src/lib_gui/qt/graphics/QtGraphicsView.h | 10 +++++++++- src/lib_gui/qt/window/QtMainWindow.cpp | 14 +++++++++++++ src/lib_gui/qt/window/QtMainWindow.h | 1 + 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/lib/utility/messaging/type/MessageSaveAsImage.h diff --git a/src/lib/utility/messaging/type/MessageSaveAsImage.h b/src/lib/utility/messaging/type/MessageSaveAsImage.h new file mode 100644 index 000000000..b133ce9b5 --- /dev/null +++ b/src/lib/utility/messaging/type/MessageSaveAsImage.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Message.h" + + +class MessageSaveAsImage: public Message +{ +public: + MessageSaveAsImage(QString path) + { + this->path = path; + } + + static const std::string getStaticType() + { + return "MessageSaveAsImage"; + } + + QString path; +}; diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.cpp b/src/lib_gui/qt/graphics/QtGraphicsView.cpp index 581d268bb..9e1d22461 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.cpp +++ b/src/lib_gui/qt/graphics/QtGraphicsView.cpp @@ -183,6 +183,7 @@ void QtGraphicsView::setSceneRect(const QRectF& rect) { QGraphicsView::setSceneRect(rect); scene()->setSceneRect(rect); + imageCached = toQImage(); } QtGraphNode* QtGraphicsView::getNodeAtCursorPosition() const @@ -602,6 +603,7 @@ void QtGraphicsView::focusInEvent(QFocusEvent* event) emit focusIn(); MessageFocusView(MessageFocusView::ViewType::GRAPH).dispatch(); + lastViewFocused = this; } void QtGraphicsView::focusOutEvent(QFocusEvent* event) @@ -724,7 +726,6 @@ void QtGraphicsView::exportGraph() QStringLiteral("PNG (*.png);;JPEG (*.JPEG);;BMP Files (*.bmp);;SVG (*.svg)")) .toStdWString()); - if (filePath.extension() == L".svg") { QSvgGenerator svgGen; @@ -852,3 +853,13 @@ void QtGraphicsView::updateTransform() float zoomFactor = m_appZoomFactor * m_zoomFactor; setTransform(QTransform(zoomFactor, 0, 0, zoomFactor, 0, 0)); } + +QtGraphicsView* QtGraphicsView::lastViewFocused; + +void QtGraphicsView::handleMessage(MessageSaveAsImage* message) +{ + if ( (lastViewFocused == this) && !imageCached.isNull() ) + { + imageCached.save(message->path); + } +} diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index e85b89ba4..2487d8baf 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.h +++ b/src/lib_gui/qt/graphics/QtGraphicsView.h @@ -6,6 +6,9 @@ #include #include "types.h" +#include "MessageListener.h" +#include "MessageSaveAsImage.h" + class GraphFocusHandler; class QPushButton; @@ -14,7 +17,7 @@ class QtGraphEdge; class QtGraphNode; class QtSelfRefreshIconButton; -class QtGraphicsView: public QGraphicsView +class QtGraphicsView: public QGraphicsView, public MessageListener { Q_OBJECT @@ -90,6 +93,8 @@ private slots: void setZoomFactor(float zoomFactor); void updateTransform(); + void handleMessage(MessageSaveAsImage* message) override; + GraphFocusHandler* m_focusHandler; QPoint m_last; @@ -141,6 +146,9 @@ private slots: float m_zoomInButtonSpeed; float m_zoomOutButtonSpeed; + + QImage imageCached; + static QtGraphicsView* lastViewFocused; }; #endif // QT_GRAPHICS_VIEW_H diff --git a/src/lib_gui/qt/window/QtMainWindow.cpp b/src/lib_gui/qt/window/QtMainWindow.cpp index 51de54750..7c918bd97 100644 --- a/src/lib_gui/qt/window/QtMainWindow.cpp +++ b/src/lib_gui/qt/window/QtMainWindow.cpp @@ -38,6 +38,7 @@ #include "MessageTabSelect.h" #include "MessageWindowClosed.h" #include "MessageZoom.h" +#include "MessageSaveAsImage.h" #include "QtAbout.h" #include "QtContextMenu.h" #include "QtFileDialog.h" @@ -710,6 +711,17 @@ void QtMainWindow::forceRefresh() MessageRefresh().refreshAll().dispatch(); } +void QtMainWindow::saveAsImage() +{ + QString filePath = QtFileDialog::showSaveFileDialog(this, tr("Save as Image"), FilePath(), + "PNG (*.png);;JPEG (*.JPEG);;BMP Files (*.bmp)"); + if (filePath.isNull()) + { + return; + } + MessageSaveAsImage(filePath).dispatch(); +} + void QtMainWindow::undo() { MessageHistoryUndo().dispatch(); @@ -924,6 +936,8 @@ void QtMainWindow::setupEditMenu() this, &QtMainWindow::openSettings, QKeySequence(Qt::CTRL + Qt::Key_Comma)); + + menu->addAction(tr("&Save as Image"), this, &QtMainWindow::saveAsImage, QKeySequence(Qt::SHIFT + Qt::CTRL + Qt::Key_S)); } void QtMainWindow::setupViewMenu() diff --git a/src/lib_gui/qt/window/QtMainWindow.h b/src/lib_gui/qt/window/QtMainWindow.h index dcc94d75b..de9dbf726 100644 --- a/src/lib_gui/qt/window/QtMainWindow.h +++ b/src/lib_gui/qt/window/QtMainWindow.h @@ -150,6 +150,7 @@ public slots: void updateRecentProjectsMenu(); void toggleView(View* view, bool fromMenu); + void saveAsImage(); private slots: void toggleShowDockWidgetTitleBars(); From 4dc36780896587abfa7c568e0db421a0b0b95845 Mon Sep 17 00:00:00 2001 From: miltolstoy Date: Tue, 10 Nov 2020 18:50:23 +0200 Subject: [PATCH 2/2] fix review comments --- src/lib/CMakeLists.txt | 1 + .../messaging/type/{ => graph}/MessageSaveAsImage.h | 10 +++++----- src/lib_gui/qt/graphics/QtGraphicsView.cpp | 12 ++++++------ src/lib_gui/qt/graphics/QtGraphicsView.h | 9 +++++++-- src/lib_gui/qt/window/QtMainWindow.cpp | 4 +++- 5 files changed, 22 insertions(+), 14 deletions(-) rename src/lib/utility/messaging/type/{ => graph}/MessageSaveAsImage.h (56%) diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 38d225b76..e9e4bb1fe 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -489,6 +489,7 @@ add_files( utility/messaging/type/graph/MessageGraphNodeHide.h utility/messaging/type/graph/MessageGraphNodeMove.h utility/messaging/type/graph/MessageScrollGraph.h + utility/messaging/type/graph/MessageSaveAsImage.h utility/messaging/type/history/MessageHistoryToPosition.h utility/messaging/type/history/MessageHistoryRedo.h diff --git a/src/lib/utility/messaging/type/MessageSaveAsImage.h b/src/lib/utility/messaging/type/graph/MessageSaveAsImage.h similarity index 56% rename from src/lib/utility/messaging/type/MessageSaveAsImage.h rename to src/lib/utility/messaging/type/graph/MessageSaveAsImage.h index b133ce9b5..012390b56 100644 --- a/src/lib/utility/messaging/type/MessageSaveAsImage.h +++ b/src/lib/utility/messaging/type/graph/MessageSaveAsImage.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef MESSAGE_SAVE_AS_IMAGE_H +#define MESSAGE_SAVE_AS_IMAGE_H #include "Message.h" @@ -6,10 +7,7 @@ class MessageSaveAsImage: public Message { public: - MessageSaveAsImage(QString path) - { - this->path = path; - } + MessageSaveAsImage(QString path) : path(path) {} static const std::string getStaticType() { @@ -18,3 +16,5 @@ class MessageSaveAsImage: public Message QString path; }; + +#endif /* MESSAGE_SAVE_AS_IMAGE_H */ diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.cpp b/src/lib_gui/qt/graphics/QtGraphicsView.cpp index 9e1d22461..5432bf6fb 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.cpp +++ b/src/lib_gui/qt/graphics/QtGraphicsView.cpp @@ -166,6 +166,8 @@ QtGraphicsView::QtGraphicsView(GraphFocusHandler* focusHandler, QWidget* parent) m_legendButton->setObjectName(QStringLiteral("legend_button")); m_legendButton->setToolTip(QStringLiteral("show legend")); connect(m_legendButton, &QPushButton::clicked, this, &QtGraphicsView::legendClicked); + + m_tabId = TabId::currentTab(); } float QtGraphicsView::getZoomFactor() const @@ -183,7 +185,8 @@ void QtGraphicsView::setSceneRect(const QRectF& rect) { QGraphicsView::setSceneRect(rect); scene()->setSceneRect(rect); - imageCached = toQImage(); + m_imageCached = toQImage(); + m_tabId = TabId::currentTab(); } QtGraphNode* QtGraphicsView::getNodeAtCursorPosition() const @@ -603,7 +606,6 @@ void QtGraphicsView::focusInEvent(QFocusEvent* event) emit focusIn(); MessageFocusView(MessageFocusView::ViewType::GRAPH).dispatch(); - lastViewFocused = this; } void QtGraphicsView::focusOutEvent(QFocusEvent* event) @@ -854,12 +856,10 @@ void QtGraphicsView::updateTransform() setTransform(QTransform(zoomFactor, 0, 0, zoomFactor, 0, 0)); } -QtGraphicsView* QtGraphicsView::lastViewFocused; - void QtGraphicsView::handleMessage(MessageSaveAsImage* message) { - if ( (lastViewFocused == this) && !imageCached.isNull() ) + if ( (message->getSchedulerId() == getSchedulerId()) && !m_imageCached.isNull() ) { - imageCached.save(message->path); + m_imageCached.save(message->path); } } diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index 2487d8baf..10985459e 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.h +++ b/src/lib_gui/qt/graphics/QtGraphicsView.h @@ -36,6 +36,11 @@ class QtGraphicsView: public QGraphicsView, public MessageListener