From c1f110234078809a3de0d37f9e62619823d0763c Mon Sep 17 00:00:00 2001 From: Stepan Beskrovnyy Date: Mon, 28 Dec 2020 16:35:52 +0300 Subject: [PATCH] superPatch beta 1.5 --- addnewtestwidget.cpp | 6 +- adminwindow.cpp | 10 +++ adminwindow.h | 2 + adminwindow.ui | 32 ++++++++- android_app.pro | 4 ++ changewidget.cpp | 113 ++++++++++++++++++++++++++++++++ changewidget.h | 51 +++++++++++++++ deletetestwidget.cpp | 56 +++++++++++++--- deletetestwidget.h | 6 ++ deletetestwidget.ui | 43 ++++++++----- question.cpp | 15 +++++ question.h | 3 + redactionwidget.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++ redactionwidget.h | 60 +++++++++++++++++ tester.cpp | 2 +- 15 files changed, 523 insertions(+), 30 deletions(-) create mode 100644 changewidget.cpp create mode 100644 changewidget.h create mode 100644 redactionwidget.cpp create mode 100644 redactionwidget.h diff --git a/addnewtestwidget.cpp b/addnewtestwidget.cpp index 086514a..3dc11fd 100644 --- a/addnewtestwidget.cpp +++ b/addnewtestwidget.cpp @@ -122,10 +122,10 @@ void addNewTestWidget::showTest() QString testString = testEntity->getName() + "\n"; for(QuestionEntity& qe: testEntity->getQuestions()){ testString+= "\n" + qe.getQuestion() + "\n"; - int varCounter = 1; + for(auto& el:qe.getVariants()){ - testString+=QString::number(varCounter) + " " + el +"\n"; - varCounter++; + testString+= " " + el +"\n"; + } testString += "Правильный ответ: "; for(auto& el: qe.getAnswers()){ diff --git a/adminwindow.cpp b/adminwindow.cpp index e321e69..d6cabd3 100644 --- a/adminwindow.cpp +++ b/adminwindow.cpp @@ -36,6 +36,7 @@ adminwindow::adminwindow(QWidget *parent) : results = new QTableView(this); results->hide(); + redactWidget = new RedactionWidget(this); connect(ui->addNewTestBtn, SIGNAL(clicked()), this, SLOT(addNewTest())); connect(ui->addNewTestBtn, SIGNAL(clicked()), this, SLOT(hideStartWindow())); @@ -58,6 +59,13 @@ adminwindow::adminwindow(QWidget *parent) : connect(ui->deleteTestBtn, SIGNAL(clicked()), this, SLOT(sendDataBase())); connect(this, SIGNAL(sDataBase(const QSqlDatabase&)), deleteWidget, SLOT(receiveDataBase(const QSqlDatabase &))); + connect(ui->redactTestBtn, SIGNAL(clicked()), this, SLOT(sendDataBase)); + connect(this, SIGNAL(sDataBase(const QSqlDataBase&)),redactWidget, SLOT(receiveDataBase(const QSqlDatabase&))); + connect(ui->redactTestBtn, SIGNAL(clicked()), this, SLOT(hideStartWindow())); + connect(ui->redactTestBtn, SIGNAL(clicked()), redactWidget, SLOT(show())); + + connect(redactWidget->exit, SIGNAL(clicked()), this, SLOT(showStartWindow())); + connect(redactWidget->exit, SIGNAL(clicked()), redactWidget, SLOT(hide())); } adminwindow::~adminwindow() @@ -84,6 +92,7 @@ void adminwindow::hideStartWindow() ui->label->hide(); ui->lblMenu->hide(); ui->logout_btn->hide(); + ui->redactTestBtn->hide(); } void adminwindow::showStartWindow() @@ -96,6 +105,7 @@ void adminwindow::showStartWindow() ui->logout_btn->show(); results->hide(); ui->pushButton->hide(); + ui->redactTestBtn->show(); } void adminwindow::checkStats() diff --git a/adminwindow.h b/adminwindow.h index bd771d6..d749681 100644 --- a/adminwindow.h +++ b/adminwindow.h @@ -4,6 +4,7 @@ #include "testentity.h" #include "addnewtestwidget.h" #include "deletetestwidget.h" +#include "redactionwidget.h" #include #include @@ -45,6 +46,7 @@ private: QTableView *results; addNewTestWidget *newTestWidget; deletetestwidget *deleteWidget; + RedactionWidget *redactWidget; }; diff --git a/adminwindow.ui b/adminwindow.ui index 358c5f9..9be6a9a 100644 --- a/adminwindow.ui +++ b/adminwindow.ui @@ -146,6 +146,34 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + Times New Roman + 22 + 75 + true + + + + РЕДАКТИРОВАТЬ ТЕСТ + + + @@ -183,8 +211,8 @@ - 260 - 420 + 250 + 470 80 21 diff --git a/android_app.pro b/android_app.pro index 046e57d..743985a 100644 --- a/android_app.pro +++ b/android_app.pro @@ -11,10 +11,12 @@ CONFIG += c++11 SOURCES += \ addnewtestwidget.cpp \ adminwindow.cpp \ + changewidget.cpp \ deletetestwidget.cpp \ loginwidget.cpp \ main.cpp \ question.cpp \ + redactionwidget.cpp \ testentity.cpp \ tester.cpp \ testselection.cpp @@ -22,9 +24,11 @@ SOURCES += \ HEADERS += \ addnewtestwidget.h \ adminwindow.h \ + changewidget.h \ deletetestwidget.h \ loginwidget.h \ question.h \ + redactionwidget.h \ testentity.h \ tester.h \ testselection.h diff --git a/changewidget.cpp b/changewidget.cpp new file mode 100644 index 0000000..36bd9b9 --- /dev/null +++ b/changewidget.cpp @@ -0,0 +1,113 @@ +#include "changewidget.h" +#include + +ChangeWidget::ChangeWidget(QWidget *parent) : QWidget(parent) +{ + + + mainLayout = new QGridLayout(this); + questionName = new QLineEdit(); + variants = new QLineEdit(); + answers = new QLineEdit(); + lb1 = new QLabel("Вопрос:"); + lb2 = new QLabel("Варианты ответа:"); + lb3 = new QLabel("Правильные ответы:"); + mainLayout->addWidget(lb1,0,0); + mainLayout->addWidget(questionName,1,0); + mainLayout->addWidget(lb2,2,0); + mainLayout->addWidget(variants,3,0); + mainLayout->addWidget(lb3,4,0); + mainLayout->addWidget(answers,5,0); + submitBtn = new QPushButton(); + mainLayout->addWidget(submitBtn,6,0); + + + + +} + +void ChangeWidget::setQuestion(QuestionEntity &p_qe,int p_test_id) +{ + question = p_qe; + test_id = p_test_id; + p_que_name = p_qe.getQuestion(); +} + +void ChangeWidget::paintWidget(bool bToChange) +{ + this->show(); + bType = bToChange; + if(bToChange) + { + submitBtn->setText("Изменить"); + questionName->setText(question.getQuestion()); + QString answersString; + QString variantsString; + for(auto & var: question.getAnswers()) + { + answersString += var + " "; + } + answersString.chop(1); + for(auto & var: question.getVariants()) + { + variantsString += var + "%"; + } + variantsString.chop(1); + variants->setText(variantsString); + answers->setText(answersString); + disconnect(submitBtn, SIGNAL(clicked()), this, SLOT(AddNewQuestion())); + connect(submitBtn, SIGNAL(clicked()), this, SLOT(UpdateCurrQuestion())); + connect(submitBtn, SIGNAL(clicked()), this, SLOT(hide())); + } + else + { + submitBtn->setText("Добавить"); + disconnect(submitBtn, SIGNAL(clicked()), this, SLOT(UpdateCurrQuestion())); + connect(submitBtn, SIGNAL(clicked()), this, SLOT(AddNewQuestion())); + connect(submitBtn, SIGNAL(clicked()), this, SLOT(hide())); + + } + +} + + +void ChangeWidget::recieveDataBase(const QSqlDatabase &db) +{ + f_db = db; +} + +void ChangeWidget::receiveQuestion(const QuestionEntity &qe) +{ + question = qe; +} + +void ChangeWidget::AddNewQuestion() +{ + QString question_Name = questionName->text(); + QString _var = variants->text(); + QString _ans = answers->text(); + QSqlQueryModel *model = new QSqlQueryModel(this); + model->setQuery(QSqlQuery("SELECT MAX(id) from test_questions;", f_db)); + int id = model->index(0,0).data().toInt() + 1; + model->setQuery(QSqlQuery("INSERT INTO test_questions VALUES ("+ QString::number(id) + ",'" + question_Name + "','" + + _var + "','" + _ans + "',"+QString::number(test_id) + ");" )); + qDebug() << "INSERT INTO test_questions VALUES ("+ QString::number(id) + ",'" + question_Name + "','" + + _var + "','" + _ans + "',"+QString::number(test_id) + ");" ; + \ +} + +void ChangeWidget::UpdateCurrQuestion() +{ + QString question_Name = questionName->text(); + QString _var = variants->text(); + QString _ans = answers->text(); + QSqlQueryModel *model = new QSqlQueryModel(this); + model->setQuery(QSqlQuery("SELECT id from test_questions WHERE question = '"+p_que_name+"';", f_db)); + int id = model->index(0,0).data().toInt(); + model->setQuery(QSqlQuery("UPDATE test_questions SET question = '"+question_Name+"',variants = '"+_var+ + "', answer = '" + _ans +"' WHERE id = " + QString::number(id) + ";" )); + qDebug() << "UPDATE test_questions SET question = '"+question_Name+"',variants = '"+_var+ + "', answer = '" + _ans +"' WHERE id = " + QString::number(id) + ";"; +} + + diff --git a/changewidget.h b/changewidget.h new file mode 100644 index 0000000..fac461e --- /dev/null +++ b/changewidget.h @@ -0,0 +1,51 @@ +#ifndef CHANGEWIDGET_H +#define CHANGEWIDGET_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class ChangeWidget : public QWidget +{ + Q_OBJECT +public: + explicit ChangeWidget(QWidget *parent = nullptr); + void setQuestion(QuestionEntity &p_qe, int p_test_id); + +signals: + void AddQuestion(); + void UpdateQuestion(); + +public slots: + void paintWidget(bool bToChange); + void recieveDataBase(const QSqlDatabase &db); + void receiveQuestion(const QuestionEntity &qe); + void AddNewQuestion(); + void UpdateCurrQuestion(); + + +private: + QSqlDatabase f_db; + QuestionEntity question; + QGridLayout *mainLayout; + QLineEdit *questionName; + QLineEdit *variants; + QLineEdit *answers; + QLabel *lb1; + QLabel *lb2; + QLabel *lb3; + QPushButton *submitBtn; + QString p_que_name; + bool bType; + int test_id; + +}; + +#endif // CHANGEWIDGET_H diff --git a/deletetestwidget.cpp b/deletetestwidget.cpp index 755aefd..d87e051 100644 --- a/deletetestwidget.cpp +++ b/deletetestwidget.cpp @@ -11,10 +11,25 @@ deletetestwidget::deletetestwidget(QWidget *parent) : ui->setupUi(this); returnButton = new QPushButton("Назад", this); returnButton->show(); - submit = new QPushButton("Удалить", this); + /*submit = new QPushButton("Удалить", this); submit->show(); - ui->verticalLayout->addWidget(submit,0,Qt::AlignmentFlag::AlignBottom); - connect(submit, SIGNAL(clicked()), this, SLOT(deleteTest())); + ui->verticalLayout->addWidget(submit,0,Qt::AlignmentFlag::AlignBottom);*/ + connect(ui->deleteTestBtn, SIGNAL(clicked()), this, SLOT(deleteTest())); + connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(Refresh())); + connect(ui->testView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(onLVdoubleClicked(const QModelIndex &))); + + ui->testView->setEditTriggers(QListView::NoEditTriggers); + + QStringListModel* strListModel = new QStringListModel(ui->testView); + QStringList *strList = new QStringList(); + QSqlQueryModel *queryModel = new QSqlQueryModel(this); + queryModel->setQuery("SELECT name FROM tests",db); + for(int i = 0; i < queryModel->rowCount(); i++ ) + { + strList->push_back(queryModel->index(i,0).data().toString()); + } + strListModel->setStringList(*strList); + ui->testView->setModel(strListModel); } deletetestwidget::~deletetestwidget() @@ -29,14 +44,15 @@ void deletetestwidget::receiveDataBase(const QSqlDatabase &p_db) void deletetestwidget::deleteTest() { - /* QString testName = ui->lineEdit->text(); + + QSqlQueryModel *model = new QSqlQueryModel(); - model->setQuery(QSqlQuery("DELETE FROM tests WHERE name = '" + testName + "';",db)); - if(model->query().lastError().type() == QSqlError::NoError ) + model->setQuery(QSqlQuery("DELETE FROM tests WHERE name = '" + test_name + "';",db)); + if(model->query().size() != 0 ) { qDebug() << model->query().lastError().text(); message = new QMessageBox(); - message->setText("Тест " + testName + " успешно удален"); + message->setText("Тест " + test_name + " успешно удален"); message->show(); } @@ -47,7 +63,7 @@ void deletetestwidget::deleteTest() } //query->exec("DELETE FROM tests_questions WHERE name = '" + testName + "';")*/ - QString testName = ui->lineEdit->text(); + /* QString testName = ui->lineEdit->text(); QSqlQueryModel *model = new QSqlQueryModel(); model->setQuery(QSqlQuery("SELECT FROM tests WHERE name = '" + testName + "';",db)); if(model->query().size() == 0 ) @@ -62,5 +78,29 @@ void deletetestwidget::deleteTest() message = new QMessageBox(); message->setText("Тест " + testName + " успешно удален"); message->show(); + }*/ + +} + +void deletetestwidget::Refresh() +{ + QStringListModel* strListModel = new QStringListModel(ui->testView); + QStringList *strList = new QStringList(); + QSqlQueryModel *queryModel = new QSqlQueryModel(this); + queryModel->setQuery("SELECT name FROM tests",db); + for(int i = 0; i < queryModel->rowCount(); i++ ) + { + strList->push_back(queryModel->index(i,0).data().toString()); } + strListModel->setStringList(*strList); + ui->testView->setModel(strListModel); +} + +void deletetestwidget::onLVdoubleClicked(const QModelIndex &inx) +{ + test_name = inx.data().toString(); + qDebug() << test_name; + QSqlQueryModel *model = new QSqlQueryModel(); + model->setQuery(QSqlQuery("SELECT name FROM tests WHERE name = " + test_name, db)); + qDebug() << test_name; } diff --git a/deletetestwidget.h b/deletetestwidget.h index 4cf86a0..a4aa7bb 100644 --- a/deletetestwidget.h +++ b/deletetestwidget.h @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include namespace Ui { class deletetestwidget; } @@ -26,10 +29,13 @@ public: public slots: void receiveDataBase(const QSqlDatabase & p_db); void deleteTest(); + void Refresh(); + void onLVdoubleClicked(const QModelIndex &inx); private: QSqlDatabase db; QErrorMessage *err; QMessageBox *message; + QString test_name; // i fucked a gummy bear }; diff --git a/deletetestwidget.ui b/deletetestwidget.ui index f1e8ff3..55f7981 100644 --- a/deletetestwidget.ui +++ b/deletetestwidget.ui @@ -6,35 +6,46 @@ 0 0 - 425 - 232 + 558 + 601 Form - + - 50 - 80 - 331 - 71 + 10 + 30 + 531 + 451 - - - QLayout::SetMinimumSize - - - + + + - Введите название теста который хотите удалить + Удалить тест - - + + + + Обновить + + + + + + + + 0 + 0 + + + diff --git a/question.cpp b/question.cpp index ccbe202..e496636 100644 --- a/question.cpp +++ b/question.cpp @@ -1,5 +1,10 @@ #include "question.h" +QuestionEntity::QuestionEntity() +{ + +} + QuestionEntity::QuestionEntity(int p_id, QString &p_question, QSet& p_answers, QStringList& p_variants): id(p_id), question(p_question), answers(p_answers), variants(p_variants) {} @@ -37,3 +42,13 @@ void QuestionEntity::setAnswers(QSet &p_answers) { this->answers = p_answers; } + +void QuestionEntity::setId(int &p_id) +{ + id = p_id; +} + +void QuestionEntity::setVariants(QStringList &p_variants) +{ + variants = p_variants; +} diff --git a/question.h b/question.h index 2a90224..20eeb44 100644 --- a/question.h +++ b/question.h @@ -7,6 +7,7 @@ class QuestionEntity { public: + QuestionEntity(); QuestionEntity(int p_id, QString& p_question, QSet& p_answers, QStringList& p_variants); int getId(); QString getQuestion() const; @@ -17,6 +18,8 @@ public: void setQuestion(QString& p_question); void setAnswers(QSet& p_answers); + void setId(int &p_id); + void setVariants(QStringList &p_variants); private: int id; diff --git a/redactionwidget.cpp b/redactionwidget.cpp new file mode 100644 index 0000000..126255a --- /dev/null +++ b/redactionwidget.cpp @@ -0,0 +1,150 @@ +#include "redactionwidget.h" +#include + +RedactionWidget::RedactionWidget(QWidget *parent) : QWidget(parent) +{ + + this->hide(); + testOutput = new QTextBrowser(); + this->hide(); + mainLayout = new QGridLayout(this); + testsList = new QListView(); + questionList = new QListView(); + change = new QPushButton("Изменить", this); + add = new QPushButton("Добавить", this); + del = new QPushButton("Удалить", this); + chgWidget = new ChangeWidget(); + qe = new QuestionEntity(); + exit = new QPushButton("Назад"); + + testsList->setEditTriggers(QListView::NoEditTriggers); + questionList->setEditTriggers(QListView::NoEditTriggers); + mainLayout->addWidget(testsList,0,0); + mainLayout->addWidget(questionList,0,1); + mainLayout->addWidget(testOutput,1,0,1,2); + mainLayout->addWidget(change, 2,0); + mainLayout->addWidget(add, 2,1); + mainLayout->addWidget(del, 3,1); + mainLayout->addWidget(exit,3,0); + + QStringListModel* strListModel = new QStringListModel(testsList); + QStringList *strList = new QStringList(); + QSqlQueryModel *queryModel = new QSqlQueryModel(this); + queryModel->setQuery("SELECT name FROM tests",db); + for(int i = 0; i < queryModel->rowCount(); i++ ) + { + strList->push_back(queryModel->index(i,0).data().toString()); + } + strListModel->setStringList(*strList); + testsList->setModel(strListModel); + + connect(testsList, SIGNAL(doubleClicked(QModelIndex)),this, SLOT(onLVdoubleClicked(QModelIndex))); + connect(questionList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onLV2doubleClicked(QModelIndex))); + connect(questionList, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(showTest(QModelIndex))); + + connect(add, SIGNAL(clicked()), this, SLOT(sendDB(db))); + connect(this, SIGNAL(sendDataBase(const QSqlDatabase &)), chgWidget, SLOT(recieveDataBase(const QSqlDatabase &))); + connect(change, SIGNAL(clicked()), this, SLOT(sendDB(db)) ); + + connect(add, SIGNAL(clicked()), this, SLOT(sendQE())); + // connect(this, SIGNAL(sendQuestion(const QuestionEntity &)), chgWidget, SLOT(receiveQuestion(const QuestionEntity &))); + connect(change, SIGNAL(clicked()), this, SLOT(sendQE()) ); + + + connect(add, SIGNAL(clicked()), this, SLOT(sendAddType())); + connect(change, SIGNAL(clicked()), this, SLOT(sendChangeType())); + connect(this, SIGNAL(sendT(bool)), chgWidget, SLOT(paintWidget(bool))); + + connect(del, SIGNAL(clicked()), this, SLOT(deleteQuestion())); + + + + + +} + +void RedactionWidget::receiveDataBase(const QSqlDatabase &p_db) +{ + db = p_db; +} + +void RedactionWidget::onLVdoubleClicked(const QModelIndex &inx) +{ + QSqlQueryModel *model = new QSqlQueryModel(); + model->setQuery(QSqlQuery("SELECT question FROM test_questions WHERE test_id = " + QString::number(inx.row()+1), db)); + QStringList *strList = new QStringList(); + QStringListModel *strListModel = new QStringListModel(questionList); + for(int i = 0; i < model->rowCount(); i++ ) + { + strList->push_back(model->index(i,0).data().toString()); + } + strListModel->setStringList(*strList); + questionList->setModel(strListModel); + +} + +void RedactionWidget::onLV2doubleClicked(const QModelIndex &inx) +{ + QString testString; + QString questionName = inx.data().toString(); + qDebug() << questionName; + QSqlQuery query("SELECT * FROM test_questions WHERE question = '" + questionName + "';",db); + QSqlQueryModel *model = new QSqlQueryModel(); + model->setQuery(query); + int id = model->index(0, 0).data().toInt(); + QString question = model->index(0, 1).data().toString(); + QStringList variantsList = model->index(0, 2).data().toString().split("%"); + QStringList answersList = model->index(0, 3).data().toString().split(" "); + QSet answers(answersList.begin(), answersList.end()); + qe->setAnswers(answers); + qe->setQuestion(question); + qe->setId(id); + qe->setVariants(variantsList); + testString+= "\n" + qe->getQuestion() + "\n"; + + for(auto& el:qe->getVariants()){ + testString+= + " " + el +"\n"; + } + testString += "Правильный ответ: "; + for(auto& el: qe->getAnswers()){ + testString += el + " "; + } + testString += "\n"; + testOutput->setText(testString); + + +} + +void RedactionWidget::sendDB(const QSqlDatabase &p_db) +{ + emit(sendDataBase(p_db)); +} + +void RedactionWidget::sendQE() +{ + //emit(sendQuestion(qe)); + QSqlQueryModel *model = new QSqlQueryModel(this); + model->setQuery(QSqlQuery("SELECT test_id from test_questions WHERE question = '" +qe-> getQuestion() +"';", db)); + int id = model->index(0,0).data().toInt(); + chgWidget->setQuestion(*qe,id); +} + +void RedactionWidget::sendAddType() +{ + emit(sendT(false)); +} + +void RedactionWidget::sendChangeType() +{ + emit(sendT(true)); +} +void RedactionWidget::deleteQuestion() +{ + QSqlQueryModel *model = new QSqlQueryModel(this); + model->setQuery(QSqlQuery("DELETE from test_questions WHERE question = '"+qe->getQuestion()+"';", db)); + +} + + + + diff --git a/redactionwidget.h b/redactionwidget.h new file mode 100644 index 0000000..c061d82 --- /dev/null +++ b/redactionwidget.h @@ -0,0 +1,60 @@ +#ifndef REDACTIONWIDGET_H +#define REDACTIONWIDGET_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "question.h" +#include "changewidget.h" + +class RedactionWidget : public QWidget +{ + Q_OBJECT +public: + explicit RedactionWidget(QWidget *parent = nullptr); + QPushButton* exit; + +signals: + void sendDataBase(const QSqlDatabase &p_db); + void sendQuestion(const QuestionEntity &qe); + void sendT(bool type); + +public slots: + void receiveDataBase(const QSqlDatabase & p_db); + void onLVdoubleClicked(const QModelIndex &inx); + void onLV2doubleClicked(const QModelIndex &inx); + void sendDB(const QSqlDatabase &p_db); + void sendQE(); + void sendAddType(); + void sendChangeType(); + void deleteQuestion(); +private: + QSqlDatabase db; + QListView *testsList; + QListView *questionList; + QGridLayout *mainLayout; + QTextBrowser *testOutput; + QPushButton *change; + QPushButton *add; + QPushButton *del; + ChangeWidget *chgWidget; + QuestionEntity *qe; + + + + + + + + + +}; + +#endif // REDACTIONWIDGET_H diff --git a/tester.cpp b/tester.cpp index 5581015..492f46f 100644 --- a/tester.cpp +++ b/tester.cpp @@ -25,7 +25,7 @@ Tester::Tester(QWidget *parent) ui->label->hide(); ui->lineEdit->hide(); ui->pushButton->hide(); - start = new QPushButton("Начать тестирование", this); + start = new QPushButton("Старт", this); start->show(); -- GitLab