Skip to content

Commit

Permalink
feat: Load tabs from files after crash if hot exit is on
Browse files Browse the repository at this point in the history
See 3fab159 for the old behavior.

Now, if hot exit is on, the tabs will be restored after a crash, and the
contents will be loaded from file instead of the saved status, because
of the reason mentioned in 3fab159.
  • Loading branch information
ouuan committed Feb 13, 2020
1 parent b15c3e3 commit d5ea61b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 45 deletions.
2 changes: 2 additions & 0 deletions include/Core/SettingsManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class SettingManager
void clearEditorStatus();
QMap<QString, QVariant> getEditorStatus(int index);
void setEditorStatus(int index, const QMap<QString, QVariant> &status);
bool isHotExitLoadFromFile();
void setHotExitLoadFromFile(bool value);

QString getCFPath();
void setCFPath(const QString &path);
Expand Down
1 change: 1 addition & 0 deletions include/appwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class AppWindow : public QMainWindow
void openPaths(const QStringList &paths, bool cpp = true, bool java = true, bool python = true, int depth = -1);
QStringList openFolder(const QString &path, bool cpp, bool java, bool python, int depth);
void openContest(const QString &path, const QString &lang, int number);
void saveEditorStatus(bool loadFromFile);
bool quit();

MainWindow *currentWindow();
Expand Down
4 changes: 2 additions & 2 deletions include/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class MainWindow : public QMainWindow

void setProblemURL(const QString &url);

EditorStatus toStatus() const;
void loadStatus(const EditorStatus &status);
EditorStatus toStatus(bool simple) const;
void loadStatus(const EditorStatus &status, bool simple);

