OfficeAssistant_Win10/OfficeAssistant_msvc/addapp.cpp

190 lines
5.1 KiB
C++
Raw Normal View History

2023-08-13 12:16:22 +08:00
#include "addapp.h"
#include <QFileDialog>
#include <QProcessEnvironment>
#include <QMessageBox>
#include <sqlitehelper.h>
#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);
SQLiteHelper sqlite_helper;
setFixedSize(450, 450);
setWindowFlags(Qt::WindowCloseButtonHint);
form = new QFormLayout;
name = new QLineEdit;
form->addRow(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), name);
if(isEdit)
{
name_label = new QLabel(orig_name_str);
form->addRow(QString::fromLocal8Bit("ȫ<EFBFBD><EFBFBD>"), name_label);
2023-08-15 11:17:09 +08:00
if(row!=nullptr)
{
name_label->setText(row->orig_name);
}
2023-08-13 12:16:22 +08:00
}
else
{
orig_name = new QLineEdit;
form->addRow(QString::fromLocal8Bit("ȫ<EFBFBD><EFBFBD>"), orig_name);
}
if(row==nullptr)
{
path = new QPushButton(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
connect(path, &QPushButton::clicked, this, &AddApp::broswer_exe);
form->addRow(QString::fromLocal8Bit("·<EFBFBD><EFBFBD>"), path);
sort = new QLineEdit;
sort->setValidator(new QIntValidator(10, 100));
form->addRow(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), sort);
QList<Categrory> categrories;
layout_inner = new QVBoxLayout;
2023-08-15 11:17:09 +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);
layout_inner->addWidget(check_box);
categories_list.insert(category.id, check_box);
}
form->addRow(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), layout_inner);
submit_btn = new QPushButton(QString::fromLocal8Bit("<EFBFBD>"));
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;
}else
{
2023-08-15 11:17:09 +08:00
name->setText(row->name);
name_str=row->name;
sort_str=QString::number(row->sort);
2023-08-13 12:16:22 +08:00
path = new QPushButton(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"));
path_str = row->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("·<EFBFBD><EFBFBD>"), path);
sort = new QLineEdit;
sort->setText(QString::number(row->sort));
sort->setValidator(new QIntValidator(10, 100));
form->addRow(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), sort);
QList<Categrory> categrories;
layout_inner = new QVBoxLayout;
2023-08-15 11:17:09 +08:00
sqlite_helper.get_category(categrories,true);
categories = row->categories;
2023-08-13 12:16:22 +08:00
for (auto category : categrories)
{
QCheckBox* check_box = new QCheckBox(category.name);
2023-08-15 11:17:09 +08:00
if(row->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);
}
form->addRow(QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), layout_inner);
submit_btn = new QPushButton(QString::fromLocal8Bit("<EFBFBD>"));
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;
delete sort;
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())
{
path_str = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
QApplication::applicationDirPath(), QString::fromLocal8Bit("Ӧ<EFBFBD>ó<EFBFBD><EFBFBD><EFBFBD> (*.exe)"));
}
else
{
path_str = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"),
path_str, QString::fromLocal8Bit("Ӧ<EFBFBD>ó<EFBFBD><EFBFBD><EFBFBD> (*.exe)"));
}
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-15 11:17:09 +08:00
2023-08-13 12:16:22 +08:00
}
void AddApp::submit() {
2023-08-15 11:17:09 +08:00
if(isEdit)
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
if (!path->text().isEmpty())
{
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;
}
if (sort_str.isEmpty()) {
sort_str = sort_str_old;
}
}
else
{
QMessageBox::critical(this, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("δ<EFBFBD><EFBFBD>д·<EFBFBD><EFBFBD>"));
return;
}
}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;
}
name_str = name->text();
sort_str = sort->text();
2023-08-13 12:16:22 +08:00
}
else
{
2023-08-15 11:17:09 +08:00
QMessageBox::critical(this, QString::fromLocal8Bit("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"), QString::fromLocal8Bit("δ<EFBFBD><EFBFBD>дȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD>"));
return;
2023-08-13 12:16:22 +08:00
}
}
2023-08-15 11:17:09 +08:00
for(auto key:categories_list.keys())
{
categories[key - 1] = categories_list[key]->checkState();
}
for (int i = 0; i < 8; i++) {
qDebug() << categories[i];
}
2023-08-13 12:16:22 +08:00
changed = true;
close();
}