20 :
QTextEdit(parent), mStandardPrompt(
">>> "), mCompoundPrompt(
"... "), mInputPrompt(
"==> "),
21 mPromptBlockNumber(0), mPromptLength(0), mPromptEndPosition(0), mCompoundPromptEndPosition(0),
22 mPromptType(Standard), mInCompletion(false), mCurrentCompoundInput(
""), mCurrentInput(
""), mCurrentHistoryIndex(-1), mCurrentCompleterIndex(0),
25 this->
document()->setMaximumBlockCount(1000);
31 gPythonContext->interpretForeground(
"print(\"Python \" + sys.version)");
32 gPythonContext->interpretForeground(
"print(sys.executable + \" on \" + sys.platform)");
43 void PythonConsole::handleStyleChanged(
int istyle)
49 void PythonConsole::keyPressEventInputMode(
QKeyEvent *e)
51 mCurrentHistoryIndex = -1;
66 if (
textCursor().selectionStart() < mPromptEndPosition)
70 mInCompletion =
false;
98 keyPressEventInputMode(e);
108 mCurrentHistoryIndex = -1;
114 if (
textCursor().selectionStart() >= mPromptEndPosition)
125 if (
textCursor().selectionStart() > mPromptEndPosition)
146 cursor.setPosition(mPromptEndPosition);
152 if (
textCursor().selectionStart() >= mPromptEndPosition)
163 if (
textCursor().selectionStart() > mPromptEndPosition)
173 cursor.setPosition(mPromptEndPosition);
194 if (
textCursor().selectionStart() < mPromptEndPosition)
198 mInCompletion =
false;
199 mCurrentHistoryIndex = -1;
261 cursor.setCharFormat(format);
265 cursor.insertText(mCompoundPrompt);
266 if (mCompoundPromptEndPosition < 0)
268 mCompoundPromptEndPosition = mPromptEndPosition;
272 cursor.insertText(mStandardPrompt);
273 mCompoundPromptEndPosition = -1;
276 cursor.insertText(mInputPrompt);
277 mCompoundPromptEndPosition = -1;
284 mPromptLength = mStandardPrompt.
length();
290 mAbortThreadWidget->
stop();
299 if (!
input.isEmpty())
302 mHistory->addHistory(
input.toStdString());
306 mCurrentCompoundInput +=
input;
309 gPythonContext->interpretBackground(
this, mCurrentCompoundInput,
true);
316 mCurrentCompoundInput =
"";
317 mAbortThreadWidget->
start();
321 mCurrentCompoundInput +=
input +
"\n";
325 mHistory->updateFromFile();
332 cursor.setPosition(mPromptEndPosition);
343 cursor.setPosition(mPromptEndPosition);
345 cursor.insertText(new_command);
352 cursor.insertText(appendix);
364 auto lastIndex = mHistory->size() - 1;
371 if (mCurrentHistoryIndex == -1)
374 mCurrentHistoryIndex = lastIndex;
379 if (mCurrentHistoryIndex == 0)
384 mCurrentHistoryIndex--;
391 auto lastIndex = mHistory->size() - 1;
393 if (mCurrentHistoryIndex == -1)
398 if (mCurrentHistoryIndex == lastIndex)
400 mCurrentHistoryIndex = -1;
405 mCurrentHistoryIndex++;
421 mCurrentInput +=
"\t";
439 else if (r.size() > 1)
441 mInCompletion =
true;
448 candidates += candidate +
" ";
449 for (
int i = 0; i < matching_prefix.
size() && i < completion.size(); ++i)
451 if (matching_prefix[i] != completion[i])
453 matching_prefix = matching_prefix.
mid(0, i);
467 return mAbortThreadWidget;
472 :
QFrame(parent), mCount(0)
478 mLabel =
new QLabel(
this);
485 mTimer =
new QTimer(
this);
489 void PythonConsoleAbortThread::handleAbortButton()
492 log_info(
"gui",
"Python console command execution aborted by user");
496 void PythonConsoleAbortThread::handleTimeout()
504 mLabel->
setText(
QString(
"Python interpreter running for %1 seconds").arg(mCount));
static SettingsItemDropdown * sSettingStyle
PythonConsoleAbortThread(QWidget *parent=nullptr)
Stores the history of python commands.
void handleThreadFinished()
void mousePressEvent(QMouseEvent *event) override
PythonConsole(QWidget *parent=nullptr)
void keyPressEvent(QKeyEvent *e) override
void handleUpKeyPressed()
void setInputMode(bool state)
void handleTabKeyPressed()
void inputReceived(QString input)
virtual void clear() override
virtual void handleStdout(const QString &output) override
PythonConsoleAbortThread * abortThreadWidget()
void handleDownKeyPressed()
QString getCurrentCommand()
void replaceCurrentCommand(const QString &new_command)
void insertAtEnd(const QString &text, QColor textColor)
virtual void handleError(const QString &output) override
void appendToCurrentCommand(const QString &new_command)
static PythonConsoleQssAdapter * instance()
void intChanged(int value)
#define log_info(channel,...)
PythonContext * gPythonContext
void setFrameStyle(int style)
bool matches(QKeySequence::StandardKey key) const const
Qt::KeyboardModifiers modifiers() const const
void setText(const QString &)
void setMargin(int margin)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
QString mid(int position, int n) const const
std::string toStdString() const const
int position() const const
void ensureCursorVisible()
void setHtml(const QString &text)
void insertPlainText(const QString &text)
virtual void keyPressEvent(QKeyEvent *e) override
virtual void mousePressEvent(QMouseEvent *e) override
void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode)
void setTextColor(const QColor &c)
void setTextCursor(const QTextCursor &cursor)
QColor textColor() const const
QTextCursor textCursor() const const
void setUndoRedoEnabled(bool enable)
void setForeground(const QBrush &brush)