work save

This commit is contained in:
royqh1979@gmail.com 2021-09-07 00:32:16 +08:00
parent c813b4ba4c
commit feafadd760
2 changed files with 131 additions and 43 deletions

View File

@ -89,19 +89,19 @@ void Project::open()
}
loadOptions();
//fNode := MakeProjectNode;
mNode = makeProjectNode();
checkProjectFileForUpdate();
mIniFile.beginGroup("Project");
int uCount = mIniFile.value("UnitCount",0).toInt();
mIniFile.endGroup();
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()));
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"),
@ -110,42 +110,74 @@ void Project::open()
QMessageBox::Ok);
newUnit->setModified(true);
} else {
newUnit->setFolder(mIniFile.value("Folder","").toString());
newUnit->setCompile(mIniFile.value("Compile", true).toBool());
newUnit->setFolder(mIniFile->value("Folder","").toString());
newUnit->setCompile(mIniFile->value("Compile", true).toBool());
newUnit->setCompileCpp(
mIniFile.value("CompileCpp",mOptions.useGPP).toBool());
mIniFile->value("CompileCpp",mOptions.useGPP).toBool());
newUnit->setLink(mIniFile.value("Link", true).toBool());
newUnit->setPriority(mIniFile.value("Priority", 1000).toInt());
newUnit->setOverrideBuildCmd(mIniFile.value("OverrideBuildCmd", false).toInt());
newUnit->setBuildCmd(mIniFile.value("BuildCmd", "").toString());
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));
newUnit->setLink(mIniFile->value("Link", true).toBool());
newUnit->setPriority(mIniFile->value("Priority", 1000).toInt());
newUnit->setOverrideBuildCmd(mIniFile->value("OverrideBuildCmd", false).toInt());
newUnit->setBuildCmd(mIniFile->value("BuildCmd", "").toString());
newUnit->setDetectEncoding(mIniFile->value("DetectEncoding", mOptions.useUTF8).toBool());
newUnit->setEncoding(mIniFile->value("Encoding",ENCODING_SYSTEM_DEFAULT).toByteArray());
newUnit->setEditor(nullptr);
newUnit->setNew(false);
newUnit->setParent(this);
newUnit->setNode(makeNewFileNode(baseFileName(newUnit->fileName()), false, folderNodeFromName(newUnit->folder())));
newUnit->node()->unitIndex = mUnits.count();
mUnits.append(newUnit);
}
mIniFile.endGroup();
mIniFile->endGroup();
}
emit changed();
// RebuildNodes;
rebuildNodes();
}
const std::weak_ptr<Project> &ProjectUnit::parent() const
void Project::setFileName(const QString &value)
{
if (mFilename!=value) {
mIniFile->sync();
mIniFile.reset();
QFile::copy(mFilename,value);
mFilename = value;
setModified(true);
mIniFile = std::make_shared<QSettings>(mFilename);
}
}
void Project::setModified(bool value)
{
QFile file(mFilename);
// only mark modified if *not* read-only
if (!file.exists()
|| (file.exists() && file.isWritable())) {
mModified=value;
}
}
void Project::sortUnitsByPriority()
{
std::sort(mUnits.begin(),mUnits.end(),[](const PProjectUnit& u1, const PProjectUnit& u2)->bool{
return (u1->priority()>u2->priority());
});
}
ProjectUnit::ProjectUnit(Project* parent)
{
mEditor = nullptr;
mNode = nullptr;
mParent = parent;
}
Project *ProjectUnit::parent() const
{
return mParent;
}
void ProjectUnit::setParent(const std::weak_ptr<Project> &newParent)
void ProjectUnit::setParent(Project* newParent)
{
mParent = newParent;
}
@ -288,10 +320,7 @@ void ProjectUnit::setModified(bool 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);
}
parent->setModified(true);
}
}
@ -309,5 +338,8 @@ bool ProjectUnit::save()
} else if (mEditor and modified()) {
result = mEditor->save();
}
if (mNode) {
mNode->text = baseFileName(mFileName);
}
return result;
}

