RedPanda-CPP/RedPandaIDE/project.cpp

316 lines
7.7 KiB
C++
Raw Normal View History

2021-09-05 23:45:05 +08:00
#include "project.h"
#include "editor.h"
#include "mainwindow.h"
2021-09-06 08:45:53 +08:00
#include "utils.h"
#include "systemconsts.h"
#include <QDir>
#include <QFileInfo>
2021-09-06 12:58:29 +08:00
#include <QMessageBox>
2021-09-05 23:45:05 +08:00
Project::Project(QObject *parent) : QObject(parent)
{
}
2021-09-06 08:45:53 +08:00
QString Project::directory()
{
QFileInfo fileInfo(mFilename);
return fileInfo.absolutePath();
}
QString Project::executableName()
{
QString exeFileName;
if (mOptions.overrideOutput && !mOptions.overridenOutput.isEmpty()) {
exeFileName = mOptions.overridenOutput;
} else {
switch(mOptions.type) {
case ProjectType::StaticLib:
exeFileName = changeFileExt(baseFileName(mFilename),STATIC_LIB_EXT);
break;
case ProjectType::DynamicLib:
exeFileName = changeFileExt(baseFileName(mFilename),DYNAMIC_LIB_EXT);
break;
default:
exeFileName = changeFileExt(baseFileName(mFilename),EXECUTABLE_EXT);
}
}
QString exePath;
if (!mOptions.exeOutput.isEmpty()) {
QDir baseDir(directory());
exePath = baseDir.filePath(mOptions.exeOutput);
} else {
exePath = directory();
}
QDir exeDir(exePath);
return exeDir.filePath(exeFileName);
}
QString Project::makeFileName()
{
2021-09-06 12:58:29 +08:00
if (mOptions.useCustomMakefile)
return mOptions.customMakefile;
2021-09-06 08:45:53 +08:00
else
2021-09-06 12:58:29 +08:00
return QDir(directory()).filePath(MAKEFILE_NAME);
}
bool Project::modified()
{
// Project file modified? Done
if (mModified)
return true;// quick exit avoids loop over all units
// Otherwise, check all units
foreach (const PProjectUnit& unit, mUnits){
if (unit->modified())
return true;
}
return false;
}
void Project::open()
{
QFile fileInfo(mFilename);
if (fileInfo.exists()
&& !fileInfo.isWritable()) {
if (QMessageBox::question(pMainWindow,
tr("Remove Readonly Attribute"),
tr("Project file '%1' is readonly.<br /> Remove the readonly attribute?")
.arg(mFilename),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes) == QMessageBox::Yes) {
fileInfo.setPermissions(
QFileDevice::WriteOwner
| QFileDevice::WriteGroup
| QFileDevice::WriteUser
);
}
}
loadOptions();
//fNode := MakeProjectNode;
checkProjectFileForUpdate();
mIniFile.beginGroup("Project");
int uCount = mIniFile.value("UnitCount",0).toInt();
mIniFile.endGroup();
//createFolderNodes;
QDir dir(directory());
for (int i=0;i<uCount;i++) {
PProjectUnit newUnit = std::make_shared<ProjectUnit>();
mIniFile.beginGroup(QString("Unit%1").arg(i));
newUnit->setFileName(dir.filePath(mIniFile.value("FileName","").toString()));
if (!QFileInfo(newUnit->fileName()).exists()) {
QMessageBox::critical(pMainWindow,
tr("File Not Found"),
tr("Project file '%1' can't be found!")
.arg(newUnit->fileName()),
QMessageBox::Ok);
newUnit->setModified(true);
} else {
newUnit->setFolder(mIniFile.value("Folder","").toString());
newUnit->setCompile(mIniFile.value("Compile", true).toBool());
if finifile.ReadInteger('Unit' + IntToStr(i + 1), 'CompileCpp', 2) = 2 then
// check if feature not present in this file
CompileCpp := Self.Options.useGPP
else
CompileCpp := finifile.ReadBool('Unit' + IntToStr(i + 1), 'CompileCpp', False);
Link := finifile.ReadBool('Unit' + IntToStr(i + 1), 'Link', True);
Priority := finifile.ReadInteger('Unit' + IntToStr(i + 1), 'Priority', 1000);
OverrideBuildCmd := finifile.ReadBool('Unit' + IntToStr(i + 1), 'OverrideBuildCmd', False);
BuildCmd := finifile.ReadString('Unit' + IntToStr(i + 1), 'BuildCmd', '');
DetectEncoding := finifile.ReadBool('Unit' + IntToStr(i + 1), 'DetectEncoding', self.fOptions.UseUTF8);
//compitible old project files
// UseUTF8:=finifile.ReadBool('Unit' + IntToStr(i + 1), 'UseUTF8', self.fOptions.UseUTF8);
Encoding :=
TFileEncodingType(finifile.ReadInteger('Unit' + IntToStr(i + 1), 'Encoding', 0));
Editor := nil;
New := FALSE;
fParent := self;
Node := MakeNewFileNode(ExtractFileName(FileName), False, FolderNodeFromName(Folder));
Node.Data := pointer(fUnits.Add(NewUnit));
}
mIniFile.endGroup();
}
emit changed();
// RebuildNodes;
2021-09-06 08:45:53 +08:00
}
2021-09-05 23:45:05 +08:00
const std::weak_ptr<Project> &ProjectUnit::parent() const
{
return mParent;
}
void ProjectUnit::setParent(const std::weak_ptr<Project> &newParent)
{
mParent = newParent;
}
Editor *ProjectUnit::editor() const
{
return mEditor;
}
void ProjectUnit::setEditor(Editor *newEditor)
{
mEditor = newEditor;
}
const QString &ProjectUnit::fileName() const
{
return mFileName;
}
void ProjectUnit::setFileName(const QString &newFileName)
{
mFileName = newFileName;
}
bool ProjectUnit::isNew() const
{
return mNew;
}
void ProjectUnit::setNew(bool newNew)
{
mNew = newNew;
}
const QString &ProjectUnit::folder() const
{
return mFolder;
}
void ProjectUnit::setFolder(const QString &newFolder)
{
mFolder = newFolder;
}
bool ProjectUnit::compile() const
{
return mCompile;
}
void ProjectUnit::setCompile(bool newCompile)
{
mCompile = newCompile;
}
bool ProjectUnit::compileCpp() const
{
return mCompileCpp;
}
void ProjectUnit::setCompileCpp(bool newCompileCpp)
{
mCompileCpp = newCompileCpp;
}
bool ProjectUnit::overrideBuildCmd() const
{
return mOverrideBuildCmd;
}
void ProjectUnit::setOverrideBuildCmd(bool newOverrideBuildCmd)
{
mOverrideBuildCmd = newOverrideBuildCmd;
}
const QString &ProjectUnit::buildCmd() const
{
return mBuildCmd;
}
void ProjectUnit::setBuildCmd(const QString &newBuildCmd)
{
mBuildCmd = newBuildCmd;
}
bool ProjectUnit::link() const
{
return mLink;
}
void ProjectUnit::setLink(bool newLink)
{
mLink = newLink;
}
int ProjectUnit::priority() const
{
return mPriority;
}
void ProjectUnit::setPriority(int newPriority)
{
mPriority = newPriority;
}
bool ProjectUnit::detectEncoding() const
{
return mDetectEncoding;
}
void ProjectUnit::setDetectEncoding(bool newDetectEncoding)
{
mDetectEncoding = newDetectEncoding;
}
const QByteArray &ProjectUnit::encoding() const
{
return mEncoding;
}
void ProjectUnit::setEncoding(const QByteArray &newEncoding)
{
mEncoding = newEncoding;
}
2021-09-06 12:58:29 +08:00
bool ProjectUnit::modified() const
2021-09-05 23:45:05 +08:00
{
if (mEditor) {
return mEditor->modified();
} else {
return false;
}
}
void ProjectUnit::setModified(bool value)
{
// Mark the change in the coupled editor
if (mEditor) {
return mEditor->setModified(value);
}
// If modified is set to true, mark project as modified too
if (value) {
std::shared_ptr<Project> parent = mParent.lock();
if (parent) {
parent->setModified(true);
}
}
}
bool ProjectUnit::save()
{
bool previous=pMainWindow->fileSystemWatcher()->blockSignals(true);
auto action = finally([&previous](){
pMainWindow->fileSystemWatcher()->blockSignals(previous);
});
bool result=true;
if (!mEditor && !QFile(mFileName).exists()) {
// file is neither open, nor saved
QStringList temp;
StringsToFile(temp,mFileName);
} else if (mEditor and modified()) {
result = mEditor->save();
}
return result;
}