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 "qt/graphics/QtGraphicsView.h"
|
||||||
|
|
||||||
|
#include <QFileDialog>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -7,6 +8,8 @@
|
|||||||
#include <QPropertyAnimation>
|
#include <QPropertyAnimation>
|
||||||
#include <QParallelAnimationGroup>
|
#include <QParallelAnimationGroup>
|
||||||
|
|
||||||
|
#include "qt/utility/QtContextMenu.h"
|
||||||
|
|
||||||
QtGraphicsView::QtGraphicsView(QWidget* parent)
|
QtGraphicsView::QtGraphicsView(QWidget* parent)
|
||||||
: QGraphicsView(parent)
|
: QGraphicsView(parent)
|
||||||
, m_zoomFactor(1.0f)
|
, m_zoomFactor(1.0f)
|
||||||
@@ -25,6 +28,11 @@ QtGraphicsView::QtGraphicsView(QWidget* parent)
|
|||||||
m_timerStopper = std::make_shared<QTimer>(this);
|
m_timerStopper = std::make_shared<QTimer>(this);
|
||||||
m_timerStopper->setSingleShot(true);
|
m_timerStopper->setSingleShot(true);
|
||||||
connect(m_timerStopper.get(), SIGNAL(timeout()), this, SLOT(stopTimer()));
|
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
|
float QtGraphicsView::getZoomFactor() const
|
||||||
@@ -169,6 +177,11 @@ void QtGraphicsView::wheelEvent(QWheelEvent* event)
|
|||||||
QGraphicsView::wheelEvent(event);
|
QGraphicsView::wheelEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtGraphicsView::contextMenuEvent(QContextMenuEvent* event)
|
||||||
|
{
|
||||||
|
QtContextMenu::getInstance()->showExtended(event, this, std::vector<QAction*>(1, m_exportGraphAction));
|
||||||
|
}
|
||||||
|
|
||||||
void QtGraphicsView::updateTimer()
|
void QtGraphicsView::updateTimer()
|
||||||
{
|
{
|
||||||
float ds = 30.0f;
|
float ds = 30.0f;
|
||||||
@@ -231,6 +244,27 @@ void QtGraphicsView::stopTimer()
|
|||||||
m_timer->stop();
|
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
|
bool QtGraphicsView::moves() const
|
||||||
{
|
{
|
||||||
return m_up || m_down || m_left || m_right;
|
return m_up || m_down || m_left || m_right;
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ protected:
|
|||||||
|
|
||||||
void wheelEvent(QWheelEvent* event);
|
void wheelEvent(QWheelEvent* event);
|
||||||
|
|
||||||
|
void contextMenuEvent(QContextMenuEvent* event);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void emptySpaceClicked();
|
void emptySpaceClicked();
|
||||||
|
|
||||||
@@ -38,6 +40,8 @@ private slots:
|
|||||||
void updateTimer();
|
void updateTimer();
|
||||||
void stopTimer();
|
void stopTimer();
|
||||||
|
|
||||||
|
void exportGraph();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool moves() const;
|
bool moves() const;
|
||||||
|
|
||||||
@@ -57,6 +61,8 @@ private:
|
|||||||
|
|
||||||
std::shared_ptr<QTimer> m_timer;
|
std::shared_ptr<QTimer> m_timer;
|
||||||
std::shared_ptr<QTimer> m_timerStopper;
|
std::shared_ptr<QTimer> m_timerStopper;
|
||||||
|
|
||||||
|
QAction* m_exportGraphAction;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QT_GRAPHICS_VIEW_H
|
#endif // QT_GRAPHICS_VIEW_H
|
||||||
|
|||||||
Reference in New Issue
Block a user