LogView patch - addText with force category, color now background
This commit is contained in:
@@ -9,6 +9,16 @@
|
||||
#include <QScrollBar>
|
||||
#include <QPixmap>
|
||||
#include <QEvent>
|
||||
#include <QTextBlockUserData>
|
||||
|
||||
|
||||
class TextBlockData: public QTextBlockUserData {
|
||||
public:
|
||||
TextBlockData(const QString & kw): keyword(kw) {}
|
||||
QString keyword;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
LogView::Category::Category() {
|
||||
@@ -48,6 +58,7 @@ LogView::LogView(QWidget * parent): QWidget(parent) {
|
||||
setLinesLimit(10000);
|
||||
QTextCursor tc(ui->textEdit->document());
|
||||
def_cf = tc.charFormat();
|
||||
def_bf = tc.blockFormat();
|
||||
}
|
||||
|
||||
|
||||
@@ -65,6 +76,7 @@ void LogView::setLogFont(QFont f) {
|
||||
ui->textEdit->document()->setDefaultFont(f);
|
||||
QTextCursor tc(ui->textEdit->document());
|
||||
def_cf = tc.charFormat();
|
||||
def_bf = tc.blockFormat();
|
||||
}
|
||||
|
||||
|
||||
@@ -154,6 +166,11 @@ void LogView::clearCategories() {
|
||||
|
||||
|
||||
void LogView::addText(const QString & text, bool insert_newline) {
|
||||
addText(text, QString(), insert_newline);
|
||||
}
|
||||
|
||||
|
||||
void LogView::addText(const QString & text, const QString & keyword, bool insert_newline) {
|
||||
if (text.isEmpty()) return;
|
||||
QTextCursor tc(ui->textEdit->document());
|
||||
QStringList sl = text.split("\n");
|
||||
@@ -162,8 +179,10 @@ void LogView::addText(const QString & text, bool insert_newline) {
|
||||
bool at_end = (bar->value() == bar->maximum()) || bar->isHidden();
|
||||
for (int i = 0; i < sl.size(); ++i) {
|
||||
tc.insertText(sl[i]);
|
||||
if (!keyword.isEmpty())
|
||||
tc.block().setUserData(new TextBlockData(keyword));
|
||||
if ((i < sl.size() - 1) || insert_newline)
|
||||
newLine();
|
||||
newLine(keyword);
|
||||
}
|
||||
if (at_end)
|
||||
scrollToBottom();
|
||||
@@ -191,19 +210,24 @@ void LogView::changeEvent(QEvent * e) {
|
||||
}
|
||||
|
||||
|
||||
void LogView::newLine() {
|
||||
void LogView::newLine(const QString & keyword) {
|
||||
QTextCursor tc(ui->textEdit->document());
|
||||
tc.movePosition(QTextCursor::End);
|
||||
tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
|
||||
QString line = tc.selectedText();
|
||||
QImage icon;
|
||||
foreach (const Category & c, categories) {
|
||||
if (line.contains(c.regexp)) {
|
||||
QTextCharFormat cf = def_cf;
|
||||
cf.setForeground(c.color);
|
||||
bool matched = false;
|
||||
if (keyword.isEmpty()) matched = line.contains(c.regexp);
|
||||
else matched = (keyword == c.regexp.pattern());
|
||||
if (matched) {
|
||||
QTextCharFormat cf = def_cf;
|
||||
QTextBlockFormat bf = def_bf;
|
||||
bf.setBackground(c.color);
|
||||
if (c.bold)
|
||||
cf.setFontWeight(QFont::Bold);
|
||||
tc.setCharFormat(cf);
|
||||
tc.setBlockFormat(bf);
|
||||
icon = c.icon_image;
|
||||
break;
|
||||
}
|
||||
@@ -222,8 +246,9 @@ void LogView::newLine() {
|
||||
if (isFilterVisible())
|
||||
filterBlock(tc.block(), fs, regexp);
|
||||
tc.movePosition(QTextCursor::End);
|
||||
tc.setCharFormat(def_cf);
|
||||
tc.insertBlock();
|
||||
tc.setCharFormat(def_cf);
|
||||
tc.setBlockFormat(def_bf);
|
||||
}
|
||||
|
||||
|
||||
@@ -240,7 +265,15 @@ void LogView::filterBlock(QTextBlock block, const QString & fs, const QRegularEx
|
||||
if (line[0] == QChar::ObjectReplacementCharacter)
|
||||
line.remove(0, 1);
|
||||
}
|
||||
if (regexp.isValid()) vis = vis && line.contains(regexp);
|
||||
if (regexp.isValid() && !regexp.pattern().isEmpty()) {
|
||||
QString kw;
|
||||
TextBlockData * bd = (TextBlockData*)block.userData();
|
||||
if (bd) kw = bd->keyword;
|
||||
if (!kw.isEmpty())
|
||||
vis = vis && (bd->keyword == regexp.pattern());
|
||||
else
|
||||
vis = vis && line.contains(regexp);
|
||||
}
|
||||
if (!fs.isEmpty()) vis = vis && line.contains(fs, Qt::CaseInsensitive);
|
||||
block.setVisible(vis);
|
||||
//qDebug() << "filterBlock" << line << vis;
|
||||
|
||||
Reference in New Issue
Block a user