150 lines
4.1 KiB
C++
150 lines
4.1 KiB
C++
#include "timemodel.h"
|
|
#include<qvector.h>
|
|
#include <iostream>
|
|
#include <qcolor.h>
|
|
|
|
|
|
TimeModel::TimeModel(QVector<QVector<int>> timeData, QObject *parent)
|
|
: QAbstractTableModel(parent)
|
|
{
|
|
this->timeData = timeData;
|
|
}
|
|
TimeModel::TimeModel(QVector<QVector<int>> timeData, QVector<int> topTime, QObject *parent)
|
|
: QAbstractTableModel(parent)
|
|
{
|
|
this->topTime = topTime;
|
|
this->timeData = timeData;
|
|
}
|
|
|
|
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("Sektor 1");
|
|
|
|
case 1:
|
|
return QString("Sektor 2");
|
|
|
|
case 2:
|
|
return QString("Sektor 3");
|
|
|
|
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
|
|
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();
|
|
}
|
|
|
|
bool TimeModel::insertColumns(int column, int count, const QModelIndex &parent)
|
|
{
|
|
beginInsertColumns(parent, column, column + count - 1);
|
|
// FIXME: Implement me!
|
|
endInsertColumns();
|
|
}
|