LogView fixes and optimizations

This commit is contained in:
2020-06-19 19:17:30 +03:00
parent a195ec4006
commit f02c38884b
3 changed files with 99 additions and 34 deletions

View File

@@ -79,7 +79,9 @@ bool LogView::isFilterVisible() const {
int LogView::linesLimit() const { int LogView::linesLimit() const {
return ui->textEdit->document()->maximumBlockCount(); int ret = ui->textEdit->document()->maximumBlockCount();
if (ret > 0) --ret;
return ret;
} }
@@ -90,17 +92,46 @@ void LogView::registerCategory(const QString & label, QString keyword, const QIm
void LogView::registerCategory(const QString & label, QRegularExpression regexp, const QImage & icon, QColor color, bool bold) { void LogView::registerCategory(const QString & label, QRegularExpression regexp, const QImage & icon, QColor color, bool bold) {
if (!regexp.isValid()) return; if (!regexp.isValid() || regexp.pattern().isEmpty()) return;
removeCategory(regexp);
Category c; Category c;
c.regexp = regexp; c.regexp = regexp;
categories.removeAll(c);
c.label = label; c.label = label;
c.image = icon; c.image = icon;
c.color = color; c.color = color;
c.bold = bold; c.bold = bold;
c.makeIcon(iconImageSize(), preferredIconSize(1., this)); c.makeIcon(iconImageSize(), preferredIconSize(1., this));
categories.append(c); categories.append(c);
ui->comboCategory->addItem(c.icon, label, regexp); ui->comboCategory->addItem(c.icon, label, QVariant(regexp));
}
void LogView::removeCategory(QString keyword) {
QRegularExpression regexp(keyword, QRegularExpression::PatternOptions(QRegularExpression::CaseInsensitiveOption));
removeCategory(regexp);
}
void LogView::removeCategory(QRegularExpression regexp) {
Category c;
c.regexp = regexp;
categories.removeAll(c);
for (int i = 1; i < ui->comboCategory->count(); ++i) {
if (ui->comboCategory->itemData(i).toRegularExpression().pattern() == regexp.pattern()) {
ui->comboCategory->removeItem(i);
--i;
}
}
}
void LogView::clearCategories() {
ui->comboCategory->blockSignals(true);
while (ui->comboCategory->count() > 1)
ui->comboCategory->removeItem(ui->comboCategory->count() - 1);
categories.clear();
ui->comboCategory->blockSignals(false);
filter();
} }
@@ -110,38 +141,34 @@ void LogView::addText(const QString & text, bool insert_newline) {
QStringList sl = text.split("\n"); QStringList sl = text.split("\n");
tc.movePosition(QTextCursor::End); tc.movePosition(QTextCursor::End);
QScrollBar * bar = ui->textEdit->verticalScrollBar(); QScrollBar * bar = ui->textEdit->verticalScrollBar();
bool at_end = (bar->value() == bar->maximum()); bool at_end = (bar->value() == bar->maximum()) || bar->isHidden();
for (int i = 0; i < sl.size(); ++i) { for (int i = 0; i < sl.size(); ++i) {
if (sl[i].isEmpty()) return;
tc.insertText(sl[i]); tc.insertText(sl[i]);
if ((i < sl.size() - 1) || insert_newline) if ((i < sl.size() - 1) || insert_newline)
newLine(); newLine();
} }
if (at_end) if (at_end)
bar->setValue(bar->maximum()); scrollToBottom();
if (ui->widgetToolbar->isVisible())
filter();
} }
void LogView::changeEvent(QEvent * e) { void LogView::changeEvent(QEvent * e) {
QWidget::changeEvent(e); QWidget::changeEvent(e);
switch (e->type()) { switch (e->type()) {
case QEvent::LanguageChange: case QEvent::LanguageChange:
ui->retranslateUi(this); ui->retranslateUi(this);
actionLogSelectAll->setText(tr("Select All")); ui->comboCategory->setItemText(0, tr("All"));
actionLogCopy->setText(tr("Copy")); actionLogSelectAll->setText(tr("Select All"));
actionLogClear->setText(tr("Clear")); actionLogCopy->setText(tr("Copy"));
break; actionLogClear->setText(tr("Clear"));
case QEvent::Polish: { break;
ui->labelIconSearch->setFixedSize(preferredIconSize(1.2, this)); case QEvent::Polish: {
QSize is = iconImageSize(), is_i = preferredIconSize(1., this); ui->labelIconSearch->setFixedSize(preferredIconSize(1.2, this));
QMutableListIterator<Category> it(categories); QSize is = iconImageSize(), is_i = preferredIconSize(1., this);
while (it.hasNext()) for (int i = 0; i < categories.size(); ++i)
it.next().makeIcon(is, is_i); categories[i].makeIcon(is, is_i);
} break; } break;
default: default: break;
break;
} }
} }
@@ -152,9 +179,7 @@ void LogView::newLine() {
tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor); tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
QString line = tc.selectedText(); QString line = tc.selectedText();
QImage icon; QImage icon;
QListIterator<Category> it(categories); foreach (const Category & c, categories) {
while (it.hasNext()) {
const Category & c(it.next());
if (line.contains(c.regexp)) { if (line.contains(c.regexp)) {
QTextCharFormat cf = def_cf; QTextCharFormat cf = def_cf;
cf.setForeground(c.color); cf.setForeground(c.color);
@@ -169,6 +194,10 @@ void LogView::newLine() {
tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor); tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
tc.insertImage(icon); tc.insertImage(icon);
} }
QRegularExpression regexp = ui->comboCategory->currentData().toRegularExpression();
QString fs = ui->lineEdit->text();
if (isFilterVisible())
filterBlock(tc.block(), fs, regexp);
tc.movePosition(QTextCursor::End); tc.movePosition(QTextCursor::End);
tc.setCharFormat(def_cf); tc.setCharFormat(def_cf);
tc.insertBlock(); tc.insertBlock();
@@ -181,13 +210,30 @@ QSize LogView::iconImageSize() {
} }
void LogView::filterBlock(QTextBlock block, const QString & fs, const QRegularExpression & regexp) {
bool vis = true;//, pvis = block.isVisible();
QString line = block.text();
if (!line.isEmpty()) {
if (line[0] == QChar::ObjectReplacementCharacter)
line.remove(0, 1);
}
if (regexp.isValid()) vis = vis && line.contains(regexp);
if (!fs.isEmpty()) vis = vis && line.contains(fs, Qt::CaseInsensitive);
block.setVisible(vis);
//qDebug() << "filterBlock" << line << vis;
//if (vis != pvis)
// ;//ui->textEdit->document()->mar
}
void LogView::setFilterVisible(bool yes) { void LogView::setFilterVisible(bool yes) {
ui->widgetToolbar->setHidden(!yes); ui->widgetToolbar->setHidden(!yes);
filter();
} }
void LogView::setLinesLimit(int l) { void LogView::setLinesLimit(int l) {
ui->textEdit->document()->setMaximumBlockCount(l); ui->textEdit->document()->setMaximumBlockCount(l <= 0 ? 0 : l + 1);
} }
@@ -196,18 +242,25 @@ void LogView::clear() {
} }
void LogView::scrollToBottom() {
QScrollBar * bar = ui->textEdit->verticalScrollBar();
bar->setValue(bar->maximum());
}
void LogView::filter() { void LogView::filter() {
QTextDocument * doc = ui->textEdit->document(); QTextDocument * doc = ui->textEdit->document();
int bc = doc->blockCount(); int bc = doc->blockCount();
QRegularExpression regexp = ui->comboCategory->currentData().toRegularExpression(); QRegularExpression regexp;
QString fs = ui->lineEdit->text(); QString fs;
if (isFilterVisible()) {
regexp = ui->comboCategory->currentData().toRegularExpression();
fs = ui->lineEdit->text();
}
QTextBlock bl; QTextBlock bl;
for (int i = 0; i < bc; ++i) { for (int i = 0; i < bc; ++i) {
bl = doc->findBlockByNumber(i); bl = doc->findBlockByNumber(i);
bool vis = true; filterBlock(bl, fs, regexp);
if (regexp.isValid()) vis = vis && bl.text().contains(regexp);
if (!fs.isEmpty()) vis = vis && bl.text().contains(fs[1], Qt::CaseInsensitive);
bl.setVisible(vis);
} }
doc->markContentsDirty(0, bl.position() + bl.length()); doc->markContentsDirty(0, bl.position() + bl.length());
} }

View File

@@ -28,6 +28,7 @@
#include "qad_export.h" #include "qad_export.h"
class QTextEdit; class QTextEdit;
class QTextBlock;
class QAction; class QAction;
namespace Ui { namespace Ui {
@@ -58,10 +59,13 @@ public:
bool bold = false); bool bold = false);
void registerCategory(const QString & label, void registerCategory(const QString & label,
QRegularExpression regexp = QRegularExpression(), QRegularExpression regexp,
const QImage & icon = QImage(), const QImage & icon = QImage(),
QColor color = QColor(), QColor color = QColor(),
bool bold = false); bool bold = false);
void removeCategory(QString keyword);
void removeCategory(QRegularExpression regexp);
void clearCategories();
void addText(const QString & text, bool insert_newline = true); void addText(const QString & text, bool insert_newline = true);
@@ -81,6 +85,7 @@ private:
void changeEvent(QEvent * e); void changeEvent(QEvent * e);
void newLine(); void newLine();
QSize iconImageSize(); QSize iconImageSize();
void filterBlock(QTextBlock block, const QString & fs, const QRegularExpression & regexp);
Ui::LogView * ui; Ui::LogView * ui;
QList<Category> categories; QList<Category> categories;
@@ -93,6 +98,7 @@ public slots:
void clear(); void clear();
private slots: private slots:
void scrollToBottom();
void filter(); void filter();
signals: signals:

View File

@@ -107,6 +107,12 @@
<property name="readOnly"> <property name="readOnly">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="acceptRichText">
<bool>false</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse</set>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>