src/ChartFermenter.cpp

changeset 371
d03a426e0b6b
parent 333
499c95108bbd
child 434
ebf4996ab396
--- a/src/ChartFermenter.cpp	Tue Jul 26 11:15:37 2022 +0200
+++ b/src/ChartFermenter.cpp	Tue Jul 26 14:26:50 2022 +0200
@@ -15,6 +15,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 #include "ChartFermenter.h"
+#include "callout.h"
 #include "MainWindow.h"
 
 
@@ -31,13 +32,17 @@
     dialog->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
     dialog->resize(1024, 600);
 
+    QPushButton *saveButton = new QPushButton(tr("Save"));
+    saveButton->setAutoDefault(false);
+    QIcon icon1;
+    icon1.addFile(QString::fromUtf8(":icons/silk/disk.png"), QSize(), QIcon::Normal, QIcon::Off);
+    saveButton->setIcon(icon1);
+
     QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
     buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
-    buttonBox->setGeometry(QRect(40, 565, 944, 36));
-    buttonBox->setLayoutDirection(Qt::LeftToRight);
-    buttonBox->setOrientation(Qt::Horizontal);
+    buttonBox->setOrientation(Qt::Vertical);
     buttonBox->setStandardButtons(QDialogButtonBox::Ok);
-    buttonBox->setCenterButtons(true);
+    buttonBox->addButton(saveButton,QDialogButtonBox::ActionRole);
 
     QSplineSeries *pv_air = new QSplineSeries();
     QSplineSeries *pv_beer = new QSplineSeries();
@@ -75,7 +80,7 @@
     pwr_heat->setOpacity(0.25);
     pwr_heat->setColor(QColorConstants::Red);
 
-    QChart *chart = new QChart();
+    chart = new QChart();
     chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name));
     chart->addSeries(pwr_cool);
     chart->addSeries(pwr_heat);
@@ -114,16 +119,19 @@
     pwr_cool->attachAxis(axisYR);
     pwr_heat->attachAxis(axisYR);
 
-
+    connect(pv_air, &QSplineSeries::hovered, this, &ChartFermenter::tooltip);
+    connect(pv_beer, &QSplineSeries::hovered, this, &ChartFermenter::tooltip);
+    connect(pv_chiller, &QSplineSeries::hovered, this, &ChartFermenter::tooltip);
 
-    QChartView *chartView = new QChartView(chart);
+    chartView = new QChartView(chart);
     chartView->setRenderHint(QPainter::Antialiasing);
-    dialog->setLayout(new QVBoxLayout);
+    dialog->setLayout(new QHBoxLayout);
     dialog->layout()->addWidget(chartView);
     dialog->layout()->addWidget(buttonBox);
 
     QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
     QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
+    QObject::connect(saveButton, SIGNAL(clicked()), this, SLOT(savePNG()));
 
     dialog->setModal(true);
     dialog->exec();
@@ -132,3 +140,44 @@
 
 ChartFermenter::~ChartFermenter() {}
 
+
+void ChartFermenter::savePNG()
+{
+    QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), QDir::homePath() + "/fermenter.png", tr("Image (*.png)"));
+    if (path.isEmpty()) {
+	QMessageBox::warning(this, tr("Save File"), tr("No image file selected."));
+	return;
+    }
+
+    QImage img((chartView->size()), QImage::Format_ARGB32);
+    QPainter painter;
+    painter.begin(&img);
+    chartView->render(&painter);
+    painter.setRenderHint(QPainter::Antialiasing);
+    painter.end();
+    img.save(path);
+}
+
+
+void ChartFermenter::tooltip(QPointF point, bool state)
+{
+    QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender());
+
+    if (t_tooltip == 0)
+	t_tooltip = new Callout(chart, series);
+
+    if (state) {
+	QDateTime timeis = QDateTime::fromMSecsSinceEpoch(point.x());
+	//qDebug() << "tooltip" << QString("%1 %2°C").arg( timeis.toString("dd-MM-yyyy hh:mm") ).arg(point.y(), 2, 'f', 1);
+
+	t_tooltip->setSeries(series);
+	t_tooltip->setText(QString("%1\n%2 %3°C").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(series->name()).arg(point.y(), 2, 'f', 1));
+	t_tooltip->setAnchor(point);
+	t_tooltip->setZValue(11);
+	t_tooltip->updateGeometry();
+	t_tooltip->show();
+    } else {
+	t_tooltip->hide();
+    }
+}
+

mercurial