#include "File_Gather_Widget.h" #include namespace Flex_Qt { void File_Gather_Widget::append(const std::uint8_t* data, size_t size) { if (!init) { Message::show( Message::error, "缓冲区还未初始化", this, 3000, Message::Top_Right); return; } file_gather.append(data, size); value->set_text(QString::fromStdString(file_gather.append_value.to_json().to_json_string())); speed->set_text(QString::fromStdString(file_gather.append_speed.to_json().to_json_string())); } File_Gather_Widget::Config::Config() { buffer_size = 100000; log_path = ""; } Psc::JSON File_Gather_Widget::Config::to_json() { return VAR_JSON_2(buffer_size, log_path); } void File_Gather_Widget::Config::from_json(Psc::JSON* that_json) { Get_J(buffer_size) Get_J(log_path) } // 缓存文件目录 和 文件缓冲区大小 File_Gather_Widget::File_Gather_Widget(std::string_view id) : Base_Cache(id) { auto layout = new QVBoxLayout(this); value = new Text_Shower(); speed = new Text_Shower(); value->set_title("值"); speed->set_title("速度"); path_select = new File_Tool("采集文件"); size_select = new Byte_Select(); size_select->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); size_select->set_number_value(0); size_select->set_title("缓冲区大小"); confirm_button = new QPushButton("确定"); layout->addWidget(path_select); layout->addWidget(size_select); layout->addWidget(value); layout->addWidget(speed); layout->addWidget(confirm_button); layout->addStretch(); QObject::connect(confirm_button, &QPushButton::clicked, [this]() { if (init) { Message::show( Message::warning, "已经初始化完成过", this, 3000, Message::Top_Right); return; } QString qpath = path_select->path(); if (qpath.isEmpty()) { Message::show( Message::error, "路径不能为空", this, 3000, Message::Top_Right); return; } std::filesystem::path path = qpath.toStdString(); try { // 如果是文件路径 std::filesystem::path parent = path.parent_path(); if (!parent.empty() && !std::filesystem::exists(parent)) { if (!std::filesystem::create_directories(parent)) { Message::show( Message::error, "目录创建失败", this, 3000, Message::Top_Right); return; } } // 如果文件不存在就创建 if (!std::filesystem::exists(path)) { std::ofstream ofs(path.string(), std::ios::binary); if (!ofs) { Message::show( Message::error, "文件创建失败", this, 3000, Message::Top_Right); return; } } } catch (const std::exception& e) { Message::show( Message::error, QString("路径错误: ") + e.what(), this, 3000, Message::Top_Right); return; } init = false; file_gather.init(path.string(), size_select->value()); init = true; Message::show( Message::success, "初始化成功", this, 2000, Message::Top_Right); // 保存初始化成功的配置文件 save(); }); auto json_file = get_config_path(); Config config; if (!std::filesystem::exists(json_file)) { save(config); } auto tt = Psc::try_parse_json_file(json_file).and_then([&config](Psc::JSON&& json) -> Psc::expected { config.from_json(&json); return {}; }); if (!tt) { Message::show( Message::success, "配置文件加载错误! " + QString::fromStdString(json_file), this, 2000, Message::Top_Right); } size_select->set_number_value(config.buffer_size); path_select->set_path(QString::fromStdString(config.log_path)); } void File_Gather_Widget::save() { Config config; config.buffer_size = size_select->value(); config.log_path = path_select->path().toStdString(); save(config); } void File_Gather_Widget::save(Config& config) { auto json_file = get_config_path(); std::ofstream ofs(json_file, std::ios::out | std::ios::trunc); ofs << config.to_json().to_json_string(); ofs.close(); } void File_Gather_Widget::contextMenuEvent(QContextMenuEvent* event) { QWidget::contextMenuEvent(event); } }