Add an entry to the recent files list for unsaved/temp recovery script

This commit is contained in:
baldurk
2026-08-13 17:46:45 +01:00
parent 434cb575a4
commit e6cd4d2e98
4 changed files with 51 additions and 4 deletions
@@ -1497,6 +1497,20 @@ QString PythonContext::versionString()
return QFormatStr("%1.%2.%3").arg(PY_MAJOR_VERSION).arg(PY_MINOR_VERSION).arg(PY_MICRO_VERSION);
}
QString PythonContext::GetTempFilename(QString filename)
{
for(QString path : QStandardPaths::standardLocations(QStandardPaths::AppDataLocation))
{
QDir tmpDir(path);
tmpDir.mkpath(lit("pytmp"));
tmpDir.cd(lit("pytmp"));
if(tmpDir.exists())
return tmpDir.absoluteFilePath(filename);
}
return QString();
}
void PythonContext::executeString(const QString &filename, const QString &source, bool debugging)
{
if(!initialised())
@@ -122,6 +122,8 @@ public:
QString tryFunctionCompletion(int line, QString expr);
QString typenameForLoc(int line, int col);
QString GetTempFilename(QString filename);
void FlushOutput() { outputTick(); }
void abort() { m_Abort = true; }
+33 -4
View File
@@ -1633,6 +1633,12 @@ PythonShell::~PythonShell()
m_Ctx.GetMainWindow()->UnregisterShortcut("CTRL+S", this);
// on a clean shutdown, remove the unsaved temp file
QString unsavedFile = interactiveContext->GetTempFilename(lit("script.py"));
if(!unsavedFile.isEmpty() && QFile::exists(unsavedFile))
QFile::remove(unsavedFile);
for(ScintillaEdit *edit : m_Scintillas)
delete edit;
@@ -1725,9 +1731,6 @@ void PythonShell::openFileModified(const QString &path)
markEditorModified(edit, false);
return;
}
RDDialog::critical(this, tr("Error reloading script"),
tr("Couldn't open %1.\n%2").arg(path).arg(f.errorString()));
});
}
}
@@ -2096,6 +2099,23 @@ void PythonShell::updateRecentFiles(bool added)
m_RecentFiles->clear();
QString unsavedFile = interactiveContext->GetTempFilename(lit("script.py"));
if(!unsavedFile.isEmpty() && QFile::exists(unsavedFile) && !m_IgnoreRecovered)
{
RDTreeWidgetItem *recent = new RDTreeWidgetItem({tr("Recovered Script")});
recent->setItalic(true);
recent->setData(0, Qt::UserRole, QString(unsavedFile));
m_RecentFiles->addChild(recent);
}
else
{
// we didn't have a recovered script, remember that and don't display the file in future
// refreshes appears in future due to temp script running. When the window is closed the script
// will be removed, and if we crash and restart then the script will be found
m_IgnoreRecovered = true;
}
for(rdcstr file : m_Ctx.Config().Python_RecentFiles)
{
RDTreeWidgetItem *recent = new RDTreeWidgetItem({QFileInfo(file).fileName()});
@@ -2209,7 +2229,9 @@ bool PythonShell::LoadScriptFromFilename(rdcstr filename)
ui->saveScript->setEnabled(true);
addRecentFile(filename);
QString unsavedFile = interactiveContext->GetTempFilename(lit("script.py"));
if(QString(filename) != unsavedFile)
addRecentFile(filename);
m_Watcher->addPath(filename);
@@ -2570,6 +2592,13 @@ void PythonShell::on_projectExplorer_itemActivated(RDTreeWidgetItem *item, int c
}
LoadScriptFromFilename(filename);
QString unsavedFile = interactiveContext->GetTempFilename(lit("script.py"));
if(filename == unsavedFile)
{
QFile::remove(unsavedFile);
updateRecentFiles(false);
}
}
}
+2
View File
@@ -144,6 +144,8 @@ private:
bool m_ContextMenuVisible = false;
bool m_HelpPrinting = false;
bool m_IgnoreRecovered = false;
RDTreeWidgetItem *m_UIExtensions, *m_Examples, *m_RecentFiles;
QFileSystemWatcher *m_Watcher = NULL;