first commit
This commit is contained in:
@@ -0,0 +1,351 @@
|
||||
#include "projectconfig.h"
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QDir>
|
||||
|
||||
ProjectConfig::ProjectConfig(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
m_projectName = "Untitled";
|
||||
m_projectPath = "";
|
||||
m_compilerPath = "gcc";
|
||||
m_assemblerPath = "gcc";
|
||||
m_linkerPath = "gcc";
|
||||
m_outputDir = "./build";
|
||||
}
|
||||
|
||||
bool ProjectConfig::loadConfig(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
||||
file.close();
|
||||
|
||||
if (doc.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
fromJson(doc.object());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProjectConfig::saveConfig(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonDocument doc(toJson());
|
||||
file.write(doc.toJson());
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QJsonObject ProjectConfig::toJson() const
|
||||
{
|
||||
QJsonObject json;
|
||||
|
||||
// 基本信息
|
||||
json["projectName"] = m_projectName;
|
||||
json["projectPath"] = m_projectPath;
|
||||
json["outputDir"] = m_outputDir;
|
||||
|
||||
// 工具链
|
||||
json["compilerPath"] = m_compilerPath;
|
||||
json["assemblerPath"] = m_assemblerPath;
|
||||
json["linkerPath"] = m_linkerPath;
|
||||
|
||||
// 虚拟目录
|
||||
QJsonObject virtualDirs;
|
||||
for (auto it = m_virtualDirs.begin(); it != m_virtualDirs.end(); ++it) {
|
||||
QJsonObject dirObj;
|
||||
dirObj["name"] = it->name;
|
||||
QJsonArray files;
|
||||
for (const QString &f : it->files) {
|
||||
files.append(f);
|
||||
}
|
||||
dirObj["files"] = files;
|
||||
virtualDirs[it.key()] = dirObj;
|
||||
}
|
||||
json["virtualDirs"] = virtualDirs;
|
||||
|
||||
// 包含目录
|
||||
QJsonArray includeDirs;
|
||||
for (const QString &dir : m_includeDirs) {
|
||||
includeDirs.append(dir);
|
||||
}
|
||||
json["includeDirs"] = includeDirs;
|
||||
|
||||
// 库文件
|
||||
QJsonArray libraries;
|
||||
for (const QString &lib : m_libraries) {
|
||||
libraries.append(lib);
|
||||
}
|
||||
json["libraries"] = libraries;
|
||||
|
||||
// 编译宏
|
||||
QJsonArray defines;
|
||||
for (const QString &def : m_defines) {
|
||||
defines.append(def);
|
||||
}
|
||||
json["defines"] = defines;
|
||||
|
||||
// 编译选项
|
||||
QJsonArray options;
|
||||
for (const QString &opt : m_compilerOptions) {
|
||||
options.append(opt);
|
||||
}
|
||||
json["compilerOptions"] = options;
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
void ProjectConfig::fromJson(const QJsonObject &json)
|
||||
{
|
||||
m_projectName = json["projectName"].toString();
|
||||
m_projectPath = json["projectPath"].toString();
|
||||
m_outputDir = json["outputDir"].toString();
|
||||
m_compilerPath = json["compilerPath"].toString();
|
||||
m_assemblerPath = json["assemblerPath"].toString();
|
||||
m_linkerPath = json["linkerPath"].toString();
|
||||
|
||||
// 虚拟目录
|
||||
m_virtualDirs.clear();
|
||||
QJsonObject virtualDirs = json["virtualDirs"].toObject();
|
||||
for (auto it = virtualDirs.begin(); it != virtualDirs.end(); ++it) {
|
||||
VirtualDir dir;
|
||||
dir.name = it->toObject()["name"].toString();
|
||||
QJsonArray files = it->toObject()["files"].toArray();
|
||||
for (const QJsonValue &f : files) {
|
||||
dir.files.append(f.toString());
|
||||
}
|
||||
m_virtualDirs[it.key()] = dir;
|
||||
}
|
||||
|
||||
// 包含目录
|
||||
m_includeDirs.clear();
|
||||
QJsonArray includeDirs = json["includeDirs"].toArray();
|
||||
for (const QJsonValue &dir : includeDirs) {
|
||||
m_includeDirs.append(dir.toString());
|
||||
}
|
||||
|
||||
// 库文件
|
||||
m_libraries.clear();
|
||||
QJsonArray libraries = json["libraries"].toArray();
|
||||
for (const QJsonValue &lib : libraries) {
|
||||
m_libraries.append(lib.toString());
|
||||
}
|
||||
|
||||
// 编译宏
|
||||
m_defines.clear();
|
||||
QJsonArray defines = json["defines"].toArray();
|
||||
for (const QJsonValue &def : defines) {
|
||||
m_defines.append(def.toString());
|
||||
}
|
||||
|
||||
// 编译选项
|
||||
m_compilerOptions.clear();
|
||||
QJsonArray options = json["compilerOptions"].toArray();
|
||||
for (const QJsonValue &opt : options) {
|
||||
m_compilerOptions.append(opt.toString());
|
||||
}
|
||||
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
// Getter/Setter 实现
|
||||
void ProjectConfig::setProjectName(const QString &name) {
|
||||
if (m_projectName != name) {
|
||||
m_projectName = name;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ProjectConfig::getProjectName() const {
|
||||
return m_projectName;
|
||||
}
|
||||
|
||||
void ProjectConfig::setProjectPath(const QString &path) {
|
||||
if (m_projectPath != path) {
|
||||
m_projectPath = path;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ProjectConfig::getProjectPath() const {
|
||||
return m_projectPath;
|
||||
}
|
||||
|
||||
void ProjectConfig::setCompilerPath(const QString &path) {
|
||||
if (m_compilerPath != path) {
|
||||
m_compilerPath = path;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ProjectConfig::getCompilerPath() const {
|
||||
return m_compilerPath;
|
||||
}
|
||||
|
||||
void ProjectConfig::setAssemblerPath(const QString &path) {
|
||||
if (m_assemblerPath != path) {
|
||||
m_assemblerPath = path;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ProjectConfig::getAssemblerPath() const {
|
||||
return m_assemblerPath;
|
||||
}
|
||||
|
||||
void ProjectConfig::setLinkerPath(const QString &path) {
|
||||
if (m_linkerPath != path) {
|
||||
m_linkerPath = path;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ProjectConfig::getLinkerPath() const {
|
||||
return m_linkerPath;
|
||||
}
|
||||
|
||||
void ProjectConfig::setOutputDir(const QString &dir) {
|
||||
if (m_outputDir != dir) {
|
||||
m_outputDir = dir;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString ProjectConfig::getOutputDir() const {
|
||||
return m_outputDir;
|
||||
}
|
||||
|
||||
// 虚拟目录操作
|
||||
void ProjectConfig::addVirtualDir(const QString &dirName) {
|
||||
if (!m_virtualDirs.contains(dirName)) {
|
||||
VirtualDir dir;
|
||||
dir.name = dirName;
|
||||
m_virtualDirs[dirName] = dir;
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectConfig::addSourceFile(const QString &dirName, const QString &filePath) {
|
||||
if (m_virtualDirs.contains(dirName)) {
|
||||
if (!m_virtualDirs[dirName].files.contains(filePath)) {
|
||||
m_virtualDirs[dirName].files.append(filePath);
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectConfig::removeSourceFile(const QString &dirName, const QString &filePath) {
|
||||
if (m_virtualDirs.contains(dirName)) {
|
||||
m_virtualDirs[dirName].files.removeAll(filePath);
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QMap<QString, VirtualDir> ProjectConfig::getVirtualDirs() const {
|
||||
return m_virtualDirs;
|
||||
}
|
||||
|
||||
void ProjectConfig::clearVirtualDirs() {
|
||||
m_virtualDirs.clear();
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
// 包含目录操作
|
||||
void ProjectConfig::addIncludeDir(const QString &dir) {
|
||||
if (!m_includeDirs.contains(dir)) {
|
||||
m_includeDirs.append(dir);
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectConfig::removeIncludeDir(const QString &dir) {
|
||||
m_includeDirs.removeAll(dir);
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
QStringList ProjectConfig::getIncludeDirs() const {
|
||||
return m_includeDirs;
|
||||
}
|
||||
|
||||
void ProjectConfig::clearIncludeDirs() {
|
||||
m_includeDirs.clear();
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
// 库文件操作
|
||||
void ProjectConfig::addLibrary(const QString &libPath) {
|
||||
if (!m_libraries.contains(libPath)) {
|
||||
m_libraries.append(libPath);
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectConfig::removeLibrary(const QString &libPath) {
|
||||
m_libraries.removeAll(libPath);
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
QStringList ProjectConfig::getLibraries() const {
|
||||
return m_libraries;
|
||||
}
|
||||
|
||||
void ProjectConfig::clearLibraries() {
|
||||
m_libraries.clear();
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
// 编译宏操作
|
||||
void ProjectConfig::addDefine(const QString ¯o) {
|
||||
if (!m_defines.contains(macro)) {
|
||||
m_defines.append(macro);
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectConfig::removeDefine(const QString ¯o) {
|
||||
m_defines.removeAll(macro);
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
QStringList ProjectConfig::getDefines() const {
|
||||
return m_defines;
|
||||
}
|
||||
|
||||
void ProjectConfig::clearDefines() {
|
||||
m_defines.clear();
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
// 编译选项操作
|
||||
void ProjectConfig::addCompilerOption(const QString &option) {
|
||||
if (!m_compilerOptions.contains(option)) {
|
||||
m_compilerOptions.append(option);
|
||||
emit configChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectConfig::removeCompilerOption(const QString &option) {
|
||||
m_compilerOptions.removeAll(option);
|
||||
emit configChanged();
|
||||
}
|
||||
|
||||
QStringList ProjectConfig::getCompilerOptions() const {
|
||||
return m_compilerOptions;
|
||||
}
|
||||
|
||||
void ProjectConfig::clearCompilerOptions() {
|
||||
m_compilerOptions.clear();
|
||||
emit configChanged();
|
||||
}
|
||||
Reference in New Issue
Block a user