From e6cd4d2e98a36c6f8ca70d14d739690d904eb385 Mon Sep 17 00:00:00 2001 From: baldurk Date: Wed, 27 May 2026 18:04:16 +0100 Subject: [PATCH] Add an entry to the recent files list for unsaved/temp recovery script --- qrenderdoc/Code/pyrenderdoc/PythonContext.cpp | 14 +++++++ qrenderdoc/Code/pyrenderdoc/PythonContext.h | 2 + qrenderdoc/Windows/PythonShell.cpp | 37 +++++++++++++++++-- qrenderdoc/Windows/PythonShell.h | 2 + 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp index 490b55f7c..21aead490 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.cpp @@ -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()) diff --git a/qrenderdoc/Code/pyrenderdoc/PythonContext.h b/qrenderdoc/Code/pyrenderdoc/PythonContext.h index fdb1f3950..ffba98517 100644 --- a/qrenderdoc/Code/pyrenderdoc/PythonContext.h +++ b/qrenderdoc/Code/pyrenderdoc/PythonContext.h @@ -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; } diff --git a/qrenderdoc/Windows/PythonShell.cpp b/qrenderdoc/Windows/PythonShell.cpp index 90dfd295f..7723c13e7 100644 --- a/qrenderdoc/Windows/PythonShell.cpp +++ b/qrenderdoc/Windows/PythonShell.cpp @@ -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); + } } } diff --git a/qrenderdoc/Windows/PythonShell.h b/qrenderdoc/Windows/PythonShell.h index 23d57ed8a..f49facfa9 100644 --- a/qrenderdoc/Windows/PythonShell.h +++ b/qrenderdoc/Windows/PythonShell.h @@ -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;