View File

@ -16,11 +16,21 @@ class Project;
class Editor;
class CppParser;
struct FolderNode;
using PFolderNode = std::shared_ptr<FolderNode>;
struct FolderNode {
QString text;
FolderNode * parent;
int unitIndex;
QList<PFolderNode> children;
};
class ProjectUnit {
public:
const std::weak_ptr<Project> &parent() const;
void setParent(const std::weak_ptr<Project> &newParent);
explicit ProjectUnit(Project* parent);
Project* parent() const;
void setParent(Project* newParent);
Editor *editor() const;
void setEditor(Editor *newEditor);
const QString &fileName() const;
@ -50,7 +60,7 @@ public:
bool save();
private:
std::weak_ptr<Project> mParent;
Project* mParent;
Editor* mEditor;
QString mFileName;
bool mNew;
@ -63,6 +73,7 @@ private:
int mPriority;
bool mDetectEncoding;
QByteArray mEncoding;
PFolderNode mNode;
};
using PProjectUnit = std::shared_ptr<ProjectUnit>;
@ -132,22 +143,67 @@ public:
QString executableName();
QString makeFileName();
bool modified();
void open();
procedure SetFileName(const value: AnsiString);
procedure SetModified(value: boolean);
procedure SortUnitsByPriority;
procedure Open;
void setFileName(const QString& value);
void setModified(bool value);
int newUnit(bool newProject,
PFolderNode parentNode,
const QString customFileName);
PProjectUnit addUnit(const QString& inFileName,
PFolderNode parentNode,
bool rebuild);
function GetFolderPath(Node: TTreeNode): AnsiString;
procedure UpdateFolders;
procedure AddFolder(const s: AnsiString);
function OpenUnit(index: integer): TEditor;
procedure CloseUnit(index: integer);
procedure DoAutoOpen;
procedure SaveUnitAs(i: integer; sFileName: AnsiString); // save single [UnitX]
procedure SaveAll; // save [Project] and all [UnitX]
procedure LoadLayout; // load all [UnitX]
procedure LoadUnitLayout(e: TEditor; Index: integer); // load single [UnitX] cursor positions
procedure SaveLayout; // save all [UnitX]
procedure SaveUnitLayout(e: TEditor; Index: integer); // save single [UnitX] cursor positions
function MakeProjectNode: TTreeNode;
function MakeNewFileNode(const s: AnsiString; IsFolder: boolean; NewParent: TTreeNode): TTreeNode;
procedure BuildPrivateResource(ForceSave: boolean = False);
procedure LoadOptions;
procedure SaveOptions;
function SaveUnits: Boolean;
// procedure Open;
function FileAlreadyExists(const s: AnsiString): boolean;
function RemoveFolder(Node: TTreeNode): boolean;
function RemoveEditor(index: integer; DoClose: boolean): boolean;
function GetUnitFromString(const s: AnsiString): integer;
procedure RebuildNodes;
function ListUnitStr(Separator: char): AnsiString;
procedure ExportToHTML;
function ShowOptions: Integer;
function AssignTemplate(const aFileName: AnsiString; aTemplate: TTemplate): boolean;
function FolderNodeFromName(const name: AnsiString): TTreeNode;
procedure CreateFolderNodes;
procedure UpdateNodeIndexes;
procedure SetNodeValue(value: TTreeNode);
procedure CheckProjectFileForUpdate;
procedure IncrementBuildNumber;
function GetCompilerOption(const OptionString: AnsiString): Char;
procedure SetCompilerOption(const OptionString: AnsiString; Value: Char);
procedure SaveToLog;
signals:
void changed();
private:
void open();
void sortUnitsByPriority();
private:
QList<PProjectUnit> mUnits;
ProjectOptions mOptions;
QSettings mIniFile;
std::shared_ptr<QSettings> mIniFile;
QString mFilename;
QString mName;
bool mModified;
QStringList mFolders;
std::shared_ptr<CppParser> mParser;
PFolderNode mNode;
};
#endif // PROJECT_H