This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp.autosave
203 lines (165 loc) · 4.97 KB
/
settings.cpp.autosave
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
#include "settings.h"
/*
struct Shortcut{
QString iconPath;
QString targetPath;
};
struct Spirit{
int id;
bool inuse;
bool active;
QList<Shortcut>shortcuts;
};
*/
bool Settings::readJsonFile(QIODevice &device, QSettings::SettingsMap &map)
{
bool ret = false;
QJsonParseError error;
map = QJsonDocument::fromJson(device.readAll() , &error).toVariant().toMap();
if( error.error == QJsonParseError::NoError )
ret = true;
return ret;
}
bool Settings::writeJsonFile(QIODevice &device, const QSettings::SettingsMap &map)
{
bool ret = false;
QJsonDocument jsonDocument = QJsonDocument::fromVariant(QVariant::fromValue(map));
if ( device.write(jsonDocument.toJson()) != -1 )
ret = true;
return ret;
}
Settings::Settings(const Settings&){
}
Settings& Settings::operator=(const Settings&){
return *settings;
}
Settings* Settings::settings = new Settings();
Settings* Settings::getInstance(){
return settings;
}
Settings::Settings()
//:
//QSettings (QSettings::IniFormat,QSettings::UserScope,"Croxx","Despirit")
{
const QSettings::Format JsonFormat = QSettings::registerFormat("json",readJsonFile,writeJsonFile);
if(!this->contains("count")){
this->clear();
initSettings();
}
}
void Settings::initSettings(){
this->setValue("count",0);
QString dataDirPath = appDataPath();
QDir dir(appDataPath());
if(!dir.exists("Despirit")){
dir.mkdir("Despirit");
}
this->beginWriteArray("spirits");
this->endArray();
qDebug()<<"init settings";
}
QString Settings::appDataPath(){
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}
int Settings::addNewSpirit(){
qDebug()<<"adding...";
int count = this->value("count").toInt();
this->beginWriteArray("spirits");
this->setArrayIndex(count);
this->setValue("id",count);
this->setValue("inuse",true);
this->setValue("active",true);
this->setValue("x",400);
this->setValue("y",400);
this->setValue("shortcut_count",0);
this->endArray();
this->setValue("count",count+1);
qDebug()<<"added";
return count;
}
bool Settings::removeSpirit(int id){
int size = this->beginReadArray("spirits");
this->endArray();
this->beginWriteArray("spirits");
for(int i=id;i<size-1;i++){
this->setArrayIndex(i);
//this->clear();
this->setArrayIndex(i+1);
QStringList keys = this->allKeys();
QStringList values;
for(int j=0;j<keys.size();j++){
values.append(this->value(keys.at(j)).toString());
}
this->setArrayIndex(i);
for(int j=0;j<keys.size();j++){
this->setValue(keys.at(j),values.at(j));
}
}
this->setArrayIndex(size-2);
this->endArray();
return true;
}
QList<int> Settings::getSpiritIdsInUse(){
QList<int>ids;
int size = this->beginReadArray("spirits");
for(int i=0;i<size;i++){
this->setArrayIndex(i);
int id = this->value("id").toInt();
qDebug()<<id;
bool inuse= this->value("inuse").toBool();
if(inuse)ids.append(id);
}
this->endArray();
return ids;
}
bool Settings::setPosition(int id,QPoint pos){
int size = this->beginReadArray("spirits");
this->endArray();
this->beginWriteArray("spirits");
this->setArrayIndex(id);
this->setValue("x",pos.x());
this->setValue("y",pos.y());
this->setArrayIndex(size-1);
this->endArray();
return true;
}
QPoint Settings::getPosition(int id){
this->beginReadArray("spirits");
this->setArrayIndex(id);
QPoint pos(this->value("x").toInt(),this->value("y").toInt());
this->endArray();
return pos;
}
int Settings::addShortcut(int id,QString lnkPath,HWND hwnd,HINSTANCE hInstance){
this->beginWriteArray("spirits");
this->setArrayIndex(id);
int count = this->value("shortcut_count").toInt();
qDebug()<<appDataPath();
lnkPath = WinUtils::convertQTPath2WinPath(lnkPath);
QFile::copy(lnkPath,appDataPath()+"/"+QString::number(id,10)+"_"+QString::number(count,10)+".lnk");
QFileInfo info(lnkPath);
QString targetPath = WinUtils::convertQTPath2WinPath(info.symLinkTarget());
QPixmap icon = WinUtils::getIconFromPEFile(hwnd,hInstance,targetPath.toStdWString().c_str());
icon.save(appDataPath()+"/"+QString::number(id,10)+"_"+QString::number(count,10)+".png");
this->setValue("shortcut_count",count+1);
this->endArray();
return count;
}
bool Settings::removeShortcut(int id,int sid){
return true;
}
int Settings::getShortcutCount(int id){
this->beginReadArray("spirits");
this->setArrayIndex(id);
int count = this->value("shortcut_count").toInt();
this->endArray();
return count;
}
QPixmap Settings::getShortcutIcon(int id,int sid){
QString iconPath = appDataPath()+"/"+QString::number(id,10)+"_"+QString::number(sid,10)+".png";
QPixmap icon;
icon.load(iconPath);
return icon;
}
void Settings::runShortcut(int id,int sid){
}