Skip to content

Commit ebbfe52

Browse files
committed
Fixed printing the output of manager and monitor.
1 parent a4b17ce commit ebbfe52

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

OMEdit/OMEditGUI/TLM/TLMCoSimulationOutputWidget.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,26 @@ void TLMCoSimulationOutputWidget::managerProcessStarted()
211211
*/
212212
void TLMCoSimulationOutputWidget::writeManagerOutput(QString output, StringHandler::SimulationMessageType type)
213213
{
214-
/* move the cursor down before adding to the logger. */
214+
// move the cursor down before adding to the logger.
215215
QTextCursor textCursor = mpManagerOutputTextBox->textCursor();
216-
textCursor.movePosition(QTextCursor::End);
217-
mpManagerOutputTextBox->setTextCursor(textCursor);
218-
/* set the text color */
219-
QTextCharFormat charFormat = mpManagerOutputTextBox->currentCharFormat();
220-
charFormat.setForeground(StringHandler::getSimulationMessageTypeColor(type));
221-
mpManagerOutputTextBox->setCurrentCharFormat(charFormat);
222-
/* append the output */
223-
mpManagerOutputTextBox->insertPlainText(output + "\n");
224-
/* move the cursor */
225-
textCursor.movePosition(QTextCursor::End);
226-
mpManagerOutputTextBox->setTextCursor(textCursor);
216+
const bool atBottom = mpManagerOutputTextBox->verticalScrollBar()->value() == mpManagerOutputTextBox->verticalScrollBar()->maximum();
217+
if (!textCursor.atEnd()) {
218+
textCursor.movePosition(QTextCursor::End);
219+
}
220+
// set the text color
221+
QTextCharFormat format;
222+
format.setForeground(StringHandler::getSimulationMessageTypeColor(type));
223+
textCursor.beginEditBlock();
224+
textCursor.insertText(output, format);
225+
textCursor.endEditBlock();
226+
// move the cursor
227+
if (atBottom) {
228+
mpManagerOutputTextBox->verticalScrollBar()->setValue(mpManagerOutputTextBox->verticalScrollBar()->maximum());
229+
// QPlainTextEdit destroys the first calls value in case of multiline
230+
// text, so make sure that the scroll bar actually gets the value set.
231+
// Is a noop if the first call succeeded.
232+
mpManagerOutputTextBox->verticalScrollBar()->setValue(mpManagerOutputTextBox->verticalScrollBar()->maximum());
233+
}
227234
}
228235

229236
/*!
@@ -254,19 +261,26 @@ void TLMCoSimulationOutputWidget::monitorProcessStarted()
254261
*/
255262
void TLMCoSimulationOutputWidget::writeMonitorOutput(QString output, StringHandler::SimulationMessageType type)
256263
{
257-
/* move the cursor down before adding to the logger. */
264+
// move the cursor down before adding to the logger.
258265
QTextCursor textCursor = mpMonitorOutputTextBox->textCursor();
259-
textCursor.movePosition(QTextCursor::End);
260-
mpMonitorOutputTextBox->setTextCursor(textCursor);
261-
/* set the text color */
262-
QTextCharFormat charFormat = mpMonitorOutputTextBox->currentCharFormat();
263-
charFormat.setForeground(StringHandler::getSimulationMessageTypeColor(type));
264-
mpMonitorOutputTextBox->setCurrentCharFormat(charFormat);
265-
/* append the output */
266-
mpMonitorOutputTextBox->insertPlainText(output + "\n");
267-
/* move the cursor */
268-
textCursor.movePosition(QTextCursor::End);
269-
mpMonitorOutputTextBox->setTextCursor(textCursor);
266+
const bool atBottom = mpMonitorOutputTextBox->verticalScrollBar()->value() == mpMonitorOutputTextBox->verticalScrollBar()->maximum();
267+
if (!textCursor.atEnd()) {
268+
textCursor.movePosition(QTextCursor::End);
269+
}
270+
// set the text color
271+
QTextCharFormat format;
272+
format.setForeground(StringHandler::getSimulationMessageTypeColor(type));
273+
textCursor.beginEditBlock();
274+
textCursor.insertText(output, format);
275+
textCursor.endEditBlock();
276+
// move the cursor
277+
if (atBottom) {
278+
mpMonitorOutputTextBox->verticalScrollBar()->setValue(mpManagerOutputTextBox->verticalScrollBar()->maximum());
279+
// QPlainTextEdit destroys the first calls value in case of multiline
280+
// text, so make sure that the scroll bar actually gets the value set.
281+
// Is a noop if the first call succeeded.
282+
mpMonitorOutputTextBox->verticalScrollBar()->setValue(mpManagerOutputTextBox->verticalScrollBar()->maximum());
283+
}
270284
}
271285

