DIPLOM (1204343), страница 9
Текст из файла (страница 9)
private:
uint timeLimit = 0;
uint elapsedTime = 0;
QTimer *timer = new QTimer(this);
};
// отображение таймера
class UserElapsedTimeWgt : public QWidget
{
Q_OBJECT
public:
UserElapsedTimeWgt(QWidget *p)
: QWidget(p)
{
showNewTime(0);
grid->addWidget(msg,0,0);
grid->addWidget(timer,0,1);
}
void showNewTime(uint t)
{
auto strTime = string_time(t);
timer->setText(strTime);
}
private:
QLabel *msg = new QLabel(trUtf8("Осталось времени:"), this);
QLabel *timer = new QLabel(this);
QGridLayout *grid = new QGridLayout(this);
};
// содержатся все виджеты с вопросами и овтетами, происходят все вычеслеления после окончания тестирования, подсчет результатов
#include <time.h>
class UserQuestionWgtT : public QWidget
{
Q_OBJECT
QGridLayout *grid = new QGridLayout(0);
public:
UserQuestionWgtT(QWidget *parent, const QString& testName)
: QWidget(parent)
, questions(testName)
{
for (auto i=0; i<(int)questions.size(); i++) {
auto wgt = new UserQuestionT(questions[i], i+1, this);
stackedWidget->addWidget(wgt);
connect(wgt, UserQuestionT::complete, this, answerRecived);
}
grid->addWidget(elapsed_wgt, 0, 0);
grid->addWidget(stackedWidget, 1, 0);
QHBoxLayout *layout_h0(new QHBoxLayout(this));
{
QVBoxLayout *main_layout_v(new QVBoxLayout);
{
main_layout_v->addStretch(1);
main_layout_v->addLayout(grid);
main_layout_v->addStretch(1);
}
layout_h0->addStretch(1);
layout_h0->addLayout(main_layout_v);
layout_h0->addStretch(1);
}
timer = new TimeCounter(questions.getTimeLeft());
timer->start();
connect(timer, TimeCounter::timeout, [=]()
{
testingComplete();
});
connect(timer, TimeCounter::timeLeft, elapsed_wgt, UserElapsedTimeWgt::showNewTime);
}
~UserQuestionWgtT()
{
delete stackedWidget;
delete timer;
}
private:
int countComplete() const {
return correctAnswers + uncorrectAnswers + skipAnswers;
}
void next()
{
if (countComplete() >= (int)questions.size()) {
testingComplete();
timer->stop();
}
else {
stackedWidget->setCurrentIndex(countComplete());
}
}
void testingComplete()
{
TestResult res;
res.elapsed_test_time = timer->getElapsedTime();
res.false_answers = uncorrectAnswers;
res.true_answers = correctAnswers;
res.skip_question = skipAnswers;
res.total_question = questions.size();
res.testing_time = time(0);
emit complete(res);
}
private:
int skipAnswers = 0;
int correctAnswers = 0;
int uncorrectAnswers = 0;
QustionListT questions;
QStackedWidget* stackedWidget = new QStackedWidget(0);
UserElapsedTimeWgt *elapsed_wgt = new UserElapsedTimeWgt(this);
TimeCounter *timer = nullptr;
private slots:
void answerRecived(UserQuestionT::AnswerErrorT status)
{
if (status == UserQuestionT::AnswerErrorT::ok)
{
correctAnswers++;
}
else if (status == UserQuestionT::AnswerErrorT::error)
{
uncorrectAnswers++;
}
else if (status == UserQuestionT::AnswerErrorT::skip)
{
skipAnswers++;
}
next();
}
signals:
//void complete(int correctAnswers, int uncorrectAnswers, int skipAnswers, uint elapsedTime);
void complete(const TestResult &);
};
// виджет где пользователь указывает свое имя, кнопка начать тестирование
class UserLoginForm : public QWidget
{
Q_OBJECT
QLineEdit* yourNameEdit = new QLineEdit(this);
QPushButton* startTestButton = new QPushButton(trUtf8("Начать тестирование"), this);
public:
UserLoginForm(QWidget *parent)
: QWidget(parent)
{
// auto stretch = new QVBoxLayout;
// stretch->addStretch();
lay->addWidget(new QLabel(trUtf8("Ваше имя:")), 0, 0, 1, 1);
lay->addWidget(yourNameEdit, 0, 1, 1, 1);
lay->addWidget(startTestButton, 1, 0, 1, 2);
//lay->addLayout(stretch, 2, 0);
connect(yourNameEdit, QLineEdit::textChanged, this, textChanged);
connect(startTestButton, QPushButton::clicked, this, beginTesting);
startTestButton->setEnabled(false);
QHBoxLayout *layout_h0(new QHBoxLayout(this));
{
QVBoxLayout *main_layout_v(new QVBoxLayout);
{
main_layout_v->addStretch(1);
main_layout_v->addLayout(lay);
main_layout_v->addStretch(1);
}
layout_h0->addStretch(1);
layout_h0->addLayout(main_layout_v);
layout_h0->addStretch(1);
}
}
private:
QGridLayout *lay = new QGridLayout(0);
private slots:
void beginTesting()
{
emit signInComplete(yourNameEdit->text().simplified());
}
void textChanged()
{
startTestButton->setEnabled(yourNameEdit->text().simplified().size() ? true : false);
}
signals:
void signInComplete(const QString& userName);
};
// виджет, который управляет виджетами тестирования
class UserMainWgt : public QWidget
{
Q_OBJECT
QStackedWidget* stackedWidget = new QStackedWidget;
UserLoginForm* userLoginForm = new UserLoginForm(this);
UserQuestionWgtT* userQuestionWgt;
ShowResultT* showResult = 0;
QString userName;
QString testName;
public:
UserMainWgt(QWidget*p, const QString& testName)
: QWidget(p)
, userQuestionWgt(new UserQuestionWgtT(this, testName))
, testName(testName)
{
connect(userLoginForm, UserLoginForm::signInComplete, this, signInComplete);
connect(userQuestionWgt, UserQuestionWgtT::complete, this, interviewComplete);
stackedWidget->addWidget(userLoginForm);
stackedWidget->addWidget(userQuestionWgt);
QHBoxLayout *layout_h0(new QHBoxLayout(this));
{
QVBoxLayout *main_layout_v(new QVBoxLayout);
{
//main_layout_v->addStretch();
main_layout_v->addWidget(stackedWidget);
//main_layout_v->addStretch();
}
//layout_h0->addStretch();
layout_h0->addLayout(main_layout_v);
// layout_h0->addStretch();
}
}
private slots:
void signInComplete(const QString& userName)
{
this->userName = userName;
stackedWidget->setCurrentWidget(userQuestionWgt);
}
void interviewComplete(TestResult testResult)
{
testResult.test_name = testName;
testResult.user_name = userName;
showResult = new ShowResultT(this, testResult);
sqliQuery->addData(testResult);// база данных реализована с помощью SQLite, она встроенная
stackedWidget->addWidget(showResult);
stackedWidget->setCurrentWidget(showResult);
}
};
// /////////////////////// ADMIN //////////////////////
class AdminLoginFormT : public QWidget
{
Q_OBJECT
QLineEdit *user_name_form = new QLineEdit(ADMIN_NAME);
QLineEdit *user_pass_form = new QLineEdit(ADMIN_PASS);
QLabel *error_label = new QLabel(trUtf8(" "));
public:
AdminLoginFormT(QWidget* p)
: QWidget(p)
{
QPushButton *submit_button = new QPushButton(trUtf8("Войти"));
QLabel *pass_label = new QLabel(trUtf8("Пароль:"));
QLabel *user_label = new QLabel(trUtf8("Имя пользователя:"));
// submit_button->setMaximumWidth(150);
user_name_form->setMaximumWidth(150);
user_pass_form->setMaximumWidth(150);
QGridLayout *lay = new QGridLayout(0);
user_pass_form->setEchoMode(QLineEdit::Password);
lay->addWidget(error_label, 0, 0, 1, 2);
lay->addWidget(user_label, 1, 0);
lay->addWidget(user_name_form, 1, 1);
lay->addWidget(pass_label, 2, 0);
lay->addWidget(user_pass_form, 2, 1);
lay->addWidget(submit_button, 3, 0, 1, 2);
connect(submit_button, QPushButton::clicked, this, clicked);
auto layout_h(new QHBoxLayout(this));
auto layout_v(new QVBoxLayout(0));
{
layout_v->addStretch(1);
layout_v->addLayout(lay);
layout_v->addStretch(1);
layout_h->addStretch(1);
layout_h->addLayout(layout_v);
layout_h->addStretch(1);
}
}
private slots:
void clicked()
{
if (user_name_form->text() == QString(ADMIN_NAME)
&& user_pass_form->text() == QString(ADMIN_PASS))
{
emit correctUserPass(user_name_form->text());
}
else
setMsg(trUtf8("Не верный пароль."));
}
private:
void setMsg(const QString& str) {
error_label->setText(str);
}
signals:
void correctUserPass(const QString& userName);
};
// this class view answer action and signailing after any changes
class AdminQuestionWgt : public QGroupBox
{
Q_OBJECT
QGridLayout *lay = new QGridLayout(this);
void init()
{
int start_lay_index = 1;
tEdit->setText(questFile.getQuestion());
lay->addWidget(tEdit, 0, 0, 1, 2);
for (const auto& it : questFile.getAnswerVariants())
{
auto check = new QRadioButton(it, this);
radioVec.push_back(check);
lay->addWidget(check, start_lay_index++, 0, 1, 2);
if (it == questFile.getAnswer())
check->setChecked(true);
connect(check, QRadioButton::clicked, this, radioChanged);
}
lay->addWidget(saveButton, start_lay_index, 0);
lay->addWidget(deleteButton, start_lay_index++, 1);
}
public:
AdminQuestionWgt(QWidget* wgt, const QuestionFileT& questFile, int showId)
: QGroupBox(wgt)
, questFile(questFile)
{
this->setTitle(QString(trUtf8("Вопрос №%1")).arg(showId));
init();
saveButton->setEnabled(false);
connect(saveButton, QPushButton::clicked, this, saveAction);
connect(deleteButton, QPushButton::clicked, this, deleteAction);
connect(tEdit, QTextEdit::textChanged, this, radioChanged);
saveButton->setMaximumWidth(150);
deleteButton->setMaximumWidth(150);
}
private:
QuestionFileT questFile;
std::vector<QRadioButton*> radioVec;
QTextEdit* tEdit = new QTextEdit(this);
QPushButton* saveButton = new QPushButton(trUtf8("Сохранить"), this);
QPushButton* deleteButton = new QPushButton(trUtf8("Удалить"), this);
private slots:
void saveAction()
{
QuestionFileT newQuest = questFile;
for (const QRadioButton* it : radioVec)
if (it->isChecked())
newQuest.setAnswer(it->text());
newQuest.setQuestion(tEdit->toPlainText().simplified());
emit changed(questFile, newQuest);
// saveButton->setEnabled(false);
}
void deleteAction()
{
std::cout << questFile.getQuestion().toStdString() << '\n';
int n = QMessageBox::warning(0, trUtf8("Предупреждение"), trUtf8("Вы точно хотите удалить вопрос?"),
trUtf8("Да"), trUtf8("Нет"),
QString(), 0, 1);
if (n == 0)
emit deleted(questFile);
}
void radioChanged()
{
saveButton->setEnabled(true);
}
signals:
void changed(const QuestionFileT& oldQ, const QuestionFileT& newQ);
void deleted(const QuestionFileT& questFile);
};
class AdminAddQuestionWgt : public QDialog
{
Q_OBJECT
bool parse(QuestionFileT& ret) const
{
QString question = tEdit->toPlainText().simplified();
if (!question.size()) {
return false;
}
std::vector<QString> variants;
for (QLineEdit* l : answersList)
{
QString variant = l->text().simplified();
if (variant.isEmpty())
break;
variants.push_back(std::move(variant));
}
if (variants.size() <= 1)














