-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
111 lines (105 loc) · 4.35 KB
/
mainwindow.cpp
File metadata and controls
111 lines (105 loc) · 4.35 KB
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
#include "mainwindow.h"
#include <QMessageBox>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <QSqlError>
QString MainWindow::databaseConnection = "DetectorConnection";
QSqlDatabase MainWindow::database = QSqlDatabase::database();
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
systemOption(new SystemOption(this)),
dataDisplay(new DataDisplay(this)),
dataWave(new DataWave(this)),
statusAnalysis(new StatusAnalysis(this)),
historyData(new HistoricalData(nullptr)),
mainWindowLayout(new QGridLayout(this))
{
resize(800, 600);
mainWindowFont.setFamily("Microsoft YaHei");
mainWindowFont.setPixelSize(14);
setFont(mainWindowFont);
mainWindowLayout->addWidget(systemOption, 0, 0, 4, 4);
mainWindowLayout->addWidget(dataDisplay, 0, 4, 4, 5);
mainWindowLayout->addWidget(dataWave, 4, 0, 6, 6);
mainWindowLayout->addWidget(statusAnalysis, 4, 6, 6, 3);
for(int i = 0; i < mainWindowLayout->columnCount(); i++)
mainWindowLayout->setColumnStretch(i, 1);
for(int i = 0; i < mainWindowLayout->rowCount(); i++)
mainWindowLayout->setRowStretch(i, 1);
mainWindowLayout->setVerticalSpacing(0);
mainWindowLayout->setHorizontalSpacing(0);
mainWindowLayout->setContentsMargins(0, 0, 0, 0);
setLayout(mainWindowLayout);
#ifdef QT_DEBUG
qDebug() << "shit";
#endif
QObject::connect(statusAnalysis, &StatusAnalysis::callHistoricalData, [=](){historyData->show();});
//QObject::connect(systemOption, &SystemOption::socketChanged, dataDisplay, &DataDisplay::currentSocketChanged);
QObject::connect(systemOption->getServer(), &TcpServer::sendData, dataWave, &DataWave::getData);
QObject::connect(systemOption->getServer(), &TcpServer::sendData, dataDisplay, &DataDisplay::getData);
QObject::connect(dataDisplay, &DataDisplay::nodeChanged, dataWave, &DataWave::nodeChanged);
databaseInit();
}
void MainWindow::databaseInit()
{
if(QSqlDatabase::contains(databaseConnection))
{
database = QSqlDatabase::database(databaseConnection);
}
else
{
database = QSqlDatabase::addDatabase("QSQLITE", databaseConnection);
database.setDatabaseName("detector.db");
}
if(!database.open())
{
QMessageBox openErrorMessageBox(QMessageBox::Warning,
QString::fromUtf8("Error"),
QString::fromUtf8("Open Database Error"));
openErrorMessageBox.exec();
QApplication::quit();
}
else
{
QSqlQuery tableExistQuery(database);
if(tableExistQuery.exec("select count(*) from sqlite_master where type = 'table' and name = 'log'"))
//if false, it means the query is failed.
{
while(tableExistQuery.next())
{
if(tableExistQuery.value(0).toInt() == 0)//value == 0 means there is no table called log.
{
QSqlQuery createQuery(database);
if(!createQuery.exec("create table log(id integer primary key autoincrement, time datatime not null, node smallint not null, accelerationX smallint not null, accelerationY smallint not null, accelerationZ smallint not null, temperature float not null, gyroscopeX smallint not null, gyroscopeY smallint not null, gyroscopeZ smallint not null, rollAngel float not null, pitchAngel float not null, driftAngel float not null)"))
{
QMessageBox createErrorMessageBox(QMessageBox::Warning,
QString::fromUtf8("Error"),
createQuery.lastError().text());
createErrorMessageBox.exec();
QApplication::quit();
}
#ifdef QT_DEBUG
else
qDebug() << "Create success";
#endif
}
}
}
else
{
QMessageBox queryErrorMessageBox(QMessageBox::Warning,
QString::fromUtf8("Error"),
tableExistQuery.lastError().text());
queryErrorMessageBox.exec();
QApplication::quit();
}
}
}
QSqlDatabase& MainWindow::getDatabase()
{
databaseInit();
return database;
}
MainWindow::~MainWindow()
{
}