refactor: open/create project
refactor: save/load project layout
This commit is contained in:
parent
1e5a130def
commit
25ac4d5048
|
@ -70,19 +70,19 @@ const char* SaveException::what() const noexcept {
|
|||
}
|
||||
|
||||
Editor::Editor(QWidget *parent):
|
||||
Editor(parent,QObject::tr("untitled"),ENCODING_AUTO_DETECT,false,true,nullptr)
|
||||
Editor(parent,QObject::tr("untitled"),ENCODING_AUTO_DETECT,nullptr,true,nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
Editor::Editor(QWidget *parent, const QString& filename,
|
||||
const QByteArray& encoding,
|
||||
bool inProject, bool isNew,
|
||||
Project* pProject, bool isNew,
|
||||
QTabWidget* parentPageControl):
|
||||
SynEdit(parent),
|
||||
mEncodingOption(encoding),
|
||||
mFilename(QFileInfo(filename).absoluteFilePath()),
|
||||
mParentPageControl(parentPageControl),
|
||||
mInProject(inProject),
|
||||
mProject(pProject),
|
||||
mIsNew(isNew),
|
||||
mSyntaxIssues(),
|
||||
mSyntaxErrorColor(Qt::red),
|
||||
|
@ -119,8 +119,8 @@ Editor::Editor(QWidget *parent, const QString& filename,
|
|||
setUseCodeFolding(false);
|
||||
}
|
||||
|
||||
if (inProject) {
|
||||
mParser = pMainWindow->project()->cppParser();
|
||||
if (mProject) {
|
||||
mParser = mProject->cppParser();
|
||||
} else {
|
||||
initParser();
|
||||
}
|
||||
|
@ -239,7 +239,7 @@ void Editor::saveFile(QString filename) {
|
|||
this->document()->saveToFile(file,encoding,
|
||||
pSettings->editor().defaultEncoding(),
|
||||
mFileEncoding);
|
||||
emit fileSaved(filename, mInProject);
|
||||
emit fileSaved(filename, inProject());
|
||||
}
|
||||
|
||||
void Editor::convertToEncoding(const QByteArray &encoding)
|
||||
|
@ -335,10 +335,10 @@ bool Editor::saveAs(const QString &name, bool fromProject){
|
|||
return false;
|
||||
}
|
||||
// Update project information
|
||||
if (mInProject && pMainWindow->project() && !fromProject) {
|
||||
PProjectUnit unit = pMainWindow->project()->findUnit(newName);
|
||||
if (mProject && !fromProject) {
|
||||
PProjectUnit unit = mProject->findUnit(newName);
|
||||
if (!unit) {
|
||||
mInProject = false;
|
||||
setProject(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -358,8 +358,8 @@ bool Editor::saveAs(const QString &name, bool fromProject){
|
|||
exception.reason());
|
||||
return false;
|
||||
}
|
||||
if (pMainWindow->project() && !fromProject) {
|
||||
pMainWindow->project()->associateEditor(this);
|
||||
if (mProject && !fromProject) {
|
||||
mProject->associateEditor(this);
|
||||
}
|
||||
pMainWindow->fileSystemWatcher()->addPath(mFilename);
|
||||
switch(getFileType(mFilename)) {
|
||||
|
@ -420,16 +420,13 @@ void Editor::setEncodingOption(const QByteArray& encoding) noexcept{
|
|||
loadFile();
|
||||
else
|
||||
pMainWindow->updateForEncodingInfo();
|
||||
if (mInProject) {
|
||||
std::shared_ptr<Project> project = pMainWindow->project();
|
||||
if (project) {
|
||||
PProjectUnit unit = project->findUnit(this);
|
||||
if (mProject) {
|
||||
PProjectUnit unit = mProject->findUnit(this);
|
||||
if (unit) {
|
||||
unit->setEncoding(mEncodingOption);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const QByteArray& Editor::fileEncoding() const noexcept{
|
||||
return mFileEncoding;
|
||||
}
|
||||
|
@ -437,7 +434,7 @@ const QString& Editor::filename() const noexcept{
|
|||
return mFilename;
|
||||
}
|
||||
bool Editor::inProject() const noexcept{
|
||||
return mInProject;
|
||||
return mProject!=nullptr;
|
||||
}
|
||||
bool Editor::isNew() const noexcept {
|
||||
return mIsNew;
|
||||
|
@ -1739,7 +1736,7 @@ bool Editor::isBraceChar(QChar ch)
|
|||
|
||||
bool Editor::shouldOpenInReadonly()
|
||||
{
|
||||
if (pMainWindow->project() && pMainWindow->project()->findUnit(mFilename))
|
||||
if (mProject && mProject->findUnit(mFilename))
|
||||
return false;
|
||||
return pSettings->editor().readOnlySytemHeader()
|
||||
&& mParser && (mParser->isSystemHeaderFile(mFilename) || mParser->isProjectHeaderFile(mFilename));
|
||||
|
@ -2682,19 +2679,22 @@ Editor::QuoteStatus Editor::getQuoteStatus()
|
|||
|
||||
void Editor::reparse()
|
||||
{
|
||||
if (!pSettings->codeCompletion().enabled())
|
||||
return;
|
||||
if (!highlighter())
|
||||
return;
|
||||
if (highlighter()->language() != QSynedit::HighlighterLanguage::Cpp
|
||||
&& highlighter()->language() != QSynedit::HighlighterLanguage::GLSL)
|
||||
return;
|
||||
if (mParser)
|
||||
mParser->setEnabled(pSettings->codeCompletion().enabled());
|
||||
return;
|
||||
//mParser->setEnabled(pSettings->codeCompletion().enabled());
|
||||
ParserLanguage language = mUseCppSyntax?ParserLanguage::CPlusPlus:ParserLanguage::C;
|
||||
if (language!=mParser->language() && !mInProject) {
|
||||
if (language!=mParser->language() && !inProject()) {
|
||||
mParser->setLanguage(language);
|
||||
resetCppParser(mParser);
|
||||
}
|
||||
parseFile(mParser,mFilename,mInProject);
|
||||
parseFile(mParser,mFilename, inProject());
|
||||
}
|
||||
|
||||
void Editor::reparseTodo()
|
||||
|
@ -3919,13 +3919,13 @@ void Editor::setUseCppSyntax(bool newUseCppSyntax)
|
|||
mUseCppSyntax = newUseCppSyntax;
|
||||
}
|
||||
|
||||
void Editor::setInProject(bool newInProject)
|
||||
void Editor::setProject(Project *pProject)
|
||||
{
|
||||
if (mInProject == newInProject)
|
||||
if (mProject == pProject)
|
||||
return;
|
||||
mInProject = newInProject;
|
||||
if (mInProject) {
|
||||
mParser = pMainWindow->project()->cppParser();
|
||||
mProject = pProject;
|
||||
if (mProject) {
|
||||
mParser = mProject->cppParser();
|
||||
if (isVisible()) {
|
||||
if (mParser && mParser->parsing()) {
|
||||
connect(mParser.get(),
|
||||
|
|
|
@ -31,6 +31,8 @@
|
|||
#define USER_CODE_IN_REPL_POS_BEGIN "%REPL_BEGIN%"
|
||||
#define USER_CODE_IN_REPL_POS_END "%REPL_END%"
|
||||
|
||||
|
||||
class Project;
|
||||
struct TabStop {
|
||||
int x;
|
||||
int endX;
|
||||
|
@ -134,7 +136,7 @@ public:
|
|||
|
||||
explicit Editor(QWidget *parent, const QString& filename,
|
||||
const QByteArray& encoding,
|
||||
bool inProject, bool isNew,QTabWidget* parentPageControl);
|
||||
Project* pProject, bool isNew,QTabWidget* parentPageControl);
|
||||
|
||||
~Editor();
|
||||
|
||||
|
@ -293,7 +295,7 @@ private:
|
|||
QByteArray mFileEncoding; // the real encoding of the file (auto detected)
|
||||
QString mFilename;
|
||||
QTabWidget* mParentPageControl;
|
||||
bool mInProject;
|
||||
Project* mProject;
|
||||
bool mIsNew;
|
||||
QMap<int,PSyntaxIssueList> mSyntaxIssues;
|
||||
QColor mSyntaxErrorColor;
|
||||
|
@ -363,7 +365,7 @@ public:
|
|||
bool event(QEvent *event) override;
|
||||
|
||||
// QWidget interface
|
||||
void setInProject(bool newInProject);
|
||||
void setProject(Project* pProject);
|
||||
|
||||
bool useCppSyntax() const;
|
||||
void setUseCppSyntax(bool newUseCppSyntax);
|
||||
|
|
|
@ -42,7 +42,7 @@ EditorList::EditorList(QTabWidget* leftPageWidget,
|
|||
}
|
||||
|
||||
Editor* EditorList::newEditor(const QString& filename, const QByteArray& encoding,
|
||||
bool inProject, bool newFile,
|
||||
Project *pProject, bool newFile,
|
||||
QTabWidget* page) {
|
||||
QTabWidget * parentPageControl = nullptr;
|
||||
if (page == nullptr)
|
||||
|
@ -52,16 +52,16 @@ Editor* EditorList::newEditor(const QString& filename, const QByteArray& encodin
|
|||
if (fileExists(filename)) {
|
||||
pMainWindow->fileSystemWatcher()->addPath(filename);
|
||||
}
|
||||
Editor * e = new Editor(parentPageControl,filename,encoding,inProject,newFile,parentPageControl);
|
||||
Editor * e = new Editor(parentPageControl,filename,encoding,pProject,newFile,parentPageControl);
|
||||
connect(e, &Editor::renamed, this, &EditorList::onEditorRenamed);
|
||||
updateLayout();
|
||||
if (pMainWindow->project()){
|
||||
PProjectUnit unit = pMainWindow->project()->findUnit(filename);
|
||||
if (unit) {
|
||||
pMainWindow->project()->associateEditorToUnit(e,unit);
|
||||
e->setInProject(true);
|
||||
}
|
||||
}
|
||||
// if (pMainWindow->project()){
|
||||
// PProjectUnit unit = pMainWindow->project()->findUnit(filename);
|
||||
// if (unit) {
|
||||
// pMainWindow->project()->associateEditorToUnit(e,unit);
|
||||
// e->setInProject(true);
|
||||
// }
|
||||
// }
|
||||
connect(e,&Editor::fileSaved,
|
||||
pMainWindow, &MainWindow::onFileSaved);
|
||||
return e;
|
||||
|
@ -368,7 +368,7 @@ Editor *EditorList::getEditorByFilename(QString filename)
|
|||
QFileInfo fileInfo(filename);
|
||||
QString fullname = fileInfo.absoluteFilePath();
|
||||
if (fileInfo.exists() && fileInfo.isFile())
|
||||
return newEditor(fullname,pSettings->editor().autoDetectFileEncoding()?ENCODING_AUTO_DETECT:pSettings->editor().defaultEncoding(),false,false);
|
||||
return newEditor(fullname,pSettings->editor().autoDetectFileEncoding()?ENCODING_AUTO_DETECT:pSettings->editor().defaultEncoding(),nullptr,false);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <QWidget>
|
||||
#include "utils.h"
|
||||
|
||||
class Project;
|
||||
class Editor;
|
||||
class EditorList : public QObject
|
||||
{
|
||||
|
@ -39,7 +40,7 @@ public:
|
|||
QWidget* panel, QObject* parent = nullptr);
|
||||
|
||||
Editor* newEditor(const QString& filename, const QByteArray& encoding,
|
||||
bool inProject, bool newFile,
|
||||
Project *pProject, bool newFile,
|
||||
QTabWidget* page=nullptr);
|
||||
|
||||
Editor* getEditor(int index=-1, QTabWidget* tabsWidget=nullptr) const;
|
||||
|
|
|
@ -375,7 +375,7 @@ int main(int argc, char *argv[])
|
|||
} else {
|
||||
if (pSettings->editor().autoLoadLastFiles())
|
||||
pMainWindow->loadLastOpens();
|
||||
if (pMainWindow->editorList()->pageCount()==0) {
|
||||
if (pMainWindow->editorList()->pageCount()==0 && !pMainWindow->project()) {
|
||||
pMainWindow->newEditor();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1191,8 +1191,9 @@ void MainWindow::openFile(const QString &filename, bool activate, QTabWidget* pa
|
|||
bool inProject = (mProject && unit);
|
||||
QByteArray encoding = unit ? unit->encoding() :
|
||||
(pSettings->editor().autoDetectFileEncoding()? ENCODING_AUTO_DETECT : pSettings->editor().defaultEncoding());
|
||||
Project * pProject = (inProject?mProject.get():nullptr);
|
||||
editor = mEditorList->newEditor(filename,encoding,
|
||||
inProject, false, page);
|
||||
pProject, false, page);
|
||||
// if (mProject) {
|
||||
// mProject->associateEditorToUnit(editor,unit);
|
||||
// }
|
||||
|
@ -1242,9 +1243,7 @@ void MainWindow::openProject(const QString &filename, bool openFiles)
|
|||
auto action = finally([this]{
|
||||
mClassBrowserModel.endUpdate();
|
||||
});
|
||||
mProject = std::make_shared<Project>(filename,DEV_INTERNAL_OPEN,
|
||||
mEditorList,
|
||||
&mFileSystemWatcher);
|
||||
mProject = Project::load(filename,mEditorList,&mFileSystemWatcher);
|
||||
updateProjectView();
|
||||
ui->projectView->expand(
|
||||
mProjectProxyModel->mapFromSource(
|
||||
|
@ -1258,6 +1257,7 @@ void MainWindow::openProject(const QString &filename, bool openFiles)
|
|||
|
||||
//parse the project
|
||||
// UpdateClassBrowsing;
|
||||
|
||||
scanActiveProject(true);
|
||||
if (openFiles) {
|
||||
PProjectUnit unit = mProject->doAutoOpen();
|
||||
|
@ -2105,7 +2105,10 @@ void MainWindow::scanActiveProject(bool parse)
|
|||
{
|
||||
if (!mProject)
|
||||
return;
|
||||
mProject->cppParser()->setEnabled(pSettings->codeCompletion().enabled());
|
||||
if (!pSettings->codeCompletion().enabled())
|
||||
return;
|
||||
if (!mProject->cppParser()->enabled())
|
||||
return;
|
||||
|
||||
//UpdateClassBrowsing;
|
||||
if (parse) {
|
||||
|
@ -2223,7 +2226,8 @@ void MainWindow::loadLastOpens()
|
|||
bool inProject = (mProject && unit);
|
||||
QByteArray encoding = unit ? unit->encoding() :
|
||||
(pSettings->editor().autoDetectFileEncoding()? ENCODING_AUTO_DETECT : pSettings->editor().defaultEncoding());
|
||||
Editor * editor = mEditorList->newEditor(editorFilename, encoding, inProject,false,page);
|
||||
Project* pProject = (inProject?mProject.get():nullptr);
|
||||
Editor * editor = mEditorList->newEditor(editorFilename, encoding, pProject,false,page);
|
||||
|
||||
if (inProject && editor) {
|
||||
mProject->loadUnitLayout(editor);
|
||||
|
@ -2247,6 +2251,8 @@ void MainWindow::loadLastOpens()
|
|||
focusedEditor = editor;
|
||||
pSettings->history().removeFile(editorFilename);
|
||||
}
|
||||
if (mProject && mEditorList->pageCount()==0)
|
||||
mProject->doAutoOpen();
|
||||
if (count>0) {
|
||||
updateEditorActions();
|
||||
//updateForEncodingInfo();
|
||||
|
@ -2315,7 +2321,7 @@ void MainWindow::newEditor()
|
|||
try {
|
||||
Editor * editor=mEditorList->newEditor("",
|
||||
pSettings->editor().defaultEncoding(),
|
||||
false,true);
|
||||
nullptr,true);
|
||||
editor->activate();
|
||||
//updateForEncodingInfo();
|
||||
} catch (FileError e) {
|
||||
|
@ -4269,7 +4275,6 @@ void MainWindow::closeProject(bool refreshEditor)
|
|||
auto action3 = finally([this]{
|
||||
mEditorList->endUpdate();
|
||||
});
|
||||
mProject->closeAllUnits();
|
||||
mProject.reset();
|
||||
|
||||
if (!mQuitting && refreshEditor) {
|
||||
|
@ -5856,12 +5861,11 @@ void MainWindow::on_actionNew_Project_triggered()
|
|||
}
|
||||
}
|
||||
|
||||
// Create an empty project
|
||||
mProject = std::make_shared<Project>(s,dialog.getProjectName(),
|
||||
mProject = Project::create(s,dialog.getProjectName(),
|
||||
mEditorList,
|
||||
&mFileSystemWatcher);
|
||||
if (!mProject->assignTemplate(dialog.getTemplate(),dialog.isCppProject())) {
|
||||
mProject = nullptr;
|
||||
&mFileSystemWatcher,
|
||||
dialog.getTemplate(),dialog.isCppProject());
|
||||
if (!mProject) {
|
||||
QMessageBox::critical(this,
|
||||
tr("New project fail"),
|
||||
tr("Can't assign project template"),
|
||||
|
@ -5869,6 +5873,7 @@ void MainWindow::on_actionNew_Project_triggered()
|
|||
}
|
||||
mProject->saveAll();
|
||||
updateProjectView();
|
||||
scanActiveProject(true);
|
||||
}
|
||||
pSettings->ui().setNewProjectDialogWidth(dialog.width());
|
||||
pSettings->ui().setNewProjectDialogHeight(dialog.height());
|
||||
|
|
|
@ -4780,6 +4780,8 @@ void parseFile(PCppParser parser, const QString& fileName, bool inProject, bool
|
|||
{
|
||||
if (!parser)
|
||||
return;
|
||||
if (!parser->enabled())
|
||||
return;
|
||||
CppFileParserThread* thread = new CppFileParserThread(parser,fileName,inProject,onlyIfNotParsed,updateView);
|
||||
thread->connect(thread,
|
||||
&QThread::finished,
|
||||
|
@ -4792,6 +4794,8 @@ void parseFileList(PCppParser parser, bool updateView)
|
|||
{
|
||||
if (!parser)
|
||||
return;
|
||||
if (!parser->enabled())
|
||||
return;
|
||||
CppFileListParserThread *thread = new CppFileListParserThread(parser,updateView);
|
||||
thread->connect(thread,
|
||||
&QThread::finished,
|
||||
|
|
|
@ -49,6 +49,7 @@ Project::Project(const QString &filename, const QString &name,
|
|||
QFileSystemWatcher* fileSystemWatcher,
|
||||
QObject *parent) :
|
||||
QObject(parent),
|
||||
mName(name),
|
||||
mModel(this),
|
||||
mEditorList(editorList),
|
||||
mFileSystemWatcher(fileSystemWatcher)
|
||||
|
@ -59,18 +60,44 @@ Project::Project(const QString &filename, const QString &name,
|
|||
std::bind(
|
||||
&EditorList::getContentFromOpenedEditor,mEditorList,
|
||||
std::placeholders::_1, std::placeholders::_2));
|
||||
if (name == DEV_INTERNAL_OPEN) {
|
||||
mModified = false;
|
||||
open();
|
||||
} else {
|
||||
mName = name;
|
||||
SimpleIni ini;
|
||||
ini.SetValue("Project","filename", toByteArray(extractRelativePath(directory(),mFilename)));
|
||||
ini.SetValue("Project","name", toByteArray(mName));
|
||||
ini.SaveFile(mFilename.toLocal8Bit());
|
||||
mRootNode = makeProjectNode();
|
||||
}
|
||||
resetCppParser(mParser,mOptions.compilerSet);
|
||||
|
||||
std::shared_ptr<Project> Project::load(const QString &filename, EditorList *editorList, QFileSystemWatcher *fileSystemWatcher, QObject *parent)
|
||||
{
|
||||
std::shared_ptr<Project> project=std::make_shared<Project>(filename,
|
||||
"",
|
||||
editorList,
|
||||
fileSystemWatcher,
|
||||
parent);
|
||||
project->open();
|
||||
project->mModified = false;
|
||||
resetCppParser(project->mParser,project->mOptions.compilerSet);
|
||||
return project;
|
||||
}
|
||||
|
||||
std::shared_ptr<Project> Project::create(
|
||||
const QString &filename, const QString &name,
|
||||
EditorList *editorList, QFileSystemWatcher *fileSystemWatcher,
|
||||
const std::shared_ptr<ProjectTemplate> pTemplate,
|
||||
bool useCpp, QObject *parent)
|
||||
{
|
||||
std::shared_ptr<Project> project=std::make_shared<Project>(filename,
|
||||
name,
|
||||
editorList,
|
||||
fileSystemWatcher,
|
||||
parent);
|
||||
SimpleIni ini;
|
||||
ini.SetValue("Project","filename", toByteArray(extractRelativePath(project->directory(),
|
||||
project->mFilename)));
|
||||
ini.SetValue("Project","name", toByteArray(project->mName));
|
||||
ini.SaveFile(project->mFilename.toLocal8Bit());
|
||||
project->mParser->setEnabled(false);
|
||||
if (!project->assignTemplate(pTemplate,useCpp))
|
||||
return std::shared_ptr<Project>();
|
||||
resetCppParser(project->mParser,project->mOptions.compilerSet);
|
||||
|
||||
project->mModified = true;
|
||||
return project;
|
||||
}
|
||||
|
||||
Project::~Project()
|
||||
|
@ -79,6 +106,7 @@ Project::~Project()
|
|||
foreach (const PProjectUnit& unit, mUnits) {
|
||||
Editor * editor = unitEditor(unit);
|
||||
if (editor) {
|
||||
editor->setProject(nullptr);
|
||||
mEditorList->forceCloseEditor(editor);
|
||||
}
|
||||
}
|
||||
|
@ -359,15 +387,15 @@ Editor* Project::openUnit(PProjectUnit& unit, bool forceOpen) {
|
|||
|
||||
Editor * editor = mEditorList->getOpenedEditorByFilename(unit->fileName());
|
||||
if (editor) {//already opened in the editors
|
||||
editor->setInProject(true);
|
||||
editor->setProject(this);
|
||||
editor->activate();
|
||||
return editor;
|
||||
}
|
||||
QByteArray encoding;
|
||||
encoding = unit->encoding();
|
||||
editor = mEditorList->newEditor(unit->fileName(), encoding, true, unit->isNew());
|
||||
editor = mEditorList->newEditor(unit->fileName(), encoding, this, unit->isNew());
|
||||
if (editor) {
|
||||
editor->setInProject(true);
|
||||
//editor->setProject(this);
|
||||
//unit->setEncoding(encoding);
|
||||
loadUnitLayout(editor);
|
||||
editor->activate();
|
||||
|
@ -386,15 +414,15 @@ Editor *Project::openUnit(PProjectUnit &unit, const PProjectEditorLayout &layout
|
|||
|
||||
Editor * editor = mEditorList->getOpenedEditorByFilename(unit->fileName());
|
||||
if (editor) {//already opened in the editors
|
||||
editor->setInProject(true);
|
||||
editor->setProject(this);
|
||||
editor->activate();
|
||||
return editor;
|
||||
}
|
||||
QByteArray encoding;
|
||||
encoding = unit->encoding();
|
||||
editor = mEditorList->newEditor(unit->fileName(), encoding, true, unit->isNew());
|
||||
editor = mEditorList->newEditor(unit->fileName(), encoding, this, unit->isNew());
|
||||
if (editor) {
|
||||
editor->setInProject(true);
|
||||
//editor->setInProject(true);
|
||||
editor->setCaretY(layout->caretY);
|
||||
editor->setCaretX(layout->caretX);
|
||||
editor->setTopLine(layout->topLine);
|
||||
|
@ -485,7 +513,7 @@ bool Project::removeUnit(PProjectUnit& unit, bool doClose , bool removeFile)
|
|||
if (doClose) {
|
||||
Editor* editor = unitEditor(unit);
|
||||
if (editor) {
|
||||
editor->setInProject(false);
|
||||
editor->setProject(nullptr);
|
||||
mEditorList->closeEditor(editor);
|
||||
}
|
||||
}
|
||||
|
@ -578,7 +606,8 @@ void Project::saveAll()
|
|||
|
||||
void Project::saveLayout()
|
||||
{
|
||||
QJsonObject jsonRoot;
|
||||
QHash<QString, PProjectEditorLayout> oldLayouts = loadLayout();
|
||||
|
||||
QHash<QString,int> editorOrderSet;
|
||||
// Write list of open project files
|
||||
int order=0;
|
||||
|
@ -594,8 +623,6 @@ void Project::saveLayout()
|
|||
Editor *e, *e2;
|
||||
// Remember what files were visible
|
||||
mEditorList->getVisibleEditors(e, e2);
|
||||
if (e)
|
||||
jsonRoot["focused"]=e->filename();
|
||||
|
||||
QJsonArray jsonLayouts;
|
||||
// save editor info
|
||||
|
@ -608,18 +635,33 @@ void Project::saveLayout()
|
|||
jsonLayout["caretY"]=editor->caretY();
|
||||
jsonLayout["topLine"]=editor->topLine();
|
||||
jsonLayout["leftChar"]=editor->leftChar();
|
||||
jsonLayout["isOpen"]=true;
|
||||
jsonLayout["focused"]=(editor==e);
|
||||
int order=editorOrderSet.value(editor->filename(),-1);
|
||||
if (order>=0) {
|
||||
jsonLayout["order"]=order;
|
||||
}
|
||||
jsonLayouts.append(jsonLayout);
|
||||
} else {
|
||||
PProjectEditorLayout oldLayout = oldLayouts.value(unit->fileName(),PProjectEditorLayout());
|
||||
if (oldLayout) {
|
||||
QJsonObject jsonLayout;
|
||||
jsonLayout["filename"]=unit->fileName();
|
||||
jsonLayout["caretX"]=oldLayout->caretX;
|
||||
jsonLayout["caretY"]=oldLayout->caretY;
|
||||
jsonLayout["topLine"]=oldLayout->topLine;
|
||||
jsonLayout["leftChar"]=oldLayout->leftChar;
|
||||
jsonLayout["isOpen"]=false;
|
||||
jsonLayout["focused"]=false;
|
||||
jsonLayouts.append(jsonLayout);
|
||||
}
|
||||
}
|
||||
jsonRoot["editorLayouts"]=jsonLayouts;
|
||||
}
|
||||
|
||||
QString jsonFilename = changeFileExt(filename(), "layout");
|
||||
QFile file(jsonFilename);
|
||||
if (file.open(QFile::WriteOnly|QFile::Truncate)) {
|
||||
QJsonDocument doc(jsonRoot);
|
||||
QJsonDocument doc(jsonLayouts);
|
||||
file.write(doc.toJson(QJsonDocument::Indented));
|
||||
file.close();
|
||||
} else {
|
||||
|
@ -740,12 +782,12 @@ void Project::associateEditorToUnit(Editor *editor, PProjectUnit unit)
|
|||
{
|
||||
if (!unit) {
|
||||
if (editor)
|
||||
editor->setInProject(false);
|
||||
editor->setProject(nullptr);
|
||||
return;
|
||||
}
|
||||
if (editor) {
|
||||
unit->setEncoding(editor->encodingOption());
|
||||
editor->setInProject(true);
|
||||
editor->setProject(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -840,7 +882,7 @@ bool Project::assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, b
|
|||
}
|
||||
mModel.beginUpdate();
|
||||
mRootNode = makeProjectNode();
|
||||
|
||||
rebuildNodes();
|
||||
mOptions = aTemplate->options();
|
||||
mOptions.compilerSet = pSettings->compilerSets().defaultIndex();
|
||||
mOptions.isCpp = useCpp;
|
||||
|
@ -887,7 +929,7 @@ bool Project::assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, b
|
|||
Editor * editor = mEditorList->newEditor(
|
||||
unit->fileName(),
|
||||
unit->encoding(),
|
||||
true,
|
||||
this,
|
||||
true);
|
||||
|
||||
QString s2 = dir.absoluteFilePath(s);
|
||||
|
@ -1148,7 +1190,7 @@ PProjectUnit Project::addUnit(const QString &inFileName, PProjectModelNode paren
|
|||
Editor * e= unitEditor(newUnit);
|
||||
if (e) {
|
||||
newUnit->setEncoding(e->fileEncoding());
|
||||
e->setInProject(true);
|
||||
e->setProject(this);
|
||||
} else {
|
||||
newUnit->setEncoding(options().encoding.toUtf8());
|
||||
}
|
||||
|
@ -1473,17 +1515,6 @@ void Project::buildPrivateResource(bool forceSave)
|
|||
stringsToFile(contents,hFile);
|
||||
}
|
||||
|
||||
void Project::closeAllUnits()
|
||||
{
|
||||
foreach (PProjectUnit unit, mUnits) {
|
||||
Editor * editor = unitEditor(unit);
|
||||
if (editor) {
|
||||
editor->setInProject(false);
|
||||
mEditorList->forceCloseEditor(editor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Project::checkProjectFileForUpdate(SimpleIni &ini)
|
||||
{
|
||||
bool cnvt = false;
|
||||
|
@ -1539,7 +1570,7 @@ void Project::closeUnit(PProjectUnit& unit)
|
|||
saveLayout();
|
||||
Editor * editor = unitEditor(unit);
|
||||
if (editor) {
|
||||
editor->setInProject(false);
|
||||
editor->setProject(nullptr);
|
||||
mEditorList->forceCloseEditor(editor);
|
||||
}
|
||||
}
|
||||
|
@ -1648,7 +1679,37 @@ void Project::createFileSystemFolderNode(
|
|||
|
||||
PProjectUnit Project::doAutoOpen()
|
||||
{
|
||||
return loadLayout();
|
||||
QHash<QString,PProjectEditorLayout> layouts = loadLayout();
|
||||
|
||||
QHash<int,PProjectEditorLayout> opennedMap;
|
||||
|
||||
QString focusedFilename;
|
||||
foreach (const PProjectEditorLayout &layout,layouts) {
|
||||
if (layout->isOpen && layout->order>=0) {
|
||||
if (layout->isFocused)
|
||||
focusedFilename = layout->filename;
|
||||
opennedMap.insert(layout->order,layout);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0;i<mUnits.count();i++) {
|
||||
PProjectEditorLayout editorLayout = opennedMap.value(i,PProjectEditorLayout());
|
||||
if (editorLayout) {
|
||||
PProjectUnit unit = findUnit(editorLayout->filename);
|
||||
openUnit(unit,editorLayout);
|
||||
}
|
||||
}
|
||||
|
||||
if (!focusedFilename.isEmpty()) {
|
||||
PProjectUnit unit = findUnit(focusedFilename);
|
||||
if (unit) {
|
||||
Editor * editor = unitEditor(unit);
|
||||
if (editor)
|
||||
editor->activate();
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
return PProjectUnit();
|
||||
}
|
||||
|
||||
bool Project::fileAlreadyExists(const QString &s)
|
||||
|
@ -1755,25 +1816,22 @@ void Project::incrementBuildNumber()
|
|||
setModified(true);
|
||||
}
|
||||
|
||||
PProjectUnit Project::loadLayout()
|
||||
QHash<QString, PProjectEditorLayout> Project::loadLayout()
|
||||
{
|
||||
QHash<QString,PProjectEditorLayout> layouts;
|
||||
QString jsonFilename = changeFileExt(filename(), "layout");
|
||||
qDebug()<<"read file"<<jsonFilename;
|
||||
QFile file(jsonFilename);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return PProjectUnit();
|
||||
return layouts;
|
||||
QByteArray content = file.readAll();
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument doc(QJsonDocument::fromJson(content,&parseError));
|
||||
file.close();
|
||||
if (parseError.error!=QJsonParseError::NoError)
|
||||
return PProjectUnit();
|
||||
if (parseError.error!=QJsonParseError::NoError || !doc.isArray())
|
||||
return layouts;
|
||||
|
||||
QJsonObject jsonRoot=doc.object();
|
||||
QJsonArray jsonLayouts=doc.array();
|
||||
|
||||
QJsonArray jsonLayouts=jsonRoot["editorLayouts"].toArray();
|
||||
|
||||
QHash<int,PProjectEditorLayout> opennedMap;
|
||||
for (int i=0;i<jsonLayouts.size();i++) {
|
||||
QJsonObject jsonLayout = jsonLayouts[i].toObject();
|
||||
QString unitFilename = jsonLayout["filename"].toString();
|
||||
|
@ -1784,32 +1842,14 @@ PProjectUnit Project::loadLayout()
|
|||
editorLayout->leftChar=jsonLayout["leftChar"].toInt();
|
||||
editorLayout->caretX=jsonLayout["caretX"].toInt();
|
||||
editorLayout->caretY=jsonLayout["caretY"].toInt();
|
||||
int order = jsonLayout["order"].toInt(-1);
|
||||
if (order>=0)
|
||||
opennedMap.insert(order,editorLayout);
|
||||
editorLayout->order=jsonLayout["order"].toInt(-1);
|
||||
editorLayout->isFocused=jsonLayout["focused"].toBool();
|
||||
editorLayout->isOpen=jsonLayout["isOpen"].toBool();
|
||||
layouts.insert(unitFilename,editorLayout);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0;i<mUnits.count();i++) {
|
||||
PProjectEditorLayout editorLayout = opennedMap.value(i,PProjectEditorLayout());
|
||||
if (editorLayout) {
|
||||
PProjectUnit unit = findUnit(editorLayout->filename);
|
||||
openUnit(unit,editorLayout);
|
||||
}
|
||||
}
|
||||
|
||||
QString focusedFilename = jsonRoot["focused"].toString();
|
||||
|
||||
if (!focusedFilename.isEmpty()) {
|
||||
PProjectUnit unit = findUnit(focusedFilename);
|
||||
if (unit) {
|
||||
Editor * editor = unitEditor(unit);
|
||||
if (editor)
|
||||
editor->activate();
|
||||
}
|
||||
return unit;
|
||||
}
|
||||
return PProjectUnit();
|
||||
return layouts;
|
||||
}
|
||||
|
||||
void Project::loadOptions(SimpleIni& ini)
|
||||
|
@ -2057,38 +2097,17 @@ void Project::loadUnitLayout(Editor *e)
|
|||
if (!e)
|
||||
return;
|
||||
|
||||
QString jsonFilename = changeFileExt(filename(), "layout");
|
||||
QFile file(jsonFilename);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
return;
|
||||
QByteArray content = file.readAll();
|
||||
QJsonParseError parseError;
|
||||
QJsonDocument doc{QJsonDocument::fromJson(content,&parseError)};
|
||||
file.close();
|
||||
if (parseError.error!=QJsonParseError::NoError)
|
||||
return;
|
||||
QHash<QString, PProjectEditorLayout> layouts = loadLayout();
|
||||
|
||||
QJsonObject jsonRoot=doc.object();
|
||||
|
||||
QJsonArray jsonLayouts=jsonRoot["editorLayouts"].toArray();
|
||||
|
||||
QHash<int,PProjectEditorLayout> opennedMap;
|
||||
for (int i=0;i<jsonLayouts.size();i++) {
|
||||
QJsonObject jsonLayout = jsonLayouts[i].toObject();
|
||||
QString unitFilename = jsonLayout["filename"].toString();
|
||||
if (unitFilename.compare(e->filename(),PATH_SENSITIVITY)==0) {
|
||||
qDebug()<<i<<unitFilename<<e->filename();
|
||||
e->setCaretY(jsonLayout["caretY"].toInt());
|
||||
e->setCaretX(jsonLayout["caretX"].toInt());
|
||||
e->setTopLine(jsonLayout["topLine"].toInt());
|
||||
e->setLeftChar(jsonLayout["leftChar"].toInt());
|
||||
return;
|
||||
PProjectEditorLayout layout = layouts.value(e->filename(),PProjectEditorLayout());
|
||||
if (layout) {
|
||||
e->setCaretY(layout->caretY);
|
||||
e->setCaretX(layout->caretX);
|
||||
e->setTopLine(layout->topLine);
|
||||
e->setLeftChar(layout->leftChar);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
PCppParser Project::cppParser()
|
||||
{
|
||||
return mParser;
|
||||
|
|
|
@ -65,6 +65,9 @@ struct ProjectEditorLayout {
|
|||
int leftChar;
|
||||
int caretX;
|
||||
int caretY;
|
||||
int order;
|
||||
bool isFocused;
|
||||
bool isOpen;
|
||||
};
|
||||
|
||||
using PProjectEditorLayout = std::shared_ptr<ProjectEditorLayout>;
|
||||
|
@ -189,6 +192,19 @@ public:
|
|||
EditorList* editorList,
|
||||
QFileSystemWatcher* fileSystemWatcher,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
static std::shared_ptr<Project> load(const QString& filename,
|
||||
EditorList* editorList,
|
||||
QFileSystemWatcher* fileSystemWatcher,
|
||||
QObject *parent = nullptr);
|
||||
static std::shared_ptr<Project> create(const QString& filename,
|
||||
const QString& name,
|
||||
EditorList* editorList,
|
||||
QFileSystemWatcher* fileSystemWatcher,
|
||||
const std::shared_ptr<ProjectTemplate> pTemplate,
|
||||
bool useCpp,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
~Project();
|
||||
QString directory() const;
|
||||
QString executable() const;
|
||||
|
@ -202,7 +218,6 @@ public:
|
|||
PProjectModelNode parentNode);
|
||||
QString folder();
|
||||
void buildPrivateResource(bool forceSave=false);
|
||||
void closeAllUnits();
|
||||
void closeUnit(PProjectUnit& unit);
|
||||
PProjectUnit doAutoOpen();
|
||||
bool fileAlreadyExists(const QString& s);
|
||||
|
@ -241,7 +256,6 @@ public:
|
|||
void updateFolders();
|
||||
void setCompilerSet(int compilerSetIndex);
|
||||
|
||||
bool assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, bool useCpp);
|
||||
bool saveAsTemplate(const QString& templateFolder,
|
||||
const QString& name,
|
||||
const QString& description,
|
||||
|
@ -277,6 +291,7 @@ signals:
|
|||
void modifyChanged(bool value);
|
||||
|
||||
private:
|
||||
bool assignTemplate(const std::shared_ptr<ProjectTemplate> aTemplate, bool useCpp);
|
||||
void checkProjectFileForUpdate(SimpleIni& ini);
|
||||
void createFolderNodes();
|
||||
void createFileSystemFolderNodes();
|
||||
|
@ -285,7 +300,8 @@ private:
|
|||
PProjectModelNode findFileSystemFolderNode(const QString& folderPath, ProjectModelNodeType nodeType);
|
||||
PProjectModelNode getCustomeFolderNodeFromName(const QString& name);
|
||||
void loadOptions(SimpleIni& ini);
|
||||
PProjectUnit loadLayout();
|
||||
//PProjectUnit
|
||||
QHash<QString, PProjectEditorLayout> loadLayout();
|
||||
|
||||
PProjectModelNode makeNewFolderNode(
|
||||
const QString& folderName,
|
||||
|
|
Loading…
Reference in New Issue