diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.cpp b/src/lib_gui/qt/graphics/QtGraphicsView.cpp index 4ab7ba52..ae9293d4 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.cpp +++ b/src/lib_gui/qt/graphics/QtGraphicsView.cpp @@ -1,5 +1,6 @@ #include "qt/graphics/QtGraphicsView.h" +#include #include #include #include @@ -7,6 +8,8 @@ #include #include +#include "qt/utility/QtContextMenu.h" + QtGraphicsView::QtGraphicsView(QWidget* parent) : QGraphicsView(parent) , m_zoomFactor(1.0f) @@ -25,6 +28,11 @@ QtGraphicsView::QtGraphicsView(QWidget* parent) m_timerStopper = std::make_shared(this); m_timerStopper->setSingleShot(true); connect(m_timerStopper.get(), SIGNAL(timeout()), this, SLOT(stopTimer())); + + m_exportGraphAction = new QAction(tr("Save as Image"), this); + m_exportGraphAction->setStatusTip(tr("Save this graph as image file")); + m_exportGraphAction->setToolTip(tr("Save this graph as image file")); + connect(m_exportGraphAction, SIGNAL(triggered()), this, SLOT(exportGraph())); } float QtGraphicsView::getZoomFactor() const @@ -169,6 +177,11 @@ void QtGraphicsView::wheelEvent(QWheelEvent* event) QGraphicsView::wheelEvent(event); } +void QtGraphicsView::contextMenuEvent(QContextMenuEvent* event) +{ + QtContextMenu::getInstance()->showExtended(event, this, std::vector(1, m_exportGraphAction)); +} + void QtGraphicsView::updateTimer() { float ds = 30.0f; @@ -231,6 +244,27 @@ void QtGraphicsView::stopTimer() m_timer->stop(); } +void QtGraphicsView::exportGraph() +{ + QString fileName = QFileDialog::getSaveFileName( + this, "Save image", QDir::homePath(), "PNG (*.png);;JPEG (*.JPEG);;BMP Files (*.bmp)"); + + if (!fileName.isNull()) + { + QImage image(scene()->sceneRect().size().toSize(), QImage::Format_ARGB32); + image.fill(Qt::transparent); + + QPainter painter(&image); + painter.setRenderHint(QPainter::Antialiasing); + scene()->render(&painter); + image.save(fileName); + + // different approach + // QPixmap pixMap = grab(); + // pixMap.save(fileName); + } +} + bool QtGraphicsView::moves() const { return m_up || m_down || m_left || m_right; diff --git a/src/lib_gui/qt/graphics/QtGraphicsView.h b/src/lib_gui/qt/graphics/QtGraphicsView.h index 63756e5b..b5e61a14 100644 --- a/src/lib_gui/qt/graphics/QtGraphicsView.h +++ b/src/lib_gui/qt/graphics/QtGraphicsView.h @@ -31,6 +31,8 @@ protected: void wheelEvent(QWheelEvent* event); + void contextMenuEvent(QContextMenuEvent* event); + signals: void emptySpaceClicked(); @@ -38,6 +40,8 @@ private slots: void updateTimer(); void stopTimer(); + void exportGraph(); + private: bool moves() const; @@ -57,6 +61,8 @@ private: std::shared_ptr m_timer; std::shared_ptr m_timerStopper; + + QAction* m_exportGraphAction; }; #endif // QT_GRAPHICS_VIEW_H