颜色修改红绿,修复帧错误卡顿bug
This commit is contained in:
@@ -42,6 +42,12 @@
|
||||
#include <QLineEdit>
|
||||
#include <QSpinBox>
|
||||
#include <QVBoxLayout>
|
||||
#include <QFileDialog>
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QMessageBox>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace repest_literals {
|
||||
@@ -66,6 +72,61 @@ namespace fbpro = filled_button::pro;
|
||||
static std::weak_ptr<MutableValue<std::vector<ConfigProfile>>> g_profiles_store;
|
||||
static std::function<void()> g_profiles_refresh;
|
||||
|
||||
static QString TactileTypeToJsonString(Tactile_TYPE type)
|
||||
{
|
||||
switch (type) {
|
||||
case Tactile_TYPE::PiezoresistiveA:
|
||||
return QStringLiteral("PiezoresistiveA");
|
||||
case Tactile_TYPE::PiezoresistiveB:
|
||||
return QStringLiteral("PiezoresistiveB");
|
||||
case Tactile_TYPE::Hall:
|
||||
default:
|
||||
return QStringLiteral("Hall");
|
||||
}
|
||||
}
|
||||
|
||||
static Tactile_TYPE TactileTypeFromJsonString(const QString& str)
|
||||
{
|
||||
if (str == QStringLiteral("PiezoresistiveA")) {
|
||||
return Tactile_TYPE::PiezoresistiveA;
|
||||
}
|
||||
if (str == QStringLiteral("PiezoresistiveB")) {
|
||||
return Tactile_TYPE::PiezoresistiveB;
|
||||
}
|
||||
return Tactile_TYPE::Hall;
|
||||
}
|
||||
|
||||
static QJsonObject ConfigProfileToJson(const ConfigProfile& profile)
|
||||
{
|
||||
QJsonObject obj;
|
||||
obj.insert(QStringLiteral("name"), profile.name);
|
||||
obj.insert(QStringLiteral("type"), TactileTypeToJsonString(profile.type));
|
||||
obj.insert(QStringLiteral("matrix_width"), profile.matrix_width);
|
||||
obj.insert(QStringLiteral("matrix_height"), profile.matrix_height);
|
||||
obj.insert(QStringLiteral("range_left"), profile.range_left);
|
||||
obj.insert(QStringLiteral("range_right"), profile.range_right);
|
||||
obj.insert(QStringLiteral("baud_rate"), profile.baud_rate);
|
||||
return obj;
|
||||
}
|
||||
|
||||
static bool ConfigProfileFromJson(const QJsonObject& obj, ConfigProfile& out_profile)
|
||||
{
|
||||
const auto name = obj.value(QStringLiteral("name")).toString();
|
||||
if (name.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
out_profile.name = name;
|
||||
out_profile.type = TactileTypeFromJsonString(obj.value(QStringLiteral("type")).toString());
|
||||
out_profile.matrix_width = obj.value(QStringLiteral("matrix_width")).toInt(0);
|
||||
out_profile.matrix_height = obj.value(QStringLiteral("matrix_height")).toInt(0);
|
||||
out_profile.range_left = obj.value(QStringLiteral("range_left")).toInt(0);
|
||||
out_profile.range_right = obj.value(QStringLiteral("range_right")).toInt(0);
|
||||
out_profile.baud_rate = obj.value(QStringLiteral("baud_rate")).toInt(0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void ShowEditProfileDialog(
|
||||
const ConfigProfile& current,
|
||||
const std::shared_ptr<MutableValue<std::vector<ConfigProfile>>>& profiles_store) {
|
||||
@@ -252,7 +313,75 @@ static auto ImportProfileLongItem(creeper::ThemeManager& manager) {
|
||||
widget::pro::MinimumHeight {40},
|
||||
widget::pro::MinimumWidth {320},
|
||||
fbpro::Radius {12},
|
||||
fbpro::Clickable {[]{ qDebug() << "ImportProfileLongItem"; }},
|
||||
fbpro::Clickable {[]{
|
||||
const QString file_name = QFileDialog::getOpenFileName(
|
||||
nullptr,
|
||||
QStringLiteral("导入配置"),
|
||||
QString(),
|
||||
QStringLiteral("配置文件 (*.conf);;所有文件 (*.*)"));
|
||||
if (file_name.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QFile file(file_name);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
QMessageBox::warning(nullptr, QStringLiteral("导入配置"),
|
||||
QStringLiteral("无法打开配置文件。"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QByteArray data = file.readAll();
|
||||
file.close();
|
||||
|
||||
QJsonParseError parse_error {};
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(data, &parse_error);
|
||||
if (doc.isNull() || parse_error.error != QJsonParseError::NoError || !doc.isArray()) {
|
||||
QMessageBox::warning(nullptr, QStringLiteral("导入配置"),
|
||||
QStringLiteral("配置文件格式不正确。"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonArray array = doc.array();
|
||||
std::vector<ConfigProfile> imported_profiles;
|
||||
imported_profiles.reserve(static_cast<std::size_t>(array.size()));
|
||||
for (const auto& value : array) {
|
||||
if (!value.isObject()) {
|
||||
continue;
|
||||
}
|
||||
ConfigProfile profile {};
|
||||
if (ConfigProfileFromJson(value.toObject(), profile)) {
|
||||
imported_profiles.push_back(profile);
|
||||
}
|
||||
}
|
||||
|
||||
if (imported_profiles.empty()) {
|
||||
QMessageBox::warning(nullptr, QStringLiteral("导入配置"),
|
||||
QStringLiteral("配置文件中没有有效的配置。"));
|
||||
return;
|
||||
}
|
||||
|
||||
auto& helper = GlobalHelper::instance();
|
||||
|
||||
// 清空现有配置
|
||||
const auto existing = helper.get_all_profile();
|
||||
for (const auto& p : existing) {
|
||||
helper.remove_profile(p.name);
|
||||
}
|
||||
|
||||
// 写入新配置到 ini
|
||||
for (const auto& p : imported_profiles) {
|
||||
helper.add_new_profile(p);
|
||||
}
|
||||
helper.reload_profiles();
|
||||
|
||||
if (auto store = g_profiles_store.lock()) {
|
||||
store->set(helper.get_all_profile());
|
||||
}
|
||||
RefreshProfilesForView();
|
||||
if (g_profiles_refresh) {
|
||||
g_profiles_refresh();
|
||||
}
|
||||
}},
|
||||
};
|
||||
}
|
||||
static auto ExportProfileLongItem(creeper::ThemeManager& manager) {
|
||||
@@ -263,7 +392,45 @@ static auto ExportProfileLongItem(creeper::ThemeManager& manager) {
|
||||
widget::pro::MinimumHeight {40},
|
||||
widget::pro::MinimumWidth {320},
|
||||
fbpro::Radius {12},
|
||||
fbpro::Clickable {[]{ qDebug() << "ExportProfileLongItem"; }},
|
||||
fbpro::Clickable {[]{
|
||||
QString file_name = QFileDialog::getSaveFileName(
|
||||
nullptr,
|
||||
QStringLiteral("导出配置"),
|
||||
QString(),
|
||||
QStringLiteral("配置文件 (*.conf);;所有文件 (*.*)"));
|
||||
if (file_name.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file_name.endsWith(QStringLiteral(".conf"), Qt::CaseInsensitive)) {
|
||||
file_name.append(QStringLiteral(".conf"));
|
||||
}
|
||||
|
||||
auto& helper = GlobalHelper::instance();
|
||||
helper.reload_profiles();
|
||||
const auto& profiles = helper.get_all_profile();
|
||||
|
||||
QJsonArray array;
|
||||
// array.reserve(static_cast<int>(profiles.size()));
|
||||
for (const auto& p : profiles) {
|
||||
array.append(ConfigProfileToJson(p));
|
||||
}
|
||||
|
||||
const QJsonDocument doc(array);
|
||||
|
||||
QFile file(file_name);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
QMessageBox::warning(nullptr, QStringLiteral("导出配置"),
|
||||
QStringLiteral("无法写入配置文件。"));
|
||||
return;
|
||||
}
|
||||
|
||||
file.write(doc.toJson(QJsonDocument::Indented));
|
||||
file.close();
|
||||
|
||||
QMessageBox::information(nullptr, QStringLiteral("导出配置"),
|
||||
QStringLiteral("配置导出完成。"));
|
||||
}},
|
||||
};
|
||||
}
|
||||
static auto ProfileItemComponent(creeper::ThemeManager& manager, ConfigProfile& profile,
|
||||
|
||||
Reference in New Issue
Block a user