-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuconfig.cpp
More file actions
96 lines (80 loc) · 3.17 KB
/
Copy pathmenuconfig.cpp
File metadata and controls
96 lines (80 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "menuconfig.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QDebug>
QVector<MenuConfig> MenuConfigManager::loadFromJson(const QString& filePath) {
QVector<MenuConfig> menus;
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "无法打开菜单配置文件:" << filePath;
return menus;
}
QByteArray data = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isNull()) {
qWarning() << "菜单配置文件格式错误:" << filePath;
return menus;
}
QJsonObject root = doc.object();
QJsonArray menusArray = root["menus"].toArray();
for (long long i=0;i<menusArray.size();++i) {
const QJsonValue& menuValue = menusArray[i];
QJsonObject menuObj = menuValue.toObject();
MenuConfig menu;
menu.title = menuObj["title"].toString();
menu.name = menuObj["name"].toString();
QJsonArray itemsArray = menuObj["items"].toArray();
for (long long j=0;j<itemsArray.size();++j) {
const QJsonValue& itemValue = itemsArray[j];
QJsonObject itemObj = itemValue.toObject();
// 如果是分隔符
if (itemObj.contains("separator") && itemObj["separator"].toBool()) {
MenuItemConfig separatorItem;
separatorItem.separator = true;
menu.items.append(separatorItem);
continue;
}
MenuItemConfig item;
item.id = itemObj["id"].toString();
item.text = itemObj["text"].toString();
item.iconPath = itemObj["icon"].toString();
item.shortcut = itemObj["shortcut"].toString();
item.standardKey = itemObj["standardKey"].toString();
item.statusTip = itemObj["statusTip"].toString();
item.slot = itemObj["slot"].toString();
if (itemObj.contains("enabled"))
item.enabled = itemObj["enabled"].toBool();
if (itemObj.contains("checkable"))
item.checkable = itemObj["checkable"].toBool();
menu.items.append(item);
}
menus.append(menu);
}
return menus;
}
QKeySequence::StandardKey MenuConfigManager::stringToStandardKey(const QString& keyStr) {
static QHash<QString, QKeySequence::StandardKey> mapping = {
{"New", QKeySequence::New},
{"Open", QKeySequence::Open},
{"Save", QKeySequence::Save},
{"SaveAs", QKeySequence::SaveAs},
{"Close", QKeySequence::Close},
{"Quit", QKeySequence::Quit},
{"Undo", QKeySequence::Undo},
{"Redo", QKeySequence::Redo},
{"ZoomIn", QKeySequence::ZoomIn},
{"ZoomOut", QKeySequence::ZoomOut},
{"Cut", QKeySequence::Cut},
{"Copy", QKeySequence::Copy},
{"Paste", QKeySequence::Paste},
{"Delete", QKeySequence::Delete},
{"Find", QKeySequence::Find},
{"Replace", QKeySequence::Replace},
{"HelpContents", QKeySequence::HelpContents},
{"WhatsThis", QKeySequence::WhatsThis}
};
return mapping.value(keyStr, QKeySequence::UnknownKey);
}