From a179a8266ea513f43cdf2d2e810d7630074d00ca Mon Sep 17 00:00:00 2001 From: endao Date: Wed, 1 Feb 2023 18:04:27 +0800 Subject: [PATCH] Add fontFamily options --- include/QtNodes/internal/NodeStyle.hpp | 46 +++++++++++++------------- resources/DefaultStyle.json | 3 +- src/DefaultNodePainter.cpp | 6 +++- src/NodeStyle.cpp | 19 ++++++++++- 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/include/QtNodes/internal/NodeStyle.hpp b/include/QtNodes/internal/NodeStyle.hpp index 5eca74924..7fed500de 100644 --- a/include/QtNodes/internal/NodeStyle.hpp +++ b/include/QtNodes/internal/NodeStyle.hpp @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include @@ -27,27 +27,27 @@ class NODE_EDITOR_PUBLIC NodeStyle : public Style QJsonObject toJson() const override; public: - QColor NormalBoundaryColor; - QColor SelectedBoundaryColor; - QColor GradientColor0; - QColor GradientColor1; - QColor GradientColor2; - QColor GradientColor3; - QColor ShadowColor; - QColor FontColor; - QColor FontColorFaded; - - QColor ConnectionPointColor; - QColor FilledConnectionPointColor; - - QColor WarningColor; - QColor ErrorColor; - - float PenWidth; - float HoveredPenWidth; - - float ConnectionPointDiameter; - - float Opacity; + QColor NormalBoundaryColor; + QColor SelectedBoundaryColor; + QColor GradientColor0; + QColor GradientColor1; + QColor GradientColor2; + QColor GradientColor3; + QColor ShadowColor; + QColor FontColor; + QColor FontColorFaded; + QString FontFamily; + QColor ConnectionPointColor; + QColor FilledConnectionPointColor; + + QColor WarningColor; + QColor ErrorColor; + + float PenWidth; + float HoveredPenWidth; + + float ConnectionPointDiameter; + + float Opacity; }; } // namespace QtNodes diff --git a/resources/DefaultStyle.json b/resources/DefaultStyle.json index da8dfe84c..9af45ba2f 100644 --- a/resources/DefaultStyle.json +++ b/resources/DefaultStyle.json @@ -1,4 +1,4 @@ -{ +{ "GraphicsViewStyle": { "BackgroundColor": [53, 53, 53], "FineGridColor": [60, 60, 60], @@ -14,6 +14,7 @@ "ShadowColor": [20, 20, 20], "FontColor" : "white", "FontColorFaded" : "gray", + "FontFamily": "Microsoft YaHei", "ConnectionPointColor": [169, 169, 169], "FilledConnectionPointColor": "cyan", "ErrorColor": "red", diff --git a/src/DefaultNodePainter.cpp b/src/DefaultNodePainter.cpp index 8febe4cb1..0ff5102c5 100644 --- a/src/DefaultNodePainter.cpp +++ b/src/DefaultNodePainter.cpp @@ -1,4 +1,4 @@ -#include "DefaultNodePainter.hpp" +#include "DefaultNodePainter.hpp" #include @@ -209,6 +209,7 @@ void DefaultNodePainter::drawNodeCaption(QPainter *painter, NodeGraphicsObject & QJsonDocument json = QJsonDocument::fromVariant(model.nodeData(nodeId, NodeRole::Style)); NodeStyle nodeStyle(json.object()); + f.setFamily(nodeStyle.FontFamily); painter->setFont(f); painter->setPen(nodeStyle.FontColor); painter->drawText(position, name); @@ -252,6 +253,9 @@ void DefaultNodePainter::drawEntryLabels(QPainter *painter, NodeGraphicsObject & s = portData.value().name; } + auto f = painter->font(); + f.setFamily(nodeStyle.FontFamily); + painter->setFont(f); painter->drawText(p, s); } } diff --git a/src/NodeStyle.cpp b/src/NodeStyle.cpp index a82bf8fe2..2f8782dc4 100644 --- a/src/NodeStyle.cpp +++ b/src/NodeStyle.cpp @@ -1,4 +1,4 @@ -#include "NodeStyle.hpp" +#include "NodeStyle.hpp" #include @@ -88,6 +88,19 @@ void NodeStyle::setNodeStyle(QString jsonText) values[#variable] = variable; \ } +#define NODE_STYLE_READ_STRING(values, variable)\ +{ \ + auto valueRef = values[#variable]; \ + NODE_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \ + variable = valueRef.toString(); \ +} + +#define NODE_STYLE_WRITE_STRING(values, variable) \ + { \ + values[#variable] = variable; \ + } + + void NodeStyle::loadJson(QJsonObject const &json) { QJsonValue nodeStyleValues = json["NodeStyle"]; @@ -113,6 +126,8 @@ void NodeStyle::loadJson(QJsonObject const &json) NODE_STYLE_READ_FLOAT(obj, ConnectionPointDiameter); NODE_STYLE_READ_FLOAT(obj, Opacity); + + NODE_STYLE_READ_STRING(obj, FontFamily); } QJsonObject NodeStyle::toJson() const @@ -139,6 +154,8 @@ QJsonObject NodeStyle::toJson() const NODE_STYLE_WRITE_FLOAT(obj, Opacity); + NODE_STYLE_WRITE_STRING(obj, FontFamily); + QJsonObject root; root["NodeStyle"] = obj;