done: save/load project layout

This commit is contained in:
Roy Qu 2022-10-02 23:32:33 +08:00
parent 4542df6049
commit 70e03abdb1
4 changed files with 157 additions and 55 deletions

View File

@ -1246,6 +1246,9 @@ void MainWindow::openProject(const QString &filename, bool openFiles)
mEditorList,
&mFileSystemWatcher);
updateProjectView();
ui->projectView->expand(
mProjectProxyModel->mapFromSource(
mProject->model()->rootIndex()));
pSettings->history().removeProject(filename);
// // if project manager isn't open then open it
@ -3865,11 +3868,17 @@ void MainWindow::onClassBrowserSortByName()
void MainWindow::onProjectSwitchCustomViewMode()
{
mProject->setModelType(ProjectModelType::Custom);
ui->projectView->expand(
mProjectProxyModel->mapFromSource(
mProject->model()->rootIndex()));
}
void MainWindow::onProjectSwitchFileSystemViewMode()
{
mProject->setModelType(ProjectModelType::FileSystem);
ui->projectView->expand(
mProjectProxyModel->mapFromSource(
mProject->model()->rootIndex()));
}
void MainWindow::onProjectRemoveFolder()
@ -3913,6 +3922,11 @@ void MainWindow::onProjectAddFolder()
if (folderNode->unitIndex>=0)
return;
QString s=tr("New folder");
int i=1;
while (fileExists(s)) {
s=tr("New folder")+QString("%1").arg(i);
i++;
}
bool ok;
s = QInputDialog::getText(ui->projectView,
tr("Add Folder"),
@ -3920,13 +3934,11 @@ void MainWindow::onProjectAddFolder()
QLineEdit::Normal, s,
&ok).trimmed();
if (ok && !s.isEmpty()) {
QString path = mProject->getNodePath(folderNode);
if (path.isEmpty()) {
mProject->addFolder(s);
} else {
mProject->addFolder(path + '/' +s);
}
mProject->saveOptions();
PProjectModelNode node = mProject->addFolder(folderNode,s);
mProject->saveAll();
setProjectViewCurrentNode(node);
updateProjectView();
}
}
@ -4515,11 +4527,12 @@ void MainWindow::closeEvent(QCloseEvent *event) {
if (!mShouldRemoveAllSettings && pSettings->editor().autoLoadLastFiles()) {
saveLastOpens();
} else {
} /*else {
//if don't save last open files, close project before editors, to save project openned editors;
if (mProject) {
closeProject(false);
}
}*/
if (mProject) {
closeProject(false);
}
mClosingAll=true;
@ -4531,11 +4544,11 @@ void MainWindow::closeEvent(QCloseEvent *event) {
}
mClosingAll=false;
if (!mShouldRemoveAllSettings && pSettings->editor().autoLoadLastFiles()) {
if (mProject) {
closeProject(false);
}
}
// if (!mShouldRemoveAllSettings && pSettings->editor().autoLoadLastFiles()) {
// if (mProject) {
// closeProject(false);
// }
// }
mTcpServer.close();
mCompilerManager->stopAllRunners();
@ -6292,15 +6305,21 @@ void MainWindow::doFilesViewRemoveFile(const QModelIndex &index)
}
}
void MainWindow::setProjectViewCurrentUnit(std::shared_ptr<ProjectUnit> unit)
{
void MainWindow::setProjectViewCurrentUnit(std::shared_ptr<ProjectUnit> unit) {
if (unit) {
QModelIndex parentIndex = mProject->model()->getParentIndex(unit->node().get());
setProjectViewCurrentNode(unit->node());
}
}
void MainWindow::setProjectViewCurrentNode(PProjectModelNode node)
{
if (node) {
QModelIndex parentIndex = mProject->model()->getParentIndex(node.get());
parentIndex = mProjectProxyModel->mapFromSource(parentIndex);
if (parentIndex.isValid()) {
ui->projectView->expandRecursively(parentIndex);
}
QModelIndex index = mProject->model()->getNodeIndex(unit->node().get());
QModelIndex index = mProject->model()->getNodeIndex(node.get());
index = mProjectProxyModel->mapFromSource(index);
if (index.isValid()) {
ui->projectView->setCurrentIndex(index);

View File

@ -67,6 +67,7 @@ class CPUDialog;
class QPlainTextEdit;
class SearchDialog;
class Project;
class ProjectModelNode;
class ProjectUnit;
class ColorSchemeItem;
@ -271,6 +272,7 @@ private:
void doFilesViewRemoveFile(const QModelIndex& index);
void setProjectViewCurrentNode(std::shared_ptr<ProjectModelNode> node);
void setProjectViewCurrentUnit(std::shared_ptr<ProjectUnit> unit);
private slots:

View File

@ -376,13 +376,50 @@ Editor *Project::openUnit(int index, bool forceOpen)
editor->setInProject(true);
//unit->setEncoding(encoding);
editor->activate();
loadUnitLayout(editor,index);
loadUnitLayout(editor);
return editor;
}
}
return nullptr;
}
Editor *Project::openUnit(PProjectEditorLayout editorLayout)
{
if (!editorLayout)
return nullptr;
PProjectUnit unit = findUnit(editorLayout->filename);
if (!unit)
return nullptr;
if (!unit->fileName().isEmpty() && fileExists(unit->fileName())) {
if (getFileType(unit->fileName())==FileType::Other) {
return nullptr;
}
Editor * editor = mEditorList->getOpenedEditorByFilename(unit->fileName());
if (editor) {//already opened in the editors
editor->setInProject(true);
editor->activate();
return editor;
}
QByteArray encoding;
encoding = unit->encoding();
editor = mEditorList->newEditor(unit->fileName(), encoding, true, unit->isNew());
if (editor) {
editor->setInProject(true);
//unit->setEncoding(encoding);
editor->activate();
editor->setTopLine(editorLayout->topLine);
editor->setLeftChar(editorLayout->leftChar);
editor->setCaretX(editorLayout->caretX);
editor->setCaretY(editorLayout->caretY);
return editor;
}
}
return nullptr;
}
Editor *Project::unitEditor(const PProjectUnit &unit) const
{
return mEditorList->getOpenedEditorByFilename(unit->fileName());
@ -558,29 +595,36 @@ void Project::saveAll()
void Project::saveLayout()
{
SimpleIni layIni;
QStringList sl;
QHash<QString,int> editorOrderSet;
// Write list of open project files
int order=0;
for (int i=0;i<mEditorList->pageCount();i++) {
Editor* e= (*mEditorList)[i];
if (e && e->inProject())
sl.append(QString("%1").arg(findUnitId(e)));
Editor* e=(*mEditorList)[i];
if (e && e->inProject() && !editorOrderSet.contains(e->filename())) {
editorOrderSet.insert(e->filename(),order);
order++;
}
}
layIni.SetValue("Editors","Order",sl.join(",").toUtf8());
// layIni.SetValue("Editors","Order",sl.join(",").toUtf8());
Editor *e, *e2;
// Remember what files were visible
mEditorList->getVisibleEditors(e, e2);
if (e)
layIni.SetLongValue("Editors","Focused", findUnitId(e));
layIni.SetValue("Editors","Focused", e->filename().toUtf8());
// save editor info
foreach (const PProjectUnit& unit,mUnits) {
QByteArray groupName = QString("Editor_%1").arg(unit->id()).toUtf8();
Editor* editor = unitEditor(unit);
if (editor) {
QByteArray groupName = QString("E_%1").arg(editor->filename()).toUtf8();
layIni.SetLongValue(groupName,"CursorCol", editor->caretX());
layIni.SetLongValue(groupName,"CursorRow", editor->caretY());
layIni.SetLongValue(groupName,"TopLine", editor->topLine());
layIni.SetLongValue(groupName,"LeftChar", editor->leftChar());
int order=editorOrderSet.value(editor->filename(),-1);
if (order>=0) {
layIni.SetLongValue(groupName,"Order",order);
}
}
// remove old data from project file
// SimpleIni ini;
@ -1073,19 +1117,26 @@ void Project::saveOptions()
ini.SaveFile(mFilename.toLocal8Bit());
}
void Project::addFolder(const QString &s)
PProjectModelNode Project::addFolder(PProjectModelNode parentFolder,const QString &s)
{
if (mFolders.indexOf(s)<0) {
QString fullPath;
QString path = getNodePath(parentFolder);
if (path.isEmpty()) {
fullPath = s;
} else {
fullPath = path + '/' +s;
}
if (mFolders.indexOf(fullPath)<0) {
mModel.beginUpdate();
auto action = finally([this]{
mModel.endUpdate();
});
mFolders.append(s);
rebuildNodes();
//todo: MainForm.ProjectView.Select(FolderNodeFromName(s));
//folderNodeFromName(s)->makeVisible();
mFolders.append(fullPath);
PProjectModelNode node = makeNewFolderNode(s,parentFolder);
setModified(true);
return node;
}
return PProjectModelNode();
}
PProjectUnit Project::addUnit(const QString &inFileName, PProjectModelNode parentNode)
@ -1716,25 +1767,37 @@ PProjectUnit Project::loadLayout()
SI_Error error = layIni.LoadFile(changeFileExt(filename(), "layout").toLocal8Bit());
if (error!=SI_OK)
return PProjectUnit();
int topLeft = layIni.GetLongValue("Editors","Focused",1);
QString focusedFilename = fromByteArray(layIni.GetValue("Editors","Focused"));
//TopRight := layIni.ReadInteger('Editors', 'FocusedRight', -1);
QString temp =layIni.GetValue("Editors","Order", "");
QStringList sl = temp.split(",",
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
Qt::SkipEmptyParts
#else
QString::SkipEmptyParts
#endif
);
foreach (const QString& s,sl) {
bool ok;
int currIdx = s.toInt(&ok);
if (ok) {
openUnit(currIdx);
QHash<int,PProjectEditorLayout> opennedMap;
SimpleIni::TNamesDepend sections;
layIni.GetAllSections(sections);
QSet<QString> sectionNames;
for(const SimpleIni::Entry& entry:sections) {
QString key(entry.pItem);
sectionNames.insert(key);
}
foreach (PProjectUnit unit,mUnits) {
QByteArray groupName = QString("E_%1").arg(unit->fileName()).toUtf8();
if (sectionNames.contains(groupName)) {
PProjectEditorLayout editorLayout = std::make_shared<ProjectEditorLayout>();
editorLayout->filename=unit->fileName();
editorLayout->topLine=layIni.GetLongValue(groupName,"TopLine");
editorLayout->leftChar=layIni.GetLongValue(groupName,"LeftChar");
editorLayout->caretX=layIni.GetLongValue(groupName,"CursorCol");
editorLayout->caretY=layIni.GetLongValue(groupName,"CursorRow");
int order = layIni.GetLongValue(groupName,"Order",-1);
if (order>=0)
opennedMap.insert(order,editorLayout);
}
}
PProjectUnit unit = findUnitById(topLeft);
for (int i=0;i<mUnits.count();i++) {
PProjectEditorLayout editorLayout = opennedMap.value(i,PProjectEditorLayout());
if (editorLayout) {
openUnit(editorLayout);
}
}
PProjectUnit unit = findUnit(focusedFilename);
if (unit) {
Editor * editor = unitEditor(unit);
if (editor)
@ -1983,7 +2046,7 @@ void Project::loadOptions(SimpleIni& ini)
}
}
void Project::loadUnitLayout(Editor *e, int index)
void Project::loadUnitLayout(Editor *e)
{
if (!e)
return;
@ -1992,7 +2055,7 @@ void Project::loadUnitLayout(Editor *e, int index)
error = layIni.LoadFile(changeFileExt(filename(), "layout").toLocal8Bit());
if (error != SI_Error::SI_OK)
return;
QByteArray groupName = (QString("Editor_%1").arg(index)).toUtf8();
QByteArray groupName = (QString("E_%1").arg(e->filename())).toUtf8();
e->setCaretY(layIni.GetLongValue(groupName,"CursorRow",1));
e->setCaretX(layIni.GetLongValue(groupName,"CursorCol",1));
e->setTopLine(layIni.GetLongValue(groupName,"TopLine",1));
@ -2676,6 +2739,11 @@ QModelIndex ProjectModel::getParentIndex(ProjectModelNode * node) const
return createIndex(row,0,parent.get());
}
QModelIndex ProjectModel::rootIndex() const
{
return getNodeIndex(mProject->rootNode().get());
}
bool ProjectModel::canDropMimeData(const QMimeData * data, Qt::DropAction action, int /*row*/, int /*column*/, const QModelIndex &parent) const
{

View File

@ -56,6 +56,16 @@ struct ProjectModelNode {
int level;
};
struct ProjectEditorLayout {
QString filename;
int topLine;
int leftChar;
int caretX;
int caretY;
};
using PProjectEditorLayout = std::shared_ptr<ProjectEditorLayout>;
class ProjectUnit {
public:
@ -144,6 +154,8 @@ public:
QModelIndex getNodeIndex(ProjectModelNode *node) const;
QModelIndex getParentIndex(ProjectModelNode * node) const;
QModelIndex rootIndex() const;
private:
// QAbstractItemModel interface
@ -189,7 +201,7 @@ public:
void setFileName(QString value);
void setModified(bool value);
void addFolder(const QString& s);
PProjectModelNode addFolder(PProjectModelNode parentFolder, const QString& s);
PProjectUnit addUnit(const QString& inFileName,
PProjectModelNode parentNode);
QString folder();
@ -205,6 +217,7 @@ public:
PProjectUnit newUnit(PProjectModelNode parentNode,
const QString& customFileName="");
Editor* openUnit(int index, bool forceOpen=true);
Editor* openUnit(PProjectEditorLayout editorLayout);
Editor* unitEditor(const PProjectUnit& unit) const;
Editor* unitEditor(const ProjectUnit* unit) const;
Editor* unitEditor(int id) const {
@ -282,8 +295,8 @@ private:
PProjectModelNode findFileSystemFolderNode(const QString& folderPath, ProjectModelNodeType nodeType);
PProjectModelNode getCustomeFolderNodeFromName(const QString& name);
void loadOptions(SimpleIni& ini);
PProjectUnit loadLayout(); // load all [UnitX]
void loadUnitLayout(Editor *e, int index); // load single [UnitX] cursor positions
PProjectUnit loadLayout();
void loadUnitLayout(Editor *e);
PProjectModelNode makeNewFolderNode(
const QString& folderName,