#include "timemodel.h" #include #include #include TimeModel::TimeModel(QVector> timeData, QVector minimumTimes, QObject *parent) : QAbstractTableModel(parent) { this->timeData = timeData; this->minimumTimes = minimumTimes; } TimeModel::TimeModel(QVector> timeData, QVector minimumTimes, QVector 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("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> 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)){ return QColor(Qt::red); } } QVector 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 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(); }