-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
187 lines (155 loc) · 6.77 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* OpenRaceLapTimer - Copyright 2015 by airbirds.de, a project of polyvision UG (haftungsbeschränkt)
*
* Author: Alexander B. Bierbrauer
*
* This file is part of OpenRaceLapTimer.
*
* OpenRaceLapTimer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* OpenRaceLapTimer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Foobar. If not, see http://www.gnu.org/licenses/.
**/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "currentrace.h"
#include "qextserialport/qextserialenumerator.h"
#include <QInputDialog>
#include "racepilot.h"
#include "currentpilotracelap.h"
#include "settings.h"
#include <QFileDialog>
#include "racetablewidget.h"
#include "currentracewidget.h"
#include "pilotswidget.h"
#include <QTabWidget>
#include <QVBoxLayout>
#include "configurationwidget.h"
#include "aboutdialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QCoreApplication::setOrganizationName("polyvision UG");
QCoreApplication::setOrganizationDomain("polyvision.org");
QCoreApplication::setApplicationName("RaceLapTimer");
ui->setupUi(this);
setWindowTitle(QString("OpenRaceLapTimer by airbirds.de - v%1").arg(RLT_VERSION));
setWindowIcon(QIcon("app_icon.png"));
QVBoxLayout *pLayout = new QVBoxLayout(ui->centralWidget);
m_pTabWidget = new QTabWidget();
pLayout->addWidget(m_pTabWidget);
this->m_pRLTDatabase = RLTDatabase::instance();
m_pConfigurationWidget = new ConfigurationWidget();
m_pConfigurationWidget->setMainWindow(this);
this->m_pTabWidget->addTab(m_pConfigurationWidget,"Configuration");
m_pPilotsWidget = new PilotsWidget();
this->m_pTabWidget->addTab(m_pPilotsWidget,"Pilots");
m_pRaceTableWidget = new RaceTableWidget();
this->m_pTabWidget->addTab(m_pRaceTableWidget,"Races");
m_pCurrentRaceWidget = new CurrentRaceWidget();
this->m_pTabWidget->addTab(m_pCurrentRaceWidget,"Current Race");
this->m_pLabelCOMPortStatus = new QLabel(this);
this->m_pLabelCOMPortStatus->setText("not connected to COM port");
this->ui->statusBar->addWidget(m_pLabelCOMPortStatus);
this->m_pLabelLastIncommingSignal = new QLabel(this);
this->m_pLabelLastIncommingSignal->setText("XXX-XXX-XXX");
this->ui->statusBar->addWidget(m_pLabelLastIncommingSignal);
connect(CurrentRace::instance(),SIGNAL(pilotDataChanged()),this,SLOT(onCurrentRacePilotDataChanged()));
connect(CurrentRace::instance(),SIGNAL(fastedLapChanged()),this,SLOT(onCurrentRaceFastestLapDataChanged()));
connect(CurrentRace::instance(),SIGNAL(raceFinished()),this,SLOT(onCurentRaceFinished()));
connect(m_pRaceTableWidget,SIGNAL(signalStartRace()),this,SLOT(onStartRaceClicked()));
connect(m_pCurrentRaceWidget,SIGNAL(signalStopRace()),this,SLOT(onStopRaceClicked()));
connect(m_pCurrentRaceWidget,SIGNAL(signalSimulate(QString)),this,SLOT(onSimulateClicked(QString)));
connect(this->ui->actionAbout, SIGNAL(triggered()), this, SLOT(openAboutDialog()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openAboutDialog(){
AboutDialog *dialog = new AboutDialog(this);
dialog->showNormal();
}
void MainWindow::onSimulateClicked(QString v)
{
CurrentRace::instance()->incommingPilotSignal(v);
}
void MainWindow::connectCOM(QString v){
this->m_pSerialPort = new QextSerialPort(v, QextSerialPort::EventDriven);
m_pSerialPort->setBaudRate(BAUD9600);
m_pSerialPort->setFlowControl(FLOW_OFF);
m_pSerialPort->setParity(PAR_NONE);
m_pSerialPort->setDataBits(DATA_8);
m_pSerialPort->setStopBits(STOP_2);
if (m_pSerialPort->open(QIODevice::ReadWrite) == true) {
connect(m_pSerialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(m_pSerialPort, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
if (!(m_pSerialPort->lineStatus() & LS_DSR))
qDebug() << "warning: device is not turned on";
qDebug() << "listening for data on" << m_pSerialPort->portName();
this->m_pLabelCOMPortStatus->setText(QString("connected to %1").arg(v));
}
else {
qDebug() << "device failed to open:" << m_pSerialPort->errorString();
}
}
void MainWindow::onReadyRead()
{
QByteArray bytes;
int a = this->m_pSerialPort->bytesAvailable();
bytes.resize(a);
this->m_pSerialPort->read(bytes.data(), bytes.size());
//qDebug() << "bytes read:" << bytes.size();
//qDebug() << "bytes:" << bytes;
m_strIncommingSerialData = QString(bytes);
int index = m_strIncommingSerialData.indexOf("#");
if(index >= 0){
//qDebug() << "before" << m_strIncommingSerialData;
m_strIncommingSerialData.chop(m_strIncommingSerialData.length() - index -1);
//qDebug() << "after" << m_strIncommingSerialData;
this->m_pLabelLastIncommingSignal->setText(m_strIncommingSerialData);
CurrentRace::instance()->incommingPilotSignal(m_strIncommingSerialData);
}
}
void MainWindow::onDsrChanged(bool status)
{
if (status)
qDebug() << "device was turned on";
else
qDebug() << "device was turned off";
}
void MainWindow::onStartRaceClicked()
{
bool ok;
QString text = QInputDialog::getText(this, tr("Start a new race"),
tr("race title:"), QLineEdit::Normal,
NULL, &ok);
if (ok && !text.isEmpty()){
int race_id = RLTDatabase::instance()->createNewRace(text);
CurrentRace::instance()->startRace(race_id,this);
this->m_pTabWidget->setCurrentIndex(3);
}
}
void MainWindow::setCurrentRaceTitle(QString v){
m_pCurrentRaceWidget->setRaceTitle(v);
}
void MainWindow::onCurrentRacePilotDataChanged(){
m_pCurrentRaceWidget->updateRaceData();
}
void MainWindow::onCurentRaceFinished(){
this->m_pCurrentRaceWidget->raceStopped();
}
void MainWindow::onCurrentRaceFastestLapDataChanged(){
// updating fastest pilot
RacePilot *fastestPilot = CurrentRace::instance()->getFastestPilot();
if(fastestPilot){
m_pCurrentRaceWidget->setFastestLapData(QString("Pilot: %1 Lap: %2 Time: %3").arg(fastestPilot->getPilotName()).arg(fastestPilot->getFastedLap()->getLap()).arg(fastestPilot->getFastedLap()->formatedLapTime()));
}
}
void MainWindow::onStopRaceClicked()
{
CurrentRace::instance()->stopRace();
m_pRaceTableWidget->updateTable();
this->m_pTabWidget->setCurrentIndex(2);
}