-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
115 lines (98 loc) · 3.18 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <QDateTime>
#include <QFileDialog>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::writefile() {
if(ui->path->text() != "File" && ui->path->text() != "") {
QFile file(ui->path->text());
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << ui->lcd->value();
// optional, as QFile destructor will already do it:
file.close();
}
}
void MainWindow::decorate() {
ui->lcd->setAutoFillBackground(true);
palette->setColor(QPalette::Window,Qt::lightGray);
double value = ui->lcd->value();
ushort cases = 0;
if(value < 15) cases = 4;
if(value < 10) cases = 5;
if(value <= 5) cases = 1;
if(value <= 0) cases = 2;
if(value <= -5) cases = 99;
if(value >= 15) cases = 3;
if(value > 20) cases = 6;
if(value >= 50) cases = 7;
if(value >= 100) cases = 8;
switch(cases) {
case 1: palette->setColor(QPalette::WindowText,Qt::red); break;
case 2: palette->setColor(QPalette::WindowText,Qt::darkRed); break;
case 3: palette->setColor(QPalette::WindowText,Qt::green); break;
case 4: palette->setColor(QPalette::WindowText,Qt::yellow); break;
case 5: palette->setColor(QPalette::WindowText,Qt::magenta); break;
case 6: palette->setColor(QPalette::WindowText,Qt::blue); break;
case 7: palette->setColor(QPalette::WindowText,Qt::cyan); break;
case 8: palette->setColor(QPalette::WindowText,Qt::white); break;
default: palette->setColor(QPalette::WindowText,Qt::black);
}
ui->lcd->setPalette(*palette);
}
void MainWindow::update(double v) {
QDateTime dateTime = QDateTime::currentDateTime();
double value = ui->lcd->value();
double diff = -1*(value - v);
QString sgn = "";
if(diff >= 0)
sgn = "+";
ui->log->appendPlainText(dateTime.toString("yyyy-MM-ddThh:mm:ss") + "\t" +
QString::number((int) value) + QString::fromUtf8(" \u21A6 ") + QString::number((int) v) + "\t(" + sgn + QString::number((int) diff) + ")");
ui->lcd->display(v);
decorate();
writefile();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFixedSize(this->geometry().width(),this->geometry().height());
palette = new QPalette();
decorate();
}
MainWindow::~MainWindow()
{
delete palette;
delete ui;
}
void MainWindow::on_plusButton_clicked()
{
update(ui->lcd->value() + 1);
}
void MainWindow::on_minusButton_clicked()
{
update(ui->lcd->value() - 1);
}
void MainWindow::on_setButton_clicked()
{
update(ui->setValue->value());
}
void MainWindow::on_resetButton_clicked()
{
ui->lcd->display(ui->resetValue->value());
ui->log->clear();
decorate();
writefile();
}
void MainWindow::on_fileButton_clicked()
{
ui->path->setText(QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("Textfiles (*.txt)")));
}
void MainWindow::on_addButton_clicked()
{
update(ui->lcd->value() + ui->addValue->value());
}