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 {
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) {
if (!regexp.isValid()) return;
if (!regexp.isValid() || regexp.pattern().isEmpty()) return;
removeCategory(regexp);
Category c;
c.regexp = regexp;
categories.removeAll(c);
c.label = label;
c.image = icon;
c.color = color;
c.bold = bold;
c.makeIcon(iconImageSize(), preferredIconSize(1., this));
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");
tc.movePosition(QTextCursor::End);
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) {
if (sl[i].isEmpty()) return;
tc.insertText(sl[i]);
if ((i < sl.size() - 1) || insert_newline)
newLine();
}
if (at_end)
bar->setValue(bar->maximum());
if (ui->widgetToolbar->isVisible())
filter();
scrollToBottom();
}
void LogView::changeEvent(QEvent * e) {
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
actionLogSelectAll->setText(tr("Select All"));
actionLogCopy->setText(tr("Copy"));
actionLogClear->setText(tr("Clear"));
break;
case QEvent::Polish: {
ui->labelIconSearch->setFixedSize(preferredIconSize(1.2, this));
QSize is = iconImageSize(), is_i = preferredIconSize(1., this);
QMutableListIterator<Category> it(categories);
while (it.hasNext())
it.next().makeIcon(is, is_i);
case QEvent::LanguageChange:
ui->retranslateUi(this);
ui->comboCategory->setItemText(0, tr("All"));
actionLogSelectAll->setText(tr("Select All"));
actionLogCopy->setText(tr("Copy"));
actionLogClear->setText(tr("Clear"));
break;
case QEvent::Polish: {
ui->labelIconSearch->setFixedSize(preferredIconSize(1.2, this));
QSize is = iconImageSize(), is_i = preferredIconSize(1., this);
for (int i = 0; i < categories.size(); ++i)
categories[i].makeIcon(is, is_i);
} break;
default:
break;
default: break;
}
}
@@ -152,9 +179,7 @@ void LogView::newLine() {
tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
QString line = tc.selectedText();
QImage icon;
QListIterator<Category> it(categories);
while (it.hasNext()) {
const Category & c(it.next());
foreach (const Category & c, categories) {
if (line.contains(c.regexp)) {
QTextCharFormat cf = def_cf;
cf.setForeground(c.color);
@@ -169,6 +194,10 @@ void LogView::newLine() {
tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
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.setCharFormat(def_cf);
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) {
ui->widgetToolbar->setHidden(!yes);
filter();
}
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() {
QTextDocument * doc = ui->textEdit->document();
int bc = doc->blockCount();
QRegularExpression regexp = ui->comboCategory->currentData().toRegularExpression();
QString fs = ui->lineEdit->text();
QRegularExpression regexp;
QString fs;
if (isFilterVisible()) {
regexp = ui->comboCategory->currentData().toRegularExpression();
fs = ui->lineEdit->text();
}
QTextBlock bl;
for (int i = 0; i < bc; ++i) {
bl = doc->findBlockByNumber(i);
bool vis = true;
if (regexp.isValid()) vis = vis && bl.text().contains(regexp);
if (!fs.isEmpty()) vis = vis && bl.text().contains(fs[1], Qt::CaseInsensitive);
bl.setVisible(vis);
filterBlock(bl, fs, regexp);
}
doc->markContentsDirty(0, bl.position() + bl.length());
}

View File

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

View File

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