DIPLOM (1204343), страница 10
Текст из файла (страница 10)
return false;
int currentId = correctAnswer->currentIndex();
if (currentId < 0 || currentId >= (int)variants.size())
return false;
const QString& correctAnswer = variants[currentId];
ret = {question, correctAnswer, variants};
return true;
}
public:
AdminAddQuestionWgt(QWidget *p)
: QDialog(p)
{
lay->addWidget(tEdit, 0, 0, 1, 2);
lay->addWidget(new QLabel(trUtf8("Правильный ответ: "), this), 1, 0, 1, 1);
lay->addWidget(correctAnswer, 1, 1, 1, 1);
int wgtOffset = 2;
int i = 0;
for (; i<MAX_ANSWER_VARIANTS; ++i)
{
auto showStr = QString(trUtf8("№%1 Вариант ответа")).arg(i+1);
correctAnswer->addItem(showStr);
auto l = new QLineEdit(this);
answersList.push_back(l);
lay->addWidget(new QLabel(showStr + ":"), i + wgtOffset, 0, 1, 1);
lay->addWidget(l, i + wgtOffset, 1, 1, 1);
}
lay->addWidget(addButton, i++ + wgtOffset, 0, 1, 2);
// connect(this, QDialog::accepted, this, save);
connect(addButton, QPushButton::clicked, this, save);
}
private:
std::vector<QLineEdit*> answersList;
QComboBox* correctAnswer = new QComboBox(this);
QTextEdit* tEdit = new QTextEdit(this);
QGridLayout* lay = new QGridLayout(this);
QPushButton *addButton = new QPushButton(trUtf8("Добавить/сохранить"), this);
private slots:
void save()
{
QuestionFileT s;
if (!parse(s)) {
QMessageBox::critical(0, trUtf8(APP_TITLE), trUtf8("Неверно заполнены поля."));
return;
}
emit complete(s);
accept();
}
signals:
void complete(const QuestionFileT&);
};
class AdminNewtestWgt : public QDialog
{
Q_OBJECT
public://TestSettings
AdminNewtestWgt(QWidget *p)
: QDialog(p)
{
lay->addWidget(new QLabel(trUtf8("Имя: "),this), 0, 0, 1, 1);
lay->addWidget(lineEdit, 0, 1, 1, 1);
lay->addWidget(new QLabel(trUtf8("Время: "),this), 1, 0, 1, 1);
lay->addWidget(timeChooseWgt, 1, 1, 1, 1);
lay->addWidget(addButton, 2, 0, 1, 2);
connect(addButton, QPushButton::clicked, this, save);
QStringList t;
for (int i=1; i<=maxTestTime/timeSteep; i++)
{
t << QString::number(i*timeSteep);
}
timeChooseWgt->addItems(t);
timeChooseWgt->setMaximumWidth(50);
}
private slots:
void save()
{
QString newName = lineEdit->text().simplified();
if (newName.isEmpty())
return;
emit complete(newName, timeChooseWgt->currentText().toInt()*60);
accept();
}
signals:
void complete(const QString&, uint);
private:
int maxTestTime = 60;//min
int timeSteep = 5;//min
QComboBox *timeChooseWgt = new QComboBox(this);
QGridLayout* lay = new QGridLayout(this);
QLineEdit *lineEdit = new QLineEdit(this);
QPushButton *addButton = new QPushButton(trUtf8("Сохранить"), this);
};
class AdminQuestionEditWgt : public QGroupBox
{
Q_OBJECT
QGridLayout *lay = new QGridLayout(0);
QPushButton *addButton = new QPushButton(trUtf8("Добавить вопрос"), this);
QPushButton *addTestButton = new QPushButton(trUtf8("Добавить тест"), this);
QPushButton *delTestButton = new QPushButton(trUtf8("Удалить тест"), this);
QPushButton *startTestButton = new QPushButton(trUtf8("Начать тестирование"), this);
QComboBox* currentTestListWgt = new QComboBox(this);
std::vector<AdminQuestionWgt*> wgtVec;
QLabel* statsWgt = new QLabel(this);
void repaint()
{
int insertWgtIndex = 3;
for (auto it : wgtVec)
delete it;
wgtVec.clear();
statsWgt->setText(trUtf8("Всего вопросов: %1").arg(qList.size()));
for (int i=0; i<(int)qList.size(); i++) {
auto* p = new AdminQuestionWgt(this, qList[i], i+1);
wgtVec.push_back(p);
lay->addWidget(p, insertWgtIndex++, 0, 1, 3);
connect(p, AdminQuestionWgt::deleted, this, deleteQuestion);
connect(p, AdminQuestionWgt::changed, this, editQuestion);
}
}
bool reloadTestList()
{
auto list = getTestsList();
// if (!list.size())
// return false;
clearTestsNameInWgt();
for (const auto& name : list) {
addNewTestNameToWgt(name);
}
return true;
}
public:
AdminQuestionEditWgt(QWidget* wgt)
: QGroupBox(wgt)
{
addButton->setMaximumWidth(150);
reloadTestList();
repaint();
lay->addWidget(new QLabel(trUtf8("Текущий тест:")), 0, 0);
lay->addWidget(currentTestListWgt, 0, 1);
lay->addWidget(delTestButton, 0, 2);
lay->addWidget(addButton, 1, 0);
lay->addWidget(startTestButton, 1, 1);
lay->addWidget(addTestButton, 1, 2);
lay->addWidget(statsWgt, 2, 0, 1, 3, Qt::AlignTop);
reloadTest(getCurTestName());
auto scroll_area = new QScrollArea(this);
QWidget* scroll_wgt = new QWidget(this);
scroll_area->setWidgetResizable(true);
{
QVBoxLayout *main_layout_v(new QVBoxLayout(this));
QHBoxLayout *layout_h0(new QHBoxLayout);
layout_h0->addLayout(lay);
main_layout_v->addLayout(layout_h0);
scroll_wgt->setLayout(main_layout_v);
scroll_area->setWidget(scroll_wgt);
}
QVBoxLayout* bl = new QVBoxLayout(this); // управляет положение в самом QGroupBox
bl->addWidget(scroll_area);
connect(addButton, QPushButton::clicked, this, addNewQuestion);
connect(currentTestListWgt, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(changeCurTest(const QString&)));
connect(delTestButton, QPushButton::clicked, this, deleteTest);
connect(addTestButton, QPushButton::clicked, this, addNewTestCtrl);
connect(startTestButton, QPushButton::clicked, this, startTestingSlot);
}
private:
void addNewTestNameToWgt(const QString& testName) {
currentTestListWgt->addItem(testName);
}
void clearTestsNameInWgt() {
currentTestListWgt->clear();
}
void changeCurTestNameInWgt(const QString& testName) {
currentTestListWgt->setCurrentText(testName);
}
QString getCurTestName() const {
return currentTestListWgt->currentText();
}
void reloadTest(const QString& testName) {
changeCurTest(testName);
}
private slots:
void deleteTest()
{
const QString& testName = getCurTestName();
std::cout << removeTest(testName) << "\n";
reloadTestList();
reloadTest(getCurTestName());
repaint();
}
void addNewTestCtrl()
{
AdminNewtestWgt dialog(this);
connect(&dialog, AdminNewtestWgt::complete, this, addNewTest);
dialog.exec();
}
void deleteQuestion(const QuestionFileT& oldQ)
{
qList.del(oldQ);
repaint();
}
void editQuestion(const QuestionFileT& oldQ, const QuestionFileT& newQ)
{
qList.edit(oldQ, newQ);
repaint();
}
void addnewQuestion(const QuestionFileT& qst)
{
qList.addNew(qst);
repaint();
}
void changeCurTest(const QString& testName)
{
qList = makeQuestionList(testName);
repaint();
}
void addNewTest(const QString& testName, uint t)
{
if (testName.isEmpty())
return;
TestSettings sets;
sets.setTestTime(testName, t);
if (QDir().mkpath(make_dir_name(testName))) {
changeCurTest(testName);
reloadTestList();
// reloadTest(getCurTestName());
changeCurTestNameInWgt(testName);
}
}
void addNewQuestion()
{
if (getCurTestName().isEmpty()) return;
AdminAddQuestionWgt dialog(this);
connect(&dialog, AdminAddQuestionWgt::complete, this, addnewQuestion);
dialog.exec();
}
void startTestingSlot()
{
// QMainWindow *widget = qobject_cast< *>();
((QMainWindow*)nativeParentWidget())->setCentralWidget(new UserMainWgt(this, getCurTestName()));
std::cout << "MainWind2: " << (int) nativeParentWidget() << '\n';
// effectiveWinId();
// emit startTesting(getCurTestName());
}
private:
QustionListT qList;
signals:
void startTesting(const QString& );
};
class AdminMainWgtT : public QWidget
{
Q_OBJECT
void changeWgt(QWidget* wgt)
{
main_layout_v->removeWidget(mainWgt);
main_layout_v->insertWidget(1, wgt);
mainWgt->deleteLater();
mainWgt = wgt;
}
void showLoginForm()
{
auto* wgt = new AdminLoginFormT(this);
changeWgt(wgt);
connect(wgt, AdminLoginFormT::correctUserPass,this, correctPass);
}
void showEditForm()
{
auto wgt = new AdminQuestionEditWgt(this);
connect(wgt, AdminQuestionEditWgt::startTesting, this, startTesting);
changeWgt(wgt);
}
public:
AdminMainWgtT(QWidget* p)
: QWidget(p)
{
// QHBoxLayout *layout_h0(new QHBoxLayout(this));
{
//main_layout_v->addStretch();
main_layout_v->addWidget(mainWgt);
//main_layout_v->addStretch();
//layout_h0->addStretch();
//layout_h0->addLayout(main_layout_v);
//layout_h0->addStretch();
}
showLoginForm();
}
private slots:
void correctPass(const QString& userNmae)
{
showEditForm();
}
private:
QVBoxLayout *main_layout_v = new QVBoxLayout(this);
QWidget* mainWgt = new QWidget(this);
signals:
void startTesting(const QString&);
};
class SqlModel : public QSqlTableModel
{
Q_OBJECT
public:
explicit SqlModel(QObject *parent = Q_NULLPTR, QSqlDatabase db = QSqlDatabase())
: QSqlTableModel(parent, db)
{
}
QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const
{
QVariant d = QSqlTableModel::data(idx, role);
if (role == Qt::DisplayRole)
{
if (idx.column() == 6)
return QDateTime::fromTime_t(d.toInt()).toString("yyyy-MM-dd hh:mm:ss");
else if (idx.column() == 7)
return string_time(d.toInt());
else if (idx.column() == 8) {
QSqlRecord sqlRecord = record(idx.row());
auto totalQuestion = sqlRecord.value("total_question").toInt();
auto trueAnswers = sqlRecord.value("true_answers").toInt();
return QString::number(userResult(trueAnswers,totalQuestion)) + "%";
}
}
return d;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const
{
return QSqlTableModel::columnCount(parent) + 1;
}
};
class StatsView : public QTableView
{
public:
StatsView(QWidget *p)
: QTableView(p)
{
SqlModel *model = new SqlModel(this, sqliQuery->getDBHandle());
model->setTable("test_results");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, trUtf8("Тест"));
model->setHeaderData(1, Qt::Horizontal, trUtf8("Тестируемый"));
model->setHeaderData(2, Qt::Horizontal, trUtf8("Всего вопросов"));
model->setHeaderData(3, Qt::Horizontal, trUtf8("Вопросов пропущено"));
model->setHeaderData(4, Qt::Horizontal, trUtf8("Правильных ответов"));
model->setHeaderData(5, Qt::Horizontal, trUtf8("Неправильных ответов"));
model->setHeaderData(6, Qt::Horizontal, trUtf8("Дата теста"));
model->setHeaderData(7, Qt::Horizontal, trUtf8("Время теста"));
model->setHeaderData(8, Qt::Horizontal, trUtf8("Результат"));
this->setModel(model);
}
};
class Stats : public QWidget
{















