initial commit
This commit is contained in:
@@ -3,11 +3,10 @@
|
||||
|
||||
qt_add_plugin(googlefonts
|
||||
CLASS_NAME GoogleFonts
|
||||
cgooglefonts.cpp cgooglefonts.h
|
||||
cgooglefonts.cpp cgooglefonts.h ../../app/cfont.cpp ../../app/cfont.h
|
||||
)
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets
|
||||
OPTIONAL_COMPONENTS PrintSupport)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Network)
|
||||
|
||||
set_target_properties(googlefonts PROPERTIES
|
||||
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
|
||||
@@ -21,6 +20,7 @@ target_link_libraries(googlefonts PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
Qt6::Network
|
||||
)
|
||||
|
||||
if(TARGET Qt6::PrintSupport)
|
||||
|
||||
@@ -4,21 +4,174 @@
|
||||
#include "cgooglefonts.h"
|
||||
#include <QString>
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QEventLoop>
|
||||
|
||||
cGoogleFonts::cGoogleFonts()
|
||||
{
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
|
||||
|
||||
QStringList arrayToString(const QJsonArray& array) {
|
||||
QStringList list;
|
||||
|
||||
foreach (const QJsonValue& item, array) {
|
||||
list.append(item.toString());
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
cGoogleFonts::~cGoogleFonts()
|
||||
{
|
||||
QMap<QString, QString> objectToMap(const QJsonObject& object) {
|
||||
QMap<QString, QString> list;
|
||||
|
||||
for (const QString &key : object.keys()) {
|
||||
QJsonValue value = object.value(key);
|
||||
list.insert(key, value.toString());
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
QString cGoogleFonts::name()
|
||||
{
|
||||
cGoogleFonts::cGoogleFonts() {
|
||||
m_key = tr("AIzaSyCqPgQFvG-k4lbPF7L0oqJLKJxoC1O6R90");
|
||||
}
|
||||
|
||||
cGoogleFonts::~cGoogleFonts() {
|
||||
}
|
||||
|
||||
QString cGoogleFonts::pluginAPIVersion() {
|
||||
return tr("1.0");
|
||||
}
|
||||
|
||||
QString cGoogleFonts::name() {
|
||||
return tr("Google Fonts");
|
||||
}
|
||||
|
||||
QString cGoogleFonts::version()
|
||||
{
|
||||
QString cGoogleFonts::version() {
|
||||
return tr("0.1");
|
||||
}
|
||||
|
||||
QList<cFont> cGoogleFonts::search(const QString& family, const QString& subset, const Sort& sort) {
|
||||
QNetworkAccessManager networkManager;
|
||||
QString urlString = "https://www.googleapis.com/webfonts/v1/webfonts";
|
||||
QUrlQuery query;
|
||||
|
||||
query.addQueryItem("key", m_key);
|
||||
|
||||
if(!family.isEmpty())
|
||||
query.addQueryItem("family", family);
|
||||
|
||||
if(!subset.isEmpty())
|
||||
query.addQueryItem("subset", subset);
|
||||
|
||||
switch(sort)
|
||||
{
|
||||
case Sort::alpha:
|
||||
query.addQueryItem("sort", "alpha");
|
||||
break;
|
||||
case Sort::date:
|
||||
query.addQueryItem("sort", "date");
|
||||
break;
|
||||
case Sort::popularity:
|
||||
query.addQueryItem("sort", "popularity");
|
||||
break;
|
||||
case Sort::style:
|
||||
query.addQueryItem("sort", "style");
|
||||
break;
|
||||
default:
|
||||
query.addQueryItem("sort", "alpha");
|
||||
break;
|
||||
}
|
||||
|
||||
QUrl url(urlString);
|
||||
url.setQuery(query);
|
||||
|
||||
QNetworkRequest request(url);
|
||||
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
|
||||
QNetworkReply* reply = networkManager.get(request);
|
||||
QEventLoop loop;
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||
loop.exec();
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
delete reply;
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(data);
|
||||
if(doc.isNull()) {
|
||||
qDebug() << "Error: JSON return is empty";
|
||||
return QList<cFont>();
|
||||
}
|
||||
|
||||
if(!doc.isObject()) {
|
||||
qDebug() << "Error: invalid JSON";
|
||||
return QList<cFont>();
|
||||
}
|
||||
|
||||
QJsonObject root = doc.object();
|
||||
|
||||
if(!root.contains("items")) {
|
||||
qDebug() << "Error: no fonts found";
|
||||
return QList<cFont>();
|
||||
}
|
||||
|
||||
QJsonValue itemsV = root.value("items");
|
||||
if(!itemsV.isArray()) {
|
||||
qDebug() << "Error: no fonts found";
|
||||
return QList<cFont>();
|
||||
}
|
||||
|
||||
QJsonArray items = itemsV.toArray();
|
||||
|
||||
if(!items.count())
|
||||
return QList<cFont>();
|
||||
|
||||
QList<cFont> fontList;
|
||||
|
||||
foreach (const QJsonValue& item, items) {
|
||||
if (item.isObject()) {
|
||||
cFont font;
|
||||
QJsonObject obj = item.toObject();
|
||||
|
||||
if(obj.contains("family"))
|
||||
font.setFamily(obj.value("family").toString());
|
||||
else
|
||||
continue;
|
||||
|
||||
if(obj.contains("variants"))
|
||||
font.setVariants(arrayToString(obj.value("variants").toArray()));
|
||||
|
||||
if(obj.contains("subsets"))
|
||||
font.setSubsets(arrayToString(obj.value("subsets").toArray()));
|
||||
|
||||
if(obj.contains("version"))
|
||||
font.setVersion(obj.value("version").toString());
|
||||
|
||||
if(obj.contains("lastModified"))
|
||||
font.setLastModified(QDate::fromString(obj.value("lastModified").toString()));
|
||||
|
||||
if(obj.contains("files")) {
|
||||
QJsonObject filesObj = obj.value("files").toObject();
|
||||
|
||||
font.setFiles(objectToMap(filesObj));
|
||||
}
|
||||
|
||||
if(obj.contains("category"))
|
||||
font.setCategory(obj.value("category").toString());
|
||||
|
||||
if(obj.contains("kind"))
|
||||
font.setKind(obj.value("kind").toString());
|
||||
|
||||
fontList.append(font);
|
||||
}
|
||||
}
|
||||
|
||||
return fontList;
|
||||
}
|
||||
|
||||
@@ -17,8 +17,13 @@ public:
|
||||
cGoogleFonts();
|
||||
~cGoogleFonts() override;
|
||||
|
||||
QString name() override;
|
||||
QString version() override;
|
||||
QString pluginAPIVersion() override;
|
||||
QString name() override;
|
||||
QString version() override;
|
||||
|
||||
QList<cFont> search(const QString& family, const QString& subset = QString(), const Sort& sort = Sort::popularity) override;
|
||||
private:
|
||||
QString m_key;
|
||||
};
|
||||
|
||||
#endif //CGOOGLEFONTS_H
|
||||
|
||||
Reference in New Issue
Block a user