ui: Save graph as image via context menu
bug id = 180
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "qt/graphics/QtGraphicsView.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QMouseEvent>
|
||||
#include <QScrollBar>
|
||||
#include <QTimer>
|
||||
@@ -7,6 +8,8 @@
|
||||
#include <QPropertyAnimation>
|
||||
#include <QParallelAnimationGroup>
|
||||
|
||||
#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<QTimer>(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<QAction*>(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;
|
||||
|
||||
@@ -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<QTimer> m_timer;
|
||||
std::shared_ptr<QTimer> m_timerStopper;
|
||||
|
||||
QAction* m_exportGraphAction;
|
||||
};
|
||||
|
||||
#endif // QT_GRAPHICS_VIEW_H
|
||||
|
||||
Reference in New Issue
Block a user