Skip to content

Commit

Permalink
plot exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
t-weber committed Jan 3, 2025
1 parent c926e82 commit 0c1b533
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/gui/dialogs/about.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ About::About(QWidget *parent, const QIcon *progIcon)
{
namespace alg = boost::algorithm;

setWindowTitle("About");
setWindowTitle("About " TRACKS_TITLE);
setSizeGripEnabled(true);


Expand Down
85 changes: 84 additions & 1 deletion src/gui/dialogs/reports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
#include <limits>
#include <cmath>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#endif


// table columns
#define TAB_DATE 0
Expand Down Expand Up @@ -53,6 +61,14 @@ Reports::Reports(QWidget* parent)
m_plot->xAxis->setTicker(QSharedPointer<QCPAxisTicker>{ticker});

connect(m_plot/*.get()*/, &QCustomPlot::mouseMove, this, &Reports::PlotMouseMove);
connect(m_plot/*.get()*/, &QCustomPlot::mousePress, this, &Reports::PlotMouseClick);

// context menu
m_context = std::make_shared<QMenu>(this);
QIcon iconSavePdf = QIcon::fromTheme("image-x-generic");
QAction *actionSavePdf = new QAction(iconSavePdf, "Save Image...", m_context.get());
m_context->addAction(actionSavePdf);
connect(actionSavePdf, &QAction::triggered, this, &Reports::SavePlotPdf);

// "all tracks" checkbox
m_all_tracks = std::make_shared<QCheckBox>(plot_panel);
Expand Down Expand Up @@ -201,6 +217,9 @@ Reports::Reports(QWidget* parent)
m_table->setColumnWidth(TAB_TIME_SUM, settings.value("dlg_reports/timesum_col").toInt());
else
m_table->setColumnWidth(TAB_TIME_SUM, 150);

if(settings.contains("dlg_reports/recent_pdfs"))
m_pdfdir = settings.value("dlg_reports/recent_pdfs").toString().toStdString();
}


Expand Down Expand Up @@ -442,7 +461,7 @@ void Reports::PlotDistances()

graph->setData(epochs, dists, true);
graph->setLineStyle(QCPGraph::lsLine);
graph->setScatterStyle(QCPScatterStyle{QCPScatterStyle::ssCircle, pen, brush, 6.});
graph->setScatterStyle(QCPScatterStyle{QCPScatterStyle::ssDisc, pen, brush, 6.});
graph->setPen(pen);
//graph->setBrush(brush);
};
Expand Down Expand Up @@ -476,6 +495,9 @@ void Reports::PlotDistances()
}


/**
* the mouse has been moved in the plot widget
*/
void Reports::PlotMouseMove(QMouseEvent *evt)
{
if(!m_plot)
Expand Down Expand Up @@ -504,6 +526,66 @@ void Reports::PlotMouseMove(QMouseEvent *evt)
}


/**
* the mouse has been clicked in the plot widget
*/
void Reports::PlotMouseClick(QMouseEvent *evt)
{
if(!m_plot || !m_context)
return;

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QPoint pos = evt->position();
#else
QPoint pos = evt->pos();
#endif

if(evt->buttons() & Qt::RightButton)
{
QPoint posGlobal = m_plot->mapToGlobal(pos);
posGlobal.rx() += 4;
posGlobal.ry() += 4;

m_context->popup(posGlobal);
}
}


/**
* save the plot as a pdf image
*/
void Reports::SavePlotPdf()
{
if(!m_plot)
return;

auto filedlg = std::make_shared<QFileDialog>(
this, "Save Plot as Image", m_pdfdir.c_str(),
"PDF Files (*.pdf)");
filedlg->setAcceptMode(QFileDialog::AcceptSave);
filedlg->setDefaultSuffix("pdf");
filedlg->selectFile("distance");
filedlg->setFileMode(QFileDialog::AnyFile);

if(!filedlg->exec())
return;

QStringList files = filedlg->selectedFiles();
if(files.size() == 0 || files[0] == "")
return;

if(!m_plot->savePdf(files[0]))
{
QMessageBox::critical(this, "Error",
QString("File \"%1\" could not be saved.").arg(files[0]));
return;
}

fs::path file{files[0].toStdString()};
m_pdfdir = file.parent_path().string();
}


void Reports::accept()
{
// save settings
Expand All @@ -514,6 +596,7 @@ void Reports::accept()

settings.setValue("dlg_reports/all_tracks", m_all_tracks->isChecked());
settings.setValue("dlg_reports/sum_distances", m_cumulative->isChecked());
settings.setValue("dlg_reports/recent_pdfs", m_pdfdir.c_str());

QByteArray split{m_split->saveState()};
settings.setValue("dlg_reports/split", split);
Expand Down
7 changes: 7 additions & 0 deletions src/gui/dialogs/reports.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QSplitter>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QMenu>

// https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
#pragma GCC diagnostic push
Expand Down Expand Up @@ -53,7 +54,9 @@ class Reports : public QDialog
virtual void reject() override;

void PlotMouseMove(QMouseEvent *evt);
void PlotMouseClick(QMouseEvent *evt);
void ResetDistPlotRange();
void SavePlotPdf();


private:
Expand All @@ -64,12 +67,16 @@ class Reports : public QDialog
std::shared_ptr<QCheckBox> m_all_tracks{}, m_cumulative{};
std::shared_ptr<QLabel> m_status{};
std::shared_ptr<QDialogButtonBox> m_buttonbox{};
std::shared_ptr<QMenu> m_context{};

const t_tracks *m_trackdb{};
typename t_tracks::t_timept_map m_monthly{}, m_yearly{};

t_real m_min_epoch{}, m_max_epoch{};
t_real m_min_dist{}, m_max_dist{};

// directory with recently saved pdf files
std::string m_pdfdir{};
};


