Repository navigation
Expand file tree
/
Copy pathglobal.cpp
More file actions
165 lines (138 loc) · 5.56 KB
/
Copy pathglobal.cpp
File metadata and controls
165 lines (138 loc) · 5.56 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
#include "global.h"
#include <QSettings>
#include <QFile>
#include <QCoreApplication>
#include <QDebug>
int Global::pixelSize = 8;
int Global::scaleWidth = 16;
int Global::scaleOffset = 4;
int Global::pixelColor_1 = 0x9ce0ef;
int Global::pixelColor_0 = 0x495028;
int Global::gridColor = 0x303030;
int Global::itemBoundColor = 0x808080;
int Global::selectedItemBoundColor = 0x00FFFF;
int Global::selectionBoxColor = 0xFFFF00;
int Global::guidesColor = 0xFFFF00;
int Global::selectedGuidesColor = 0xFF8C00;
int Global::backgroundColorLeftTop = 0xC5CFDF;
int Global::backgroundColorRightBottom = 0xDCE5F2;
bool Global::editMode = false;
int Global::exportImgColor_0 = 0xFFFFFF;
int Global::exportImgColor_1 = 0x000000;
// 新建项目默认尺寸
int Global::defaultProjectWidth = 128;
int Global::defaultProjectHeight = 64;
// 新建项目默认取模方式 (-1 表示未选择,使用空)
int Global::defaultProjectMode = -1;
void Global::initSettings()
{
QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
// 判断文件是否存在
if (!QFile::exists(iniFilePath)) {
// 文件不存在,创建默认配置
createDefaultSettings(iniFilePath);
} else {
// 文件存在,读取配置
readSettings(iniFilePath);
}
}
void Global::createDefaultSettings(const QString &filePath)
{
// 创建QSettings对象,指定文件路径和格式
QSettings settings(filePath, QSettings::IniFormat);
settings.beginGroup("General");
settings.setValue("export_image_color_0", QString::number(exportImgColor_0, 16).toUpper());
settings.setValue("export_image_color_1", QString::number(exportImgColor_1, 16).toUpper());
settings.setValue("default_project_width", defaultProjectWidth);
settings.setValue("default_project_height", defaultProjectHeight);
settings.setValue("default_project_mode", defaultProjectMode);
settings.setValue("pixel_size", pixelSize);
settings.endGroup();
}
void Global::readSettings(const QString &filePath)
{
// 创建QSettings对象,指定文件路径和格式
QSettings settings(filePath, QSettings::IniFormat);
settings.beginGroup("General");
bool ok;
exportImgColor_0 = settings.value("export_image_color_0", QString::number(exportImgColor_0, 16)).toString().toInt(&ok, 16);
exportImgColor_1 = settings.value("export_image_color_1", QString::number(exportImgColor_1, 16)).toString().toInt(&ok, 16);
defaultProjectWidth = settings.value("default_project_width", defaultProjectWidth).toInt();
defaultProjectHeight = settings.value("default_project_height", defaultProjectHeight).toInt();
defaultProjectMode = settings.value("default_project_mode", defaultProjectMode).toInt();
pixelSize = qBound(4, settings.value("pixel_size", pixelSize).toInt(), 64);
settings.endGroup();
}
void Global::saveDefaultProjectSize(int width, int height)
{
QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
QSettings settings(iniFilePath, QSettings::IniFormat);
settings.beginGroup("General");
settings.setValue("default_project_width", width);
settings.setValue("default_project_height", height);
settings.endGroup();
// 更新静态变量
defaultProjectWidth = width;
defaultProjectHeight = height;
}
void Global::saveDefaultProjectMode(int mode)
{
QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
QSettings settings(iniFilePath, QSettings::IniFormat);
settings.beginGroup("General");
settings.setValue("default_project_mode", mode);
settings.endGroup();
// 更新静态变量
defaultProjectMode = mode;
}
void Global::savePixelSize(int size)
{
QString iniFilePath = QCoreApplication::applicationDirPath() + "/config.ini";
QSettings settings(iniFilePath, QSettings::IniFormat);
settings.beginGroup("General");
settings.setValue("pixel_size", size);
settings.endGroup();
// 更新静态变量
pixelSize = size;
}
bool Global::readWindowLayout(QRect &geometry, bool &maximized)
{
QSettings settings(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
settings.beginGroup("Window");
const int x = settings.value("x", 0).toInt();
const int y = settings.value("y", 0).toInt();
const int width = settings.value("width", 0).toInt();
const int height = settings.value("height", 0).toInt();
maximized = settings.value("maximized", false).toBool();
settings.endGroup();
if (width <= 0 || height <= 0)
return false;
geometry = QRect(x, y, width, height);
return true;
}
void Global::saveWindowLayout(const QRect &geometry, bool maximized)
{
QSettings settings(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
settings.beginGroup("Window");
settings.setValue("x", geometry.x());
settings.setValue("y", geometry.y());
settings.setValue("width", geometry.width());
settings.setValue("height", geometry.height());
settings.setValue("maximized", maximized);
settings.endGroup();
}
bool Global::readSearchBarVisible()
{
QSettings settings(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
settings.beginGroup("SearchBar");
const bool visible = settings.value("visible", true).toBool();
settings.endGroup();
return visible;
}
void Global::saveSearchBarVisible(bool visible)
{
QSettings settings(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
settings.beginGroup("SearchBar");
settings.setValue("visible", visible);
settings.endGroup();
}