bolding timestamps on log

This commit is contained in:
kunalgosar 2017-03-10 11:55:36 -08:00
parent 494b602d4c
commit a974282bdf
3 changed files with 36 additions and 13 deletions

View file

@ -1,6 +1,6 @@
QPlainTextEdit {
font-family: Inconsolata, Lucida Console, Andale Mono, Monaco;
font-family: Courier New, Courier, Monotype;
font-size: 16px;
padding-left: 28px;
padding-top: 7px;
@ -11,7 +11,7 @@ QPlainTextEdit {
}
QLineEdit {
font-family: Inconsolata, Lucida Console, Andale Mono, Monaco;
font-family: Courier New, Courier, Monotype;
padding-left: 7px;
background-color: #CCCCCC;
border-width: 0;

View file

@ -28,17 +28,22 @@ const int SEARCH_BUTTON_LEFT = 25;
const int SEARCH_BUTTON_WIDTH = 20;
const int SEARCH_TOGGLE_BUTTON_WIDTH = 50;
const int SEARCH_TEXT_WIDTH = 240;
const int TIME_STAMP_LENGTH = 16;
const QColor HIGHLIGHT_COLOR = QColor("#3366CC");
const QColor BOLD_COLOR = QColor("#445c8c");
const QString BOLD_PATTERN = "\\[\\d*\\/.*:\\d*:\\d*\\]";
class KeywordHighlighter : public QSyntaxHighlighter {
class Highlighter : public QSyntaxHighlighter {
public:
KeywordHighlighter(QTextDocument* parent = nullptr);
Highlighter(QTextDocument* parent = nullptr);
void setBold(int indexToBold);
QString keyword;
protected:
void highlightBlock(const QString& text) override;
private:
QTextCharFormat boldFormat;
QTextCharFormat keywordFormat;
};
@ -101,9 +106,8 @@ void BaseLogDialog::initControls() {
_logTextBox = new QPlainTextEdit(this);
_logTextBox->setReadOnly(true);
_logTextBox->show();
_highlighter = new KeywordHighlighter(_logTextBox->document());
_highlighter = new Highlighter(_logTextBox->document());
connect(_logTextBox, SIGNAL(selectionChanged()), SLOT(updateSelection()));
}
void BaseLogDialog::showEvent(QShowEvent* event) {
@ -116,7 +120,9 @@ void BaseLogDialog::resizeEvent(QResizeEvent* event) {
void BaseLogDialog::appendLogLine(QString logLine) {
if (logLine.contains(_searchTerm, Qt::CaseInsensitive)) {
int indexToBold = _logTextBox->document()->characterCount();
_logTextBox->appendPlainText(logLine.trimmed());
_highlighter->setBold(indexToBold);
}
}
@ -175,6 +181,7 @@ void BaseLogDialog::showLogData() {
_logTextBox->clear();
_logTextBox->appendPlainText(getCurrentLog());
_logTextBox->ensureCursorVisible();
_highlighter->rehighlight();
}
void BaseLogDialog::updateSelection() {
@ -187,16 +194,28 @@ void BaseLogDialog::updateSelection() {
}
}
KeywordHighlighter::KeywordHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {
Highlighter::Highlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {
boldFormat.setFontWeight(75);
boldFormat.setForeground(BOLD_COLOR);
keywordFormat.setForeground(HIGHLIGHT_COLOR);
}
}
void KeywordHighlighter::highlightBlock(const QString& text) {
void Highlighter::highlightBlock(const QString& text) {
QRegExp expression(BOLD_PATTERN);
int index = text.indexOf(expression, 0);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, boldFormat);
index = text.indexOf(expression, index + length);
}
if (keyword.isNull() || keyword.isEmpty()) {
return;
}
int index = text.indexOf(keyword, 0, Qt::CaseInsensitive);
index = text.indexOf(keyword, 0, Qt::CaseInsensitive);
int length = keyword.length();
while (index >= 0) {
@ -204,3 +223,7 @@ void KeywordHighlighter::highlightBlock(const QString& text) {
index = text.indexOf(keyword, index + length, Qt::CaseInsensitive);
}
}
void Highlighter::setBold(int indexToBold) {
setFormat(indexToBold, TIME_STAMP_LENGTH, boldFormat);
}

View file

@ -23,7 +23,7 @@ const int BUTTON_MARGIN = 8;
class QPushButton;
class QLineEdit;
class QPlainTextEdit;
class KeywordHighlighter;
class Highlighter;
class BaseLogDialog : public QDialog {
Q_OBJECT
@ -56,7 +56,7 @@ private:
QPushButton* _searchPrevButton { nullptr };
QPushButton* _searchNextButton { nullptr };
QString _searchTerm;
KeywordHighlighter* _highlighter { nullptr };
Highlighter* _highlighter { nullptr };
void initControls();
void showLogData();