diff options
author | Mahmoud Badri <mahmoud.badri@qt.io> | 2023-06-09 22:11:20 +0300 |
---|---|---|
committer | Mahmoud Badri <mahmoud.badri@qt.io> | 2023-06-19 15:15:11 +0300 |
commit | c77719ff5d44947acc85daf794bcbd3f9f8c0702 (patch) | |
tree | 968cb3706aa37d2ffe130cb133c977ddc7a6b38c /examples/demos/FX_Material_Showroom/src | |
parent | 91e3396421eff3622141f01b2b905eeab8583138 (diff) |
Add FX & Material showroom demo
Fixes: QTBUG-113477
Pick-to: 6.5.2
Change-Id: I2cdd68027224516b6e6ddacad99d06b6a4dc1ba0
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Diffstat (limited to 'examples/demos/FX_Material_Showroom/src')
5 files changed, 312 insertions, 0 deletions
diff --git a/examples/demos/FX_Material_Showroom/src/app_environment.h b/examples/demos/FX_Material_Showroom/src/app_environment.h new file mode 100644 index 000000000..cc243900a --- /dev/null +++ b/examples/demos/FX_Material_Showroom/src/app_environment.h @@ -0,0 +1,11 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QGuiApplication> + +void set_qt_environment() +{ + qputenv("QT_QUICK_CONTROLS_CONF", ":/qtquickcontrols2.conf"); + qputenv("QT_ENABLE_HIGHDPI_SCALING", "0"); + qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1"); +} diff --git a/examples/demos/FX_Material_Showroom/src/imagedownloader.cpp b/examples/demos/FX_Material_Showroom/src/imagedownloader.cpp new file mode 100644 index 000000000..a83012d86 --- /dev/null +++ b/examples/demos/FX_Material_Showroom/src/imagedownloader.cpp @@ -0,0 +1,98 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "imagedownloader.h" + +#include <QCoreApplication> +#include <QDir> +#include <QFile> +#include <QNetworkReply> +#include <QNetworkRequest> + +ImageDownloader::ImageDownloader(QObject *parent) + : QObject(parent) + , m_baseLocal(QCoreApplication::applicationDirPath() + "/content/") +{} + +void ImageDownloader::downloadImages() +{ + m_downloadedCount = 0; + + m_downloadPaths = getDownloadList(); + if (m_downloadPaths.isEmpty()) + emit finished(); + else + downloadNextAsset(); +} + +void ImageDownloader::downloadNextAsset() +{ + if (m_downloadedCount < m_downloadPaths.size()) { + emit downloadStart(m_downloadedCount + 1); + + const QString &assetUrl = m_downloadPaths[m_downloadedCount]; + QUrl url(BASE_REMOTE + assetUrl); + QNetworkRequest request(url); + + QNetworkReply *reply = m_manager.get(request); + + connect(reply, &QNetworkReply::finished, this, &ImageDownloader::onDownloadFinished); + connect(reply, &QNetworkReply::downloadProgress, + this, &ImageDownloader::onDownloadProgress); + } +} + +QStringList ImageDownloader::getDownloadList() const +{ + QStringList downloadList; + std::copy_if(assetsList.begin(), assetsList.end(), std::back_inserter(downloadList), + [&](const QString &imgPath) { + QString localPath = m_baseLocal + imgPath; + return !QFile(localPath).exists(); + }); + + return downloadList; +} + +int ImageDownloader::downloadCount() const +{ + return m_downloadPaths.size(); +} + +void ImageDownloader::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + emit downloadProgress(double(bytesReceived)/double(bytesTotal)); +} + +void ImageDownloader::onDownloadFinished() +{ + QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender()); + reply->deleteLater(); + + if (reply->error() == QNetworkReply::NoError) { + QByteArray imageData = reply->readAll(); + + QFileInfo fi(m_baseLocal + m_downloadPaths[m_downloadedCount]); + QDir dir (fi.path()); + if (!dir.exists()) { + if (!dir.mkpath(".")) + qWarning() << "Failed to create save path:" << fi.path(); + } + QFile file(fi.filePath()); + if (file.open(QIODevice::WriteOnly)) { + file.write(imageData); + file.close(); + } else { + qWarning() << "Failed to save image:" << file.errorString(); + } + } else { + qWarning() << "Download failed:" << reply->errorString(); + } + + ++m_downloadedCount; + + if (m_downloadedCount == m_downloadPaths.size()) + emit finished(); + else + downloadNextAsset(); +} diff --git a/examples/demos/FX_Material_Showroom/src/imagedownloader.h b/examples/demos/FX_Material_Showroom/src/imagedownloader.h new file mode 100644 index 000000000..ae3d8d541 --- /dev/null +++ b/examples/demos/FX_Material_Showroom/src/imagedownloader.h @@ -0,0 +1,141 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QNetworkAccessManager> +#include <QString> + +class ImageDownloader : public QObject +{ + Q_OBJECT + +public: + ImageDownloader(QObject *parent = nullptr); + + int downloadCount() const; + +public slots: + void downloadImages(); + +signals: + void finished(); + void downloadStart(int num); + void downloadProgress(double progress); + +private slots: + void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); + void onDownloadFinished(); + +private: + void downloadNextAsset(); + QStringList getDownloadList() const; + + QStringList m_downloadPaths; + QString m_savePath; + int m_downloadedCount = 0; + QNetworkAccessManager m_manager; + QString m_baseLocal; + + const static inline QString BASE_REMOTE = "https://download.qt.io/learning/examples/FX_Material_Showroom_Assets/"; + + const static inline QStringList assetsList { + "images/_Desert.hdr", + "images/ambientdot.tif", + "images/AOBake.png", + "images/Asphalt010_2K_NormalGL.png", + "images/Asphalt010_2K_Roughness.png", + "images/BubblesIcon.png", + "images/CheckEmptyVector.png", + "images/CheckFilledVector.png", + "images/cloud.png", + "images/CloudsIcon.png", + "images/debris.png", + "images/dotsprite.tif", + "images/dreamstime_xl_241509362.hdr", + "images/dreamstime_xxl_216322508.hdr", + "images/dust.png", + "images/DustIcon.png", + "images/Environment_2K_NIGHT.png", + "images/ExplosionIcon.png", + "images/Fabric004_2K_NormalGL.png", + "images/Fabric031_2K_Color.png", + "images/Fabric031_2K_Detail.png", + "images/Fabric031_2K_Displacement.png", + "images/Fabric031_2K_NormalGL.png", + "images/Fabric031_2K_Roughness.png", + "images/FabricmatIcon.png", + "images/fire.png", + "images/FlameIcon.png", + "images/FlashIcon.png", + "images/HeatwaveIcon.png", + "images/LeathermatIcon.png", + "images/LighttrailIcon.png", + "images/lineSprite.tif", + "images/Metal009_2K_NormalGL.png", + "images/Metal009_2K_Roughness.png", + "images/Metal029_2K_Displacement.jpg", + "images/Metal029_2K_Displacement.png", + "images/noisenormal.png", + "images/NoneIcon.png", + "images/ParticleIcon.png", + "images/raindrops_multi.tga", + "images/RainIcon.png", + "images/rainsplash.png", + "images/ShockwaveIcon.png", + "images/smoke.png", + "images/SmokeIcon.png", + "images/snowflake_DF.tga", + "images/snowflake_DF_multi.tga", + "images/SnowIcon.png", + "images/spark.png", + "images/SparkIcon.png", + "images/steam.png", + "images/SteamIcon.png", + "images/HDR/dreamstime_xl_119184006.png", + "images/HDR/dreamstime_xl_182890747.png", + "images/HDR/dreamstime_xl_224613382.png", + "images/HDR/dreamstime_xxl_241783592.png", + "images/Icons/BrushedSteelmatIcon.png", + "images/Icons/CarbonFibermatIcon.png", + "images/Icons/CheckboxImagesfxIcon-1.png", + "images/Icons/CheckboxImagesfxIcon-10.png", + "images/Icons/CheckboxImagesfxIcon-2.png", + "images/Icons/CheckboxImagesfxIcon-3.png", + "images/Icons/CheckboxImagesfxIcon-4.png", + "images/Icons/CheckboxImagesfxIcon-4.tif", + "images/Icons/CheckboxImagesfxIcon-5.png", + "images/Icons/CheckboxImagesfxIcon-6.png", + "images/Icons/CheckboxImagesfxIcon-7.png", + "images/Icons/CheckboxImagesfxIcon-8.png", + "images/Icons/CheckboxImagesfxIcon-9.png", + "images/Icons/CheckboxImagesfxIcon.png", + "images/Icons/CheckboxImagesmodelIcon-1.png", + "images/Icons/CheckboxImagesmodelIcon-2.png", + "images/Icons/CheckboxImagesmodelIcon-3.png", + "images/Icons/CheckboxImagesmodelIcon.png", + "images/Icons/CoppermatIcon.png", + "images/Icons/FabricmatIcon.png", + "images/Icons/GlassmatIcon.png", + "images/Icons/Icon_Colorful.png", + "images/Icons/Icon_Dark.png", + "images/Icons/Icon_Light.png", + "images/Icons/LeathermatIcon.png", + "images/Icons/PlasticTexturedmatIcon.png", + "images/Icons/SilvermatIcon.png", + "images/Icons/StonematIcon.png", + "images/Icons/WoodmatIcon.png", + "images/leathertextures/Leather037_2K-PNG/Leather037_2K_Color.png", + "images/leathertextures/Leather037_2K-PNG/Leather037_2K_NormalGL.png", + "images/leathertextures/Leather037_2K-PNG/Leather037_2K_Roughness.png", + "images/stonetextures/Rock023_2K_AmbientOcclusion.png", + "images/stonetextures/Rock023_2K_Color.png", + "images/stonetextures/Rock023_2K_NormalGL.png", + "images/stonetextures/Rock023_2K_Roughness.png", + "images/woodtextures/Wood048_2K-PNG/Wood048_2K_Color.png", + "images/woodtextures/Wood048_2K-PNG/Wood048_2K_NormalGL.png", + "images/woodtextures/Wood048_2K-PNG/Wood048_2K_Roughness.png", + "Meshes/bunnyUV.mesh", + "Meshes/floor.mesh", + "Meshes/materialBall.mesh", + "Meshes/stanford_Dragon.mesh" + }; +}; diff --git a/examples/demos/FX_Material_Showroom/src/import_qml_plugins.h b/examples/demos/FX_Material_Showroom/src/import_qml_plugins.h new file mode 100644 index 000000000..9a31eefd5 --- /dev/null +++ b/examples/demos/FX_Material_Showroom/src/import_qml_plugins.h @@ -0,0 +1,8 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QQmlExtensionPlugin> + +Q_IMPORT_QML_PLUGIN(contentPlugin) +Q_IMPORT_QML_PLUGIN(PocketDemoPlugin) +Q_IMPORT_QML_PLUGIN(ComponentBundles_MaterialBundlePlugin) diff --git a/examples/demos/FX_Material_Showroom/src/main.cpp b/examples/demos/FX_Material_Showroom/src/main.cpp new file mode 100644 index 000000000..890170f91 --- /dev/null +++ b/examples/demos/FX_Material_Showroom/src/main.cpp @@ -0,0 +1,54 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QGuiApplication> +#include <QQmlApplicationEngine> + +#include "imagedownloader.h" +#include "app_environment.h" +#include "import_qml_plugins.h" + +using namespace Qt::Literals::StringLiterals; + +int main(int argc, char *argv[]) +{ + set_qt_environment(); + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + const QUrl url(u"qrc:Main/main.qml"_qs); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, + [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, Qt::QueuedConnection); + + engine.addImportPath(QCoreApplication::applicationDirPath() + "/qml"); + engine.addImportPath(":/"); + + engine.load(url); + + if (engine.rootObjects().isEmpty()) + return -1; + + ImageDownloader downloader; + + QObject::connect(&downloader, &ImageDownloader::downloadProgress, &app, [&] (double progress) { + QMetaObject::invokeMethod(engine.rootObjects().at(0), "downloadProgress", + Q_ARG(QVariant, progress)); + }); + + QObject::connect(&downloader, &ImageDownloader::downloadStart, &app, [&] (int num) { + QMetaObject::invokeMethod(engine.rootObjects().at(0), "downloadStart", + Q_ARG(QVariant, num), + Q_ARG(QVariant, downloader.downloadCount())); + }); + + QObject::connect(&downloader, &ImageDownloader::finished, &app, [&] { + QMetaObject::invokeMethod(engine.rootObjects().at(0), "downloadComplete"); + }); + + downloader.downloadImages(); + + return app.exec(); +} |