OfficeAssistant_Win10/OfficeAssistant_msvc/applicationmanagerpage.cpp

221 lines
7.5 KiB
C++
Raw Normal View History

2023-08-18 19:09:55 +08:00
#include "applicationmanagerpage.h"
2023-08-13 12:16:22 +08:00
#include <QSvgRenderer>
#include <QPainter>
2023-08-15 11:17:09 +08:00
#include <ctime>
2023-08-13 12:16:22 +08:00
#include "applicationmanager.h"
#include "config.h"
2023-08-15 11:17:09 +08:00
#include "mainscreen.h"
#include "mainwindowlayout.h"
2023-08-13 12:16:22 +08:00
ApplicationManagerPage::ApplicationManagerPage(int category, ApplicationManager *application_manager,QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->category = category;
2023-08-18 19:09:55 +08:00
this->parent = parent;
2023-08-13 12:16:22 +08:00
this->application_manager = application_manager;
sqlite_helper.get_buttons(button_structs);
layout = new QVBoxLayout;
sqlite_helper.get_all_software(softwares, category);
table = new QTableWidget(this);
2023-08-18 19:09:55 +08:00
table->setColumnCount(9);
2023-08-13 12:16:22 +08:00
QStringList titles;
2023-08-18 19:09:55 +08:00
titles<< QString::fromLocal8Bit("图标") << QString::fromLocal8Bit("名称") << QString::fromLocal8Bit("全名") << QString::fromLocal8Bit("开发者") << QString::fromLocal8Bit("最后使用日期")<<QString::fromLocal8Bit("排序") << QString::fromLocal8Bit("添加到导航条") <<QString::fromLocal8Bit("设置") << QString::fromLocal8Bit("打开");
2023-08-13 12:16:22 +08:00
table->setHorizontalHeaderLabels(titles);
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->setStyleSheet("QTableWidget{border:1px;outline:1px;align: center;} QQTableWidget::widget QTableWidget::item {text-align: center;}");
table->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
int i = 0;
2023-08-18 19:09:55 +08:00
for (i = 0; i < 9; i++)
2023-08-13 12:16:22 +08:00
{
table->horizontalHeader()->setSectionResizeMode(i, QHeaderView::Stretch);
}
i = 0;
QTableWidgetItem* item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignCenter);
table->setItemPrototype(item);
layout->addWidget(table);
setLayout(layout);
}
ApplicationManagerPage::~ApplicationManagerPage()
{
for (auto button : buttons)
{
delete button;
}
delete table;
delete layout;
}
2023-08-18 19:09:55 +08:00
void ApplicationManagerPage::onclicked(int state)
2023-08-13 12:16:22 +08:00
{
for(int i=0;i<table->rowCount();i++)
{
2023-08-18 19:09:55 +08:00
if(((QCheckBox *)table->cellWidget(i,6))->checkState()==Qt::Checked)
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
if(!checked[table->item(i,2)->text()])
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
QString orig_name = table->item(i, 2)->text();
bool ok=sqlite_helper.edit_software(table->item(i, 1)->text(), orig_name, rows[orig_name].exe_file, QString::number(rows[orig_name].sort), rows[orig_name].categories,"navbar");
2023-08-13 12:16:22 +08:00
if(!ok)
{
2023-08-18 19:09:55 +08:00
QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("更新部分或全部失败"));
2023-08-13 12:16:22 +08:00
break;
}
2023-08-18 19:09:55 +08:00
checked[table->item(i, 2)->text()] = true;
2023-08-13 12:16:22 +08:00
}
}else
{
2023-08-15 11:17:09 +08:00
if (checked[table->item(i, 2)->text()])
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
QString orig_name = table->item(i, 2)->text();
bool ok = sqlite_helper.edit_software(table->item(i, 1)->text(), orig_name, rows[orig_name].exe_file, QString::number(rows[orig_name].sort), rows[orig_name].categories, "app");
2023-08-13 12:16:22 +08:00
if (!ok)
{
2023-08-18 19:09:55 +08:00
QMessageBox::critical(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("更新部分或全部失败"));
2023-08-13 12:16:22 +08:00
break;
}
2023-08-18 19:09:55 +08:00
checked[table->item(i, 2)->text()] = false;
2023-08-13 12:16:22 +08:00
}
}
}
2023-08-18 19:09:55 +08:00
//在后面refresh避免因为refresh导致信号和槽断开
2023-08-13 12:16:22 +08:00
}
2023-08-18 19:09:55 +08:00
//刷新页面
2023-08-13 12:16:22 +08:00
void ApplicationManagerPage::refresh()
{
2023-08-21 15:10:19 +08:00
for(auto button:application_manager->buttons)
{
button->update();
}
2023-08-13 12:16:22 +08:00
int rowcount = table->rowCount();
2023-08-18 19:09:55 +08:00
for(auto i : connections)
{
disconnect(i);
}
2023-08-13 12:16:22 +08:00
for(int i=0;i<rowcount;i++)
{
table->removeRow(0);
}
2023-08-15 20:12:36 +08:00
2023-08-13 12:16:22 +08:00
softwares.clear();
sqlite_helper.get_all_software(softwares, category);
int i = 0;
for (auto software : softwares)
{
table->insertRow(i);
2023-08-15 11:17:09 +08:00
table->setItem(i, 1, new QTableWidgetItem(software.name));
table->setItem(i, 2, new QTableWidgetItem(software.orig_name));
table->setItem(i, 3, new QTableWidgetItem(software.dev));
2023-08-13 12:16:22 +08:00
QString date;
std::tm* time;
2023-08-15 11:17:09 +08:00
if(software.use_time!=0)
{
time = std::localtime(&software.use_time);
date = QString::number(1900 + time->tm_year, 10);
date += "/";
date += QString::number(1 + time->tm_mon, 10);
date += "/";
date += QString::number(time->tm_mday, 10);
table->setItem(i, 4, new QTableWidgetItem(date));
}
2023-08-18 19:09:55 +08:00
else
{
table->setItem(i, 4, new QTableWidgetItem(QString::fromLocal8Bit("从未使用")));
}
2023-08-13 12:16:22 +08:00
Record2 record2;
2023-08-15 11:17:09 +08:00
record2.name = software.name;
2023-08-18 19:09:55 +08:00
record2.icon = new QLabel;
QFile file(software.img);
record2.icon->setMaximumSize(50, 50);
record2.icon->setScaledContents(true);
2023-08-15 11:17:09 +08:00
if (file.exists())
{
2023-08-18 19:09:55 +08:00
QPixmap pixmap(software.img);
//pixmap.scaled(40, 40);
record2.icon->setPixmap(pixmap);
2023-08-15 11:17:09 +08:00
}
else {
2023-08-18 19:09:55 +08:00
QPixmap pixmap(QApplication::applicationDirPath() + DEFAULT_PNG);
//pixmap.scaled(40, 40);
record2.icon->setPixmap(pixmap);
2023-08-15 11:17:09 +08:00
}
2023-08-18 19:09:55 +08:00
2023-08-15 11:17:09 +08:00
table->setCellWidget(i, 0, record2.icon);
2023-08-21 15:10:19 +08:00
if(!software.locked)
{
record2.settings = new MiniButton(software.orig_name, "settings");
record2.settings->setText(QString::fromLocal8Bit("设置"));
record2.settings->setMaximumSize(60, 40);
QMetaObject::Connection connect1 = connect(record2.settings, &MiniButton::clicked, record2.settings, &MiniButton::onclick2);
connections << connect1;
QMetaObject::Connection connect2 = connect(record2.settings, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
connections << connect2;
table->setCellWidget(i, 7, record2.settings);
}
2023-08-13 12:16:22 +08:00
record2.open = new MiniButton(software.orig_name, "open");
2023-08-18 19:09:55 +08:00
record2.open->setText(QString::fromLocal8Bit("打开"));
2023-08-15 11:17:09 +08:00
record2.open->setMaximumSize(60, 40);
2023-08-18 19:09:55 +08:00
QMetaObject::Connection connect3=connect(record2.open, &MiniButton::clicked, record2.open, &MiniButton::onclick2);
connections << connect3;
QMetaObject::Connection connect4=connect(record2.open, &MiniButton::click2, application_manager, &ApplicationManager::onclick2);
connections << connect4;
2023-08-13 12:16:22 +08:00
record2.exe_file = software.path;
record2.orig_name = software.orig_name;
2023-08-15 11:17:09 +08:00
record2.url = software.url;
2023-08-18 19:09:55 +08:00
for (int i = 0; i < CATEGORIES_NUM; i++)
2023-08-13 12:16:22 +08:00
{
record2.categories[i] = false;
}
2023-08-18 19:09:55 +08:00
for (int i = 0; i < CATEGORIES_NUM; i++)
2023-08-13 12:16:22 +08:00
{
2023-08-15 11:17:09 +08:00
if (software.category_id.contains(QString::number(i+1)))
2023-08-13 12:16:22 +08:00
{
record2.categories[i] = true;
}
}
2023-08-15 20:12:36 +08:00
record2.op = software.op;
record2.func = software.func;
2023-08-18 19:09:55 +08:00
QMetaObject::Connection connect5 = connect(this, &ApplicationManagerPage::onclick3, (MainWindowLayout*)((MainScreen*)application_manager->parent)->parent, &MainWindowLayout::clickButton);
connections<<connect5;
2023-08-13 12:16:22 +08:00
record2.sort = software.sort;
record2.check_box = new QCheckBox;
2023-08-18 19:09:55 +08:00
if (software.is_navbar)
2023-08-13 12:16:22 +08:00
{
record2.check_box->setCheckState(Qt::Checked);
checked.insert(software.orig_name, true);
}
else
{
record2.check_box->setCheckState(Qt::Unchecked);
checked.insert(software.orig_name, false);
}
2023-08-18 19:09:55 +08:00
if(software.locked)
{
record2.check_box->setEnabled(false);
}
else
{
record2.check_box->setEnabled(true);
}
QMetaObject::Connection connect6 = connect(record2.check_box, &QCheckBox::stateChanged, this, &ApplicationManagerPage::onclicked);
connections << connect6;
QMetaObject::Connection connect7 = connect(record2.check_box, &QCheckBox::stateChanged, mainWindowLayout->navBar, &NavBar::refresh);
connections << connect7;
table->setItem(i, 5, new QTableWidgetItem(QString::number(software.sort)));
2023-08-15 11:17:09 +08:00
QTableWidgetItem* check_box_item = new QTableWidgetItem;
2023-08-18 19:09:55 +08:00
table->setItem(i, 6, check_box_item);
table->setCellWidget(i, 6, record2.check_box);
2023-08-15 11:17:09 +08:00
check_box_item->setTextAlignment(Qt::AlignCenter);
2023-08-18 19:09:55 +08:00
table->setCellWidget(i, 8, record2.open);
2023-08-13 12:16:22 +08:00
rows.insert(software.orig_name, record2);
2023-08-15 11:17:09 +08:00
table->resizeColumnToContents(0);
2023-08-13 12:16:22 +08:00
i++;
}
}