From 164409cee95219a2b9f6dfb2932d6fbc956ba0d2 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Tue, 3 Sep 2019 20:51:00 -0300 Subject: [PATCH] Add Python console option to save history across sessions --- src/Gui/PythonConsole.cpp | 69 ++++++++++++++++++++++++++++++++++++++- src/Gui/PythonConsole.h | 11 ++++--- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 56b71255d4..e09fbb0a5c 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -92,7 +92,7 @@ struct PythonConsoleP InteractiveInterpreter* interpreter; CallTipsList* callTipsList; ConsoleHistory history; - QString output, error, info; + QString output, error, info, historyFile; QStringList statements; bool interactive; QMap colormap; // Color map @@ -106,6 +106,7 @@ struct PythonConsoleP interpreter = 0; callTipsList = 0; interactive = false; + historyFile = QString::fromUtf8((App::Application::getUserAppDataDir() + "PythonHistory.log").c_str()); colormap[QLatin1String("Text")] = Qt::black; colormap[QLatin1String("Bookmark")] = Qt::cyan; colormap[QLatin1String("Breakpoint")] = Qt::red; @@ -472,11 +473,13 @@ PythonConsole::PythonConsole(QWidget *parent) .arg(QString::fromLatin1(version), QString::fromLatin1(platform)); d->output = d->info; printPrompt(PythonConsole::Complete); + loadHistory(); } /** Destroys the object and frees any allocated resources */ PythonConsole::~PythonConsole() { + saveHistory(); Base::PyGILStateLocker lock; getWindowParameter()->Detach( this ); delete pythonSyntax; @@ -1273,6 +1276,16 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e ) this->setWordWrapMode(QTextOption::NoWrap); } + QAction* saveh = menu.addAction(tr("Save history")); + saveh->setToolTip(tr("Saves python history across FreeCAD sessions")); + saveh->setCheckable(true); + + if (hGrp->GetBool("SavePythonHistory", false)) { + saveh->setChecked(true); + } else { + saveh->setChecked(false); + } + QAction* exec = menu.exec(e->globalPos()); if (exec == wrap) { if (wrap->isChecked()) { @@ -1282,7 +1295,14 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e ) this->setWordWrapMode(QTextOption::NoWrap); hGrp->SetBool("PythonWordWrap", false); } + } else if (exec == saveh) { + if (saveh->isChecked()) { + hGrp->SetBool("SavePythonHistory", true); + } else { + hGrp->SetBool("SavePythonHistory", false); + } } + } void PythonConsole::onClearConsole() @@ -1363,6 +1383,53 @@ QString PythonConsole::readline( void ) return inputBuffer.append(QChar::fromLatin1('\n')); //< pass a newline here, since the readline-caller may need it! } +/** + * loads history contents from the default history file + */ +void PythonConsole::loadHistory() const +{ + // only load contents if history is empty, to not overwrite anything + if (!d->history.isEmpty()) + return; + ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter(). + GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); + if (!hGrp->GetBool("SavePythonHistory", false)) + return; + QFile f(d->historyFile); + if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { + QString l; + while (!f.atEnd()) { + l = QString::fromUtf8(f.readLine()); + if (!l.isEmpty()) { + l.chop(1); // removes the last \n + d->history.append(l); + } + } + f.close(); + } +} + +/** + * saves the current history to the default history file + */ +void PythonConsole::saveHistory() const +{ + if (d->history.isEmpty()) + return; + ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter(). + GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General"); + if (!hGrp->GetBool("SavePythonHistory", false)) + return; + QFile f(d->historyFile); + if (f.open(QIODevice::WriteOnly)) { + QTextStream t (&f); + const QStringList& hist = d->history.values(); + for (QStringList::ConstIterator it = hist.begin(); it != hist.end(); ++it) + t << *it << "\n"; + f.close(); + } +} + // --------------------------------------------------------------------- PythonConsoleHighlighter::PythonConsoleHighlighter(QObject* parent) diff --git a/src/Gui/PythonConsole.h b/src/Gui/PythonConsole.h index 13efa1f457..17763e5097 100644 --- a/src/Gui/PythonConsole.h +++ b/src/Gui/PythonConsole.h @@ -79,13 +79,13 @@ public: void append(const QString &inputLine); const QStringList& values() const; void restart(); - void markScratch( void ); - void doScratch( void ); + void markScratch( void ); + void doScratch( void ); private: QStringList _history; QStringList::ConstIterator _it; - int _scratchBegin; + int _scratchBegin; QString _prefix; }; @@ -150,6 +150,8 @@ private: void insertPythonError (const QString&); void runSourceFromMimeData(const QString&); void appendOutput(const QString&, int); + void loadHistory() const; + void saveHistory() const; Q_SIGNALS: void pendingSource( void ); @@ -162,7 +164,8 @@ private: private: PythonConsoleHighlighter* pythonSyntax; - QString *_sourceDrain; + QString *_sourceDrain; + QString _historyFile; }; /**