diff --git a/Bookshelf.pro b/Bookshelf.pro index 439a818..eb28fa1 100644 --- a/Bookshelf.pro +++ b/Bookshelf.pro @@ -23,13 +23,17 @@ msvc { SOURCES += \ corehost.cpp \ + countrymanagement.cpp \ dbunit.cpp \ + labelsmanagement.cpp \ main.cpp \ mainwindow.cpp HEADERS += \ corehost.h \ + countrymanagement.h \ dbunit.h \ + labelsmanagement.h \ mainwindow.h # Default rules for deployment. diff --git a/countrymanagement.cpp b/countrymanagement.cpp new file mode 100644 index 0000000..e612254 --- /dev/null +++ b/countrymanagement.cpp @@ -0,0 +1,120 @@ +#include "countrymanagement.h" +#include "dbunit.h" +#include +#include +#include +#include +#include +#include + +using namespace ViewComp; + +CountryManagement::CountryManagement(DBUnit* database, QWidget* parent) + : QDialog(parent), db_host(database), keywords_input(new QLineEdit(this)), + country_view(new QTableView(this)), country_model(new QStandardItemModel(this)), + append(new QPushButton("添加", this)), remove(new QPushButton("移除", this)) +{ + country_view->setModel(country_model); + + // ============================ + auto layout = new QGridLayout(this); + layout->addWidget(keywords_input, 0, 0, 1, 4); + layout->addWidget(country_view, 1, 0, 3, 4); + layout->addWidget(append, 4, 0, 1, 2); + layout->addWidget(remove, 4, 2, 1, 2); + country_model->setHorizontalHeaderLabels(QStringList() << "国家名称" + << "简介"); + + connect(append, &QPushButton::clicked, [this]() { + QInputDialog in(this); + in.setWindowTitle("输入国家名称"); + in.setLabelText("输入非重复国家名称:"); + if (in.exec() == QDialog::Rejected) + return; + + auto q = db_host->getQuery(); + q.prepare("insert into country_information " + "(name, describe)values(:n, :d)"); + q.bindValue(":n", in.textValue()); + q.bindValue(":d", "请手动修改国家简述"); + if (!q.exec()) { + qDebug() << q.lastError(); + return; + } + q.prepare("select max(cid) from country_information"); + if (!q.exec()) { + qDebug() << q.lastError(); + return; + } + if (q.next()) { + QList row; + row << new QStandardItem(in.textValue()); + row.last()->setData(q.value(0)); + row << new QStandardItem("请手动修改国家简述"); + country_model->appendRow(row); + } + }); + connect(remove, &QPushButton::clicked, [this]() { + auto index = country_view->currentIndex(); + if (!index.isValid()) + return; + + auto inindex = index.sibling(index.row(), 0); + auto q = db_host->getQuery(); + q.prepare("delete from country_information where cid = :id"); + q.bindValue(":id", inindex.data(Qt::UserRole + 1)); + if (!q.exec()) { + qDebug() << q.lastError(); + return; + } + + country_model->removeRow(index.row()); + }); + + connect(keywords_input, &QLineEdit::textChanged, this, &CountryManagement::input_query); +} + +void +CountryManagement::init_country_model(QStandardItemModel* model) +{ + model->removeRows(0, model->rowCount()); + auto q = db_host->getQuery(); + q.prepare("select cid, name, describe from country_information"); + if (!q.exec()) { + qDebug() << q.lastError(); + return; + } + while (q.next()) { + QList row; + row << new QStandardItem(q.value(1).toString()); + row.last()->setData(q.value(0)); + row << new QStandardItem(q.value(2).toString()); + country_model->appendRow(row); + } +} + +void +CountryManagement::input_query(const QString& keywords) +{ + country_model->removeRows(0, country_model->rowCount()); + if (keywords.isEmpty()) + return; + + QString str = "select cid, name, describe from country_information "; + if (keywords != "*") { + str += "where name like '%" + keywords + "%'"; + } + auto q = db_host->getQuery(); + q.prepare(str); + if (!q.exec()) { + qDebug() << q.lastError(); + return; + } + while (q.next()) { + QList row; + row << new QStandardItem(q.value(1).toString()); + row.last()->setData(q.value(0)); + row << new QStandardItem(q.value(2).toString()); + country_model->appendRow(row); + } +} diff --git a/countrymanagement.h b/countrymanagement.h new file mode 100644 index 0000000..2ab399c --- /dev/null +++ b/countrymanagement.h @@ -0,0 +1,30 @@ +#ifndef COUNTRYMANAGEMENT_H +#define COUNTRYMANAGEMENT_H + +#include +#include +#include +#include + +class DBUnit; + +namespace ViewComp { +class CountryManagement : public QDialog { +public: + CountryManagement(DBUnit* database, QWidget* parent = nullptr); + +private: + DBUnit* const db_host; + + QLineEdit* const keywords_input; + QTableView* const country_view; + QStandardItemModel* const country_model; + QPushButton *const append, *const remove; + + void init_country_model(QStandardItemModel* model); + void input_query(const QString& keywords); +}; + +} + +#endif // COUNTRYMANAGEMENT_H diff --git a/example.db b/example.db index 9bb5923..43c8429 100644 Binary files a/example.db and b/example.db differ diff --git a/labelsmanagement.cpp b/labelsmanagement.cpp index 3ad6e06..fef99b5 100644 --- a/labelsmanagement.cpp +++ b/labelsmanagement.cpp @@ -8,6 +8,8 @@ #include #include +using namespace ViewComp; + LabelsManagement::LabelsManagement(DBUnit* tool, QWidget* parent) : QDialog(parent), db_host(tool), keywords_input(new QLineEdit(this)), labels_view(new QTableView(this)), labels_model(new QStandardItemModel(this)), @@ -46,11 +48,13 @@ LabelsManagement::LabelsManagement(DBUnit* tool, QWidget* parent) qDebug() << q.lastError(); return; } - QList row; - row << new QStandardItem(in.textValue()); - row.last()->setData(q.value(0), Qt::UserRole + 1); - row << new QStandardItem("请手动修改标签简介"); - labels_model->appendRow(row); + if (q.next()) { + QList row; + row << new QStandardItem(in.textValue()); + row.last()->setData(q.value(0), Qt::UserRole + 1); + row << new QStandardItem("请手动修改标签简介"); + labels_model->appendRow(row); + } }); connect(remove, &QPushButton::clicked, [this]() { auto index = labels_view->currentIndex(); diff --git a/labelsmanagement.h b/labelsmanagement.h index 19d7c94..d9796e3 100644 --- a/labelsmanagement.h +++ b/labelsmanagement.h @@ -10,6 +10,7 @@ class DBUnit; +namespace ViewComp { class LabelsManagement : public QDialog { Q_OBJECT public: @@ -27,4 +28,6 @@ private: void input_query(const QString& keywords); }; +} + #endif // LABELSMANAGEMENT_H diff --git a/main.cpp b/main.cpp index 0af8d02..82106a4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include "countrymanagement.h" #include "dbunit.h" #include "labelsmanagement.h" #include "mainwindow.h" @@ -10,7 +11,7 @@ int main(int argc, char *argv[]) // w.show(); DBUnit tool; - LabelsManagement m(&tool); + ViewComp::CountryManagement m(&tool); m.show(); return a.exec();