From cf87052b7fd852c1446931f7a0433848b3d5edb6 Mon Sep 17 00:00:00 2001 From: NissimHadar Date: Mon, 5 Feb 2018 17:29:00 -0800 Subject: [PATCH 1/4] Added option to select snapshot filename in scripts. --- interface/src/Application.cpp | 12 ++++++------ interface/src/Application.h | 6 ++++-- .../src/scripting/WindowScriptingInterface.cpp | 8 ++++---- .../src/scripting/WindowScriptingInterface.h | 4 ++-- interface/src/ui/Snapshot.cpp | 16 ++++++++++++---- interface/src/ui/Snapshot.h | 4 ++-- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 3e7dd3e223..710c9aca73 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -6934,10 +6934,10 @@ void Application::loadAvatarBrowser() const { DependencyManager::get()->openTablet(); } -void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRatio) { - postLambdaEvent([notify, includeAnimated, aspectRatio, this] { +void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRatio, const QString& filename) { + postLambdaEvent([notify, includeAnimated, aspectRatio, filename, this] { // Get a screenshot and save it - QString path = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getScreenshot(aspectRatio)); + QString path = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getScreenshot(aspectRatio), filename); // If we're not doing an animated snapshot as well... if (!includeAnimated) { // Tell the dependency manager that the capture of the still snapshot has taken place. @@ -6949,9 +6949,9 @@ void Application::takeSnapshot(bool notify, bool includeAnimated, float aspectRa }); } -void Application::takeSecondaryCameraSnapshot() { - postLambdaEvent([this] { - QString snapshotPath = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getSecondaryCameraScreenshot()); +void Application::takeSecondaryCameraSnapshot(const QString& filename) { + postLambdaEvent([filename, this] { + QString snapshotPath = Snapshot::saveSnapshot(getActiveDisplayPlugin()->getSecondaryCameraScreenshot(), filename); emit DependencyManager::get()->stillSnapshotTaken(snapshotPath, true); }); } diff --git a/interface/src/Application.h b/interface/src/Application.h index ddb8ce11e5..471f3103b8 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -266,8 +266,10 @@ public: float getGameLoopRate() const { return _gameLoopCounter.rate(); } - void takeSnapshot(bool notify, bool includeAnimated = false, float aspectRatio = 0.0f); - void takeSecondaryCameraSnapshot(); + // Note that takeSnapshot has a default value, as this method is used internally. + void takeSnapshot(bool notify, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = ""); + void takeSecondaryCameraSnapshot(const QString& filename); + void shareSnapshot(const QString& filename, const QUrl& href = QUrl("")); graphics::SkyboxPointer getDefaultSkybox() const { return _defaultSkybox; } diff --git a/interface/src/scripting/WindowScriptingInterface.cpp b/interface/src/scripting/WindowScriptingInterface.cpp index 8a7743a849..3a3d313fb4 100644 --- a/interface/src/scripting/WindowScriptingInterface.cpp +++ b/interface/src/scripting/WindowScriptingInterface.cpp @@ -430,12 +430,12 @@ bool WindowScriptingInterface::setDisplayTexture(const QString& name) { return qApp->getActiveDisplayPlugin()->setDisplayTexture(name); // Plugins that don't know how, answer false. } -void WindowScriptingInterface::takeSnapshot(bool notify, bool includeAnimated, float aspectRatio) { - qApp->takeSnapshot(notify, includeAnimated, aspectRatio); +void WindowScriptingInterface::takeSnapshot(bool notify, bool includeAnimated, float aspectRatio, const QString& filename) { + qApp->takeSnapshot(notify, includeAnimated, aspectRatio, filename); } -void WindowScriptingInterface::takeSecondaryCameraSnapshot() { - qApp->takeSecondaryCameraSnapshot(); +void WindowScriptingInterface::takeSecondaryCameraSnapshot(const QString& filename) { + qApp->takeSecondaryCameraSnapshot(filename); } void WindowScriptingInterface::shareSnapshot(const QString& path, const QUrl& href) { diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index dc71611c5b..41310c45fd 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -357,13 +357,13 @@ public slots: * var aspect = 1920 / 1080; * Window.takeSnapshot(notify, animated, aspect); */ - void takeSnapshot(bool notify = true, bool includeAnimated = false, float aspectRatio = 0.0f); + void takeSnapshot(bool notify = true, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = ""); /**jsdoc * Takes a still snapshot of the current view from the secondary camera that can be set up through the {@link Render} API. * @function Window.takeSecondaryCameraSnapshot */ - void takeSecondaryCameraSnapshot(); + void takeSecondaryCameraSnapshot(const QString& filename = ""); /**jsdoc * Emit a {@link Window.connectionAdded|connectionAdded} or a {@link Window.connectionError|connectionError} signal that diff --git a/interface/src/ui/Snapshot.cpp b/interface/src/ui/Snapshot.cpp index 59ecce5bc7..c1e077a980 100644 --- a/interface/src/ui/Snapshot.cpp +++ b/interface/src/ui/Snapshot.cpp @@ -73,9 +73,9 @@ SnapshotMetaData* Snapshot::parseSnapshotData(QString snapshotPath) { return data; } -QString Snapshot::saveSnapshot(QImage image) { +QString Snapshot::saveSnapshot(QImage image, const QString& filename) { - QFile* snapshotFile = savedFileForSnapshot(image, false); + QFile* snapshotFile = savedFileForSnapshot(image, false, filename); // we don't need the snapshot file, so close it, grab its filename and delete it snapshotFile->close(); @@ -92,7 +92,7 @@ QTemporaryFile* Snapshot::saveTempSnapshot(QImage image) { return static_cast(savedFileForSnapshot(image, true)); } -QFile* Snapshot::savedFileForSnapshot(QImage & shot, bool isTemporary) { +QFile* Snapshot::savedFileForSnapshot(QImage & shot, bool isTemporary, const QString& userSelectedFilename) { // adding URL to snapshot QUrl currentURL = DependencyManager::get()->currentShareableAddress(); @@ -104,7 +104,15 @@ QFile* Snapshot::savedFileForSnapshot(QImage & shot, bool isTemporary) { QDateTime now = QDateTime::currentDateTime(); - QString filename = FILENAME_PATH_FORMAT.arg(username, now.toString(DATETIME_FORMAT)); + // If user has requested specific filename then use it, else create the filename + // 'jpg" is appended, as the image is saved in jpg format. This is the case for all snapshots + // (see definition of FILENAME_PATH_FORMAT) + QString filename; + if (userSelectedFilename != "") { + filename = userSelectedFilename + ".jpg"; + } else { + filename = FILENAME_PATH_FORMAT.arg(username, now.toString(DATETIME_FORMAT)); + } const int IMAGE_QUALITY = 100; diff --git a/interface/src/ui/Snapshot.h b/interface/src/ui/Snapshot.h index 1246e1d004..edbfe5d272 100644 --- a/interface/src/ui/Snapshot.h +++ b/interface/src/ui/Snapshot.h @@ -37,7 +37,7 @@ class Snapshot : public QObject, public Dependency { Q_OBJECT SINGLETON_DEPENDENCY public: - static QString saveSnapshot(QImage image); + static QString saveSnapshot(QImage image, const QString& filename); static QTemporaryFile* saveTempSnapshot(QImage image); static SnapshotMetaData* parseSnapshotData(QString snapshotPath); @@ -51,7 +51,7 @@ public slots: Q_INVOKABLE QString getSnapshotsLocation(); Q_INVOKABLE void setSnapshotsLocation(const QString& location); private: - static QFile* savedFileForSnapshot(QImage & image, bool isTemporary); + static QFile* savedFileForSnapshot(QImage & image, bool isTemporary, const QString& userSelectedFilename = ""); }; #endif // hifi_Snapshot_h From faec9a62b6320a5ad05c1cb660871ed9881ff20e Mon Sep 17 00:00:00 2001 From: NissimHadar Date: Tue, 6 Feb 2018 11:01:50 -0800 Subject: [PATCH 2/4] Changed "" to QString() --- interface/src/Application.h | 2 +- interface/src/scripting/WindowScriptingInterface.h | 4 ++-- interface/src/ui/Snapshot.cpp | 2 +- interface/src/ui/Snapshot.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.h b/interface/src/Application.h index 471f3103b8..33f784a07f 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -267,7 +267,7 @@ public: float getGameLoopRate() const { return _gameLoopCounter.rate(); } // Note that takeSnapshot has a default value, as this method is used internally. - void takeSnapshot(bool notify, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = ""); + void takeSnapshot(bool notify, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = QString()); void takeSecondaryCameraSnapshot(const QString& filename); void shareSnapshot(const QString& filename, const QUrl& href = QUrl("")); diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index 41310c45fd..b38d27aac5 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -357,13 +357,13 @@ public slots: * var aspect = 1920 / 1080; * Window.takeSnapshot(notify, animated, aspect); */ - void takeSnapshot(bool notify = true, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = ""); + void takeSnapshot(bool notify = true, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = QString()); /**jsdoc * Takes a still snapshot of the current view from the secondary camera that can be set up through the {@link Render} API. * @function Window.takeSecondaryCameraSnapshot */ - void takeSecondaryCameraSnapshot(const QString& filename = ""); + void takeSecondaryCameraSnapshot(const QString& filename = QString()); /**jsdoc * Emit a {@link Window.connectionAdded|connectionAdded} or a {@link Window.connectionError|connectionError} signal that diff --git a/interface/src/ui/Snapshot.cpp b/interface/src/ui/Snapshot.cpp index c1e077a980..9b3089d78d 100644 --- a/interface/src/ui/Snapshot.cpp +++ b/interface/src/ui/Snapshot.cpp @@ -108,7 +108,7 @@ QFile* Snapshot::savedFileForSnapshot(QImage & shot, bool isTemporary, const QSt // 'jpg" is appended, as the image is saved in jpg format. This is the case for all snapshots // (see definition of FILENAME_PATH_FORMAT) QString filename; - if (userSelectedFilename != "") { + if (!userSelectedFilename.isNull()) { filename = userSelectedFilename + ".jpg"; } else { filename = FILENAME_PATH_FORMAT.arg(username, now.toString(DATETIME_FORMAT)); diff --git a/interface/src/ui/Snapshot.h b/interface/src/ui/Snapshot.h index edbfe5d272..62d3ed3db8 100644 --- a/interface/src/ui/Snapshot.h +++ b/interface/src/ui/Snapshot.h @@ -51,7 +51,7 @@ public slots: Q_INVOKABLE QString getSnapshotsLocation(); Q_INVOKABLE void setSnapshotsLocation(const QString& location); private: - static QFile* savedFileForSnapshot(QImage & image, bool isTemporary, const QString& userSelectedFilename = ""); + static QFile* savedFileForSnapshot(QImage & image, bool isTemporary, const QString& userSelectedFilename = QString()); }; #endif // hifi_Snapshot_h From d5bf9cdbcd1da585e14c0d1748f5388f97446716 Mon Sep 17 00:00:00 2001 From: NissimHadar Date: Tue, 6 Feb 2018 16:16:25 -0800 Subject: [PATCH 3/4] Updated JSDoc comments. --- interface/src/scripting/WindowScriptingInterface.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index b38d27aac5..cc81f0a082 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -334,6 +334,8 @@ public slots: * @param {number} aspectRatio=0 - The width/height ratio of the snapshot required. If the value is 0 the * full resolution is used (window dimensions in desktop mode; HMD display dimensions in HMD mode), otherwise one of the * dimensions is adjusted in order to match the aspect ratio. + * @param {string} filename=QString() - If this value is not null then the image will be saved to this filename, with an appended ",jpg". + * otherwise, the image will be saved as 'hifi-snap-by--YYYY-MM-DD_HH-MM-SS' * @example Using the snapshot function and signals. * function onStillSnapshotTaken(path, notify) { * print("Still snapshot taken: " + path); @@ -355,13 +357,17 @@ public slots: * var notify = true; * var animated = true; * var aspect = 1920 / 1080; - * Window.takeSnapshot(notify, animated, aspect); + * var filename = QString(); + * Window.takeSnapshot(notify, animated, aspect, filename); */ void takeSnapshot(bool notify = true, bool includeAnimated = false, float aspectRatio = 0.0f, const QString& filename = QString()); /**jsdoc * Takes a still snapshot of the current view from the secondary camera that can be set up through the {@link Render} API. * @function Window.takeSecondaryCameraSnapshot + * @param {string} filename=QString() - If this value is not null then the image will be saved to this filename, with an appended ",jpg" + * + * var filename = QString(); */ void takeSecondaryCameraSnapshot(const QString& filename = QString()); From 5579eac034a9ca5cd1ed13032a1c9e1f1b538a29 Mon Sep 17 00:00:00 2001 From: NissimHadar Date: Tue, 6 Feb 2018 16:42:25 -0800 Subject: [PATCH 4/4] Nothing... --- interface/src/scripting/WindowScriptingInterface.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index cc81f0a082..76decf4362 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -365,7 +365,7 @@ public slots: /**jsdoc * Takes a still snapshot of the current view from the secondary camera that can be set up through the {@link Render} API. * @function Window.takeSecondaryCameraSnapshot - * @param {string} filename=QString() - If this value is not null then the image will be saved to this filename, with an appended ",jpg" + * @param {string} filename=QString() - If this value is not null then the image will be saved to this filename, with an appended ".jpg" * * var filename = QString(); */