OfficeAssistant_Win10/OfficeAssistant_msvc/addapp.cpp

232 lines
6.3 KiB
C++
Raw Normal View History

2023-08-18 19:09:55 +08:00
#include "addapp.h"
2023-08-13 12:16:22 +08:00
#include <QFileDialog>
#include <QProcessEnvironment>
#include <QMessageBox>
#include <sqlitehelper.h>
2023-08-15 20:12:36 +08:00
#include "config.h"
2023-08-13 12:16:22 +08:00
#include "applicationmanager.h"
2023-08-15 11:17:09 +08:00
2023-08-13 12:16:22 +08:00
#define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
2023-08-15 11:17:09 +08:00
2023-08-13 12:16:22 +08:00
AddApp::AddApp(bool isEdit, Record2 *row , QWidget* parent)
: QDialog(parent)
{
ui.setupUi(this);
2023-08-15 20:12:36 +08:00
this->row=row;
2023-08-13 12:16:22 +08:00
SQLiteHelper sqlite_helper;
2023-08-18 19:09:55 +08:00
setFixedSize(450, 600);
2023-08-15 20:12:36 +08:00
Record2 record;
2023-08-13 12:16:22 +08:00
setWindowFlags(Qt::WindowCloseButtonHint);
form = new QFormLayout;
name = new QLineEdit;
2023-08-21 15:10:19 +08:00
if(isEdit)
2023-08-13 12:16:22 +08:00
{
2023-08-21 15:10:19 +08:00
record=*row;//最初为啥这么写我也忘了
2023-08-13 12:16:22 +08:00
}
2023-08-21 15:10:19 +08:00
2023-08-15 20:12:36 +08:00
if (record.orig_name.isEmpty())
2023-08-13 12:16:22 +08:00
{
2023-08-18 19:09:55 +08:00
path = new QPushButton(QString::fromLocal8Bit("浏览"));
2023-08-13 12:16:22 +08:00
connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe);
2023-08-18 19:09:55 +08:00
form->addRow(QString::fromLocal8Bit("路径"), path);
2023-08-21 15:10:19 +08:00
if (isEdit)
{
sqlite_helper.get_a_software(row->orig_name, &record);
name_label = new QLabel(orig_name_str);
form->addRow(QString::fromLocal8Bit("全名"), name_label);
if (!record.orig_name.isEmpty())
{
name_label->setText(record.orig_name);
}
orig_name_str = record.orig_name;
}
else
{
orig_name = new QLineEdit;
form->addRow(QString::fromLocal8Bit("全名"), orig_name);
}
form->addRow(QString::fromLocal8Bit("名称"), name);
2023-08-13 12:16:22 +08:00
sort = new QLineEdit;
sort->setValidator(new QIntValidator(10, 100));
2023-08-21 15:10:19 +08:00
sort->setText("50");
2023-08-18 19:09:55 +08:00
form->addRow(QString::fromLocal8Bit("排序"), sort);
2023-08-13 12:16:22 +08:00
QList<Categrory> categrories;
layout_inner = new QVBoxLayout;
2023-08-15 20:12:36 +08:00
sqlite_helper.get_category(categrories, true);
2023-08-13 12:16:22 +08:00
for (auto category : categrories)
{
QCheckBox* check_box = new QCheckBox(category.name);
2023-08-15 20:12:36 +08:00
2023-08-13 12:16:22 +08:00
layout_inner->addWidget(check_box);
categories_list.insert(category.id, check_box);
}
2023-08-18 19:09:55 +08:00
form->addRow(QString::fromLocal8Bit("类别"), layout_inner);
submit_btn = new QPushButton(QString::fromLocal8Bit("提交"));
2023-08-13 12:16:22 +08:00
form->addWidget(submit_btn);
this->setLayout(form);
connect(path, &QPushButton::click, this, &AddApp::broswer_exe);
connect(submit_btn, &QPushButton::clicked, this, &AddApp::submit);
this->isEdit = isEdit;
2023-08-15 20:12:36 +08:00
}
else
2023-08-13 12:16:22 +08:00
{
2023-08-15 20:12:36 +08:00
name->setText(record.name);
name_str = record.name;
sort_str = QString::number(record.sort);
2023-08-18 19:09:55 +08:00
path = new QPushButton(QString::fromLocal8Bit("浏览"));
2023-08-15 20:12:36 +08:00
path_str = record.exe_file;
2023-08-13 12:16:22 +08:00
path->setText(QString::fromWCharArray(fs::path(path_str.toStdWString()).filename().c_str()));
connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe);
2023-08-18 19:09:55 +08:00
if (record.op == "soft")
{
form->addRow(QString::fromLocal8Bit("路径"), path);
}
2023-08-21 15:10:19 +08:00
if (isEdit)
{
sqlite_helper.get_a_software(row->orig_name, &record);
name_label = new QLabel(orig_name_str);
form->addRow(QString::fromLocal8Bit("全名"), name_label);
if (!record.orig_name.isEmpty())
{
name_label->setText(record.orig_name);
}
orig_name_str = record.orig_name;
}
else
{
orig_name = new QLineEdit;
form->addRow(QString::fromLocal8Bit("全名"), orig_name);
}
form->addRow(QString::fromLocal8Bit("名称"), name);
2023-08-13 12:16:22 +08:00
sort = new QLineEdit;
2023-08-15 20:12:36 +08:00
sort->setText(QString::number(record.sort));
2023-08-13 12:16:22 +08:00
sort->setValidator(new QIntValidator(10, 100));
2023-08-18 19:09:55 +08:00
form->addRow(QString::fromLocal8Bit("排序"), sort);
2023-08-13 12:16:22 +08:00
QList<Categrory> categrories;
layout_inner = new QVBoxLayout;
2023-08-15 20:12:36 +08:00
sqlite_helper.get_category(categrories, true);
this->categories = record.categories;
2023-08-13 12:16:22 +08:00
for (auto category : categrories)
{
QCheckBox* check_box = new QCheckBox(category.name);
2023-08-15 20:12:36 +08:00
if (record.categories[category.id - 1])
2023-08-13 12:16:22 +08:00
{
check_box->setCheckState(Qt::Checked);
}
layout_inner->addWidget(check_box);
categories_list.insert(category.id, check_box);
}
2023-08-18 19:09:55 +08:00
form->addRow(QString::fromLocal8Bit("类别"), layout_inner);
submit_btn = new QPushButton(QString::fromLocal8Bit("提交"));
2023-08-13 12:16:22 +08:00
form->addWidget(submit_btn);
this->setLayout(form);
connect(path, &QPushButton::click, this, &AddApp::broswer_exe);
connect(submit_btn, &QPushButton::clicked, this, &AddApp::submit);
this->isEdit = isEdit;
}
}
AddApp::~AddApp()
{
//delete categories;
delete name;
if(isEdit)
{
delete name_label;
}else
{
delete orig_name;
}
delete path;
2023-08-15 20:12:36 +08:00
//delete sort;
2023-08-13 12:16:22 +08:00
delete submit_btn;
delete form;
}
void AddApp::broswer_exe(){
2023-08-15 11:17:09 +08:00
QString path_str_old = path_str;
if(path_str.isEmpty())
{
2023-08-18 19:09:55 +08:00
path_str = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择程序"),
QApplication::applicationDirPath(), QString::fromLocal8Bit("应用程序 (*.exe)"));
2023-08-15 11:17:09 +08:00
}
else
{
2023-08-18 19:09:55 +08:00
path_str = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("选择程序"),
path_str, QString::fromLocal8Bit("应用程序 (*.exe)"));
2023-08-15 11:17:09 +08:00
}
if (path_str.isEmpty()) {
path_str = path_str_old;
}
2023-08-13 12:16:22 +08:00
fs::path file(path_str.toStdWString());
path->setText(QString::fromStdWString(file.filename().c_str()));
2023-08-21 15:10:19 +08:00
if(!isEdit)
{
orig_name->setText(QString::fromStdWString(file.filename().c_str()));
}
2023-08-13 12:16:22 +08:00
}
void AddApp::submit() {
2023-08-18 20:51:05 +08:00
if(sort->text().toInt()<10||sort->text().toInt()>100)
{
QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("排序必须在10-100之间"));
return;
}
2023-08-18 19:09:55 +08:00
if (isEdit)
2023-08-13 12:16:22 +08:00
{
2023-08-18 19:09:55 +08:00
QString name_str_old = name_str;
QString sort_str_old = sort_str;
name_str = name->text();
sort_str = sort->text();
if (name_str.isEmpty()) {
name_str = name_str_old;
2023-08-15 11:17:09 +08:00
}
2023-08-18 19:09:55 +08:00
if (sort_str.isEmpty()) {
sort_str = sort_str_old;
2023-08-15 11:17:09 +08:00
}
2023-08-15 20:12:36 +08:00
}
else
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
if ((!orig_name->text().isEmpty()) && (!path->text().isEmpty()))
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
orig_name_str = orig_name->text();
if (name->text().isEmpty()) {
name_str = orig_name_str;
}
2023-08-15 20:12:36 +08:00
else {
name_str = name->text();
}
2023-08-15 11:17:09 +08:00
sort_str = sort->text();
2023-08-13 12:16:22 +08:00
}
else
{
2023-08-18 19:09:55 +08:00
QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("未填写全名或路径"));
2023-08-15 11:17:09 +08:00
return;
2023-08-13 12:16:22 +08:00
}
}
2023-08-15 20:12:36 +08:00
categories = new bool[8];
2023-08-18 19:09:55 +08:00
for (int i = 0; i < CATEGORIES_NUM; i++) {
2023-08-15 20:12:36 +08:00
categories[i] = false;
}
categories[ALL-1] = true;
2023-08-15 11:17:09 +08:00
for(auto key:categories_list.keys())
{
categories[key - 1] = categories_list[key]->checkState();
}
2023-08-15 20:12:36 +08:00
2023-08-18 19:09:55 +08:00
for (int i = 0; i < CATEGORIES_NUM; i++) {
2023-08-15 11:17:09 +08:00
qDebug() << categories[i];
}
2023-08-13 12:16:22 +08:00
changed = true;
close();
}