Changeset View
Changeset View
Standalone View
Standalone View
src/InfoData.cpp
- This file was added.
1 | #include "InfoData.h" | ||||
---|---|---|---|---|---|
2 | #include <QDebug> | ||||
3 | #include <QFile> | ||||
4 | #include <QJsonDocument> | ||||
5 | #include <QJsonObject> | ||||
6 | #include <QVariantMap> | ||||
7 | | ||||
8 | InfoData::InfoData(QObject *parent) | ||||
9 | : QObject(parent) | ||||
10 | { | ||||
11 | QString infoSheetData; | ||||
12 | QFile file("://info-data.json"); | ||||
13 | | ||||
14 | if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
15 | infoSheetData = file.readAll(); | ||||
16 | file.close(); | ||||
17 | } | ||||
18 | | ||||
19 | QJsonObject jsonObject; | ||||
20 | QJsonDocument data = QJsonDocument::fromJson(infoSheetData.toUtf8()); | ||||
21 | | ||||
22 | if (!data.isEmpty()) { | ||||
23 | jsonObject = data.object(); | ||||
24 | m_jsonMap = jsonObject.toVariantMap(); | ||||
25 | } | ||||
26 | } | ||||
27 | | ||||
28 | QString InfoData::component() const | ||||
29 | { | ||||
30 | return m_component; | ||||
31 | } | ||||
32 | | ||||
33 | QString InfoData::higUrl() const | ||||
34 | { | ||||
35 | return m_higUrl; | ||||
36 | } | ||||
37 | | ||||
38 | QString InfoData::sourceUrl() const | ||||
39 | { | ||||
40 | return m_sourceUrl; | ||||
41 | } | ||||
42 | | ||||
43 | QString InfoData::title() const | ||||
44 | { | ||||
45 | return m_title; | ||||
46 | } | ||||
47 | | ||||
48 | QString InfoData::text() const | ||||
49 | { | ||||
50 | return m_text; | ||||
51 | } | ||||
52 | | ||||
53 | void InfoData::setComponent(const QString &componentName) | ||||
54 | { | ||||
55 | if (componentName != m_component) { | ||||
56 | m_component = componentName; | ||||
57 | emit componentChanged(); | ||||
58 | m_jsonMap.contains(componentName) ? setComponentData() : clearComponentData(); | ||||
59 | } | ||||
60 | } | ||||
61 | | ||||
62 | void InfoData::clearComponentData() | ||||
63 | { | ||||
64 | m_sourceUrl = QString(); | ||||
65 | m_higUrl = QString(); | ||||
66 | m_title = QString(); | ||||
67 | m_text = QString(); | ||||
68 | } | ||||
69 | | ||||
70 | void InfoData::setComponentData() | ||||
71 | { | ||||
72 | QVariantMap componentVariant = qvariant_cast<QVariantMap>(m_jsonMap[m_component]); | ||||
73 | | ||||
74 | if (!componentVariant.isEmpty()) { | ||||
75 | m_sourceUrl = componentVariant.contains("sourceUrl") ? componentVariant["sourceUrl"].toString() : QString(); | ||||
76 | sourceUrlChanged(); | ||||
77 | m_higUrl = componentVariant.contains("higUrl") ? componentVariant["higUrl"].toString() : QString(); | ||||
78 | higUrlChanged(); | ||||
79 | m_title = componentVariant.contains("title") ? componentVariant["title"].toString() : QString(); | ||||
80 | titleChanged(); | ||||
81 | m_text = componentVariant.contains("text") ? componentVariant["text"].toString() : QString(); | ||||
82 | textChanged(); | ||||
83 | } | ||||
84 | } |