Skip to content

Commit

Permalink
修复格式化json时对于子obj和子array缺少逗号的情况,优化显示,选择更便于阅读的字体显示
Browse files Browse the repository at this point in the history
  • Loading branch information
HuaGouFdog committed May 14, 2024
1 parent d9e4071 commit eb1c19c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 34 deletions.
7 changes: 7 additions & 0 deletions source/thriftwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <cstdlib>
#include <QGraphicsDropShadowEffect>
#include <QFontDatabase>
#include "utils.h"
#pragma comment(lib, "ws2_32.lib")


Expand Down Expand Up @@ -1064,6 +1065,12 @@ void thriftwidget::sendThriftRequest(QVector<uint32_t> dataArray)
//对数据进行染色
qDebug() << "===============2";
handleMessage(dataTemp_2);
//将数据更加细致格式化
QString needToJsonData = ui->textEdit_data->toPlainText();
ui->textEdit_data->clear();
utils_parsingJsonInfo(ui->textEdit_data, needToJsonData);


ui->textEdit->append("染色数据(颜色信息可查看thrift协议报文说明):");
ui->textEdit->append(dataTemp_2);
ui->textEdit->append("------------------------------------------------------------------------------");
Expand Down
10 changes: 5 additions & 5 deletions source/thriftwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2970,7 +2970,7 @@ border-radius: 2px;
<bool>false</bool>
</property>
<attribute name="headerVisible">
<bool>true</bool>
<bool>false</bool>
</attribute>
<attribute name="headerDefaultSectionSize">
<number>100</number>
Expand Down Expand Up @@ -3447,7 +3447,7 @@ QTabBar QToolButton:hover {
<property name="font">
<font>
<family>Cascadia Code SemiBold</family>
<pointsize>10</pointsize>
<pointsize>11</pointsize>
<stylestrategy>PreferAntialias</stylestrategy>
</font>
</property>
Expand All @@ -3465,8 +3465,8 @@ border-radius: 2px;</string>
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Cascadia Code SemiBold'; font-size:10pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'OPPOSans B';&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Cascadia Code SemiBold'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -3643,7 +3643,7 @@ p, li { white-space: pre-wrap; }
<property name="font">
<font>
<family>Cascadia Code SemiBold</family>
<pointsize>10</pointsize>
<pointsize>11</pointsize>
</font>
</property>
<property name="styleSheet">
Expand Down
19 changes: 9 additions & 10 deletions source/toolswidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,8 @@ QScrollBar::up-arrow:vertical {
<widget class="QPlainTextEdit" name="plainTextEdit_json_source">
<property name="font">
<font>
<family>OPPOSans B</family>
<pointsize>10</pointsize>
<family>Cascadia Code SemiBold</family>
<pointsize>11</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
Expand All @@ -1364,9 +1364,8 @@ QScrollBar::up-arrow:vertical {
<property name="styleSheet">
<string notr="true">QPlainTextEdit{
color: rgb(38, 38, 38);
font: 10pt &quot;OPPOSans B&quot;;
border-radius: 14px;
background-color: rgba(255, 255, 255,200);
background-color: rgb(255, 255, 255);
border: 0px solid rgba(255, 255, 255, 0);
border-radius: 5px;
padding:5px;
Expand All @@ -1379,8 +1378,8 @@ padding:5px;
<widget class="QTextEdit" name="textEdit_json_target">
<property name="font">
<font>
<family>OPPOSans B</family>
<pointsize>10</pointsize>
<family>Cascadia Code SemiBold</family>
<pointsize>11</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
Expand All @@ -1389,9 +1388,9 @@ padding:5px;
<property name="styleSheet">
<string notr="true">QTextEdit{
color: rgb(56, 56, 56);
font: 10pt &quot;OPPOSans B&quot;;

border-radius: 14px;
background-color: rgba(255, 255, 255,200);
background-color: rgb(255, 255, 255);
border: 0px solid rgba(255, 255, 255, 0);
border-radius: 5px;
padding:5px;
Expand All @@ -1401,8 +1400,8 @@ padding:5px;
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'OPPOSans B'; font-size:10pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:15pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Cascadia Code SemiBold'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'OPPOSans B'; font-size:15pt;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</widget>
Expand Down
10 changes: 5 additions & 5 deletions source/ui_thriftwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ class Ui_thriftwidget
treeWidget->setUniformRowHeights(true);
treeWidget->setSortingEnabled(false);
treeWidget->setAnimated(false);
treeWidget->header()->setVisible(true);
treeWidget->header()->setVisible(false);
treeWidget->header()->setDefaultSectionSize(100);
treeWidget->header()->setMinimumSectionSize(20);
treeWidget->header()->setStretchLastSection(true);
Expand Down Expand Up @@ -1991,7 +1991,7 @@ class Ui_thriftwidget
textEdit_data->setObjectName(QStringLiteral("textEdit_data"));
QFont font7;
font7.setFamily(QStringLiteral("Cascadia Code SemiBold"));
font7.setPointSize(10);
font7.setPointSize(11);
font7.setStyleStrategy(QFont::PreferAntialias);
textEdit_data->setFont(font7);
textEdit_data->setStyleSheet(QLatin1String("border: 1px solid rgba(199, 199, 199, 150);\n"
Expand Down Expand Up @@ -2063,7 +2063,7 @@ class Ui_thriftwidget
plainTextEdit->setObjectName(QStringLiteral("plainTextEdit"));
QFont font10;
font10.setFamily(QStringLiteral("Cascadia Code SemiBold"));
font10.setPointSize(10);
font10.setPointSize(11);
plainTextEdit->setFont(font10);
plainTextEdit->setStyleSheet(QLatin1String("border: 1px solid rgba(199, 199, 199, 150);\n"
"border-radius: 2px;"));
Expand Down Expand Up @@ -2380,8 +2380,8 @@ class Ui_thriftwidget
textEdit_data->setHtml(QApplication::translate("thriftwidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:'Cascadia Code SemiBold'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'OPPOSans B';\"><br /></p></body></html>", nullptr));
"</style></head><body style=\" font-family:'Cascadia Code SemiBold'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>", nullptr));
tabWidget_response->setTabText(tabWidget_response->indexOf(tab_3), QApplication::translate("thriftwidget", " Data ", nullptr));
textEdit_info->setHtml(QApplication::translate("thriftwidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
Expand Down
15 changes: 7 additions & 8 deletions source/ui_toolswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,17 +707,16 @@ class Ui_toolswidget
plainTextEdit_json_source = new QPlainTextEdit(splitter);
plainTextEdit_json_source->setObjectName(QStringLiteral("plainTextEdit_json_source"));
QFont font3;
font3.setFamily(QStringLiteral("OPPOSans B"));
font3.setPointSize(10);
font3.setFamily(QStringLiteral("Cascadia Code SemiBold"));
font3.setPointSize(11);
font3.setBold(false);
font3.setItalic(false);
font3.setWeight(50);
plainTextEdit_json_source->setFont(font3);
plainTextEdit_json_source->setStyleSheet(QLatin1String("QPlainTextEdit{\n"
"color: rgb(38, 38, 38);\n"
"font: 10pt \"OPPOSans B\";\n"
"border-radius: 14px;\n"
"background-color: rgba(255, 255, 255,200);\n"
" background-color: rgb(255, 255, 255);\n"
"border: 0px solid rgba(255, 255, 255, 0);\n"
"border-radius: 5px;\n"
"padding:5px;\n"
Expand All @@ -728,9 +727,9 @@ class Ui_toolswidget
textEdit_json_target->setFont(font3);
textEdit_json_target->setStyleSheet(QLatin1String("QTextEdit{\n"
"color: rgb(56, 56, 56);\n"
"font: 10pt \"OPPOSans B\";\n"
"\n"
"border-radius: 14px;\n"
"background-color: rgba(255, 255, 255,200);\n"
" background-color: rgb(255, 255, 255);\n"
"border: 0px solid rgba(255, 255, 255, 0);\n"
"border-radius: 5px;\n"
"padding:5px;\n"
Expand Down Expand Up @@ -828,8 +827,8 @@ class Ui_toolswidget
textEdit_json_target->setHtml(QApplication::translate("toolswidget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:'OPPOSans B'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:15pt;\"><br /></p></body></html>", nullptr));
"</style></head><body style=\" font-family:'Cascadia Code SemiBold'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'OPPOSans B'; font-size:15pt;\"><br /></p></body></html>", nullptr));
} // retranslateUi

};
Expand Down
29 changes: 23 additions & 6 deletions source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ void utils_parsingJsonObject(QTextEdit * textEdit, QJsonObject &object, const in
int objectLent = object.length();
for (QJsonObject::const_iterator it = object.constBegin(); it != object.constEnd(); ++it)
{
qDebug() << "it.key() = " << it.key() << " objectLent = " << objectLent << " index = " << index;
if(index < objectLent)
endingSymbol = QStringLiteral(",");
else
Expand Down Expand Up @@ -487,13 +488,13 @@ void utils_parsingJsonObject(QTextEdit * textEdit, QJsonObject &object, const in
{
QString obj = QString("%1").arg(value.toDouble(), 0, 'f', 9);
//QString obj = value.toString();
qDebug() << "obj = " << obj;
//qDebug() << "obj = " << obj;
//判断倒数9位有没有非零,如果都是零,显示整数
if (!containsNonZeroDigit(obj.mid(obj.length() - 10))) {
//全是0
obj = obj.mid(0,obj.length()- 10);
}
qDebug() << "obj2 = " << obj;
//qDebug() << "obj2 = " << obj;
textEdit->append(QString("<span style='white-space:pre;color:#9C278F;'>%1\"%2\"</span>:<span style='color:#128BF1;'>%3</span>%4").arg(spacing).arg(it.key()).arg(obj).arg(endingSymbol));
}
else if(value.isBool())
Expand All @@ -512,12 +513,19 @@ void utils_parsingJsonObject(QTextEdit * textEdit, QJsonObject &object, const in
textEdit->append(QString("<span style='white-space:pre;color:#9C278F;'>%1\"%2\"</span>:").arg(spacing).arg(it.key()));
QJsonObject obj = value.toObject();
utils_parsingJsonObject(textEdit,obj,nextLayers);
}
else if(value.isArray())
if(index < objectLent) {
endingSymbol = QStringLiteral(",");
textEdit->insertPlainText(endingSymbol);
}
} else if(value.isArray())
{
textEdit->append(QString("<span style='white-space:pre;color:#9C278F;'>%1\"%2\"</span>:").arg(spacing).arg(it.key()));
QJsonArray arr = value.toArray();
utils_parsingJsonArray(textEdit,arr,nextLayers);
if(index < objectLent) {
endingSymbol = QStringLiteral(",");
textEdit->insertPlainText(endingSymbol);
}
}
else
{
Expand Down Expand Up @@ -594,29 +602,38 @@ void utils_parsingJsonArray(QTextEdit * textEdit, QJsonArray &array,const int nu
{
QString obj = QString("%1").arg(ref.toDouble(), 0, 'f', 9);
//QString obj = ref.toString();
qDebug() << "obj = " << obj;
//qDebug() << "obj = " << obj;
//判断倒数9位有没有非零,如果都是零,显示整数
if (!containsNonZeroDigit(obj.mid(obj.length()-10))) {
//全是0
obj = obj.mid(0, obj.length()- 10);
}
qDebug() << "obj2 = " << obj;
//qDebug() << "obj2 = " << obj;
textEdit->append(QString("<span style='white-space:pre;color:#128BF1;'>%1%2</span>%3").arg(spacing).arg(obj).arg(endingSymbol));
}
else if(jsonValueType == QJsonValue::Array)
{
QJsonArray arr = ref.toArray();
utils_parsingJsonArray(textEdit,arr,nextLayers);
if(index < arrayLent) {
endingSymbol = QStringLiteral(",");
textEdit->insertPlainText(endingSymbol);
}
}
else if(jsonValueType == QJsonValue::Object)
{
QJsonObject obj = ref.toObject();
utils_parsingJsonObject(textEdit,obj,nextLayers);
if(index < arrayLent) {
endingSymbol = QStringLiteral(",");
textEdit->insertPlainText(endingSymbol);
}
}
else
{
textEdit->append(QString("<span style='white-space:pre;color:#128BF1;'>%1%2</span>%3").arg(spacing).arg(ref.toInt()).arg(endingSymbol));
}
++index;
}
spacingBeforeBrackets.replace("[","]");
textEdit->append(spacingBeforeBrackets);
Expand Down

0 comments on commit eb1c19c

Please sign in to comment.