1
0
Fork 0

GUI: drastically improve log performance (especially when not visible)

This commit is contained in:
mrexodia 2017-03-12 18:52:02 +01:00
parent edf7760267
commit 0ccb4eedb3
No known key found for this signature in database
GPG Key ID: FC89E0AAA0C1AAD8
2 changed files with 45 additions and 9 deletions

View File

@ -5,6 +5,7 @@
#include <QRegularExpression>
#include <QDesktopServices>
#include <QClipboard>
#include <QTimer>
/**
* @brief LogView::LogView The constructor constructs a rich text browser
@ -20,6 +21,11 @@ LogView::LogView(QWidget* parent) : QTextBrowser(parent), logRedirection(NULL)
this->setLoggingEnabled(true);
autoScroll = true;
flushTimer = new QTimer(this);
flushTimer->setInterval(500);
connect(flushTimer, SIGNAL(timeout()), this, SLOT(flushTimerSlot()));
connect(Bridge::getBridge(), SIGNAL(close()), flushTimer, SLOT(stop()));
connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(updateStyle()));
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(updateStyle()));
connect(Bridge::getBridge(), SIGNAL(addMsgToLog(QByteArray)), this, SLOT(addMsgToLogSlot(QByteArray)));
@ -128,6 +134,19 @@ void LogView::contextMenuEvent(QContextMenuEvent* event)
wMenu.exec(event->globalPos());
}
void LogView::showEvent(QShowEvent* event)
{
flushTimerSlot();
flushTimer->start();
QTextBrowser::showEvent(event);
}
void LogView::hideEvent(QHideEvent* event)
{
flushTimer->stop();
QTextBrowser::hideEvent(event);
}
/**
* @brief linkify Add hyperlink HTML to the message where applicable.
* @param msg The message passed by reference.
@ -176,7 +195,6 @@ void LogView::addMsgToLogSlot(QByteArray msg)
{
fclose(logRedirection);
logRedirection = NULL;
//msg += tr("fwrite() failed (GetLastError()= %1 ). Log redirection stopped.\n").arg(GetLastError());
redirectError = true;
}
}
@ -209,7 +227,6 @@ void LogView::addMsgToLogSlot(QByteArray msg)
{
fclose(logRedirection);
logRedirection = NULL;
//msg += tr("fwrite() failed (GetLastError()= %1 ). Log redirection stopped.\n").arg(GetLastError());
redirectError = true;
}
if(loggingEnabled)
@ -243,13 +260,12 @@ void LogView::addMsgToLogSlot(QByteArray msg)
msgUtf16.replace(QString("\r\n"), QString("<br/>\n"));
}
linkify(msgUtf16);
QTextCursor cursor = this->textCursor();
cursor.movePosition(QTextCursor::End);
if(redirectError)
msgUtf16 += tr("fwrite() failed (GetLastError()= %1 ). Log redirection stopped.\n").arg(GetLastError());
cursor.insertHtml(msgUtf16);
if(autoScroll)
this->moveCursor(QTextCursor::End);
msgUtf16.append(tr("fwrite() failed (GetLastError()= %1 ). Log redirection stopped.\n").arg(GetLastError()));
if(logBuffer.length() >= 10000 * 10) //limit the log buffer to ~10mb
logBuffer.clear();
logBuffer.append(msgUtf16);
}
/**
@ -409,3 +425,17 @@ void LogView::pasteSlot()
clipboardText.append('\n');
addMsgToLogSlot(clipboardText.toUtf8());
}
void LogView::flushTimerSlot()
{
if(logBuffer.isEmpty())
return;
setUpdatesEnabled(false);
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::End);
cursor.insertHtml(logBuffer);
if(autoScroll)
moveCursor(QTextCursor::End);
setUpdatesEnabled(true);
logBuffer.clear();
}

View File

@ -11,7 +11,9 @@ public:
explicit LogView(QWidget* parent = 0);
~LogView();
void setupContextMenu();
void contextMenuEvent(QContextMenuEvent* event);
void contextMenuEvent(QContextMenuEvent* event) override;
void showEvent(QShowEvent* event) override;
void hideEvent(QHideEvent* event) override;
public slots:
void refreshShortcutsSlot();
@ -29,6 +31,8 @@ public slots:
void clearLogSlot();
void saveSlot();
void toggleLoggingSlot();
void flushTimerSlot();
private:
bool loggingEnabled;
bool autoScroll;
@ -47,6 +51,8 @@ private:
QAction* actionCopyToDebuggeeNotes;
FILE* logRedirection;
QString logBuffer;
QTimer* flushTimer;
};
#endif // LOGVIEW_H