Added a "User parameter:BaseApp/Preferences/PythonConsole/ProfilerInterval" int property which sets how often (in milliseconds) the Python profiler runs while Python code is running. Setting this value to zero will totally disable it. Also added a preference in the Python console screen which allows the user to set the value of this property to between 0 (disabled) and 5000 (once every 5 seconds). (+1 squashed commits) Squashed commits: [cca88ac633] Made the Python profiler only run when the console is running code This has two purposes. First, it prevents a performance impact from running the profiler whenever Python code is running. Second, it prevents crashes caused by Qt's process events function being called too frequently. When the Python code is running in the console, it monopolizes the main thread and prevents events from being processed. Therefore, causing events to be processed in the callback should not force events to be processed too frequently, because the normal loop is being prevented by the Python code. (+1 squashed commits) Squashed commits: [45f86917e6] Made long-running Python code not freeze the GUI without multithreading Removed the background thread running Python code and replaced it with a custom profiler which the Python interpreter runs frequently (at every Python opcode I believe) on the main thread whenever Python code is running. The profiler will make Qt process any new events every 200 ms, preventing "App not responding" messages and making sure any Ctrl+C keypresses will be processed. This prevents the previous issue where running anything GUI-related from Python would crash the program (because Qt isn't thread-safe). (+1 squashed commits) Squashed commits: [0ef7d810b3] Made the process of getting thread IDs cross-platform compatible Instead of using <threads.h>, now the standard <thread> header from C++ 11 is used to find the thread ID, since <threads.h> is apparently not available on Windows. (+1 squashed commits) Squashed commits: [74c7b867f2] # This is a combination of 2 commits. Python from the console now runs in the background In a nutshell, all Python code which is input from the interactive console now runs in a seperate QThread which runs in the background, instead of on the UI thread. This means that long operates operations will no longer cause the app to display an "App not responding" message, because the UI thread is now free to keep running unencumbered. However, it is still not possible to run multiple Python statements at the same time. If the user tries to run some Python while a previous statement is still being processed, instead it will display an error message in the console stating that the previous command is still being processed. I also added a seperate QTimer which runs once every 100ms to flush any output from the Python code back to the console. I can't flush the output to the console from the background thread because the relevant Qt5 code is not thread-safe (it causes random segfaults). So I added this timer as a work-around, since it runs in the main UI thread. Implemented Ctrl+C keyboard interrupts in the console This is implemented by detecting a Ctrl+C key event in the Python console in the main Qt UI thread, and sending a keyboard interrupt to the background thread that runs the Python code.
194 lines
6.0 KiB
C++
194 lines
6.0 KiB
C++
/***************************************************************************
|
|
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
|
* *
|
|
* This file is part of the FreeCAD CAx development system. *
|
|
* *
|
|
* This library is free software; you can redistribute it and/or *
|
|
* modify it under the terms of the GNU Library General Public *
|
|
* License as published by the Free Software Foundation; either *
|
|
* version 2 of the License, or (at your option) any later version. *
|
|
* *
|
|
* This library is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU Library General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Library General Public *
|
|
* License along with this library; see the file COPYING.LIB. If not, *
|
|
* write to the Free Software Foundation, Inc., 59 Temple Place, *
|
|
* Suite 330, Boston, MA 02111-1307, USA *
|
|
* *
|
|
***************************************************************************/
|
|
|
|
#ifndef GUI_PYTHONCONSOLE_H
|
|
#define GUI_PYTHONCONSOLE_H
|
|
|
|
#include <Python.h>
|
|
#include <QTimer>
|
|
#include "PythonEditor.h"
|
|
|
|
|
|
class QPlainTextEdit;
|
|
class QPushButton;
|
|
|
|
namespace Gui {
|
|
|
|
/**
|
|
* This class implements an interactive Python interpreter.
|
|
* @author Werner Mayer
|
|
*/
|
|
class GuiExport InteractiveInterpreter
|
|
{
|
|
public:
|
|
InteractiveInterpreter();
|
|
~InteractiveInterpreter();
|
|
|
|
bool isOccupied() const;
|
|
bool interrupt() const;
|
|
|
|
bool push(const char*);
|
|
int compileCommand(const char*) const;
|
|
bool hasPendingInput( ) const;
|
|
void setBuffer(const QStringList&);
|
|
QStringList getBuffer() const;
|
|
void clearBuffer();
|
|
|
|
private:
|
|
bool runSource(const char*) const;
|
|
PyObject* compile(const char*) const;
|
|
void runCode(PyCodeObject*) const;
|
|
void setPrompt();
|
|
|
|
private:
|
|
struct InteractiveInterpreterP* d;
|
|
};
|
|
|
|
/**
|
|
* This class implements the history for the Python console.
|
|
* @author Werner Mayer
|
|
*/
|
|
class GuiExport ConsoleHistory
|
|
{
|
|
public:
|
|
ConsoleHistory();
|
|
~ConsoleHistory();
|
|
|
|
void first();
|
|
bool more();
|
|
bool next();
|
|
bool prev(const QString &prefix = QString());
|
|
bool isEmpty() const;
|
|
const QString& value() const;
|
|
void append(const QString &inputLine);
|
|
const QStringList& values() const;
|
|
void restart();
|
|
void markScratch( );
|
|
void doScratch( );
|
|
|
|
private:
|
|
QStringList _history;
|
|
QStringList::ConstIterator _it;
|
|
int _scratchBegin;
|
|
QString _prefix;
|
|
};
|
|
|
|
/**
|
|
* Python text console with syntax highlighting.
|
|
* @author Werner Mayer
|
|
*/
|
|
class PythonConsoleHighlighter;
|
|
class GuiExport PythonConsole : public TextEdit, public WindowParameter
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
enum Prompt {
|
|
Complete = 0,
|
|
Incomplete = 1,
|
|
Flush = 2,
|
|
Special = 3
|
|
};
|
|
|
|
explicit PythonConsole(QWidget *parent = nullptr);
|
|
~PythonConsole() override;
|
|
|
|
void OnChange( Base::Subject<const char*> &rCaller,const char* rcReason ) override;
|
|
void printStatement( const QString& cmd );
|
|
QString readline( );
|
|
|
|
public Q_SLOTS:
|
|
void onSaveHistoryAs();
|
|
void onInsertFileName();
|
|
void onCopyHistory();
|
|
void onCopyCommand();
|
|
void onClearConsole();
|
|
void onFlush();
|
|
|
|
private Q_SLOTS:
|
|
void visibilityChanged (bool visible);
|
|
|
|
protected:
|
|
void keyPressEvent ( QKeyEvent * e ) override;
|
|
void showEvent ( QShowEvent * e ) override;
|
|
void dropEvent ( QDropEvent * e ) override;
|
|
void dragEnterEvent ( QDragEnterEvent * e ) override;
|
|
void dragMoveEvent ( QDragMoveEvent * e ) override;
|
|
void changeEvent ( QEvent * e ) override;
|
|
void mouseReleaseEvent( QMouseEvent * e ) override;
|
|
|
|
void overrideCursor(const QString& txt);
|
|
|
|
/** Pops up the context menu with some extensions */
|
|
void contextMenuEvent ( QContextMenuEvent* e ) override;
|
|
bool canInsertFromMimeData ( const QMimeData * source ) const override;
|
|
QMimeData * createMimeDataFromSelection () const override;
|
|
void insertFromMimeData ( const QMimeData * source ) override;
|
|
QTextCursor inputBegin( ) const;
|
|
|
|
private:
|
|
void runSource(const QString&);
|
|
bool isComment(const QString&) const;
|
|
void printPrompt(Prompt);
|
|
void insertPythonOutput(const QString&);
|
|
void insertPythonError (const QString&);
|
|
void runSourceFromMimeData(const QString&);
|
|
void appendOutput(const QString&, int);
|
|
void loadHistory() const;
|
|
void saveHistory() const;
|
|
void flushOutput();
|
|
|
|
Q_SIGNALS:
|
|
void pendingSource( );
|
|
|
|
private:
|
|
struct PythonConsoleP* d;
|
|
|
|
PythonConsoleHighlighter* pythonSyntax{nullptr};
|
|
QString *_sourceDrain{nullptr};
|
|
QString _historyFile;
|
|
QTimer *flusher{nullptr};
|
|
|
|
friend class PythonStdout;
|
|
friend class PythonStderr;
|
|
};
|
|
|
|
/**
|
|
* Syntax highlighter for Python console.
|
|
* @author Werner Mayer
|
|
*/
|
|
class GuiExport PythonConsoleHighlighter : public PythonSyntaxHighlighter
|
|
{
|
|
public:
|
|
explicit PythonConsoleHighlighter(QObject* parent);
|
|
~PythonConsoleHighlighter() override;
|
|
|
|
void highlightBlock (const QString & text) override;
|
|
|
|
protected:
|
|
void colorChanged(const QString& type, const QColor& col) override;
|
|
};
|
|
|
|
} // namespace Gui
|
|
|
|
#endif // GUI_PYTHONCONSOLE_H
|