From 24850984a3398bb3080fc4c54ce8f9b0a4663be8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 16 Dec 2024 22:15:41 +1100 Subject: [PATCH] Allow setting plot title - Closes https://github.com/SchrodingersGat/lumberjack/issues/108 --- src/mainwindow.cpp | 5 ++++- src/plot_widget.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/plot_widget.hpp | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3c4a03b..937e50c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -444,7 +444,10 @@ void MainWindow::addPlot() PlotWidget *plot = new PlotWidget(); - QDockWidget *dock = new QDockWidget(tr("Plot") + QString(" ") + QString::number(plotIndex), this); + QString title = tr("Plot") + QString(" ") + QString::number(plotIndex); + + QDockWidget *dock = new QDockWidget(title, this); + dock->setWindowTitle(title); dock->setObjectName("plot-" + QString::number(plotIndex)); dock->setAllowedAreas(Qt::AllDockWidgetAreas); plot->setParent(dock); diff --git a/src/plot_widget.cpp b/src/plot_widget.cpp index 82a6a9c..d44ea76 100644 --- a/src/plot_widget.cpp +++ b/src/plot_widget.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -397,6 +399,7 @@ void PlotWidget::onContextMenu(const QPoint &pos) syncAction->setChecked(isTimescaleSynced()); QAction *bgColor = plotMenu->addAction(tr("Set Color")); + QAction *plotTitle = plotMenu->addAction(tr("Set Title")); menu.addMenu(plotMenu); @@ -476,6 +479,10 @@ void PlotWidget::onContextMenu(const QPoint &pos) { selectBackgroundColor(); } + else if (action == plotTitle) + { + setPlotTitle(); + } } @@ -544,6 +551,37 @@ void PlotWidget::selectBackgroundColor() } +void PlotWidget::setPlotTitle(QString title) +{ + bool ok = true; + + QDockWidget *dock = qobject_cast(parent()); + + QString currentTitle = !!dock ? dock->windowTitle() : windowTitle(); + + if (title.isEmpty()) + { + title = QInputDialog::getText( + this, + tr("Set Title"), + tr("Set plot tile"), + QLineEdit::Normal, + windowTitle() + ); + } + + if (ok && !title.isEmpty()) + { + setWindowTitle(title); + + if (dock) + { + dock->setWindowTitle(title); + } + } +} + + /** * @brief PlotWidget::exportDataToFile - export data to a file */ diff --git a/src/plot_widget.hpp b/src/plot_widget.hpp index be84711..8142f96 100644 --- a/src/plot_widget.hpp +++ b/src/plot_widget.hpp @@ -68,6 +68,7 @@ public slots: void autoScale(QSharedPointer curve); void setBackgroundColor(QColor color); + void setPlotTitle(QString title = QString()); void onContextMenu(const QPoint &pos);