work save

This commit is contained in:
royqh1979@gmail.com 2021-09-12 22:45:00 +08:00
parent 276c56e270
commit fdafe622f1
10 changed files with 119 additions and 44 deletions

View File

@ -194,6 +194,7 @@
#ifndef INCLUDED_SimpleIni_h
#define INCLUDED_SimpleIni_h
#include <QDebug>
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
@ -376,7 +377,7 @@ public:
public:
FileWriter(FILE * a_file) : m_file(a_file) { }
void Write(const char * a_pBuf) {
fputs(a_pBuf, m_file);
fwrite(a_pBuf,sizeof(char),strlen(a_pBuf),m_file);
}
private:
FileWriter(const FileWriter &); // disable

View File

@ -15,6 +15,7 @@
#include "../parser/cppparser.h"
#include "../autolinkmanager.h"
#include "../platform.h"
#include "../project.h"
#define COMPILE_PROCESS_END "---//END//----"
@ -325,6 +326,18 @@ QString Compiler::getCIncludeArguments()
return result;
}
QString Compiler::getProjectIncludeArguments()
{
QString result;
if (mProject) {
foreach (const QString& folder,mProject->options().includes) {
result += QString(" -I\"%1\"").arg(folder);
}
result += QString(" -I\"%1\"").arg(extractFilePath(mProject->filename()));
}
return result;
}
QString Compiler::getCppIncludeArguments()
{
QString result;
@ -483,6 +496,16 @@ void Compiler::runCommand(const QString &cmd, const QString &arguments, const Q
}
}
const std::shared_ptr<Project> &Compiler::project() const
{
return mProject;
}
void Compiler::setProject(const std::shared_ptr<Project> &newProject)
{
mProject = newProject;
}
bool Compiler::isRebuild() const
{
return mRebuild;

View File

@ -6,6 +6,7 @@
#include "../common.h"
#include "../parser/cppparser.h"
class Project;
class Compiler : public QThread
{
Q_OBJECT
@ -22,6 +23,9 @@ public:
bool isRebuild() const;
void setRebuild(bool isRebuild);
const std::shared_ptr<Project> &project() const;
void setProject(const std::shared_ptr<Project> &newProject);
signals:
void compileStarted();
void compileFinished();
@ -48,6 +52,7 @@ protected:
virtual QString getCCompileArguments(bool checkSyntax);
virtual QString getCppCompileArguments(bool checkSyntax);
virtual QString getCIncludeArguments();
virtual QString getProjectIncludeArguments();
virtual QString getCppIncludeArguments();
virtual QString getLibraryArguments(FileType fileType);
virtual QString parseFileIncludesForAutolink(
@ -69,6 +74,7 @@ protected:
PCompileIssue mLastIssue;
QString mFilename;
bool mRebuild;
std::shared_ptr<Project> mProject;
private:
bool mStop;

View File

@ -61,7 +61,7 @@ void CompilerManager::compile(const QString& filename, const QByteArray& encodin
}
}
void CompilerManager::checkSyntax(const QString &filename, const QString &content, bool isAscii)
void CompilerManager::checkSyntax(const QString &filename, const QString &content, bool isAscii, std::shared_ptr<Project> project)
{
if (!pSettings->compilerSets().defaultSet()) {
QMessageBox::critical(pMainWindow,
@ -77,6 +77,7 @@ void CompilerManager::checkSyntax(const QString &filename, const QString &conten
mSyntaxCheckErrorCount = 0;
mBackgroundSyntaxChecker = new StdinCompiler(filename,content,isAscii,true,true);
mBackgroundSyntaxChecker->setProject(project);
connect(mBackgroundSyntaxChecker, &Compiler::compileFinished, this ,&CompilerManager::onSyntaxCheckFinished);
connect(mBackgroundSyntaxChecker, &Compiler::compileIssue, this, &CompilerManager::onSyntaxCheckIssue);
connect(mBackgroundSyntaxChecker, &Compiler::compileFinished, pMainWindow, &MainWindow::onCompileFinished);

View File

@ -8,6 +8,7 @@
class ExecutableRunner;
class Compiler;
class Project;
class CompilerManager : public QObject
{
Q_OBJECT
@ -19,7 +20,8 @@ public:
bool running();
void compile(const QString& filename, const QByteArray& encoding, bool rebuild, bool silent=false,bool onlyCheckSyntax=false);
void checkSyntax(const QString&filename, const QString& content, bool isAscii);
void compileProject(std::shared_ptr<Project> project);
void checkSyntax(const QString&filename, const QString& content, bool isAscii, std::shared_ptr<Project> project);
void run(const QString& filename, const QString& arguments, const QString& workDir);
void stopRun();
void stopCompile();

View File

@ -49,18 +49,21 @@ bool FileCompiler::prepareForCompile()
case FileType::CSource:
mArguments += getCCompileArguments(mOnlyCheckSyntax);
mArguments += getCIncludeArguments();
mArguments += getProjectIncludeArguments();
strFileType = "C";
mCompiler = compilerSet()->CCompiler();
break;
case FileType::CppSource:
mArguments += getCCompileArguments(mOnlyCheckSyntax);
mArguments += getCIncludeArguments();
mArguments += getCppCompileArguments(mOnlyCheckSyntax);
mArguments += getCppIncludeArguments();
mArguments += getProjectIncludeArguments();
strFileType = "C++";
mCompiler = compilerSet()->cppCompiler();
break;
default:
throw CompileError(tr("Can't find the compiler for file %1").arg(mFilename));
}
mArguments += getLibraryArguments(fileType);
if (!fileExists(mCompiler)) {

View File

@ -36,14 +36,16 @@ bool StdinCompiler::prepareForCompile()
mArguments += " -x c - ";
mArguments += getCCompileArguments(mOnlyCheckSyntax);
mArguments += getCIncludeArguments();
mArguments += getProjectIncludeArguments();
strFileType = "C";
mCompiler = compilerSet()->CCompiler();
break;
case FileType::CppSource:
case FileType::CppHeader:
mArguments += " -x c++ - ";
mArguments += getCCompileArguments(mOnlyCheckSyntax);
mArguments += getCIncludeArguments();
mArguments += getCppCompileArguments(mOnlyCheckSyntax);
mArguments += getCppIncludeArguments();
mArguments += getProjectIncludeArguments();
strFileType = "C++";
mCompiler = compilerSet()->cppCompiler();
break;

View File

@ -759,8 +759,14 @@ void MainWindow::checkSyntaxInBack(Editor *e)
mCheckSyntaxInBack=true;
ui->tableIssues->clearIssues();
CompileTarget target =getCompileTarget();
if (target ==CompileTarget::Project) {
mCompilerManager->checkSyntax(e->filename(),e->lines()->text(),
e->fileEncoding() == ENCODING_ASCII);
e->fileEncoding() == ENCODING_ASCII, mProject);
} else {
mCompilerManager->checkSyntax(e->filename(),e->lines()->text(),
e->fileEncoding() == ENCODING_ASCII, nullptr);
}
// if not PrepareForCompile(cttStdin,True) then begin
// fCheckSyntaxInBack:=False;
// Exit;
@ -775,6 +781,26 @@ void MainWindow::checkSyntaxInBack(Editor *e)
bool MainWindow::compile(bool rebuild)
{
CompileTarget target =getCompileTarget();
if (target == CompileTarget::Project) {
if (!mProject->saveUnits())
return false;
updateAppTitle();
// Check if saves have been succesful
for (int i=0; i<mEditorList->pageCount();i++) {
Editor * e= (*(mEditorList))[i];
if (e->inProject() && e->modified())
return false;
}
// Increment build number automagically
if (mProject->options().versionInfo.autoIncBuildNr) {
mProject->incrementBuildNumber();
}
mProject->buildPrivateResource();
mCompilerManager->compileProject(mProject);
} else {
Editor * editor = mEditorList->getEditor();
if (editor != NULL ) {
ui->tableIssues->clearIssues();
@ -792,6 +818,7 @@ bool MainWindow::compile(bool rebuild)
mCompilerManager->compile(editor->filename(),editor->fileEncoding(),rebuild);
return true;
}
}
return false;
}
@ -1802,7 +1829,17 @@ void MainWindow::dropEvent(QDropEvent *event)
foreach(const QUrl& url, event->mimeData()->urls()){
if (!url.isLocalFile())
continue;
openFile(url.toLocalFile());
QString file = url.toLocalFile();
if (getFileType(file)==FileType::Project) {
openProject(file);
return;
}
}
foreach(const QUrl& url, event->mimeData()->urls()){
if (!url.isLocalFile())
continue;
QString file = url.toLocalFile();
openFile(file);
}
}
}
@ -1813,6 +1850,8 @@ void MainWindow::on_actionSave_triggered()
if (editor != NULL) {
try {
editor->save();
if (editor->inProject() && (mProject))
mProject->saveAll();
} catch(FileError e) {
QMessageBox::critical(this,tr("Error"),e.reason());
}
@ -2242,21 +2281,17 @@ CompileTarget MainWindow::getCompileTarget()
Editor* e = mEditorList->getEditor();
if (e!=nullptr) {
// Treat makefiles as InProject files too
// if ((mProject) and (e.InProject or (fProject.MakeFileName = e.FileName)) then begin
// Result := cttProject;
// end else begin
// Result := cttFile;
// end;
if (mProject
&& (e->inProject() || (mProject->makeFileName() == e->filename()))
) {
target = CompileTarget::Project;
} else {
target = CompileTarget::File;
}
// // No editors have been opened. Check if a project is open
// end else if Assigned(fProject) then begin
// Result := cttProject;
// // No project, no editor...
// end else begin
// Result := cttNone;
// end;
// No editors have been opened. Check if a project is open
} else if (mProject) {
target = CompileTarget::Project;
}
return target;
}

View File

@ -147,11 +147,14 @@ void Project::open()
newUnit->setPriority(ini.GetLongValue(groupName,"Priority", 1000));
newUnit->setOverrideBuildCmd(ini.GetBoolValue(groupName,"OverrideBuildCmd", false));
newUnit->setBuildCmd(fromByteArray(ini.GetValue(groupName,"BuildCmd", "")));
QByteArray defaultEncoding = ENCODING_SYSTEM_DEFAULT;
QByteArray defaultEncoding = toByteArray(mOptions.encoding);
if (ini.GetBoolValue(groupName,"DetectEncoding",true)){
defaultEncoding = ENCODING_AUTO_DETECT;
}
newUnit->setEncoding(ini.GetValue(groupName, "FileEncoding",defaultEncoding));
if (QTextCodec::codecForName(newUnit->encoding())==nullptr) {
newUnit->setEncoding(ENCODING_AUTO_DETECT);
}
newUnit->setEditor(nullptr);
newUnit->setNew(false);
newUnit->setParent(this);
@ -265,7 +268,7 @@ Editor *Project::openUnit(int index)
return editor;
}
QByteArray encoding;
encoding = mOptions.encoding.toLocal8Bit();
encoding = unit->encoding();
editor = pMainWindow->editorList()->newEditor(fullPath, encoding, true, false);
editor->setInProject(true);
unit->setEditor(editor);
@ -399,8 +402,7 @@ void Project::saveAll()
void Project::saveLayout()
{
QString s = changeFileExt(mFilename, "layout");
QSettings layIni(mFilename,QSettings::IniFormat);
QSettings layIni(changeFileExt(mFilename, "layout"),QSettings::IniFormat);
QStringList sl;
// Write list of open project files
for (int i=0;i<pMainWindow->editorList()->pageCount();i++) {
@ -1244,9 +1246,9 @@ void Project::loadOptions(SimpleIni& ini)
mOptions.addCharset = ini.GetBoolValue("Project", "AddCharset", true);
bool useUTF8 = ini.GetBoolValue("Project", "UseUTF8", false);
if (useUTF8) {
mOptions.encoding = fromByteArray(ini.GetValue("Project","Encoding", ENCODING_SYSTEM_DEFAULT));
} else {
mOptions.encoding = fromByteArray(ini.GetValue("Project","Encoding", ENCODING_UTF8));
} else {
mOptions.encoding = fromByteArray(ini.GetValue("Project","Encoding", ENCODING_AUTO_DETECT));
}
mOptions.versionInfo.major = ini.GetLongValue("VersionInfo", "Major", 0);

View File

@ -174,7 +174,7 @@ public:
PProjectUnit addUnit(const QString& inFileName,
PFolderNode parentNode,
bool rebuild);
void buildPrivateResource(bool forceSave);
void buildPrivateResource(bool forceSave=false);
void checkProjectFileForUpdate(SimpleIni& ini);
void closeUnit(int index);
void createFolderNodes();