Skip to content

Commit 0940ad4

Browse files
committed
finished the prototype that's not very perfect
1 parent eac9e21 commit 0940ad4

File tree

5 files changed

+104
-5
lines changed

5 files changed

+104
-5
lines changed

highlighter/highlighter.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Highlighter::Highlighter(QWidget *parent):
1717
{
1818
setupUi();
1919
mFilter = QSharedPointer<LogFilter>(new LogFilter());
20+
connect(mJsonEdit, &JsonTextEdit::updated, this, &Highlighter::onJsonObjectUpdated);
2021
}
2122

2223
void Highlighter::setupUi()
@@ -61,3 +62,82 @@ QSharedPointer<AbstractLineFilter> Highlighter::logHighlighter()
6162
{
6263
return mFilter;
6364
}
65+
66+
void Highlighter::onJsonObjectUpdated(const QJsonObject &jsonObject)
67+
{
68+
mFilter->clearRule();
69+
qDebug()<<"jsonUpdate";
70+
// traverse every rules
71+
foreach (const QString &ruleName, jsonObject.keys()) {
72+
QJsonValue value = jsonObject.value(ruleName);
73+
qDebug()<<"key="<<ruleName;
74+
if (value.isObject()) {
75+
QJsonObject propsObject = value.toObject();
76+
LogFilter::Rule filterRule(true, QColor(0, 0, 0), QColor(255, 255, 255));
77+
78+
// traverse every properties
79+
foreach (const QString &propName, propsObject.keys()) {
80+
QJsonValue propValue = propsObject.value(propName);
81+
if (propValue.isNull()) continue;
82+
83+
updateFilterRule(&filterRule, propName, propValue);
84+
}
85+
86+
// set properties to filter
87+
if (ruleName != "default") {
88+
mFilter->addRule(filterRule);
89+
} else {
90+
mFilter->setDefaultRule(filterRule);
91+
}
92+
}
93+
}
94+
95+
mFilter->update();
96+
}
97+
98+
void Highlighter::updateFilterRule(LogFilter::Rule *filterRule,
99+
const QString &propName, const QJsonValue &propValue)
100+
{
101+
if (propName == "pattern") {
102+
if (propValue.isString()) {
103+
filterRule->pattern = QRegularExpression(propValue.toString());
104+
}
105+
} else if (propName == "visible") {
106+
if (propValue.isBool()) {
107+
filterRule->visible = propValue.toBool();
108+
}
109+
} else if (propName == "foreground") {
110+
if (propValue.isArray()) {
111+
QJsonArray propArrayValue = propValue.toArray();
112+
if (propArrayValue.size() == 3 && isDoubleJsonArray(propArrayValue)) {
113+
114+
int r = propArrayValue.at(0).toInt() % 256;
115+
int g = propArrayValue.at(1).toInt() % 256;
116+
int b = propArrayValue.at(2).toInt() % 256;
117+
118+
filterRule->format.setForeground(QColor(r, g, b));
119+
}
120+
}
121+
} else if (propName == "background") {
122+
if (propValue.isArray()) {
123+
QJsonArray propArrayValue = propValue.toArray();
124+
if (propArrayValue.size() == 3 && isDoubleJsonArray(propArrayValue)) {
125+
126+
int r = propArrayValue.at(0).toInt() % 256;
127+
int g = propArrayValue.at(1).toInt() % 256;
128+
int b = propArrayValue.at(2).toInt() % 256;
129+
130+
filterRule->format.setBackground(QColor(r, g, b));
131+
}
132+
}
133+
}
134+
}
135+
136+
bool Highlighter::isDoubleJsonArray(const QJsonArray &jsonArray)
137+
{
138+
foreach (const QJsonValue jsonValue, jsonArray) {
139+
if (jsonValue.type() != QJsonValue::Type::Double)
140+
return false;
141+
}
142+
return true;
143+
}

highlighter/highlighter.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef HIGHLIGHTER_H
1+
#ifndef HIGHLIGHTER_H
22
#define HIGHLIGHTER_H
33

44
#include <QObject>
@@ -13,6 +13,13 @@
1313
#include <QLayout>
1414
#include <QVBoxLayout>
1515
#include <QFontMetrics>
16+
#include <QJsonObject>
17+
#include <QJsonArray>
18+
#include <QJsonValue>
19+
#include <QJsonDocument>
20+
#include <QString>
21+
#include <QRegularExpression>
22+
#include <QColor>
1623

1724
#include "../logviewer/abstractlinefilter.h"
1825
#include "logfilter.h"
@@ -33,8 +40,14 @@ class Highlighter : public QDockWidget
3340
signals:
3441
void logHighlighterChanged();
3542

43+
private slots:
44+
void onJsonObjectUpdated(const QJsonObject &jsonObject);
45+
3646
private:
3747
void setupUi();
48+
void updateFilterRule(LogFilter::Rule *filterRule,
49+
const QString &propName, const QJsonValue &propValue);
50+
bool isDoubleJsonArray(const QJsonArray &jsonArray);
3851

3952
private:
4053
// ui

highlighter/logfilter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
LogFilter::LogFilter():
44
mRules(0)
55
{
6-
setDefaultRule(Rule(".*", true, QColor(0 ,0 ,0), QColor(255, 0, 0)));
6+
setDefaultRule(Rule(".*", true, QColor(0 ,0 ,0), QColor(255, 255, 255)));
77
}
88

99
void LogFilter::addRule(const Rule &rule)
1010
{
11+
qDebug()<<"addRule";
1112
mRules.append(rule);
1213
}
1314

1415
void LogFilter::clearRule()
1516
{
17+
qDebug()<<"clearRule";
1618
mRules.clear();
1719
}
1820

@@ -35,5 +37,6 @@ void LogFilter::onNextLine(const QString &line)
3537

3638
void LogFilter::setDefaultRule(const Rule &rule)
3739
{
40+
qDebug()<<"setDefaultRule";
3841
mDefaultRule = rule;
3942
}

highlighter/logfilter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ class LogFilter : public AbstractLineFilter
2525
Rule() {}
2626
Rule(QString pattern, bool visible, QColor foreground, QColor background):
2727
pattern(pattern),
28-
visible(visible)
29-
{
28+
visible(visible) {
3029
format.setForeground(QBrush(foreground));
3130
format.setBackground(QBrush(background));
3231
}
3332

33+
Rule(bool visible, QColor foreground, QColor background) {
34+
Rule("", visible, foreground, background);
35+
}
36+
3437
QRegularExpression pattern;
3538
bool visible;
3639
QTextCharFormat format;

logviewer/logviewer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void LogViewer::updateContent()
137137
mFilter->onNextLine(line);
138138
if (mFilter->visible()) {
139139
textEdit->setCurrentCharFormat(mFilter->format());
140-
textEdit->insertPlainText(line+"\n");
140+
textEdit->insertPlainText(line);
141141
}
142142

143143
byteLine = buffer->readLine();

0 commit comments

Comments
 (0)