ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvDefaultPluginInterface.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <CVLog.h>
11 
12 #include <QDebug>
13 #include <QFile>
14 #include <QJsonArray>
15 #include <QJsonDocument>
16 #include <QJsonObject>
17 
18 // This class keeps JSON from being included in the header
20 public:
21  inline QString field(const QString &fieldName) {
22  return doc.object().value(fieldName).toString();
23  }
24 
25  ccPluginInterface::ReferenceList references(const QString &fieldName) {
27 
28  const QJsonArray array = doc.object().value(fieldName).toArray();
29 
30  for (const QJsonValue &value : array) {
31  const QJsonObject object = value.toObject();
32 
33  list += ccPluginInterface::Reference{object["text"].toString(),
34  object["url"].toString()};
35  }
36 
37  return list;
38  }
39 
40  ccPluginInterface::ContactList contacts(const QString &fieldName) {
42 
43  const QJsonArray array = doc.object().value(fieldName).toArray();
44 
45  for (const QJsonValue &value : array) {
46  const QJsonObject object = value.toObject();
47 
48  list += ccPluginInterface::Contact{object["name"].toString(),
49  object["email"].toString()};
50  }
51 
52  return list;
53  }
54 
55  QString m_IID;
56  QJsonDocument doc;
57 };
58 
60  : m_data(new ccDefaultPluginData) {
61  if (resourcePath.isNull()) {
62  return;
63  }
64 
65  QFile myFile(resourcePath);
66 
67  bool opened = myFile.open(QIODevice::ReadOnly);
68 
69  if (!opened) {
70  CVLog::Error(QStringLiteral("Could not load plugin resources: %1")
71  .arg(resourcePath));
72  return;
73  }
74 
75  QByteArray json = myFile.readAll();
76 
77  QJsonParseError jsonError;
78 
79  m_data->doc = QJsonDocument::fromJson(json, &jsonError);
80 
81  if (jsonError.error != QJsonParseError::NoError) {
82  CVLog::Error(QStringLiteral("Could not parse plugin info: %1")
83  .arg(jsonError.errorString()));
84  return;
85  }
86 }
87 
89 
90 const QString &ccDefaultPluginInterface::IID() const { return m_data->m_IID; }
91 
92 void ccDefaultPluginInterface::setIID(const QString &iid) {
93  m_data->m_IID = iid;
94 }
95 
97  return m_data->doc.object().value("core").toBool();
98 }
99 
101  return m_data->field("name");
102 }
103 
105  return m_data->field("description");
106 }
107 
109  return QIcon(m_data->field("icon"));
110 }
111 
113  const {
114  return m_data->references("references");
115 }
116 
118  return m_data->contacts("authors");
119 }
120 
122  const {
123  return m_data->contacts("maintainers");
124 }
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
ccPluginInterface::ContactList contacts(const QString &fieldName)
QString field(const QString &fieldName)
ccPluginInterface::ReferenceList references(const QString &fieldName)
ccDefaultPluginInterface(const QString &resourcePath=QString())
virtual ContactList getAuthors() const override
virtual ReferenceList getReferences() const override
virtual QString getName() const override
Returns (short) name (for menu entry, etc.)
virtual QString getDescription() const override
Returns long name/description (for tooltip, etc.)
virtual ContactList getMaintainers() const override
virtual QIcon getIcon() const override
Returns icon.
virtual bool isCore() const override
Is this plugin a core plugin?
QList< Reference > ReferenceList
QList< Contact > ContactList