Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

feat: menu action for graph 'save as image' #1104

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/lib/utility/messaging/type/graph/MessageSaveAsImage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef MESSAGE_SAVE_AS_IMAGE_H
#define MESSAGE_SAVE_AS_IMAGE_H

#include "Message.h"


class MessageSaveAsImage: public Message<MessageSaveAsImage>
{
public:
MessageSaveAsImage(QString path) : path(path) {}

static const std::string getStaticType()
{
return "MessageSaveAsImage";
}

QString path;
};

#endif /* MESSAGE_SAVE_AS_IMAGE_H */
13 changes: 12 additions & 1 deletion src/lib_gui/qt/graphics/QtGraphicsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
15 changes: 14 additions & 1 deletion src/lib_gui/qt/graphics/QtGraphicsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <QGraphicsView>

#include "types.h"
#include "MessageListener.h"
#include "MessageSaveAsImage.h"


class GraphFocusHandler;
class QPushButton;
Expand All @@ -14,7 +17,7 @@ class QtGraphEdge;
class QtGraphNode;
class QtSelfRefreshIconButton;

class QtGraphicsView: public QGraphicsView
class QtGraphicsView: public QGraphicsView, public MessageListener<MessageSaveAsImage>
{
Q_OBJECT

Expand All @@ -33,6 +36,11 @@ class QtGraphicsView: public QGraphicsView

void updateZoom(float delta);

Id getSchedulerId() const override
{
return m_tabId;
}

protected:
void resizeEvent(QResizeEvent* event);

Expand Down Expand Up @@ -90,6 +98,8 @@ private slots:
void setZoomFactor(float zoomFactor);
void updateTransform();

void handleMessage(MessageSaveAsImage* message) override;

GraphFocusHandler* m_focusHandler;

QPoint m_last;
Expand Down Expand Up @@ -141,6 +151,9 @@ private slots:

float m_zoomInButtonSpeed;
float m_zoomOutButtonSpeed;

QImage m_imageCached;
Id m_tabId;
};

#endif // QT_GRAPHICS_VIEW_H
16 changes: 16 additions & 0 deletions src/lib_gui/qt/window/QtMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/lib_gui/qt/window/QtMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public slots:
void updateRecentProjectsMenu();

void toggleView(View* view, bool fromMenu);
void saveAsImage();

private slots:
void toggleShowDockWidgetTitleBars();
Expand Down