Implement cache plot save to file
MODIFY
src/cachesim/cacheplotview.cpp
src/cachesim/cacheplotview.cpp
@@ -46,6 +46,24 @@ void CachePlotView::leaveEvent(QEvent* event) {
QGraphicsView::leaveEvent(event);
}
+QPixmap CachePlotView::getPlotPixmap() {
+ std::vector<QGraphicsItem*> itemsToHide = {m_coordX, m_coordY, m_marker};
+
+ for (const auto& i : itemsToHide) {
+ if (i)
+ i->hide();
+ }
+
+ QPixmap p = grab();
+
+ for (const auto& i : itemsToHide) {
+ if (i)
+ i->show();
+ }
+
+ return p;
+};
+
void CachePlotView::setPlot(QChart* chart) {
if (m_chart) {
// Propagate the previous chart size to the new plot

MODIFY
src/cachesim/cacheplotview.h
src/cachesim/cacheplotview.h
@@ -29,6 +29,12 @@ public:
void setPlot(QChart* chart);
+ /**
+ * @brief getPlotPixmap
+ * @returns a pixmap containing only the plot (ie. no coordinate values nor marker)
+ */
+ QPixmap getPlotPixmap();
+
protected:
void resizeEvent(QResizeEvent* event);
void mouseMoveEvent(QMouseEvent* event);

MODIFY
src/cachesim/cacheplotwidget.cpp
src/cachesim/cacheplotwidget.cpp
#include "ui_cacheplotwidget.h"
#include <QClipboard>
+#include <QFileDialog>
#include <QToolBar>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QChartView>
@@ -88,6 +89,7 @@ void CachePlotWidget::setupToolbar() {
m_savePlotAction = new QAction("Save plot to file", this);
m_savePlotAction->setIcon(saveIcon);
m_toolbar->addAction(m_savePlotAction);
+ connect(m_savePlotAction, &QAction::triggered, this, &CachePlotWidget::savePlot);
}
void CachePlotWidget::plotTypeChanged() {
@@ -104,6 +106,14 @@ void CachePlotWidget::plotTypeChanged() {
variablesChanged();
}
+void CachePlotWidget::savePlot() {
+ const QString filename = QFileDialog::getSaveFileName(this, "Save file", "", "Images (*.png)");
+ if (!filename.isEmpty()) {
+ const QPixmap p = m_ui->plotView->getPlotPixmap();
+ p.save(filename, "PNG");
+ }
+}
+
void CachePlotWidget::copyPlotDataToClipboard() const {
std::vector<Variable> allVariables;
for (int i = 0; i < N_Variables; i++) {

MODIFY
src/cachesim/cacheplotwidget.h
src/cachesim/cacheplotwidget.h
@@ -48,6 +48,7 @@ private:
void setupStackedVariablesList();
void setPlot(QChart* plot);
void copyPlotDataToClipboard() const;
+ void savePlot();
std::vector<CachePlotWidget::Variable> gatherVariables() const;
QChart* createRatioPlot(const Variable num, const Variable den) const;

Implement cache plot save to file