RedPanda-CPP/RedPandaIDE/projecttemplate.cpp

265 lines
8.7 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "projecttemplate.h"
#include <QFile>
2021-09-16 12:03:10 +08:00
#include <QMessageBox>
#include "mainwindow.h"
#include "settings.h"
2021-09-16 12:03:10 +08:00
ProjectTemplate::ProjectTemplate(QObject *parent) : QObject(parent)
{
}
int ProjectTemplate::unitCount()
{
2021-09-16 12:03:10 +08:00
if (!mIni || mVersion<=0)
2021-09-16 23:51:05 +08:00
return 0;
2021-09-16 12:03:10 +08:00
return mIni->GetLongValue("Project","UnitCount",0);
}
PTemplateUnit ProjectTemplate::unit(int index)
{
2021-09-16 12:03:10 +08:00
if (!mIni || mVersion<=0)
return PTemplateUnit();
QString section = QString("Unit%1").arg(index);
PTemplateUnit unit = std::make_shared<TemplateUnit>();
2022-01-10 10:53:16 +08:00
unit->Source = fromByteArray(mIni->GetValue(toByteArray(section), "Source", ""));
unit->Target = fromByteArray(mIni->GetValue(toByteArray(section), "Target", ""));
2021-09-16 12:03:10 +08:00
unit->CText = fromByteArray(mIni->GetValue(toByteArray(section), "C", ""));
unit->CppText = fromByteArray(mIni->GetValue(toByteArray(section), "Cpp", ""));
if (unit->CppText.isEmpty())
unit->CppText = unit->CText;
2021-09-16 12:03:10 +08:00
unit->CName = fromByteArray(mIni->GetValue(toByteArray(section), "CName", ""));
unit->CppName = fromByteArray(mIni->GetValue(toByteArray(section), "CppName", ""));
if (unit->CppName.isEmpty())
unit->CppName = unit->CName;
2021-09-16 12:03:10 +08:00
return unit;
}
void ProjectTemplate::setUnit(int index, PTemplateUnit newUnit)
{
2021-09-16 12:03:10 +08:00
if (!mIni || mVersion<=0)
return;
2021-09-16 12:03:10 +08:00
QByteArray section = toByteArray(QString("Unit%1").arg(index));
mIni->SetValue(section,"C", toByteArray(newUnit->CText));
mIni->SetValue(section,"Cpp", toByteArray(newUnit->CppText));
mIni->SetValue(section,"CName", toByteArray(newUnit->CName));
mIni->SetValue(section,"CppName", toByteArray(newUnit->CppName));
2022-01-10 10:53:16 +08:00
mIni->SetValue(section,"Source", toByteArray(newUnit->Source));
mIni->SetValue(section,"Target", toByteArray(newUnit->Target));
2021-09-16 12:03:10 +08:00
}
int ProjectTemplate::addUnit()
{
if (!mIni || mVersion<=0)
return -1;
int count = unitCount() +1;
2021-09-16 23:51:05 +08:00
QByteArray section = toByteArray(QString("Unit%1").arg(count-1));
2021-09-16 12:03:10 +08:00
mIni->SetValue(section, "C", "");
mIni->SetValue(section, "Cpp", "");
mIni->SetLongValue("Project", "UnitCount", count);
return count;
}
void ProjectTemplate::readTemplateFile(const QString &fileName)
{
if (mIni)
mIni=nullptr;
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
2021-09-16 12:03:10 +08:00
mFileName = fileName;
mIni = std::make_shared<SimpleIni>();
QByteArray data = file.readAll();
if (mIni->LoadData(data.toStdString()) != SI_OK) {
2021-09-16 12:03:10 +08:00
QMessageBox::critical(pMainWindow,
tr("Read failed."),
tr("Can't read template file '%1'.").arg(fileName),
QMessageBox::Ok);
return;
}
} else {
QMessageBox::critical(pMainWindow,
tr("Can't Open Template"),
tr("Can't open template file '%1' for read.").arg(fileName),
2021-09-16 12:03:10 +08:00
QMessageBox::Ok);
return;
2021-09-16 12:03:10 +08:00
}
mVersion = mIni->GetLongValue("Template", "Ver", 0);
if (mVersion<=0) {
QMessageBox::critical(pMainWindow,
tr("Old version template"),
tr("Template file '%1' has version '%2', which is unsupported.")
.arg(fileName)
.arg(mVersion),
QMessageBox::Ok);
return;
2021-09-16 12:03:10 +08:00
}
QString lang = pSettings->environment().language();
2021-09-16 12:03:10 +08:00
// template info
mIcon = fromByteArray(mIni->GetValue("Template", "Icon", ""));
if (!lang.isEmpty()) {
mCategory = fromByteArray(mIni->GetValue("Template", QString("Category[%1]").arg(lang).toUtf8(), ""));
mName = fromByteArray(mIni->GetValue("Template", QString("Name[%1]").arg(lang).toUtf8(), ""));
mDescription = fromByteArray(mIni->GetValue("Template", QString("Description[%1]").arg(lang).toUtf8(), ""));
}
if (mCategory.isEmpty())
mCategory = fromByteArray(mIni->GetValue("Template", "Category", ""));
if (mName.isEmpty())
mName = fromByteArray(mIni->GetValue("Template", "Name", ""));
if (mDescription.isEmpty())
mDescription = fromByteArray(mIni->GetValue("Template", "Description", ""));
2021-09-16 12:03:10 +08:00
mOptions.icon = mIni->GetValue("Project", "Icon", "");
mOptions.type = static_cast<ProjectType>(mIni->GetLongValue("Project", "Type", 0)); // default = gui
2022-07-04 11:39:06 +08:00
mOptions.objFiles = fromByteArray(mIni->GetValue("Project", "ObjFiles", "")).split(";",
2022-07-24 11:19:11 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
2022-07-04 11:39:06 +08:00
Qt::SkipEmptyParts
#else
QString::SkipEmptyParts
#endif
);
mOptions.includeDirs = fromByteArray(mIni->GetValue("Project", "Includes", "")).split(";",
2022-07-24 11:19:11 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
2022-07-04 11:39:06 +08:00
Qt::SkipEmptyParts
#else
QString::SkipEmptyParts
#endif
);
mOptions.binDirs = fromByteArray(mIni->GetValue("Project", "Bins", "")).split(";",
2022-07-24 11:19:11 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
2022-07-04 11:39:06 +08:00
Qt::SkipEmptyParts
#else
QString::SkipEmptyParts
#endif
);
mOptions.libDirs = fromByteArray(mIni->GetValue("Project", "Libs", "")).split(";",
2022-07-24 11:19:11 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
2022-07-04 11:39:06 +08:00
Qt::SkipEmptyParts
#else
QString::SkipEmptyParts
#endif
);
mOptions.resourceIncludes = fromByteArray(mIni->GetValue("Project", "ResourceIncludes", "")).split(";",
2022-07-24 11:19:11 +08:00
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
2022-07-04 11:39:06 +08:00
Qt::SkipEmptyParts
#else
QString::SkipEmptyParts
#endif
);
2021-09-16 12:03:10 +08:00
mOptions.compilerCmd = fromByteArray(mIni->GetValue("Project", "Compiler", ""));
mOptions.cppCompilerCmd = fromByteArray(mIni->GetValue("Project", "CppCompiler", ""));
mOptions.linkerCmd = fromByteArray(mIni->GetValue("Project", "Linker",""));
mOptions.isCpp = mIni->GetBoolValue("Project", "IsCpp", false);
2021-09-16 12:03:10 +08:00
mOptions.includeVersionInfo = mIni->GetBoolValue("Project", "IncludeVersionInfo", false);
mOptions.supportXPThemes = mIni->GetBoolValue("Project", "SupportXPThemes", false);
mOptions.exeOutput = fromByteArray(mIni->GetValue("Project", "ExeOutput", ""));
mOptions.objectOutput = fromByteArray(mIni->GetValue("Project", "ObjectOutput", ""));
mOptions.logOutput = fromByteArray(mIni->GetValue("Project", "LogOutput", ""));
mOptions.execEncoding = mIni->GetValue("Project","ExecEncoding", ENCODING_SYSTEM_DEFAULT);
2021-09-16 12:03:10 +08:00
mOptions.staticLink = mIni->GetBoolValue("Project", "StaticLink",true);
mOptions.addCharset = mIni->GetBoolValue("Project", "AddCharset",true);
bool useUTF8 = mIni->GetBoolValue("Project", "UseUTF8", false);
if (useUTF8) {
mOptions.encoding = fromByteArray(mIni->GetValue("Project","Encoding", ENCODING_UTF8));
} else {
mOptions.encoding = fromByteArray(mIni->GetValue("Project","Encoding", ENCODING_AUTO_DETECT));
}
mOptions.modelType = (ProjectModelType)mIni->GetLongValue("Project", "ModelType", (int)ProjectModelType::FileSystem);
2021-09-16 12:03:10 +08:00
}
bool ProjectTemplate::save()
{
if (mIni) {
return mIni->SaveFile(toByteArray(mFileName)) == SI_OK ;
}
return false;
}
const QString &ProjectTemplate::category() const
{
return mCategory;
}
void ProjectTemplate::setCategory(const QString &newCategory)
{
mCategory = newCategory;
}
const QString &ProjectTemplate::description() const
{
return mDescription;
}
2021-09-16 12:03:10 +08:00
void ProjectTemplate::setDescription(const QString &newDescription)
{
mDescription = newDescription;
}
const QString &ProjectTemplate::fileName() const
{
return mFileName;
}
void ProjectTemplate::setFileName(const QString &newFileName)
{
mFileName = newFileName;
}
const QString &ProjectTemplate::icon() const
{
return mIcon;
}
void ProjectTemplate::setIcon(const QString &newIcon)
{
mIcon = newIcon;
}
const QString &ProjectTemplate::name() const
{
return mName;
}
void ProjectTemplate::setName(const QString &newName)
{
mName = newName;
}
const ProjectOptions &ProjectTemplate::options() const
{
return mOptions;
}
void ProjectTemplate::setOptions(const ProjectOptions &newOptions)
{
mOptions = newOptions;
}
2021-09-16 23:51:05 +08:00
int ProjectTemplate::version() const
{
return mVersion;
}