diff --git a/addnewtestwidget.cpp b/addnewtestwidget.cpp index 8965bb748e4383c04ca861f9b1e191142ac6d614..ab3872292ee648ad7c339911229b09daea9e5160 100644 --- a/addnewtestwidget.cpp +++ b/addnewtestwidget.cpp @@ -141,10 +141,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 ddba2d1718fc1bab244d0f4d134a2b07fdad6345..582c7581b5b455e7364b074598d66aa55b6ed81a 100644 --- a/adminwindow.cpp +++ b/adminwindow.cpp @@ -35,6 +35,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 f29b8a7f4a170052125f7983259a170ca2897763..58b11d9567861725c4a1e043e974cc67b854261d 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 @@ -44,6 +45,7 @@ private: QTableView *results; addNewTestWidget *newTestWidget; deletetestwidget *deleteWidget; + RedactionWidget *redactWidget; }; diff --git a/adminwindow.ui b/adminwindow.ui index 358c5f9c6d4f5a43f11e47daa066ec18b791b938..9be6a9a3987dadda16023ccd7d39c6ac60f06278 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 046e57d4b1b7c623e52091e1fd190d09ff101c55..743985aaf69e4f1d44801924bef326827e0ef285 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 0000000000000000000000000000000000000000..36bd9b92168f3948294a09a4e5cd48c4b6489ed5 --- /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 0000000000000000000000000000000000000000..fac461e05f3d859d367879d401f19c6c54edeac4 --- /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 0922c44ff8c94fee88d62422b9ddd7d86d5e14ac..d9141690e41407d10ab78503d80ad95ff99c591f 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() @@ -44,5 +59,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 4cf86a02dcb206fe9603dd4cc34231d1b0c07ef2..a4aa7bbef08121884d0734d49fba82d38ee3365e 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 f1e8ff37566c7e0fea3f054717fedde37caaaa97..55f79816e21a361cba5c54b662b0651620fe8984 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 ccbe20228098528a1fab4e77c35cb263228b6b9b..e496636ea291a8f042366d503ca26c72e98ad3c6 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 2a90224345a1f359f2b0825e9637619417391e81..20eeb44c3a1e7179e42ea8fce2ff65ca02117d6b 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 0000000000000000000000000000000000000000..126255ad36fd459649b824f0a680501a648cb049 --- /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 0000000000000000000000000000000000000000..c061d82f35ec269cc9d65cc5445e8e49fba1169e --- /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