@@ -211,19 +211,26 @@ void TLMCoSimulationOutputWidget::managerProcessStarted()
211211 */
212212void 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 */
255262void 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/* !
0 commit comments