2024-05-15 08:34:54 +08:00
|
|
|
#include "MainWidget.h"
|
|
|
|
#include "./ui_MainWidget.h"
|
|
|
|
|
2024-05-15 10:59:43 +08:00
|
|
|
MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget)
|
2024-05-15 08:34:54 +08:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2024-05-15 10:59:43 +08:00
|
|
|
|
2024-05-15 13:17:24 +08:00
|
|
|
setWindowTitle(u8"OneLevelXmlOpr v1.0.0");
|
2024-05-15 12:10:32 +08:00
|
|
|
setWindowIcon(QIcon("://resource/xml.ico"));
|
2024-05-15 11:51:30 +08:00
|
|
|
|
2024-05-15 10:59:43 +08:00
|
|
|
width_.push_back(280);
|
|
|
|
for (int i = 0; i < 30; ++i) {
|
|
|
|
width_.push_back(160);
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:17:24 +08:00
|
|
|
ui->edStatus->setEnabled(false);
|
|
|
|
ui->btnSave->setEnabled(false);
|
|
|
|
|
2024-05-15 10:59:43 +08:00
|
|
|
connect(ui->btnRead, &QPushButton::clicked, this, [&]() { read(); });
|
|
|
|
connect(ui->btnSearch, &QPushButton::clicked, this, [&]() { search(); });
|
2024-05-15 13:17:24 +08:00
|
|
|
connect(ui->btnSave, &QPushButton::clicked, this, [&]() { save(); });
|
2024-05-15 08:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWidget::~MainWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2024-05-15 10:59:43 +08:00
|
|
|
|
|
|
|
void MainWidget::set_work_exe(char* path)
|
|
|
|
{
|
|
|
|
exe_path_.clear();
|
|
|
|
exe_path_.append(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::generate_table_widget()
|
|
|
|
{
|
|
|
|
tab_widget_ = new QTableWidget();
|
2024-05-15 13:17:24 +08:00
|
|
|
connect(tab_widget_, &QTableWidget::itemChanged, this, [&](QTableWidgetItem* item) { item_changed_handle(item); });
|
|
|
|
|
2024-05-15 10:59:43 +08:00
|
|
|
auto config = ini_.get_config();
|
|
|
|
auto keys = splitString(config.purpose, ",");
|
|
|
|
keys_.clear();
|
|
|
|
QStringList list;
|
|
|
|
for (const auto& item : keys) {
|
|
|
|
if (item.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
keys_.push_back(item);
|
|
|
|
list.append(QString::fromStdString(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
tab_widget_->setColumnCount(list.size());
|
|
|
|
tab_widget_->setHorizontalHeaderLabels(list);
|
|
|
|
|
|
|
|
for (auto i = 0; i < keys.size(); ++i) {
|
|
|
|
tab_widget_->setColumnWidth(i, width_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHBoxLayout* lay = new QHBoxLayout();
|
|
|
|
lay->addWidget(tab_widget_);
|
|
|
|
ui->widget->setLayout(lay);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles)
|
|
|
|
{
|
|
|
|
tab_widget_->clearContents();
|
|
|
|
tab_widget_->setRowCount(0);
|
|
|
|
for (const auto& ele : eles) {
|
|
|
|
int row_cnt = tab_widget_->rowCount();
|
|
|
|
tab_widget_->insertRow(row_cnt);
|
|
|
|
for (auto i = 0; i < keys_.size(); ++i) {
|
|
|
|
const char* data = ele->Attribute(keys_[i].c_str());
|
|
|
|
QTableWidgetItem* wgItem = new QTableWidgetItem();
|
2024-05-15 11:51:30 +08:00
|
|
|
|
|
|
|
if (i == 0) {
|
2024-05-15 13:17:24 +08:00
|
|
|
wgItem->setFlags(wgItem->flags() & ~Qt::ItemIsEditable);
|
2024-05-15 11:51:30 +08:00
|
|
|
}
|
|
|
|
|
2024-05-15 10:59:43 +08:00
|
|
|
wgItem->setText(QString(data));
|
|
|
|
tab_widget_->setItem(row_cnt, i, wgItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::read()
|
|
|
|
{
|
|
|
|
if (!ini_.set_work_exe(exe_path_)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
OprBase base = ini_.get_config();
|
|
|
|
if (!xml_.open(base.xml_path)) {
|
2024-05-15 13:17:24 +08:00
|
|
|
QString status = u8"Open【" + QString::fromStdString(base.xml_path.c_str()) + u8"】Failed.";
|
2024-05-15 10:59:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
xml_.set_baseinfo(base);
|
|
|
|
if (!xml_.parse_xml(vec_)) {
|
2024-05-15 13:17:24 +08:00
|
|
|
QString status = u8"parse_xml【" + QString::fromStdString(base.xml_path.c_str()) + u8"】Failed.";
|
2024-05-15 10:59:43 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
|
|
|
|
auto_add_ = true;
|
2024-05-15 10:59:43 +08:00
|
|
|
generate_table_widget();
|
|
|
|
push_content(vec_);
|
2024-05-15 13:17:24 +08:00
|
|
|
current_ = vec_;
|
|
|
|
auto_add_ = false;
|
2024-05-15 11:51:30 +08:00
|
|
|
|
2024-05-15 13:17:24 +08:00
|
|
|
QString status = u8"Open And Parse【" + QString::fromLocal8Bit(base.xml_path.c_str()) + u8"】Success.";
|
2024-05-15 11:51:30 +08:00
|
|
|
ui->edStatus->setText(status);
|
2024-05-15 13:17:24 +08:00
|
|
|
|
|
|
|
ui->btnRead->setEnabled(false);
|
|
|
|
ui->btnSave->setEnabled(true);
|
2024-05-15 10:59:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::search()
|
|
|
|
{
|
|
|
|
QString key = ui->edSearchKey->text().trimmed();
|
|
|
|
if (key.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
if (tab_widget_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_.clear();
|
2024-05-15 10:59:43 +08:00
|
|
|
for (const auto& item : vec_) {
|
|
|
|
for (auto i = 0; i < keys_.size(); ++i) {
|
2024-05-15 13:17:24 +08:00
|
|
|
const char* data = item->Attribute(keys_[i].c_str());
|
|
|
|
QString qdata(data);
|
2024-05-15 10:59:43 +08:00
|
|
|
if (!qdata.contains(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
current_.push_back(item);
|
2024-05-15 10:59:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
push_content(current_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::item_changed_handle(QTableWidgetItem* item)
|
|
|
|
{
|
|
|
|
if (auto_add_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (item == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int row = item->row();
|
|
|
|
int col = item->column();
|
|
|
|
|
|
|
|
QString xml_key = tab_widget_->item(row, 0)->text();
|
|
|
|
for (const auto& ele : current_) {
|
|
|
|
const char* data = ele->Attribute(keys_[0].c_str());
|
|
|
|
QString qdata(data);
|
|
|
|
if (qdata != xml_key) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ele->SetAttribute(keys_[col].c_str(), item->text().toLocal8Bit().constData());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::save()
|
|
|
|
{
|
|
|
|
if (xml_.save()) {
|
|
|
|
QMessageBox::information(this, u8"提示", u8"保存成功");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QMessageBox::information(this, u8"提示", u8"保存失败");
|
|
|
|
}
|
2024-05-15 10:59:43 +08:00
|
|
|
}
|