11 #include <QPushButton>
22 :
QTextEdit(parent), mStandardPrompt(
">>> "), mCompoundPrompt(
"... "), mInputPrompt(
"==> "),
23 mPromptBlockNumber(0), mPromptLength(0), mPromptEndPosition(0), mCompoundPromptEndPosition(0),
24 mPromptType(Standard), mInCompletion(false), mCurrentCompoundInput(
""), mCurrentInput(
""), mCurrentHistoryIndex(-1), mCurrentCompleterIndex(0),
27 this->
document()->setMaximumBlockCount(1000);
45 void PythonConsole::handleStyleChanged(
int istyle)
51 void PythonConsole::keyPressEventInputMode(
QKeyEvent *e)
53 mCurrentHistoryIndex = -1;
68 if (
textCursor().selectionStart() < mPromptEndPosition)
72 mInCompletion =
false;
112 keyPressEventInputMode(e);
125 mCurrentHistoryIndex = -1;
131 if (
textCursor().selectionStart() >= mPromptEndPosition)
142 if (
textCursor().position() <= mPromptEndPosition)
148 if (posInLine >= 4 && posInLine % 4 == 0)
152 if (before.
right(4) ==
" ")
175 cursor.setPosition(mPromptEndPosition);
181 if (
textCursor().selectionStart() >= mPromptEndPosition)
192 if (
textCursor().selectionStart() > mPromptEndPosition)
202 cursor.setPosition(mPromptEndPosition);
232 if (
textCursor().selectionStart() < mPromptEndPosition)
236 mInCompletion =
false;
237 mCurrentHistoryIndex = -1;
299 cursor.setCharFormat(format);
303 cursor.insertText(mCompoundPrompt);
304 if (mCompoundPromptEndPosition < 0)
306 mCompoundPromptEndPosition = mPromptEndPosition;
310 cursor.insertText(mStandardPrompt);
311 mCompoundPromptEndPosition = -1;
314 cursor.insertText(mInputPrompt);
315 mCompoundPromptEndPosition = -1;
322 mPromptLength = mStandardPrompt.
length();
328 mAbortThreadWidget->
stop();
338 if (!
input.trimmed().isEmpty())
341 mHistory->addHistory(
input.toStdString());
343 if (!isCompound() &&
input.trimmed().isEmpty())
351 mCurrentCompoundInput =
"";
353 mAbortThreadWidget->
start();
357 else if (isCompound() &&
input.trimmed().isEmpty())
363 mCurrentCompoundInput =
"";
365 mAbortThreadWidget->
start();
377 if (
input.trimmed() !=
"") mCurrentCompoundInput +=
input +
"\n";
385 while (indent < expanded.
length() && expanded[indent] ==
' ') indent++;
391 mHistory->updateFromFile();
398 cursor.setPosition(mPromptEndPosition);
410 cursor.setPosition(mPromptEndPosition);
413 cursor.insertText(expandTabs(new_command));
422 void PythonConsole::discardInput(
bool wholeStatement)
428 mCurrentCompoundInput =
"";
431 mCurrentHistoryIndex = -1;
432 mInCompletion =
false;
446 cursor.insertText(appendix);
458 auto lastIndex = mHistory->size() - 1;
465 if (mCurrentHistoryIndex == -1)
468 mCurrentHistoryIndex = lastIndex;
473 if (mCurrentHistoryIndex == 0)
478 mCurrentHistoryIndex--;
485 auto lastIndex = mHistory->size() - 1;
487 if (mCurrentHistoryIndex == -1)
492 if (mCurrentHistoryIndex == lastIndex)
494 mCurrentHistoryIndex = -1;
499 mCurrentHistoryIndex++;
515 mCurrentInput +=
" ";
533 else if (r.size() > 1)
535 mInCompletion =
true;
542 candidates += candidate +
" ";
543 for (
int i = 0; i < matching_prefix.
size() && i < completion.size(); ++i)
545 if (matching_prefix[i] != completion[i])
547 matching_prefix = matching_prefix.
mid(0, i);
561 return mAbortThreadWidget;
566 :
QFrame(parent), mCount(0)
572 mLabel =
new QLabel(
this);
579 mTimer =
new QTimer(
this);
583 void PythonConsoleAbortThread::handleAbortButton()
586 log_info(
"gui",
"Python console command execution aborted by user");
590 void PythonConsoleAbortThread::handleTimeout()
598 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 insertFromMimeData(const QMimeData *source) override
void insertAtEnd(const QString &text, QColor textColor)
virtual void handleError(const QString &output) override
void appendToCurrentCommand(const QString &new_command)
static PythonConsoleQssAdapter * instance()
std::vector< std::tuple< std::string, std::string > > complete(const QString &text, bool use_console_context)
int checkCompleteStatement(const QString &text)
bool isThreadRunning() const
bool interpretBackground(QObject *caller, const QString &input, bool multiple_expressions=false)
void setConsole(PythonConsole *console)
void interpretForeground(const QString &input)
void intChanged(int value)
#define log_info(channel,...)
void setMarginWidth(QLayout *layout, int marginWidth)
PythonContext * gPythonContext
void setFrameStyle(int style)
bool matches(QKeySequence::StandardKey key) const const
Qt::KeyboardModifiers modifiers() const const
void setText(const QString &)
bool hasText() const const
QString text() const const
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 left(int n) const const
QString mid(int position, int n) const const
QString & replace(int position, int n, QChar after)
QString right(int n) const const
std::string toStdString() const const
QString trimmed() const const
void insertText(const QString &text)
bool movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode, int n)
int position() const const
void removeSelectedText()
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)