From 46f48b84404874a98a6ca736a9c0379048c04074 Mon Sep 17 00:00:00 2001 From: Mike Solar Date: Tue, 15 Aug 2023 20:12:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=BA=86=E5=87=A0=E4=B8=AAbug?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OfficeAssistant_msvc/addapp.cpp | 62 +++++--- OfficeAssistant_msvc/addapp.h | 1 + OfficeAssistant_msvc/applicationmanager.cpp | 30 +++- OfficeAssistant_msvc/applicationmanager.h | 1 + .../applicationmanagerpage.cpp | 12 +- OfficeAssistant_msvc/sqlitehelper.cpp | 141 ++++++++++++------ OfficeAssistant_msvc/sqlitehelper.h | 5 +- 7 files changed, 175 insertions(+), 77 deletions(-) diff --git a/OfficeAssistant_msvc/addapp.cpp b/OfficeAssistant_msvc/addapp.cpp index d6e0c5d..15e375e 100644 --- a/OfficeAssistant_msvc/addapp.cpp +++ b/OfficeAssistant_msvc/addapp.cpp @@ -3,7 +3,7 @@ #include #include #include - +#include "config.h" #include "applicationmanager.h" @@ -16,27 +16,34 @@ AddApp::AddApp(bool isEdit, Record2 *row , QWidget* parent) : QDialog(parent) { ui.setupUi(this); + this->row=row; SQLiteHelper sqlite_helper; setFixedSize(450, 450); + Record2 record; + sqlite_helper.get_a_software(row->orig_name, &record); setWindowFlags(Qt::WindowCloseButtonHint); form = new QFormLayout; name = new QLineEdit; form->addRow(QString::fromLocal8Bit("名称"), name); - if(isEdit) + + if (isEdit) { name_label = new QLabel(orig_name_str); form->addRow(QString::fromLocal8Bit("全名"), name_label); - if(row!=nullptr) + + if (!record.orig_name.isEmpty()) { - name_label->setText(row->orig_name); + name_label->setText(record.orig_name); } + orig_name_str = record.orig_name; } else { orig_name = new QLineEdit; form->addRow(QString::fromLocal8Bit("全名"), orig_name); } - if(row==nullptr) + + if (record.orig_name.isEmpty()) { path = new QPushButton(QString::fromLocal8Bit("浏览")); connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe); @@ -46,11 +53,12 @@ AddApp::AddApp(bool isEdit, Record2 *row , QWidget* parent) form->addRow(QString::fromLocal8Bit("排序"), sort); QList categrories; layout_inner = new QVBoxLayout; - sqlite_helper.get_category(categrories,true); + sqlite_helper.get_category(categrories, true); for (auto category : categrories) { QCheckBox* check_box = new QCheckBox(category.name); + layout_inner->addWidget(check_box); categories_list.insert(category.id, check_box); } @@ -61,28 +69,29 @@ AddApp::AddApp(bool isEdit, Record2 *row , QWidget* parent) connect(path, &QPushButton::click, this, &AddApp::broswer_exe); connect(submit_btn, &QPushButton::clicked, this, &AddApp::submit); this->isEdit = isEdit; - }else + } + else { - name->setText(row->name); - name_str=row->name; - sort_str=QString::number(row->sort); + name->setText(record.name); + name_str = record.name; + sort_str = QString::number(record.sort); path = new QPushButton(QString::fromLocal8Bit("浏览")); - path_str = row->exe_file; + path_str = record.exe_file; path->setText(QString::fromWCharArray(fs::path(path_str.toStdWString()).filename().c_str())); connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe); form->addRow(QString::fromLocal8Bit("路径"), path); sort = new QLineEdit; - sort->setText(QString::number(row->sort)); + sort->setText(QString::number(record.sort)); sort->setValidator(new QIntValidator(10, 100)); form->addRow(QString::fromLocal8Bit("排序"), sort); QList categrories; layout_inner = new QVBoxLayout; - sqlite_helper.get_category(categrories,true); - categories = row->categories; + sqlite_helper.get_category(categrories, true); + this->categories = record.categories; for (auto category : categrories) { QCheckBox* check_box = new QCheckBox(category.name); - if(row->categories[category.id-1]) + if (record.categories[category.id - 1]) { check_box->setCheckState(Qt::Checked); } @@ -112,7 +121,7 @@ AddApp::~AddApp() delete orig_name; } delete path; - delete sort; + //delete sort; delete submit_btn; delete form; } @@ -146,7 +155,6 @@ void AddApp::submit() { QString sort_str_old = sort_str; name_str = name->text(); sort_str = sort->text(); - if (name_str.isEmpty()) { name_str = name_str_old; } @@ -159,7 +167,8 @@ void AddApp::submit() { QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("未填写路径")); return; } - }else + } + else { if ((!orig_name->text().isEmpty()) && (!path->text().isEmpty())) { @@ -167,7 +176,10 @@ void AddApp::submit() { if (name->text().isEmpty()) { name_str = orig_name_str; } - name_str = name->text(); + else { + name_str = name->text(); + } + sort_str = sort->text(); } else @@ -176,10 +188,22 @@ void AddApp::submit() { return; } } + categories = new bool[8]; + for (int i = 0; i < 8; i++) { + categories[i] = false; + } + categories[ALL-1] = true; + if (row->op == "soft") { + categories[SOFT-1] = true; + } + else if (row->op == "app") { + categories[INNER-1] = true; + } for(auto key:categories_list.keys()) { categories[key - 1] = categories_list[key]->checkState(); } + for (int i = 0; i < 8; i++) { qDebug() << categories[i]; } diff --git a/OfficeAssistant_msvc/addapp.h b/OfficeAssistant_msvc/addapp.h index bb217cc..3241940 100644 --- a/OfficeAssistant_msvc/addapp.h +++ b/OfficeAssistant_msvc/addapp.h @@ -55,6 +55,7 @@ private: QString orig_name_str; QString path_str; QVBoxLayout* layout_inner; + Record2 *row; bool isEdit; private slots: void broswer_exe(); diff --git a/OfficeAssistant_msvc/applicationmanager.cpp b/OfficeAssistant_msvc/applicationmanager.cpp index ae6104c..172c9cb 100644 --- a/OfficeAssistant_msvc/applicationmanager.cpp +++ b/OfficeAssistant_msvc/applicationmanager.cpp @@ -24,7 +24,7 @@ ApplicationManager::ApplicationManager(QWidget *parent) layout_top->setMargin(5); layout_bottom = new QHBoxLayout(this); QList categrories; - sqlite_helper.get_category(categrories,false); + sqlite_helper.get_category(categrories,false,true); sqlite_helper.get_buttons(button_structs); for (auto button_struct : button_structs) { @@ -45,13 +45,13 @@ ApplicationManager::ApplicationManager(QWidget *parent) //ui.tabWidget->setMovable(false); //ui.tabWidget->setDocumentMode(false); //ui.tabWidget->setCurrentIndex(0); + tab_bar = new QTabBar(this); - tab_bar->addTab(QString::fromLocal8Bit("全部")); layout_bottom_top = new QVBoxLayout; - for(int i=2;i<=8;i++) + for(int i=1;i<=8;i++) { - tab_bar->addTab(categrories[i - 2].name); + tab_bar->addTab(categrories[i - 1].name + "(" + QString::number(categrories[i - 1].total) + ")"); } layout_bottom_top->addWidget(tab_bar, 1); application_manager_page_all = new ApplicationManagerPage(ALL, this, this); @@ -66,6 +66,7 @@ ApplicationManager::ApplicationManager(QWidget *parent) application_manager_page->setHidden(true); map.insert(i, application_manager_page); } + connect(this,&ApplicationManager::refresh,this,&ApplicationManager::onNumChange); tab_bar->setCurrentIndex(0); connect(tab_bar, &QTabBar::currentChanged, this, &ApplicationManager::tabChange); QString style = "QTabBar::tab{height:80;width:150; border:2px} QTabBar::tab:hover{ border:2px;height:80;width:150;color:"; @@ -210,6 +211,7 @@ void ApplicationManager::onclick1(QString op, QString func, QString path, QStrin { QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("更新部分或全部失败")); } + emit refresh(); }else if(func=="app_category") { AddCategory add_category; @@ -225,6 +227,8 @@ void ApplicationManager::onclick1(QString op, QString func, QString path, QStrin { QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("添加失败")); } + emit refresh(); + } } }else if(op=="soft") @@ -251,7 +255,7 @@ void ApplicationManager::onclick2(QString orig_name, QString op) } else { emit changeUrl(application_manager_page_all->rows[orig_name].url); - WinExec(application_manager_page_all->rows[orig_name].exe_file.toStdString().c_str(), SW_SHOW); + WinExec((QApplication::applicationDirPath()+application_manager_page_all->rows[orig_name].exe_file).toStdString().c_str(), SW_SHOW); } } else if (op == "settings") @@ -265,10 +269,16 @@ void ApplicationManager::onclick2(QString orig_name, QString op) { QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("编辑失败")); } + emit refresh(); + } } else { - emit application_manager_page_all->onclick3(application_manager_page_all->rows[orig_name].op, application_manager_page_all->rows[orig_name].func,application_manager_page_all->rows[orig_name].exe_file,application_manager_page_all->rows[orig_name].url); + emit application_manager_page_all->onclick3(application_manager_page_all->rows[orig_name].op, + application_manager_page_all->rows[orig_name].func, + application_manager_page_all->rows[orig_name].url, + application_manager_page_all->rows[orig_name].exe_file + ); } } @@ -281,3 +291,11 @@ void ApplicationManager::tabChange(int index) } map[index + 1]->setHidden(false); } + +void ApplicationManager::onNumChange() { + QList categrories; + sqlite_helper.get_category(categrories, false, true); + for (int i = 0; i < categrories.size(); i++) { + tab_bar->setTabText(i, categrories[i].name + "(" + QString::number(categrories[i].total) + ")"); + } +} \ No newline at end of file diff --git a/OfficeAssistant_msvc/applicationmanager.h b/OfficeAssistant_msvc/applicationmanager.h index 432ad08..48e2ec1 100644 --- a/OfficeAssistant_msvc/applicationmanager.h +++ b/OfficeAssistant_msvc/applicationmanager.h @@ -77,4 +77,5 @@ public slots: void onclick1(QString op, QString func, QString path, QString url); void onclick2(QString orig_name, QString op); void tabChange(int index); + void onNumChange(); }; diff --git a/OfficeAssistant_msvc/applicationmanagerpage.cpp b/OfficeAssistant_msvc/applicationmanagerpage.cpp index 06dcfd2..1989f92 100644 --- a/OfficeAssistant_msvc/applicationmanagerpage.cpp +++ b/OfficeAssistant_msvc/applicationmanagerpage.cpp @@ -174,14 +174,7 @@ void ApplicationManagerPage::refresh() { table->removeRow(0); } - for(auto row:rows) - { - disconnect(row.settings, &MiniButton::click0, row.settings, &MiniButton::onclick2); - disconnect(row.settings, &MiniButton::click2, application_manager, &ApplicationManager::onclick2); - disconnect(row.open, &MiniButton::click0, row.open, &MiniButton::onclick2); - disconnect(row.open, &MiniButton::click2, application_manager, &ApplicationManager::onclick2); - disconnect(row.check_box, &QCheckBox::clicked, this, &ApplicationManagerPage::onclicked); - } + softwares.clear(); sqlite_helper.get_all_software(softwares, category); int i = 0; @@ -241,6 +234,9 @@ void ApplicationManagerPage::refresh() record2.categories[i] = true; } } + record2.op = software.op; + record2.func = software.func; + connect(this, &ApplicationManagerPage::onclick3, (MainWindowLayout*)((MainScreen*)application_manager->parent)->parent, &MainWindowLayout::clickButton); record2.sort = software.sort; record2.check_box = new QCheckBox; if (software.type == "navbar") diff --git a/OfficeAssistant_msvc/sqlitehelper.cpp b/OfficeAssistant_msvc/sqlitehelper.cpp index 89a2a4a..dbac278 100644 --- a/OfficeAssistant_msvc/sqlitehelper.cpp +++ b/OfficeAssistant_msvc/sqlitehelper.cpp @@ -18,6 +18,7 @@ #include #include #include +#include "applicationmanager.h" #define MAX_KEY_LENGTH 255 #define MAX_VALUE_NAME 16383 @@ -782,7 +783,7 @@ bool SQLiteHelper::update_software() { int total = query2.value("total").toInt(); query2.prepare("update kmd_category set total=:total where id=:id;"); - query2.bindValue(":total", total + 1); + query2.bindValue(":total", total); query2.bindValue(":id", i + 1); if (!query2.exec()) { @@ -1177,24 +1178,18 @@ bool SQLiteHelper::update_app() QSqlQuery query2(db); if (categories[i]) { - query2.prepare("select total from kmd_category where id=:id;"); - query2.bindValue(":id", i + 1); - if (!query2.exec()) - { - QSqlQuery rollback(db); - rollback.exec("rollback;"); - return false; - } - if (query2.next()) + query2.prepare("SELECT COUNT(*) AS total FROM kmd_menu WHERE category_id LIKE :categoryId;"); + query2.bindValue(":categoryId", "%" + QString::number(i + 1) + "%"); + if (query2.exec() && query2.first()) { int total = query2.value("total").toInt(); - query2.prepare("update kmd_category set total=:total where id=:id;"); - query2.bindValue(":total", total + 1); + query2.prepare("UPDATE kmd_category SET total=:total WHERE id=:id;"); + query2.bindValue(":total", total); query2.bindValue(":id", i + 1); if (!query2.exec()) { QSqlQuery rollback(db); - rollback.exec("rollback;"); + rollback.exec("ROLLBACK;"); return false; } } @@ -1251,7 +1246,7 @@ bool SQLiteHelper::insert_software(QString name, QString orig_name, QString path query.addBindValue("app"); query.addBindValue(categories_str); query.addBindValue(name); - query.addBindValue(name); + query.addBindValue(orig_name); time_t create_time; time(&create_time); query.addBindValue(create_time); @@ -1266,6 +1261,31 @@ bool SQLiteHelper::insert_software(QString name, QString orig_name, QString path //QMessageBox::critical(nullptr, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("写入数据库失败")); return false; } + for (int i = 0; i < 8; i++) + { + if (categories[i]) + { + QSqlQuery query2(db); + + query2.prepare("SELECT COUNT(*) AS total FROM kmd_menu WHERE category_id LIKE :categoryId;"); + query2.bindValue(":categoryId", "%" + QString::number(i + 1) + "%"); + if (query2.exec() && query2.first()) + { + int total = query2.value("total").toInt(); + query2.prepare("UPDATE kmd_category SET total=:total WHERE id=:id;"); + query2.bindValue(":total", total); + query2.bindValue(":id", i + 1); + qDebug()<< query2.lastError(); + if (!query2.exec()) + { + query.exec("ROLLBACK;"); + return false; + } + } + } + } + + return true; } @@ -1293,12 +1313,12 @@ bool SQLiteHelper::set_category(QList& categrories) qDebug() << work.lastError(); return true; } -bool SQLiteHelper::get_category(QList& categrories,bool is_edit) +bool SQLiteHelper::get_category(QList& categrories,bool is_edit,bool all) { QSqlQuery query(db); if(is_edit) { - if (!query.exec("select id,name,status,sort from kmd_category where is_edit=1;")) + if (!query.exec("select id,name,status,sort,total from kmd_category where is_edit=1;")) { return false; } @@ -1313,12 +1333,29 @@ bool SQLiteHelper::get_category(QList& categrories,bool is_edit) categrory.id = query.value("id").toInt(); categrory.sort = query.value("sort").toInt(); categrory.display = query.value("status").toBool(); + categrory.total = query.value("total").toInt(); + categrories << categrory; + } + } + else if (all&&(!is_edit)) { + if (!query.exec("select id,name,status,sort,total from kmd_category;")) + { + return false; + } + while (query.next()) + { + Categrory categrory; + categrory.name = query.value("name").toString(); + categrory.id = query.value("id").toInt(); + categrory.sort = query.value("sort").toInt(); + categrory.display = query.value("status").toBool(); + categrory.total = query.value("total").toInt(); categrories << categrory; } } else { - if (!query.exec("select id,name,status,sort from kmd_category;")) + if (!query.exec("select id,name,status,sort,total from kmd_category;")) { return false; } @@ -1333,6 +1370,7 @@ bool SQLiteHelper::get_category(QList& categrories,bool is_edit) categrory.id = query.value("id").toInt(); categrory.sort = query.value("sort").toInt(); categrory.display = query.value("status").toBool(); + categrory.total = query.value("total").toInt(); categrories << categrory; } } @@ -1393,7 +1431,7 @@ bool SQLiteHelper::edit_software(QString name, QString orig_name, QString path, query.prepare(sql); query.bindValue(":name", name); query.bindValue(":path", path); - query.bindValue(":sort", sort.toInt()); + query.bindValue(":sort", sort); QString categories_str = ""; for (int i = 0; i < 8; i++) { @@ -1415,34 +1453,21 @@ bool SQLiteHelper::edit_software(QString name, QString orig_name, QString path, for(int i=0;i<8;i++) { QSqlQuery query2(db); - if(categories[i]) + + query2.prepare("SELECT COUNT(*) AS total FROM kmd_menu WHERE category_id LIKE :categoryId;"); + query2.bindValue(":categoryId", "%" + QString::number(i + 1) + "%"); + if (query2.exec() && query2.first()) { - query2.prepare("select total from kmd_category where id=:id;"); + int total = query2.value("total").toInt(); + query2.prepare("UPDATE kmd_category SET total=:total WHERE id=:id;"); + query2.bindValue(":total", total); query2.bindValue(":id", i + 1); - if(!query2.exec()) + qDebug() << query2.lastError(); + if (!query2.exec()) { - QSqlQuery rollback(db); - rollback.exec("rollback;"); + query.exec("ROLLBACK;"); return false; } - if(query2.next()) - { - QSqlQuery total(db); - QSqlQuery query3(db); - QString total_sql = "select id from kmd_menu where category_id like '%"; - total_sql += QString::number(i + 1); - total_sql += "%';"; - total.exec(total_sql); - query3.prepare("update kmd_category set total=:total where id=:id;"); - query3.bindValue(":total", total.size());; - query3.bindValue(":id", i + 1); - if (!query3.exec()) - { - QSqlQuery rollback(db); - rollback.exec("rollback;"); - return false; - } - } } } QSqlQuery commit(db); @@ -1456,7 +1481,7 @@ bool SQLiteHelper::edit_software(QString name, QString orig_name, QString path, query.prepare(sql); query.bindValue(":name", name); query.bindValue(":path", path); - query.bindValue(":sort", sort.toInt()); + query.bindValue(":sort", sort); QString categories_str = ""; for (int i = 0; i < 8; i++) { @@ -1496,7 +1521,7 @@ bool SQLiteHelper::edit_software(QString name, QString orig_name, QString path, total_sql += "%';"; total.exec(total_sql); query3.prepare("update kmd_category set total=:total where id=:id;"); - query3.bindValue(":total", total.size());; + query3.bindValue(":total", total.size()+1); query3.bindValue(":id", i + 1); if (!query3.exec()) { @@ -1512,3 +1537,33 @@ bool SQLiteHelper::edit_software(QString name, QString orig_name, QString path, return true; } } +bool SQLiteHelper::get_a_software(QString orig_name, Record2 *record) { + QString sql = "select * from kmd_menu where orig_name=:orig_name;"; + QSqlQuery query(db); + query.prepare(sql); + query.bindValue(":orig_name", orig_name); + if (!query.exec()) + { + return false; + } + if (query.next()) + { + for (int i = 0; i < 8; i++) { + if (query.value("category_id").toString().contains(QString::number(i + 1))) { + record->categories[i] = true; + } + else { + record->categories[i] = false; + } + } + record->sort = query.value("sort").toInt(); + record->name = query.value("name").toString(); + record->orig_name = query.value("orig_name").toString(); + record->op = query.value("op").toString(); + record->func = query.value("func").toString(); + record->exe_file = query.value("path").toString(); + record->url = query.value("url").toString(); + return true; + } + return false; +} diff --git a/OfficeAssistant_msvc/sqlitehelper.h b/OfficeAssistant_msvc/sqlitehelper.h index 7efe87c..54b9f6e 100644 --- a/OfficeAssistant_msvc/sqlitehelper.h +++ b/OfficeAssistant_msvc/sqlitehelper.h @@ -9,6 +9,7 @@ #include "buttonstruct.h" struct ConfigResponse; class ConfigRequest; +struct Record2; class SoftwareRequest; typedef struct tagRecord{ int id; @@ -34,6 +35,7 @@ typedef struct tagRecord{ struct Categrory { int id; + int total; QString name; bool display; int sort; @@ -50,9 +52,10 @@ public: bool get_software(QList* buttons, ConfigResponse* config_response); bool update_app(); bool get_buttons(QList &buttons); - bool get_category(QList& categrories, bool is_edit); + bool get_category(QList& categrories, bool is_edit,bool all=false); bool set_category(QList& categrories); bool get_all_software(QList &softwares,int category); + bool get_a_software(QString orig_name, Record2 *record); QSqlDatabase db; ~SQLiteHelper();