diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 38d225b7..e9e4bb1f 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/graph/MessageSaveAsImage.h b/src/lib/utility/messaging/type/graph/MessageSaveAsImage.h new file mode 100644 index 00000000..012390b5 --- /dev/null +++ b/src/lib/utility/messaging/type/graph/MessageSaveAsImage.h @@ -0,0 +1,20 @@ +#ifndef MESSAGE_SAVE_AS_IMAGE_H +#define MESSAGE_SAVE_AS_IMAGE_H + +#include "Message.h" + + +class MessageSaveAsImage: public Message +{ +public: + MessageSaveAsImage(QString path) : path(path) {} + + static const std::string getStaticType() + { + return "MessageSaveAsImage"; + } + + 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 581d268b..5432bf6f 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,6 +185,8 @@ void QtGraphicsView::setSceneRect(const QRectF& rect) { QGraphicsView::setSceneRect(rect); scene()->setSceneRect(rect); + m_imageCached = toQImage(); + m_tabId = TabId::currentTab(); } QtGraphNode* QtGraphicsView::getNodeAtCursorPosition() const @@ -724,7 +728,6 @@ void QtGraphicsView::exportGraph() QStringLiteral("PNG (*.png);;JPEG (*.JPEG);;BMP Files (*.bmp);;SVG (*.svg)")) .toStdWString()); - if (filePath.extension() == L".svg") { QSvgGenerator svgGen; @@ -852,3 +855,11 @@ void QtGraphicsView::updateTransform() float zoomFactor = m_appZoomFactor * m_zoomFactor; setTransform(QTransform(zoomFactor, 0, 0, zoomFactor, 0, 0)); } + +void QtGraphicsView::handleMessage(MessageSaveAsImage* message) +{ + if ( (message->getSchedulerId() == getSchedulerId()) && !m_imageCached.isNull() ) + { + m_imageCached.save(message->path); + } +} diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index e85b89ba..10985459 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 @@ -33,6 +36,11 @@ public: void updateZoom(float delta); + Id getSchedulerId() const override + { + return m_tabId; + } + protected: void resizeEvent(QResizeEvent* event); @@ -90,6 +98,8 @@ private: void setZoomFactor(float zoomFactor); void updateTransform(); + void handleMessage(MessageSaveAsImage* message) override; + GraphFocusHandler* m_focusHandler; QPoint m_last; @@ -141,6 +151,9 @@ private: float m_zoomInButtonSpeed; float m_zoomOutButtonSpeed; + + QImage m_imageCached; + Id m_tabId; }; #endif // QT_GRAPHICS_VIEW_H diff --git a/src/lib_gui/qt/window/QtMainWindow.cpp b/src/lib_gui/qt/window/QtMainWindow.cpp index 51de5475..bbf240b3 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,19 @@ 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 m(filePath); + m.setSchedulerId(TabId::currentTab()); + m.dispatch(); +} + void QtMainWindow::undo() { MessageHistoryUndo().dispatch(); @@ -924,6 +938,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 dcc94d75..de9dbf72 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();