Files
School/Anul 1/Semestrul 2/OOP/Labs/MockTest1/ParticipantModel.cpp
T
2024-08-31 12:07:21 +03:00

97 lines
2.4 KiB
C++

#include "ParticipantModel.h"
#include <QRadioButton>
#include <QMessageBox>
#include <QPushButton>
ParticipantModel::ParticipantModel(ParticipantRepo& repo, QuestionRepo& qrepo, QObject* parent) : repo{ repo }, qrepo{ qrepo }, QAbstractTableModel{ parent }
{
}
int ParticipantModel::rowCount(const QModelIndex& parent) const
{
return this->qrepo.size();
}
int ParticipantModel::columnCount(const QModelIndex& parent) const
{
return 4;
}
QVariant ParticipantModel::data(const QModelIndex& index, int role) const
{
int row = index.row();
int column = index.column();
Question q = this->qrepo.get_all()[row];
if(role == Qt::DisplayRole)
{
switch(column)
{
case 0:
return QString::number(q.id());
case 1:
return QString::fromStdString(q.text());
case 2:
return QString::number(q.score());
case 3:
return QString("Select");
default:
break;
}
}
if(role == Qt::BackgroundRole)
{
if(this->answered.find(row) != this->answered.end())
{
return QColor{ Qt::green };
}
}
return QVariant{};
}
QVariant ParticipantModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
switch(section)
{
case 0:
return QString{ "ID" };
case 1:
return QString{ "Text" };
case 2:
return QString{ "Score" };
case 3:
return QString{ "Selected" };
default:
break;
}
}
else {
return QString::number(section+1);
}
}
return QVariant{};
}
void ParticipantModel::answerQuestion(const int index, const std::string& answer)
{
Question q = this->qrepo.get_all()[index];
if(this->answered.find(index) != this->answered.end())
{
QMessageBox::critical(nullptr, "Error", "Question already answered!");
return;
}
if(q.correct_answer() == answer)
{
QMessageBox::information(nullptr, "Correct", "Correct answer!");
}
else
{
QMessageBox::information(nullptr, "Incorrect", "Incorrect answer!");
}
this->answered.insert(index);
}