Use LogView to implement level highlighting for other logs

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li
2025-04-15 03:42:28 +08:00
parent 41a790d258
commit 4ac6a0629b
6 changed files with 158 additions and 95 deletions

View File

@ -52,90 +52,81 @@
#include <BuildConfig.h>
class LogFormatProxyModel : public QIdentityProxyModel {
public:
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
QVariant data(const QModelIndex& index, int role) const override
{
const LogColors& colors = APPLICATION->themeManager()->getLogColors();
QVariant LogFormatProxyModel::data(const QModelIndex& index, int role) const
{
const LogColors& colors = APPLICATION->themeManager()->getLogColors();
switch (role) {
case Qt::FontRole:
return m_font;
case Qt::ForegroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.foreground.value(level);
switch (role) {
case Qt::FontRole:
return m_font;
case Qt::ForegroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.foreground.value(level);
if (result.isValid())
return result;
if (result.isValid())
return result;
break;
}
case Qt::BackgroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.background.value(level);
if (result.isValid())
return result;
break;
}
break;
}
case Qt::BackgroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.background.value(level);
return QIdentityProxyModel::data(index, role);
if (result.isValid())
return result;
break;
}
}
void setFont(QFont font) { m_font = font; }
return QIdentityProxyModel::data(index, role);
}
QModelIndex find(const QModelIndex& start, const QString& value, bool reverse) const
{
QModelIndex parentIndex = parent(start);
auto compare = [this, start, parentIndex, value](int r) -> QModelIndex {
QModelIndex idx = index(r, start.column(), parentIndex);
if (!idx.isValid() || idx == start) {
return QModelIndex();
}
QVariant v = data(idx, Qt::DisplayRole);
QString t = v.toString();
if (t.contains(value, Qt::CaseInsensitive))
return idx;
QModelIndex LogFormatProxyModel::find(const QModelIndex& start, const QString& value, bool reverse) const
{
QModelIndex parentIndex = parent(start);
auto compare = [this, start, parentIndex, value](int r) -> QModelIndex {
QModelIndex idx = index(r, start.column(), parentIndex);
if (!idx.isValid() || idx == start) {
return QModelIndex();
};
if (reverse) {
int from = start.row();
int to = 0;
for (int i = 0; i < 2; ++i) {
for (int r = from; (r >= to); --r) {
auto idx = compare(r);
if (idx.isValid())
return idx;
}
// prepare for the next iteration
from = rowCount() - 1;
to = start.row();
}
} else {
int from = start.row();
int to = rowCount(parentIndex);
for (int i = 0; i < 2; ++i) {
for (int r = from; (r < to); ++r) {
auto idx = compare(r);
if (idx.isValid())
return idx;
}
// prepare for the next iteration
from = 0;
to = start.row();
}
}
QVariant v = data(idx, Qt::DisplayRole);
QString t = v.toString();
if (t.contains(value, Qt::CaseInsensitive))
return idx;
return QModelIndex();
}
};
if (reverse) {
int from = start.row();
int to = 0;
private:
QFont m_font;
};
for (int i = 0; i < 2; ++i) {
for (int r = from; (r >= to); --r) {
auto idx = compare(r);
if (idx.isValid())
return idx;
}
// prepare for the next iteration
from = rowCount() - 1;
to = start.row();
}
} else {
int from = start.row();
int to = rowCount(parentIndex);
for (int i = 0; i < 2; ++i) {
for (int r = from; (r < to); ++r) {
auto idx = compare(r);
if (idx.isValid())
return idx;
}
// prepare for the next iteration
from = 0;
to = start.row();
}
}
return QModelIndex();
}
LogPage::LogPage(InstancePtr instance, QWidget* parent) : QWidget(parent), ui(new Ui::LogPage), m_instance(instance)
{