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/>.
|
|
|
|
*/
|
2021-09-15 22:19:59 +08:00
|
|
|
#include "projecttemplate.h"
|
|
|
|
#include <QFile>
|
2021-09-16 12:03:10 +08:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include "mainwindow.h"
|
2022-01-08 11:46:39 +08:00
|
|
|
#include "settings.h"
|
2021-09-16 12:03:10 +08:00
|
|
|
|
2021-09-15 22:19:59 +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);
|
2021-09-15 22:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PTemplateUnit ProjectTemplate::unit(int index)
|
|
|
|
{
|
2021-09-16 12:03:10 +08:00
|
|
|
if (!mIni || mVersion<=0)
|
2021-09-15 22:19:59 +08:00
|
|
|
return PTemplateUnit();
|
|
|
|
QString section = QString("Unit%1").arg(index);
|
|
|
|
PTemplateUnit unit = std::make_shared<TemplateUnit>();
|
2023-02-21 20:35:41 +08:00
|
|
|
QString lang = pSettings->environment().language();
|
|
|
|
if (!lang.isEmpty()) {
|
|
|
|
unit->Source = fromByteArray(mIni->GetValue(toByteArray(section), QString("Source[%1]").arg(lang).toUtf8(), ""));
|
|
|
|
unit->CText = fromByteArray(mIni->GetValue(toByteArray(section), QString("C[%1]").arg(lang).toUtf8(), ""));
|
|
|
|
unit->CppText = fromByteArray(mIni->GetValue(toByteArray(section), QString("Cpp[%1]").arg(lang).toUtf8(), ""));
|
|
|
|
}
|
|
|
|
if (unit->Source.isEmpty())
|
|
|
|
unit->Source = fromByteArray(mIni->GetValue(toByteArray(section), "Source", ""));
|
|
|
|
if (unit->CText.isEmpty())
|
|
|
|
unit->CText = fromByteArray(mIni->GetValue(toByteArray(section), "C", ""));
|
|
|
|
if (unit->CppText.isEmpty())
|
|
|
|
unit->CppText = fromByteArray(mIni->GetValue(toByteArray(section), "Cpp", ""));
|
2021-09-15 22:19:59 +08:00
|
|
|
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", ""));
|
2021-09-15 22:19:59 +08:00
|
|
|
if (unit->CppName.isEmpty())
|
|
|
|
unit->CppName = unit->CName;
|
2023-02-21 20:35:41 +08:00
|
|
|
unit->Target = fromByteArray(mIni->GetValue(toByteArray(section), "Target", ""));
|
|
|
|
|
2021-09-16 12:03:10 +08:00
|
|
|
return unit;
|
2021-09-15 22:19:59 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 12:03:10 +08:00
|
|
|
void ProjectTemplate::readTemplateFile(const QString &fileName)
|
|
|
|
{
|
|
|
|
if (mIni)
|
|
|
|
mIni=nullptr;
|
2022-03-15 20:17:47 +08:00
|
|
|
QFile file(fileName);
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
2021-09-16 12:03:10 +08:00
|
|
|
mFileName = fileName;
|
|
|
|
mIni = std::make_shared<SimpleIni>();
|
2022-03-15 20:17:47 +08:00
|
|
|
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,
|
2022-03-15 20:17:47 +08:00
|
|
|
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);
|
2021-09-15 22:19:59 +08:00
|
|
|
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);
|
2021-09-15 22:19:59 +08:00
|
|
|
return;
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
|
|
|
|
2022-01-08 11:46:39 +08:00
|
|
|
QString lang = pSettings->environment().language();
|
2021-09-16 12:03:10 +08:00
|
|
|
// template info
|
|
|
|
mIcon = fromByteArray(mIni->GetValue("Template", "Icon", ""));
|
2022-01-08 11:46:39 +08:00
|
|
|
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", ""));
|
2023-08-18 10:04:02 +08:00
|
|
|
mIconInfo=fromByteArray(mIni->GetValue("Template", "IconInfo", ""));
|
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.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",""));
|
2022-12-26 22:55:00 +08:00
|
|
|
mOptions.resourceCmd = fromByteArray(mIni->GetValue("Project", "ResourceCommand", ""));
|
2022-02-01 16:17:28 +08:00
|
|
|
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", ""));
|
2022-06-23 19:07:48 +08:00
|
|
|
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) {
|
2023-01-24 10:21:19 +08:00
|
|
|
mOptions.encoding = mIni->GetValue("Project","Encoding", ENCODING_UTF8);
|
2021-09-16 12:03:10 +08:00
|
|
|
} else {
|
2023-01-24 10:21:19 +08:00
|
|
|
mOptions.encoding = mIni->GetValue("Project","Encoding", pSettings->editor().defaultEncoding());
|
2021-09-16 12:03:10 +08:00
|
|
|
}
|
2023-01-24 10:21:19 +08:00
|
|
|
if (mOptions.encoding == ENCODING_AUTO_DETECT)
|
|
|
|
mOptions.encoding = ENCODING_SYSTEM_DEFAULT;
|
2022-02-08 12:33:10 +08:00
|
|
|
mOptions.modelType = (ProjectModelType)mIni->GetLongValue("Project", "ModelType", (int)ProjectModelType::FileSystem);
|
2022-10-23 15:22:26 +08:00
|
|
|
mOptions.classBrowserType = (ProjectClassBrowserType)mIni->GetLongValue("Project", "ClassBrowserType", (int)ProjectClassBrowserType::CurrentFile);
|
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-15 22:19:59 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-07 12:09:22 +08:00
|
|
|
const QString ProjectTemplate::folder() const
|
|
|
|
{
|
|
|
|
return extractFileDir(mFileName);
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:03:10 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-07 21:41:57 +08:00
|
|
|
void ProjectTemplate::setVersion(int newVersion)
|
|
|
|
{
|
|
|
|
mVersion = newVersion;
|
|
|
|
}
|
|
|
|
|
2023-08-18 10:04:02 +08:00
|
|
|
QString ProjectTemplate::iconInfo() const
|
|
|
|
{
|
|
|
|
return mIconInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProjectTemplate::setIconInfo(const QString &newIconInfo)
|
|
|
|
{
|
|
|
|
mIconInfo = newIconInfo;
|
|
|
|
}
|
|
|
|
|