272286
/*!

OMEdit/OMEditGUI/TLM/TLMCoSimulationThread.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void TLMCoSimulationThread::runManager()
105105
// start the executable
106106
mpManagerProcess->start(fileName, args);
107107
mManagerProcessId = Utilities::getProcessId(mpManagerProcess);
108-
emit sendManagerOutput(QString("%1 %2").arg(fileName).arg(args.join(" ")), StringHandler::OMEditInfo);
108+
emit sendManagerOutput(QString("%1 %2\n").arg(fileName).arg(args.join(" ")), StringHandler::OMEditInfo);
109109
}
110110

111111
void TLMCoSimulationThread::runMonitor()
@@ -134,7 +134,7 @@ void TLMCoSimulationThread::runMonitor()
134134
environment.insert("TLMPluginPath", tlmCoSimulationOptions.getTLMPluginPath());
135135
mpMonitorProcess->setProcessEnvironment(environment);
136136
mpMonitorProcess->start(fileName, args);
137-
emit sendMonitorOutput(QString("%1 %2").arg(fileName).arg(args.join(" ")), StringHandler::OMEditInfo);
137+
emit sendMonitorOutput(QString("%1 %2\n").arg(fileName).arg(args.join(" ")), StringHandler::OMEditInfo);
138138
}
139139

140140
/*!
@@ -174,9 +174,9 @@ void TLMCoSimulationThread::readManagerStandardError()
174174
void TLMCoSimulationThread::managerProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
175175
{
176176
setIsManagerProcessRunning(false);
177-
QString exitCodeStr = tr("TLMManager process failed. Exited with code %1.").arg(QString::number(exitCode));
177+
QString exitCodeStr = tr("TLMManager process failed. Exited with code %1.\n").arg(QString::number(exitCode));
178178
if (exitStatus == QProcess::NormalExit && exitCode == 0) {
179-
emit sendManagerOutput(tr("TLMManager process finished successfully."), StringHandler::OMEditInfo);
179+
emit sendManagerOutput(tr("TLMManager process finished successfully.\n"), StringHandler::OMEditInfo);
180180
} else if (mpManagerProcess->error() == QProcess::UnknownError) {
181181
emit sendManagerOutput(exitCodeStr, StringHandler::Error);
182182
} else {
@@ -243,9 +243,9 @@ void TLMCoSimulationThread::monitorProcessFinished(int exitCode, QProcess::ExitS
243243
if (mpProgressFileTimer) {
244244
mpProgressFileTimer->stop();
245245
}
246-
QString exitCodeStr = tr("TLMMonitor process failed. Exited with code %1.").arg(QString::number(exitCode));
246+
QString exitCodeStr = tr("TLMMonitor process failed. Exited with code %1.\n").arg(QString::number(exitCode));
247247
if (exitStatus == QProcess::NormalExit && exitCode == 0) {
248-
emit sendMonitorOutput(tr("TLMMonitor process finished successfully."), StringHandler::OMEditInfo);
248+
emit sendMonitorOutput(tr("TLMMonitor process finished successfully.\n"), StringHandler::OMEditInfo);
249249
} else if (mpMonitorProcess->error() == QProcess::UnknownError) {
250250
emit sendMonitorOutput(exitCodeStr, StringHandler::Error);
251251
} else {

0 commit comments

Comments
 (0)