src/ChartCarbonate.cpp

changeset 371
d03a426e0b6b
parent 335
c4b4a2879bb8
child 434
ebf4996ab396
equal deleted inserted replaced
370:a730825bc5e4 371:d03a426e0b6b
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 #include "ChartCarbonate.h" 17 #include "ChartCarbonate.h"
18 #include "callout.h"
18 #include "MainWindow.h" 19 #include "MainWindow.h"
19 20
20 21
21 ChartCarbonate::ChartCarbonate(QString code, QString name, QWidget *parent) : QDialog(parent) 22 ChartCarbonate::ChartCarbonate(QString code, QString name, QWidget *parent) : QDialog(parent)
22 { 23 {
29 dialog->setWindowTitle(tr("BMSapp - Carbonation ") + "\"" + name + "\""); 30 dialog->setWindowTitle(tr("BMSapp - Carbonation ") + "\"" + name + "\"");
30 dialog->setObjectName(QString::fromUtf8("ChartCarbonate")); 31 dialog->setObjectName(QString::fromUtf8("ChartCarbonate"));
31 dialog->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint); 32 dialog->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
32 dialog->resize(1024, 600); 33 dialog->resize(1024, 600);
33 34
35 QPushButton *saveButton = new QPushButton(tr("Save"));
36 saveButton->setAutoDefault(false);
37 QIcon icon1;
38 icon1.addFile(QString::fromUtf8(":icons/silk/disk.png"), QSize(), QIcon::Normal, QIcon::Off);
39 saveButton->setIcon(icon1);
40
34 QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog); 41 QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
35 buttonBox->setObjectName(QString::fromUtf8("buttonBox")); 42 buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
36 buttonBox->setGeometry(QRect(40, 565, 944, 36)); 43 buttonBox->setOrientation(Qt::Vertical);
37 buttonBox->setLayoutDirection(Qt::LeftToRight);
38 buttonBox->setOrientation(Qt::Horizontal);
39 buttonBox->setStandardButtons(QDialogButtonBox::Ok); 44 buttonBox->setStandardButtons(QDialogButtonBox::Ok);
40 buttonBox->setCenterButtons(true); 45 buttonBox->addButton(saveButton,QDialogButtonBox::ActionRole);
41 46
42 QSplineSeries *temperature = new QSplineSeries(); 47 temperature = new QSplineSeries();
43 QSplineSeries *pressure = new QSplineSeries(); 48 pressure = new QSplineSeries();
44 49
45 query.prepare("SELECT * FROM log_co2pressure WHERE code=:code ORDER BY datetime"); 50 query.prepare("SELECT * FROM log_co2pressure WHERE code=:code ORDER BY datetime");
46 query.bindValue(":code", code); 51 query.bindValue(":code", code);
47 query.exec(); 52 query.exec();
48 while (query.next()) { 53 while (query.next()) {
56 pressure->setName(tr("Pressure bar")); 61 pressure->setName(tr("Pressure bar"));
57 QPen pen(QColorConstants::Svg::navy); 62 QPen pen(QColorConstants::Svg::navy);
58 pen.setWidth(3); 63 pen.setWidth(3);
59 pressure->setPen(pen); 64 pressure->setPen(pen);
60 65
61 QChart *chart = new QChart(); 66 chart = new QChart();
62 chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name)); 67 chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name));
63 chart->addSeries(temperature); 68 chart->addSeries(temperature);
64 chart->addSeries(pressure); 69 chart->addSeries(pressure);
65 70
66 QDateTimeAxis *axisX = new QDateTimeAxis; 71 QDateTimeAxis *axisX = new QDateTimeAxis;
88 axisYP->setTitleText(tr("Pressure bar")); 93 axisYP->setTitleText(tr("Pressure bar"));
89 axisYP->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); 94 axisYP->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
90 chart->addAxis(axisYP, Qt::AlignLeft); 95 chart->addAxis(axisYP, Qt::AlignLeft);
91 pressure->attachAxis(axisYP); 96 pressure->attachAxis(axisYP);
92 97
93 QChartView *chartView = new QChartView(chart); 98 chart->setAcceptHoverEvents(true);
99
100 connect(temperature, &QSplineSeries::hovered, this, &ChartCarbonate::tooltip);
101 connect(pressure, &QSplineSeries::hovered, this, &ChartCarbonate::tooltip);
102
103 chartView = new QChartView(chart);
94 chartView->setRenderHint(QPainter::Antialiasing); 104 chartView->setRenderHint(QPainter::Antialiasing);
95 dialog->setLayout(new QVBoxLayout); 105 dialog->setLayout(new QHBoxLayout);
96 dialog->layout()->addWidget(chartView); 106 dialog->layout()->addWidget(chartView);
97 dialog->layout()->addWidget(buttonBox); 107 dialog->layout()->addWidget(buttonBox);
98 108
99 QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept())); 109 QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
100 QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); 110 QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
111 QObject::connect(saveButton, SIGNAL(clicked()), this, SLOT(savePNG()));
101 112
102 dialog->setModal(true); 113 dialog->setModal(true);
103 dialog->exec(); 114 dialog->exec();
104 } 115 }
105 116
106 117
107 ChartCarbonate::~ChartCarbonate() {} 118 ChartCarbonate::~ChartCarbonate() {}
108 119
120
121 void ChartCarbonate::savePNG()
122 {
123 QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), QDir::homePath() + "/carbonation.png", tr("Image (*.png)"));
124 if (path.isEmpty()) {
125 QMessageBox::warning(this, tr("Save File"), tr("No image file selected."));
126 return;
127 }
128
129 QImage img((chartView->size()), QImage::Format_ARGB32);
130 QPainter painter;
131 painter.begin(&img);
132 chartView->render(&painter);
133 painter.setRenderHint(QPainter::Antialiasing);
134 painter.end();
135 img.save(path);
136 }
137
138
139 void ChartCarbonate::tooltip(QPointF point, bool state)
140 {
141 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender());
142
143 if (t_tooltip == 0)
144 t_tooltip = new Callout(chart, series);
145
146 if (state) {
147 QDateTime timeis = QDateTime::fromMSecsSinceEpoch(point.x());
148 QString suffix = (series == temperature) ? "°C":" bar";
149 t_tooltip->setSeries(series);
150 t_tooltip->setText(QString("%1\n%2%3").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 2, 'f', 1).arg(suffix));
151 t_tooltip->setAnchor(point);
152 t_tooltip->setZValue(11);
153 t_tooltip->updateGeometry();
154 t_tooltip->show();
155 } else {
156 t_tooltip->hide();
157 }
158 }
159

mercurial