Expand Down
86 changes: 84 additions & 2 deletions src/gui/dialogs/statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#include <limits>
#include <cmath>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#endif


Statistics::Statistics(QWidget* parent)
: QDialog(parent)
Expand All @@ -36,8 +44,15 @@ Statistics::Statistics(QWidget* parent)
ticker->setDateTimeSpec(Qt::LocalTime);
ticker->setDateTimeFormat("yyyy-MM-dd");
m_plot->xAxis->setTicker(QSharedPointer<QCPAxisTicker>{ticker});

connect(m_plot.get(), &QCustomPlot::mouseMove, this, &Statistics::PlotMouseMove);
connect(m_plot.get(), &QCustomPlot::mousePress, this, &Statistics::PlotMouseClick);

// context menu
m_context = std::make_shared<QMenu>(this);
QIcon iconSavePdf = QIcon::fromTheme("image-x-generic");
QAction *actionSavePdf = new QAction(iconSavePdf, "Save Image...", m_context.get());
m_context->addAction(actionSavePdf);
connect(actionSavePdf, &QAction::triggered, this, &Statistics::SavePlotPdf);

// speed/pace checkbox
m_speed_check = std::make_shared<QCheckBox>(this);
Expand Down Expand Up @@ -151,6 +166,9 @@ Statistics::Statistics(QWidget* parent)

if(settings.contains("dlg_statistics/speed_check"))
m_speed_check->setChecked(settings.value("dlg_statistics/speed_check").toBool());

if(settings.contains("dlg_statistics/recent_pdfs"))
m_pdfdir = settings.value("dlg_statistics/recent_pdfs").toString().toStdString();
}


Expand Down Expand Up @@ -276,7 +294,7 @@ void Statistics::PlotSpeeds()

graph->setData(epochs, paces, true);
graph->setLineStyle(QCPGraph::lsLine);
graph->setScatterStyle(QCPScatterStyle{QCPScatterStyle::ssCircle, pen, brush, 6.});
graph->setScatterStyle(QCPScatterStyle{QCPScatterStyle::ssDisc, pen, brush, 6.});
graph->setPen(pen);
//graph->setBrush(brush);
};
Expand Down Expand Up @@ -305,6 +323,9 @@ void Statistics::PlotSpeeds()
}


/**
* the mouse has been moved in the plot widget
*/
void Statistics::PlotMouseMove(QMouseEvent *evt)
{
if(!m_plot)
Expand Down Expand Up @@ -338,6 +359,66 @@ void Statistics::PlotMouseMove(QMouseEvent *evt)
}


/**
* the mouse has been clicked in the plot widget
*/
void Statistics::PlotMouseClick(QMouseEvent *evt)
{
if(!m_plot || !m_context)
return;

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QPoint pos = evt->position();
#else
QPoint pos = evt->pos();
#endif

if(evt->buttons() & Qt::RightButton)
{
QPoint posGlobal = m_plot->mapToGlobal(pos);
posGlobal.rx() += 4;
posGlobal.ry() += 4;

m_context->popup(posGlobal);
}
}


/**
* save the plot as a pdf image
*/
void Statistics::SavePlotPdf()
{
if(!m_plot)
return;

auto filedlg = std::make_shared<QFileDialog>(
this, "Save Plot as Image", m_pdfdir.c_str(),
"PDF Files (*.pdf)");
filedlg->setAcceptMode(QFileDialog::AcceptSave);
filedlg->setDefaultSuffix("pdf");
filedlg->selectFile("pace");
filedlg->setFileMode(QFileDialog::AnyFile);

if(!filedlg->exec())
return;

QStringList files = filedlg->selectedFiles();
if(files.size() == 0 || files[0] == "")
return;

if(!m_plot->savePdf(files[0]))
{
QMessageBox::critical(this, "Error",
QString("File \"%1\" could not be saved.").arg(files[0]));
return;
}

fs::path file{files[0].toStdString()};
m_pdfdir = file.parent_path().string();
}


void Statistics::accept()
{
// save settings
Expand All @@ -353,6 +434,7 @@ void Statistics::accept()
}

settings.setValue("dlg_statistics/speed_check", m_speed_check->isChecked());
settings.setValue("dlg_statistics/recent_pdfs", m_pdfdir.c_str());

QDialog::accept();
}
Expand Down
7 changes: 7 additions & 0 deletions src/gui/dialogs/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QtWidgets/QLabel>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QMenu>

// https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
#pragma GCC diagnostic push
Expand Down Expand Up @@ -48,14 +49,17 @@ class Statistics : public QDialog
virtual void reject() override;

void PlotMouseMove(QMouseEvent *evt);
void PlotMouseClick(QMouseEvent *evt);
void ResetSpeedPlotRange();
void SavePlotPdf();


private:
std::shared_ptr<QCustomPlot> m_plot{};
std::shared_ptr<QCheckBox> m_speed_check{};
std::shared_ptr<QLabel> m_status{};
std::shared_ptr<QDialogButtonBox> m_buttonbox{};
std::shared_ptr<QMenu> m_context{};

static constexpr const t_real s_lengths[] = { 25., 20., 15., 10., 5., 0. };
static constexpr const t_size s_num_lengths = sizeof(s_lengths)/sizeof(s_lengths[0]);
Expand All @@ -65,6 +69,9 @@ class Statistics : public QDialog
t_real m_min_pace{}, m_max_pace{};

const t_tracks *m_trackdb{};

// directory with recently saved pdf files
std::string m_pdfdir{};
};


Expand Down
Loading

0 comments on commit 0c1b533

Please sign in to comment.