void save(bool force, const QString &head);
void saveAs();
Expand Down
9 changes: 8 additions & 1 deletion src/Core/SettingsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ void SettingManager::setCurrentIndex(int index)
}
void SettingManager::clearEditorStatus()
{
setNumberOfTabs(0);
mSettings->remove("editor_status");
}
QMap<QString, QVariant> SettingManager::getEditorStatus(int index)
Expand All @@ -455,6 +454,14 @@ void SettingManager::setEditorStatus(int index, const QMap<QString, QVariant> &s
{
mSettings->setValue("editor_status/" + QString::number(index), status);
}
bool SettingManager::isHotExitLoadFromFile()
{
return mSettings->value("hot_exit_load_from_file", "false").toBool();
}
void SettingManager::setHotExitLoadFromFile(bool value)
{
mSettings->setValue("hot_exit_load_from_file", value ? "true" : "false");
}

int SettingManager::getTransparency()
{
Expand Down
56 changes: 35 additions & 21 deletions src/appwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ AppWindow::AppWindow(bool noHotExit, QWidget *parent) : QMainWindow(parent), ui(
auto oldSize = size();
setUpdatesEnabled(false);

// save these so that they won't be affected by saveEditorStatus() in onEditorChanged()
QVector<MainWindow::EditorStatus> status;
for (int i = 0; i < length; ++i)
status.push_back(settingManager->getEditorStatus(i));
bool loadFromFile = settingManager->isHotExitLoadFromFile();
int currentIndex = settingManager->getCurrentIndex();

for (int i = 0; i < length; ++i)
{
if (progress.wasCanceled())
break;
auto status = MainWindow::EditorStatus(settingManager->getEditorStatus(i));
progress.setValue(i);
openTab("");
currentWindow()->loadStatus(status);
currentWindow()->loadStatus(status[i], loadFromFile);
progress.setLabelText(currentWindow()->getTabTitle(true, false));
}

Expand All @@ -76,11 +82,10 @@ AppWindow::AppWindow(bool noHotExit, QWidget *parent) : QMainWindow(parent), ui(
setUpdatesEnabled(true);
resize(oldSize);

settingManager->clearEditorStatus();
settingManager->setHotExitLoadFromFile(true);

int index = settingManager->getCurrentIndex();
if (index >= 0 && index < ui->tabWidget->count())
ui->tabWidget->setCurrentIndex(index);
if (currentIndex >= 0 && currentIndex < ui->tabWidget->count())
ui->tabWidget->setCurrentIndex(currentIndex);
}
}

Expand Down Expand Up @@ -438,25 +443,31 @@ void AppWindow::openContest(const QString &path, const QString &lang, int number
openTabs(tabs);
}

bool AppWindow::quit()
void AppWindow::saveEditorStatus(bool loadFromFile)
{
settingManager->clearEditorStatus();
if (settingManager->isUseHotExit())
if (ui->tabWidget->count() == 1 && windowIndex(0)->isUntitled() && !windowIndex(0)->isTextChanged())
{
if (ui->tabWidget->count() == 1 && windowIndex(0)->isUntitled() && !windowIndex(0)->isTextChanged())
{
settingManager->setNumberOfTabs(0);
settingManager->setCurrentIndex(-1);
}
else
settingManager->setNumberOfTabs(0);
settingManager->setCurrentIndex(-1);
}
else
{
settingManager->setNumberOfTabs(ui->tabWidget->count());
settingManager->setCurrentIndex(ui->tabWidget->currentIndex());
for (int i = 0; i < ui->tabWidget->count(); ++i)
{
settingManager->setNumberOfTabs(ui->tabWidget->count());
settingManager->setCurrentIndex(ui->tabWidget->currentIndex());
for (int i = 0; i < ui->tabWidget->count(); ++i)
{
settingManager->setEditorStatus(i, windowIndex(i)->toStatus().toMap());
}
settingManager->setEditorStatus(i, windowIndex(i)->toStatus(loadFromFile).toMap());
}
}
}

bool AppWindow::quit()
{
if (settingManager->isUseHotExit())
{
settingManager->setHotExitLoadFromFile(false);
saveEditorStatus(false);
return true;
}
else
Expand Down Expand Up @@ -711,6 +722,10 @@ void AppWindow::onTabChanged(int index)
void AppWindow::onEditorChanged()
{
Core::Log::i("appwindow/onEditorChanged", "Invoked onEditorChanged");

if (settingManager->isUseHotExit() && settingManager->isHotExitLoadFromFile())
saveEditorStatus(true);

if (currentWindow() != nullptr)
{
QMap<QString, QVector<int>> tabsByName;
Expand Down Expand Up @@ -764,7 +779,6 @@ void AppWindow::onSettingsApplied()
connect(server, &Network::CompanionServer::onRequestArrived, this, &AppWindow::onIncomingCompanionRequest);
diagonistics = true;
onTabChanged(ui->tabWidget->currentIndex());
onEditorChanged();

Core::Log::i("appwindow/onSettingsApplied", "Applied settings to appwindow");
}
Expand Down
56 changes: 35 additions & 21 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mainwindow.hpp"

#include "Core/Compiler.hpp"
#include "Core/EventLogger.hpp"
#include "Core/MessageLogger.hpp"
#include "Core/Runner.hpp"
#include "Extensions/EditorTheme.hpp"
Expand Down Expand Up @@ -303,44 +304,57 @@ QMap<QString, QVariant> MainWindow::EditorStatus::toMap() const
}
#undef TOSTATUS

MainWindow::EditorStatus MainWindow::toStatus() const
MainWindow::EditorStatus MainWindow::toStatus(bool simple) const
{
EditorStatus status;

status.isLanguageSet = isLanguageSet;
status.filePath = filePath;
status.savedText = savedText;
status.problemURL = problemURL;
status.editorText = editor->toPlainText();
status.language = language;
status.editorCursor = editor->textCursor().position();
status.editorAnchor = editor->textCursor().anchor();
status.horizontalScrollBarValue = editor->horizontalScrollBar()->value();
status.verticalScrollbarValue = editor->verticalScrollBar()->value();
status.untitledIndex = untitledIndex;
status.input = testcases->inputs();
status.expected = testcases->expecteds();

if (!simple)
{
status.editorText = editor->toPlainText();
status.editorCursor = editor->textCursor().position();
status.editorAnchor = editor->textCursor().anchor();
status.horizontalScrollBarValue = editor->horizontalScrollBar()->value();
status.verticalScrollbarValue = editor->verticalScrollBar()->value();
status.input = testcases->inputs();
status.expected = testcases->expecteds();
}

return status;
}

void MainWindow::loadStatus(const EditorStatus &status)
void MainWindow::loadStatus(const EditorStatus &status, bool simple)
{
filePath = status.filePath;
updateWatcher();
savedText = status.savedText;
setProblemURL(status.problemURL);
editor->setPlainText(status.editorText);
if (status.isLanguageSet)
setLanguage(status.language);
auto cursor = editor->textCursor();
cursor.setPosition(status.editorAnchor);
cursor.setPosition(status.editorCursor, QTextCursor::KeepAnchor);
editor->setTextCursor(cursor);
editor->horizontalScrollBar()->setValue(status.horizontalScrollBarValue);
editor->verticalScrollBar()->setValue(status.verticalScrollbarValue);
untitledIndex = status.untitledIndex;
testcases->loadStatus(status.input, status.expected);

Core::Log::i("MainWindow/loadStatus") << INFO_OF(simple) << ' ' << INFO_OF(status.filePath) << endl;

if (!simple)
{
filePath = status.filePath;
updateWatcher();
savedText = status.savedText;
editor->setPlainText(status.editorText);
auto cursor = editor->textCursor();
cursor.setPosition(status.editorAnchor);
cursor.setPosition(status.editorCursor, QTextCursor::KeepAnchor);
editor->setTextCursor(cursor);
editor->horizontalScrollBar()->setValue(status.horizontalScrollBarValue);
editor->verticalScrollBar()->setValue(status.verticalScrollbarValue);
testcases->loadStatus(status.input, status.expected);
}
else if (!status.filePath.isEmpty())
{
loadFile(status.filePath);
}
}

void MainWindow::applyCompanion(Network::CompanionData data)
Expand Down

0 comments on commit d5ea61b

Please sign in to comment.