174 lines
5.2 KiB
C++
174 lines
5.2 KiB
C++
#include "timemodel.h"
|
|
#include <iostream>
|
|
#include <qcolor.h>
|
|
#include <qvector.h>
|
|
|
|
TimeModel::TimeModel(QVector<QVector<int>> timeData, QVector<int> minimumTimes,
|
|
QObject * parent)
|
|
: QAbstractTableModel(parent) {
|
|
this->timeData = timeData;
|
|
this->minimumTimes = minimumTimes;
|
|
}
|
|
TimeModel::TimeModel(QVector<QVector<int>> timeData, QVector<int> minimumTimes,
|
|
QVector<int> topTime, QObject * parent)
|
|
: QAbstractTableModel(parent) {
|
|
this->topTime = topTime;
|
|
this->timeData = timeData;
|
|
this->minimumTimes = minimumTimes;
|
|
}
|
|
|
|
QVariant TimeModel::headerData(int section, Qt::Orientation orientation,
|
|
int role) const {
|
|
if (role != Qt::DisplayRole)
|
|
return QVariant();
|
|
|
|
if (orientation == Qt::Horizontal) {
|
|
switch (section) {
|
|
case 0:
|
|
return QString("Brücke");
|
|
|
|
case 1:
|
|
return QString("Gerade");
|
|
|
|
case 2:
|
|
return QString("Kurven");
|
|
|
|
case 3:
|
|
return QString("Runde");
|
|
default:
|
|
return QVariant();
|
|
}
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
int TimeModel::rowCount(const QModelIndex & parent) const {
|
|
if (parent.isValid())
|
|
return 0;
|
|
|
|
return this->timeData.size();
|
|
}
|
|
|
|
int TimeModel::columnCount(const QModelIndex & parent) const {
|
|
if (parent.isValid())
|
|
return 0;
|
|
return 4;
|
|
}
|
|
|
|
QColor TimeModel::getColor(const QVector<QVector<int>> data, const int col,
|
|
const int row) const {
|
|
if (row == 0) {
|
|
return QColor(Qt::gray);
|
|
}
|
|
else if (data.size() > row) { // check for enough rows
|
|
if (data.at(row).size() > col) { // check for enough cols
|
|
if (col < 3) {
|
|
if (data.at(row).at(col) < this->minimumTimes.at(col)) {
|
|
return QColor(Qt::red);
|
|
}
|
|
}
|
|
else {
|
|
// lap time
|
|
if (data.at(row).at(0) < this->minimumTimes.at(0) ||
|
|
data.at(row).at(1) < this->minimumTimes.at(1) ||
|
|
data.at(row).at(2) < this->minimumTimes.at(2) ||
|
|
data.at(row).at(0) + data.at(row).at(1) +
|
|
data.at(row).at(2) <
|
|
this->minimumTimes.at(3)) {
|
|
return QColor(Qt::red);
|
|
}
|
|
}
|
|
|
|
QVector<int> compare;
|
|
for (int i = 0; i <= row; i++) {
|
|
if (data.at(i).size() > col) {
|
|
compare.append(data.at(i).at(col));
|
|
}
|
|
}
|
|
if (data.at(row).at(col) ==
|
|
getMin(compare)) { // at least personal best time
|
|
if (col < 3) { // sector time
|
|
// std::cout << this->topTime.size() <<
|
|
// std::endl;
|
|
if (this->topTime.size() >
|
|
col) { // absolute top time exists
|
|
// std::cout << "local besttime"
|
|
// << std::endl;
|
|
if (this->topTime.at(col) >= data.at(row).at(col)) {
|
|
return QColor(Qt::magenta);
|
|
}
|
|
}
|
|
}
|
|
else { // lap time
|
|
if (this->topTime.size() == 3) {
|
|
if (this->topTime.at(0) + this->topTime.at(1) +
|
|
this->topTime.at(2) >=
|
|
data.at(row).at(col)) {
|
|
return QColor(Qt::magenta);
|
|
}
|
|
}
|
|
}
|
|
return QColor(Qt::green);
|
|
}
|
|
else {
|
|
return QColor(Qt::gray);
|
|
}
|
|
}
|
|
else {
|
|
return QColor(Qt::white);
|
|
}
|
|
}
|
|
else {
|
|
return QColor(Qt::white);
|
|
}
|
|
}
|
|
|
|
int TimeModel::getMin(const QVector<int> x) const {
|
|
int min;
|
|
if (x.size() > 0) {
|
|
min = x.at(0);
|
|
}
|
|
for (int i = 1; i < x.size(); i++) {
|
|
if (x.at(i) < min) {
|
|
min = x.at(i);
|
|
}
|
|
}
|
|
return min;
|
|
}
|
|
|
|
QVariant TimeModel::data(const QModelIndex & index, int role) const {
|
|
// std::cout << "data called" << std::endl;
|
|
if (!index.isValid())
|
|
return QVariant();
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
if (timeData.at(index.row()).size() > index.column()) {
|
|
return QString::number(
|
|
(double)timeData.at(index.row()).at(index.column()) / 1000);
|
|
}
|
|
else {
|
|
return QString("∞");
|
|
}
|
|
}
|
|
else if (role == Qt::BackgroundColorRole) {
|
|
return getColor(timeData, index.column(), index.row());
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
bool TimeModel::insertRows(int row, int count, const QModelIndex & parent) {
|
|
// std::cout << "insert triggert" << std::endl;
|
|
beginInsertRows(parent, row, row + count - 1);
|
|
// FIXME: Implement me!
|
|
endInsertRows();
|
|
return true;
|
|
}
|
|
|
|
bool TimeModel::insertColumns(int column, int count,
|
|
const QModelIndex & parent) {
|
|
beginInsertColumns(parent, column, column + count - 1);
|
|
// FIXME: Implement me!
|
|
endInsertColumns();
|
|
return true;
|
|
}
|