Skip to content

Commit 7a1f3f4

Browse files
lassoanjcfr
authored andcommitted
ENH: Make console message printing methods available in Python
Make console message printing methods (printMessage, printOutputMessage, printErrorMessage) available in Python. This is useful for example when custom display hook is used in Python and output is not redirected to stderr/stdout.
1 parent 8c7ea6e commit 7a1f3f4

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,9 @@ void ctkPythonConsole::initialize(ctkAbstractPythonManager* newPythonManager)
578578
d->initializeInteractiveConsole();
579579

580580
this->connect(PythonQt::self(), SIGNAL(pythonStdOut(QString)),
581-
d, SLOT(printOutputMessage(QString)));
581+
this, SLOT(printOutputMessage(QString)));
582582
this->connect(PythonQt::self(), SIGNAL(pythonStdErr(QString)),
583-
d, SLOT(printErrorMessage(QString)));
583+
this, SLOT(printErrorMessage(QString)));
584584

585585
PythonQt::self()->setRedirectStdInCallback(
586586
ctkConsole::stdInRedirectCallBack, reinterpret_cast<void*>(this));

Libs/Widgets/ctkConsole.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -846,32 +846,6 @@ void ctkConsolePrivate::printString(const QString& text)
846846
this->textCursor().insertText(text);
847847
}
848848

849-
//----------------------------------------------------------------------------
850-
void ctkConsolePrivate::printOutputMessage(const QString& text)
851-
{
852-
Q_Q(ctkConsole);
853-
QString textToPrint = text;
854-
if (this->MessageOutputSize == 0)
855-
{
856-
textToPrint.prepend("\n");
857-
}
858-
this->MessageOutputSize += textToPrint.size();
859-
q->printMessage(textToPrint, q->outputTextColor());
860-
}
861-
862-
//----------------------------------------------------------------------------
863-
void ctkConsolePrivate::printErrorMessage(const QString& text)
864-
{
865-
Q_Q(ctkConsole);
866-
QString textToPrint = text;
867-
if (this->MessageOutputSize == 0)
868-
{
869-
textToPrint.prepend("\n");
870-
}
871-
this->MessageOutputSize += textToPrint.size();
872-
q->printMessage(textToPrint, q->errorTextColor());
873-
}
874-
875849
//-----------------------------------------------------------------------------
876850
void ctkConsolePrivate::printCommand(const QString& cmd)
877851
{
@@ -1339,6 +1313,32 @@ void ctkConsole::printMessage(const QString& message, const QColor& color)
13391313
d->printString(message);
13401314
}
13411315

1316+
//----------------------------------------------------------------------------
1317+
void ctkConsole::printOutputMessage(const QString& text)
1318+
{
1319+
Q_D(ctkConsole);
1320+
QString textToPrint = text;
1321+
if (d->MessageOutputSize == 0)
1322+
{
1323+
textToPrint.prepend("\n");
1324+
}
1325+
d->MessageOutputSize += textToPrint.size();
1326+
this->printMessage(textToPrint, this->outputTextColor());
1327+
}
1328+
1329+
//----------------------------------------------------------------------------
1330+
void ctkConsole::printErrorMessage(const QString& text)
1331+
{
1332+
Q_D(ctkConsole);
1333+
QString textToPrint = text;
1334+
if (d->MessageOutputSize == 0)
1335+
{
1336+
textToPrint.prepend("\n");
1337+
}
1338+
d->MessageOutputSize += textToPrint.size();
1339+
this->printMessage(textToPrint, this->errorTextColor());
1340+
}
1341+
13421342
//-----------------------------------------------------------------------------
13431343
void ctkConsole::clear()
13441344
{

Libs/Widgets/ctkConsole.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@ class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
172172
/// \sa scrollBarPolicy()
173173
void setScrollBarPolicy(const Qt::ScrollBarPolicy& newScrollBarPolicy);
174174

175-
/// Prints text on the console
176-
void printMessage(const QString& message, const QColor& color);
177-
178175
/// Returns the string used as primary prompt
179176
virtual QString ps1() const;
180177

@@ -256,6 +253,17 @@ public Q_SLOTS:
256253
/// Print the console help with shortcuts.
257254
virtual void printHelp();
258255

256+
/// Prints text on the console
257+
void printMessage(const QString& message, const QColor& color);
258+
259+
/// Print a message
260+
/// \sa ctkConsole::outputTextColor
261+
void printOutputMessage(const QString& text);
262+
263+
/// Print a message
264+
/// \sa ctkConsole::errorTextColor
265+
void printErrorMessage(const QString& text);
266+
259267
protected:
260268

261269
/// Prompt the user for input

Libs/Widgets/ctkConsole_p.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,6 @@ public Q_SLOTS:
123123
/// FALSE - Just insert the word replacing only the text from the current position until StartOfWord
124124
void insertCompletion(const QString& text);
125125

126-
/// Print a message
127-
/// \sa ctkConsole::outputTextColor
128-
void printOutputMessage(const QString& text);
129-
130-
/// Print a message
131-
/// \sa ctkConsole::errorTextColor
132-
void printErrorMessage(const QString& text);
133-
134126
/// Update the value of ScrollbarAtBottom given the current position of the scollbar
135127
void onScrollBarValueChanged(int value);
136128

0 commit comments

Comments
 (0)