logview improvments and fixes
This commit is contained in:
@@ -28,11 +28,19 @@ void LogView::Category::makeIcon(QSize size, QSize size_icon) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
LogView::LogView(QWidget * parent): QWidget(parent) {
|
||||
ui = new Ui::LogView();
|
||||
ui->setupUi(this);
|
||||
ui->textEdit->setContextMenuPolicy(Qt::ActionsContextMenu);
|
||||
actionLogSelectAll = new QAction(QIcon(":/icons/select-all.png"), tr("Select All"));
|
||||
actionLogCopy = new QAction(QIcon(":/icons/edit-copy.png"), tr("Copy"));
|
||||
actionLogClear = new QAction(QIcon(":/icons/edit-clear.png"), tr("Clear"));
|
||||
connect(actionLogSelectAll, SIGNAL(triggered(bool)), ui->textEdit, SLOT(selectAll()));
|
||||
connect(actionLogCopy, SIGNAL(triggered(bool)), ui->textEdit, SLOT(copy()));
|
||||
connect(actionLogClear, SIGNAL(triggered(bool)), ui->textEdit, SLOT(clear()));
|
||||
ui->textEdit->addAction(actionLogSelectAll);
|
||||
ui->textEdit->addAction(actionLogCopy);
|
||||
ui->textEdit->addAction(actionLogClear);
|
||||
ui->buttonClear->setDefaultAction(ui->actionClear);
|
||||
ui->labelIconSearch->setFixedSize(preferredIconSize(1.2, this));
|
||||
ui->comboCategory->addItem(tr("All"));
|
||||
@@ -66,7 +74,7 @@ QFont LogView::logFont() const {
|
||||
|
||||
|
||||
bool LogView::isFilterVisible() const {
|
||||
return !ui->widgetToolbar->isHidden();
|
||||
return ui->widgetToolbar->isVisible();
|
||||
}
|
||||
|
||||
|
||||
@@ -76,43 +84,64 @@ int LogView::linesLimit() const {
|
||||
|
||||
|
||||
void LogView::registerCategory(const QString & label, QString keyword, const QImage & icon, QColor color, bool bold) {
|
||||
if (keyword.isEmpty()) keyword = label;
|
||||
if (keyword.isEmpty()) return;
|
||||
Category & c(categories[keyword]);
|
||||
QRegularExpression regexp(keyword, QRegularExpression::PatternOptions(QRegularExpression::CaseInsensitiveOption));
|
||||
registerCategory(label, regexp, icon, color, bold);
|
||||
}
|
||||
|
||||
|
||||
void LogView::registerCategory(const QString & label, QRegularExpression regexp, const QImage & icon, QColor color, bool bold) {
|
||||
if (!regexp.isValid()) return;
|
||||
Category c;
|
||||
c.regexp = regexp;
|
||||
categories.removeAll(c);
|
||||
c.label = label;
|
||||
c.keyword = keyword;
|
||||
c.image = icon;
|
||||
c.color = color;
|
||||
c.bold = bold;
|
||||
c.makeIcon(iconImageSize(), preferredIconSize(1., this));
|
||||
ui->comboCategory->addItem(c.icon, label, keyword);
|
||||
categories.append(c);
|
||||
ui->comboCategory->addItem(c.icon, label, regexp);
|
||||
}
|
||||
|
||||
|
||||
void LogView::addText(const QString & text, bool insert_newline) {
|
||||
if (text.isEmpty()) return;
|
||||
QTextCursor tc(ui->textEdit->document());
|
||||
QStringList sl = text.split("\n");
|
||||
tc.movePosition(QTextCursor::End);
|
||||
QScrollBar * bar = ui->textEdit->verticalScrollBar();
|
||||
bool at_end = (bar->value() == bar->maximum());
|
||||
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());
|
||||
filter();
|
||||
if (ui->widgetToolbar->isVisible())
|
||||
filter();
|
||||
}
|
||||
|
||||
|
||||
void LogView::changeEvent(QEvent * e) {
|
||||
if (e->type() == QEvent::Polish) {
|
||||
ui->labelIconSearch->setFixedSize(preferredIconSize(1.2, this));
|
||||
QSize is = iconImageSize(), is_i = preferredIconSize(1., this);
|
||||
QMutableMapIterator<QString, Category> it(categories);
|
||||
while (it.hasNext())
|
||||
it.next().value().makeIcon(is, is_i);
|
||||
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);
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,16 +152,16 @@ void LogView::newLine() {
|
||||
tc.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
|
||||
QString line = tc.selectedText();
|
||||
QImage icon;
|
||||
QMapIterator<QString, Category> it(categories);
|
||||
QListIterator<Category> it(categories);
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
if (line.contains(it.key(), Qt::CaseInsensitive)) {
|
||||
const Category & c(it.next());
|
||||
if (line.contains(c.regexp)) {
|
||||
QTextCharFormat cf = def_cf;
|
||||
cf.setForeground(it.value().color);
|
||||
if (it.value().bold)
|
||||
cf.setForeground(c.color);
|
||||
if (c.bold)
|
||||
cf.setFontWeight(QFont::Bold);
|
||||
tc.setCharFormat(cf);
|
||||
icon = it.value().icon_image;
|
||||
icon = c.icon_image;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -170,13 +199,14 @@ void LogView::clear() {
|
||||
void LogView::filter() {
|
||||
QTextDocument * doc = ui->textEdit->document();
|
||||
int bc = doc->blockCount();
|
||||
QString fs[2] = {ui->comboCategory->currentData().toString(), ui->lineEdit->text()};
|
||||
QRegularExpression regexp = ui->comboCategory->currentData().toRegularExpression();
|
||||
QString fs = ui->lineEdit->text();
|
||||
QTextBlock bl;
|
||||
for (int i = 0; i < bc; ++i) {
|
||||
bl = doc->findBlockByNumber(i);
|
||||
bool vis = true;
|
||||
if (!fs[0].isEmpty()) vis = vis && bl.text().contains(fs[0], Qt::CaseInsensitive);
|
||||
if (!fs[1].isEmpty()) vis = vis && bl.text().contains(fs[1], Qt::CaseInsensitive);
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user