src/ChartiSpindel.cpp

changeset 371
d03a426e0b6b
parent 334
9203fa01ddbf
child 372
d9c78eb19728
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 "ChartiSpindel.h" 17 #include "ChartiSpindel.h"
18 #include "callout.h"
18 #include "MainWindow.h" 19 #include "MainWindow.h"
19 20
20 21
21 ChartiSpindel::ChartiSpindel(QString code, QString name, QWidget *parent) : QDialog(parent) 22 ChartiSpindel::ChartiSpindel(QString code, QString name, QWidget *parent) : QDialog(parent)
22 { 23 {
29 dialog->setWindowTitle(tr("BMSapp - iSpindel ") + "\"" + name + "\""); 30 dialog->setWindowTitle(tr("BMSapp - iSpindel ") + "\"" + name + "\"");
30 dialog->setObjectName(QString::fromUtf8("ChartiSpindel")); 31 dialog->setObjectName(QString::fromUtf8("ChartiSpindel"));
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 *density = new QSplineSeries(); 48 density = new QSplineSeries();
44 QSplineSeries *battery = new QSplineSeries(); 49 battery = new QSplineSeries();
45 50
46 query.prepare("SELECT * FROM log_ispindel WHERE code=:code ORDER BY datetime"); 51 query.prepare("SELECT * FROM log_ispindel WHERE code=:code ORDER BY datetime");
47 query.bindValue(":code", code); 52 query.bindValue(":code", code);
48 query.exec(); 53 query.exec();
49 while (query.next()) { 54 while (query.next()) {
51 temperature->append(timestamp, query.value("temperature").toDouble()); 56 temperature->append(timestamp, query.value("temperature").toDouble());
52 density->append(timestamp, query.value("sg").toDouble()); 57 density->append(timestamp, query.value("sg").toDouble());
53 battery ->append(timestamp, query.value("battery").toDouble()); 58 battery ->append(timestamp, query.value("battery").toDouble());
54 } 59 }
55 60
56 temperature->setName(tr("Temp °C")); 61 temperature->setName(tr("Temperature"));
57 temperature->setColor(QColorConstants::Svg::red); 62 temperature->setColor(QColorConstants::Svg::red);
58 density->setName(tr("SG")); 63 density->setName(tr("SG"));
59 QPen pen(QColorConstants::Svg::navy); 64 QPen pen(QColorConstants::Svg::navy);
60 pen.setWidth(3); 65 pen.setWidth(3);
61 density->setPen(pen); 66 density->setPen(pen);
62 battery->setName(tr("Battery")); 67 battery->setName(tr("Battery"));
63 battery->setColor(QColorConstants::Svg::limegreen); 68 battery->setColor(QColorConstants::Svg::limegreen);
64 69
65 QChart *chart = new QChart(); 70 chart = new QChart();
66 chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name)); 71 chart->setTitle(QString("%1 \"%2\"").arg(code).arg(name));
67 chart->addSeries(battery); 72 chart->addSeries(battery);
68 chart->addSeries(temperature); 73 chart->addSeries(temperature);
69 chart->addSeries(density); 74 chart->addSeries(density);
70 75
100 axisYB->setTitleText(tr("Battery volt")); 105 axisYB->setTitleText(tr("Battery volt"));
101 axisYB->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); 106 axisYB->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
102 chart->addAxis(axisYB, Qt::AlignRight); 107 chart->addAxis(axisYB, Qt::AlignRight);
103 battery->attachAxis(axisYB); 108 battery->attachAxis(axisYB);
104 109
105 QChartView *chartView = new QChartView(chart); 110 connect(temperature, &QSplineSeries::hovered, this, &ChartiSpindel::tooltip);
111 connect(density, &QSplineSeries::hovered, this, &ChartiSpindel::tooltip);
112 connect(battery, &QSplineSeries::hovered, this, &ChartiSpindel::tooltip);
113
114 chartView = new QChartView(chart);
106 chartView->setRenderHint(QPainter::Antialiasing); 115 chartView->setRenderHint(QPainter::Antialiasing);
107 dialog->setLayout(new QVBoxLayout); 116 dialog->setLayout(new QHBoxLayout);
108 dialog->layout()->addWidget(chartView); 117 dialog->layout()->addWidget(chartView);
109 dialog->layout()->addWidget(buttonBox); 118 dialog->layout()->addWidget(buttonBox);
110 119
111 QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept())); 120 QObject::connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
112 QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); 121 QObject::connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
122 QObject::connect(saveButton, SIGNAL(clicked()), this, SLOT(savePNG()));
113 123
114 dialog->setModal(true); 124 dialog->setModal(true);
115 dialog->exec(); 125 dialog->exec();
116 } 126 }
117 127
118 128
119 ChartiSpindel::~ChartiSpindel() {} 129 ChartiSpindel::~ChartiSpindel() {}
120 130
131
132 void ChartiSpindel::savePNG()
133 {
134 QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), QDir::homePath() + "/ispindel.png", tr("Image (*.png)"));
135 if (path.isEmpty()) {
136 QMessageBox::warning(this, tr("Save File"), tr("No image file selected."));
137 return;
138 }
139
140 QImage img((chartView->size()), QImage::Format_ARGB32);
141 QPainter painter;
142 painter.begin(&img);
143 chartView->render(&painter);
144 painter.setRenderHint(QPainter::Antialiasing);
145 painter.end();
146 img.save(path);
147 }
148
149
150 void ChartiSpindel::tooltip(QPointF point, bool state)
151 {
152 QAbstractSeries *series = qobject_cast<QAbstractSeries *>(sender());
153
154 if (t_tooltip == 0)
155 t_tooltip = new Callout(chart, series);
156
157 if (state) {
158 QDateTime timeis = QDateTime::fromMSecsSinceEpoch(point.x());
159 t_tooltip->setSeries(series);
160 if (series == temperature)
161 t_tooltip->setText(QString("%1\nTemperature %2°C").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 2, 'f', 1));
162 else if (series == density)
163 t_tooltip->setText(QString("%1\nDensity %2 SG").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 5, 'f', 4));
164 else if (series == battery)
165 t_tooltip->setText(QString("%1\nBattery %2 volt").arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 3, 'f', 2));
166 t_tooltip->setAnchor(point);
167 t_tooltip->setZValue(11);
168 t_tooltip->updateGeometry();
169 t_tooltip->show();
170 } else {
171 t_tooltip->hide();
172 }
173 }
174

mercurial