-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfluxhub.h
49 lines (42 loc) · 1.48 KB
/
fluxhub.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef FLUXHUB_H
#define FLUXHUB_H
#include <QObject>
#include <QQuickItem>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QQmlEngine>
#include <QQmlProperty>
#include <QList>
#include <QColor>
class FluxHub : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString demoJson READ demoJson CONSTANT)
explicit FluxHub(QQmlEngine* engine, QObject *parent = nullptr);
QString demoJson() const;
Q_INVOKABLE QQuickItem *visualize(QQuickItem *rootItem, const QVariantMap &qmlJsonObj);
signals:
public slots:
void destroyNode(QQuickItem *rootNode){
delete rootNode;
}
private:
QQuickItem *createObjectNode(QQuickItem *currentItem, const QString &key, const QJsonObject &jsonObj, int level);
QQuickItem * createLeafNode(QQuickItem *currentItem, const QString &key, const QJsonValue &jsonVal, int level);
QQuickItem * createArrayNode(QQuickItem *currentItem, const QString &key, const QJsonArray &jsonArray, int level);
inline QQuickItem *createLevelItem(int level, QQuickItem *parentItem);
inline void setNodeText(QQuickItem *item, const QString &text){
QQmlProperty nodeTextProp(item, "text");
nodeTextProp.write(text);
}
inline QString levelColor(int level){
return level >= m_levelColors.size()? m_levelColors.back() : m_levelColors[level];
}
private:
QQmlEngine *m_engine = nullptr;
QList<QQmlComponent*> m_levelComponents;
QList<QString> m_levelColors;
};
#endif // FLUXHUB_H