-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththemedata.cpp
More file actions
213 lines (183 loc) · 4.42 KB
/
Copy paththemedata.cpp
File metadata and controls
213 lines (183 loc) · 4.42 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "themedata.h"
#include <iostream>
// 读取标签数据
void ThemeData::Tag::ReadTag(QByteArray::iterator bg, QByteArray::iterator ed)
{
QString s = QString::fromUtf8(bg,ed-bg);
if (*(ed - 1) == '/')
{
// 处理自闭合标签
ed = bg;
while (*(++ed) != ' ');
// 提取标签名
name = QString::fromUtf8(bg,ed-bg);
// 定位到 Value 属性的值
while (*(bg++) != '"');
ed = bg;
while (*(++ed) != '"');
// 提取属性值
QString str = QString::fromUtf8(bg,ed-bg);
// 根据属性值类型进行赋值
if (str[0] == '#')
{
if (str.length() == 7)
type = rgb;
else
type = rgba;
i = str.mid(1).toUInt(nullptr, 16);
}
else if (str.contains('e'))
{
type = bol;
i = (str[0] == 't') ? 1 : 0;
}
else if (str.contains('.'))
{
type = flt;
f = str.toFloat();
}
else
{
type = itg;
i = str.toUInt();
}
}
else
{
// 处理起始和结束标签
if(*bg=='!')type=cmt,bg+=3,ed-=2;
else if(*bg=='/')type=tl,++bg;
else type=hd;
// 提取标签名
name = QString::fromUtf8(bg,ed-bg);
}
}
// 写入标签数据
void ThemeData::Tag::WriteTag(QByteArray& dat)
{
if (type == hd || type == tl || type == cmt)
{
if(type==tl)dat+='/';
else if(type==cmt)dat+="!--";
dat += name.toUtf8();
if(type==cmt)dat+="--";
return;
}
// 添加标签名和 Value 属性
dat += (name + " Value=\"").toUtf8();
// 根据属性类型添加值
switch (type)
{
case rgb:
dat += ("#" + QString("%1").arg(i, 6, 16, QChar('0'))).toUtf8();
break;
case rgba:
dat += ("#" + QString("%1").arg(i, 8, 16, QChar('0'))).toUtf8();
break;
case itg:
dat += QString::number(i, 10).toUtf8();
break;
case bol:
dat += (i ? "true" : "false");
break;
case flt:
dat += QString::number(f,'f',6).toUtf8();
break;
}
dat += "\" /";
}
// 加载主题数据
bool ThemeData::LoadData(const QString& fileName)
{
QFile file(fileName);
if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
// 读取文件内容
QByteArray dat = file.readAll();
file.close();
// 解析数据
v.clear();
auto bg = std::find(dat.begin(), dat.end(), '\n');
while ((bg = std::find(bg, dat.end(), '<')) != dat.end())
{
// 创建标签并读取数据
v.push_back({});
v.back().ReadTag(++bg, std::find(bg, dat.end(), '>'));
}
return true;
}
// 保存主题数据
bool ThemeData::SaveData(const QString& fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
// 写入 XML 头部
QByteArray dat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
int tabs = 0;
// 遍历标签数据并写入文件
for (auto& tag : v)
{
if (tag.type == tl)--tabs;
// 添加缩进
for (int i = 0; i < tabs; ++i)dat+='\t';
// 写入标签内容
tag.WriteTag(dat+='<');
dat+=">\n";
if (tag.type == hd)++tabs;
}
// 写入文件
file.write(dat);
file.close();
modified=false;
return true;
}
// 构造函数
ThemeData::ThemeData()
{
// 初始化函数体为空
}
ThemeData::~ThemeData()
{
}
ThemeData& ThemeData::operator=(const ThemeData& src) {
v = src.v;
modified = src.modified;
return *this;
}
QVector<ThemeData::Tag>::const_iterator ThemeData::begin() const
{
return v.cbegin();
}
QVector<ThemeData::Tag>::const_iterator ThemeData::end() const
{
return v.cend();
}
const ThemeData::Tag& ThemeData::at(uint i) const
{
return v[i];
}
bool ThemeData::Modify(int i,float f)
{
if(v[i].type != 4)
return false;
v[i].f = f;
// if(modified == true)
emit Modified();
std::cout << "Modified triggered\n";
return true;
}
bool ThemeData::Modify(int i,unsigned int u)
{
if(v[i].type >= 4)
return false;
v[i].i = u;
// if(modified == true)
emit Modified();
std::cout << "Modified triggered\n";
return true;
}
uint ThemeData::size() const
{
return v.size();
}