130 lines
2.9 KiB
C++
130 lines
2.9 KiB
C++
|
|
#include "toolBarConfig.h"
|
||
|
|
#include <QFile>
|
||
|
|
#include <QJsonDocument>
|
||
|
|
#include <QJsonArray>
|
||
|
|
#include <QJsonObject>
|
||
|
|
#include <QDebug>
|
||
|
|
|
||
|
|
ToolBarConfig::ToolBarConfig(QObject *parent)
|
||
|
|
: QObject(parent)
|
||
|
|
{
|
||
|
|
addDefaultTools();
|
||
|
|
}
|
||
|
|
|
||
|
|
void ToolBarConfig::addDefaultTools()
|
||
|
|
{
|
||
|
|
// 添加基本工具作为后备
|
||
|
|
m_tools["image"] = ToolInfo("image", "图像", ":/images/element/icon_image.png");
|
||
|
|
m_tools["text"] = ToolInfo("text", "文本", ":/images/element/icon_text.png");
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ToolBarConfig::loadFromFile(const QString &filePath)
|
||
|
|
{
|
||
|
|
QFile file(filePath);
|
||
|
|
if (!file.exists()) {
|
||
|
|
qDebug() << "配置文件不存在:" << filePath;
|
||
|
|
return false; // 文件不存在,使用默认工具
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||
|
|
qWarning() << "无法打开配置文件:" << filePath;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QByteArray data = file.readAll();
|
||
|
|
file.close();
|
||
|
|
|
||
|
|
return loadFromJson(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ToolBarConfig::loadFromJson(const QByteArray &jsonData)
|
||
|
|
{
|
||
|
|
QJsonParseError error;
|
||
|
|
QJsonDocument doc = QJsonDocument::fromJson(jsonData, &error);
|
||
|
|
|
||
|
|
if (error.error != QJsonParseError::NoError) {
|
||
|
|
qWarning() << "JSON解析错误:" << error.errorString();
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!doc.isArray()) {
|
||
|
|
qWarning() << "配置文件格式错误: 应为数组";
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 清空现有工具,但保留默认工具
|
||
|
|
m_tools.clear();
|
||
|
|
addDefaultTools();
|
||
|
|
|
||
|
|
// 加载配置文件中的工具
|
||
|
|
QJsonArray toolArray = doc.array();
|
||
|
|
for (const QJsonValue &value : toolArray) {
|
||
|
|
if (!value.isObject()) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
ToolInfo info = parseTool(value.toObject());
|
||
|
|
if (!info.type.isEmpty()) {
|
||
|
|
m_tools[info.type] = info;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
ToolInfo ToolBarConfig::parseTool(const QJsonObject &json)
|
||
|
|
{
|
||
|
|
ToolInfo info;
|
||
|
|
info.type = json["type"].toString();
|
||
|
|
info.name = json["name"].toString();
|
||
|
|
info.iconPath = json["iconPath"].toString();
|
||
|
|
|
||
|
|
if (json.contains("properties")) {
|
||
|
|
QJsonObject props = json["properties"].toObject();
|
||
|
|
for (auto it = props.begin(); it != props.end(); ++it) {
|
||
|
|
info.properties[it.key()] = it->toVariant();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return info;
|
||
|
|
}
|
||
|
|
|
||
|
|
ToolInfo ToolBarConfig::getTool(const QString &type) const
|
||
|
|
{
|
||
|
|
return m_tools.value(type);
|
||
|
|
}
|
||
|
|
|
||
|
|
QList<ToolInfo> ToolBarConfig::getAllTools() const
|
||
|
|
{
|
||
|
|
return m_tools.values();
|
||
|
|
}
|
||
|
|
|
||
|
|
QStringList ToolBarConfig::getToolTypes() const
|
||
|
|
{
|
||
|
|
return m_tools.keys();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ToolBarConfig::contains(const QString &type) const
|
||
|
|
{
|
||
|
|
return m_tools.contains(type);
|
||
|
|
}
|
||
|
|
|
||
|
|
QIcon ToolInfo::getIcon() const
|
||
|
|
{
|
||
|
|
if (iconPath.isEmpty()) {
|
||
|
|
return QIcon();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 尝试从资源文件加载
|
||
|
|
if (iconPath.startsWith(":/")) {
|
||
|
|
return QIcon(iconPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 尝试从文件系统加载
|
||
|
|
if (QFile::exists(iconPath)) {
|
||
|
|
return QIcon(iconPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
return QIcon();
|
||
|
|
}
|