Snapshot dialogs

This commit is contained in:
Brad Davis 2016-01-30 10:58:48 -08:00 committed by Bradley Austin Davis
parent eb7988e4ef
commit 27c38599a9
9 changed files with 260 additions and 215 deletions

View file

@ -0,0 +1,117 @@
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.XmlListModel 2.0
import "../../windows"
import "../../js/Utils.js" as Utils
import "../models"
Window {
id: root
resizable: true
width: 516
height: 616
minSize: Qt.vector2d(500, 600);
maxSize: Qt.vector2d(1000, 800);
property alias source: image.source
Rectangle {
anchors.fill: parent
color: "white"
Item {
anchors { fill: parent; margins: 8 }
Image {
id: image
anchors { top: parent.top; left: parent.left; right: parent.right; bottom: notesLabel.top; bottomMargin: 8 }
fillMode: Image.PreserveAspectFit
}
Text {
id: notesLabel
anchors { left: parent.left; bottom: notes.top; bottomMargin: 8; }
text: "Notes about this image"
font.pointSize: 14
font.bold: true
color: "#666"
}
TextArea {
id: notes
anchors { left: parent.left; bottom: parent.bottom; right: shareButton.left; rightMargin: 8 }
height: 60
}
Button {
id: shareButton
anchors { verticalCenter: notes.verticalCenter; right: parent.right; }
width: 120; height: 50
text: "Share"
style: ButtonStyle {
background: Rectangle {
implicitWidth: 120
implicitHeight: 50
border.width: control.activeFocus ? 2 : 1
color: "#333"
radius: 9
}
label: Text {
color: shareButton.enabled ? "white" : "gray"
font.pixelSize: 18
font.bold: true
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
anchors.fill: parent
text: shareButton.text
}
}
onClicked: {
enabled = false;
uploadTimer.start();
}
Timer {
id: uploadTimer
running: false
interval: 5
repeat: false
onTriggered: {
var uploaded = SnapshotUploader.uploadSnapshot(root.source.toString())
console.log("Uploaded result " + uploaded)
if (!uploaded) {
console.log("Upload failed ");
}
}
}
}
}
Action {
id: shareAction
text: qsTr("OK")
enabled: root.result ? true : false
shortcut: Qt.Key_Return
onTriggered: {
root.destroy();
}
}
Action {
id: cancelAction
text: qsTr("Cancel")
shortcut: Qt.Key_Escape
onTriggered: {
root.destroy();
}
}
}
}

View file

@ -17,7 +17,6 @@ Overlay {
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var value = properties[key];
console.log("OVERLAY rectangle property " + key + " set to value " + value);
switch (key) {
case "height": root.height = value; break;
case "width": root.width = value; break;
@ -30,7 +29,7 @@ Overlay {
case "borderColor": rectangle.border.color = Qt.rgba(value.red / 255, value.green / 255, value.blue / 255, rectangle.border.color.a); break;
case "borderWidth": rectangle.border.width = value; break;
case "radius": rectangle.radius = value; break;
default: console.log("OVERLAY Unhandled rectangle property " + key);
default: console.warn("OVERLAY Unhandled rectangle property " + key);
}
}
}

View file

@ -46,8 +46,7 @@ Overlay {
case "backgroundColor": background.color = Qt.rgba(value.red / 255, value.green / 255, value.blue / 255, background.color.a); break;
case "font": textField.font.pixelSize = value.size; break;
case "lineHeight": textField.lineHeight = value; break;
default:
console.log("OVERLAY text unhandled property " + key);
default: console.warn("OVERLAY text unhandled property " + key);
}
}
}

View file

@ -1213,6 +1213,7 @@ void Application::initializeUi() {
// For some reason there is already an "Application" object in the QML context,
// though I can't find it. Hence, "ApplicationInterface"
rootContext->setContextProperty("SnapshotUploader", new SnapshotUploader());
rootContext->setContextProperty("ApplicationInterface", this);
rootContext->setContextProperty("AnimationCache", DependencyManager::get<AnimationCache>().data());
rootContext->setContextProperty("Audio", &AudioScriptingInterface::getInstance());
@ -1848,7 +1849,7 @@ void Application::keyPressEvent(QKeyEvent* event) {
if (isShifted && isMeta) {
auto offscreenUi = DependencyManager::get<OffscreenUi>();
offscreenUi->getRootContext()->engine()->clearComponentCache();
OffscreenUi::information("Debugging", "Component cache cleared");
//OffscreenUi::information("Debugging", "Component cache cleared");
// placeholder for dialogs being converted to QML.
}
break;
@ -4554,10 +4555,10 @@ void Application::takeSnapshot() {
return;
}
if (!_snapshotShareDialog) {
_snapshotShareDialog = new SnapshotShareDialog(fileName, _glWidget);
}
_snapshotShareDialog->show();
DependencyManager::get<OffscreenUi>()->load("hifi/dialogs/SnapshotShareDialog.qml", [=](QQmlContext*, QObject* dialog) {
dialog->setProperty("source", QUrl::fromLocalFile(fileName));
connect(dialog, SIGNAL(uploadSnapshot(const QString& snapshot)), this, SLOT(uploadSnapshot(const QString& snapshot)));
});
}
float Application::getRenderResolutionScale() const {

View file

@ -60,7 +60,6 @@
#include "ui/OctreeStatsDialog.h"
#include "ui/OverlayConductor.h"
#include "ui/overlays/Overlays.h"
#include "ui/SnapshotShareDialog.h"
#include "UndoStackScriptingInterface.h"
class OffscreenGLCanvas;
@ -455,7 +454,6 @@ private:
NodeToOctreeSceneStats _octreeServerSceneStats;
ControllerScriptingInterface* _controllerScriptingInterface{ nullptr };
QPointer<LogDialog> _logDialog;
QPointer<SnapshotShareDialog> _snapshotShareDialog;
FileLogger* _logger;

View file

@ -9,13 +9,17 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QImage>
#include <QTemporaryFile>
#include <QUrl>
#include <QtCore/QDateTime>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QTemporaryFile>
#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtNetwork/QHttpMultiPart>
#include <QtGui/QImage>
#include <AccountManager.h>
#include <AddressManager.h>
@ -23,6 +27,8 @@
#include <avatar/MyAvatar.h>
#include <FileUtils.h>
#include <NodeList.h>
#include <OffscreenUi.h>
#include <SharedUtil.h>
#include "Application.h"
#include "Snapshot.h"
@ -128,4 +134,116 @@ QFile* Snapshot::savedFileForSnapshot(QImage & shot, bool isTemporary) {
}
}
const QString FORUM_URL = "https://alphas.highfidelity.io";
const QString FORUM_UPLOADS_URL = FORUM_URL + "/uploads";
const QString FORUM_POST_URL = FORUM_URL + "/posts";
const QString FORUM_REPLY_TO_TOPIC = "244";
const QString FORUM_POST_TEMPLATE = "<img src='%1'/><p>%2</p>";
const QString SHARE_DEFAULT_ERROR = "The server isn't responding. Please try again in a few minutes.";
const QString SUCCESS_LABEL_TEMPLATE = "Success!!! Go check out your image ...<br/><a style='color:#333;text-decoration:none' href='%1'>%1</a>";
QString SnapshotUploader::uploadSnapshot(const QUrl& fileUrl) {
if (AccountManager::getInstance().getAccountInfo().getDiscourseApiKey().isEmpty()) {
OffscreenUi::warning(nullptr, "", "Your Discourse API key is missing, you cannot share snapshots. Please try to relog.");
return QString();
}
QHttpPart apiKeyPart;
apiKeyPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"api_key\""));
apiKeyPart.setBody(AccountManager::getInstance().getAccountInfo().getDiscourseApiKey().toLatin1());
QString filename = fileUrl.toLocalFile();
qDebug() << filename;
QFile* file = new QFile(filename);
Q_ASSERT(file->exists());
file->open(QIODevice::ReadOnly);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() + "\""));
imagePart.setBodyDevice(file);
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(apiKeyPart);
multiPart->append(imagePart);
QUrl url(FORUM_UPLOADS_URL);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
QString result;
QEventLoop loop;
QSharedPointer<QNetworkReply> reply(NetworkAccessManager::getInstance().post(request, multiPart));
QObject::connect(reply.data(), &QNetworkReply::finished, [&] {
loop.quit();
qDebug() << reply->errorString();
for (const auto& header : reply->rawHeaderList()) {
qDebug() << "Header " << QString(header);
}
auto replyResult = reply->readAll();
qDebug() << QString(replyResult);
QJsonDocument jsonResponse = QJsonDocument::fromJson(replyResult);
const QJsonObject& responseObject = jsonResponse.object();
if (!responseObject.contains("url")) {
OffscreenUi::warning(this, "", SHARE_DEFAULT_ERROR);
return;
}
result = responseObject["url"].toString();
});
loop.exec();
return result;
}
QString SnapshotUploader::sendForumPost(const QString& snapshotPath, const QString& notes) {
// post to Discourse forum
QNetworkRequest request;
request.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
QUrl forumUrl(FORUM_POST_URL);
QUrlQuery query;
query.addQueryItem("api_key", AccountManager::getInstance().getAccountInfo().getDiscourseApiKey());
query.addQueryItem("topic_id", FORUM_REPLY_TO_TOPIC);
query.addQueryItem("raw", FORUM_POST_TEMPLATE.arg(snapshotPath, notes));
forumUrl.setQuery(query);
QByteArray postData = forumUrl.toEncoded(QUrl::RemoveFragment);
request.setUrl(forumUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply* requestReply = NetworkAccessManager::getInstance().post(request, postData);
QEventLoop loop;
QString result;
connect(requestReply, &QNetworkReply::finished, [&] {
loop.quit();
QJsonDocument jsonResponse = QJsonDocument::fromJson(requestReply->readAll());
requestReply->deleteLater();
const QJsonObject& responseObject = jsonResponse.object();
if (!responseObject.contains("id")) {
QString errorMessage(SHARE_DEFAULT_ERROR);
if (responseObject.contains("errors")) {
QJsonArray errorArray = responseObject["errors"].toArray();
if (!errorArray.first().toString().isEmpty()) {
errorMessage = errorArray.first().toString();
}
}
OffscreenUi::warning(this, "", errorMessage);
return;
}
const QString urlTemplate = "%1/t/%2/%3/%4";
result = urlTemplate.arg(FORUM_URL,
responseObject["topic_slug"].toString(),
QString::number(responseObject["topic_id"].toDouble()),
QString::number(responseObject["post_number"].toDouble()));
});
loop.exec();
return result;
}

View file

@ -43,4 +43,12 @@ private:
static QFile* savedFileForSnapshot(QImage & image, bool isTemporary);
};
class SnapshotUploader : public QObject{
Q_OBJECT
public:
SnapshotUploader(QObject* parent = nullptr) : QObject(parent) {}
Q_INVOKABLE QString uploadSnapshot(const QUrl& fileUrl);
Q_INVOKABLE QString sendForumPost(const QString& snapshotPath, const QString& notes);
};
#endif // hifi_Snapshot_h

View file

@ -9,16 +9,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "SnapshotShareDialog.h"
#include "AccountManager.h"
#include "SharedUtil.h"
#if 0
#include <QFile>
#include <QHttpMultiPart>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>
#include <QUrlQuery>
#include <OffscreenUi.h>
@ -26,13 +18,6 @@ const int NARROW_SNAPSHOT_DIALOG_SIZE = 500;
const int WIDE_SNAPSHOT_DIALOG_WIDTH = 650;
const int SUCCESS_LABEL_HEIGHT = 140;
const QString FORUM_URL = "https://alphas.highfidelity.io";
const QString FORUM_UPLOADS_URL = FORUM_URL + "/uploads";
const QString FORUM_POST_URL = FORUM_URL + "/posts";
const QString FORUM_REPLY_TO_TOPIC = "244";
const QString FORUM_POST_TEMPLATE = "<img src='%1'/><p>%2</p>";
const QString SHARE_DEFAULT_ERROR = "The server isn't responding. Please try again in a few minutes.";
const QString SUCCESS_LABEL_TEMPLATE = "Success!!! Go check out your image ...<br/><a style='color:#333;text-decoration:none' href='%1'>%1</a>";
const QString SHARE_BUTTON_STYLE = "border-width:0;border-radius:9px;border-radius:9px;font-family:Arial;font-size:18px;"
"font-weight:100;color:#FFFFFF;width: 120px;height: 50px;";
const QString SHARE_BUTTON_ENABLED_STYLE = "background-color: #333;";
@ -45,33 +30,6 @@ SnapshotShareDialog::SnapshotShareDialog(QString fileName, QWidget* parent) :
_fileName(fileName)
{
setAttribute(Qt::WA_DeleteOnClose);
_ui.setupUi(this);
QPixmap snaphsotPixmap(fileName);
float snapshotRatio = static_cast<float>(snaphsotPixmap.size().width()) / snaphsotPixmap.size().height();
// narrow snapshot
if (snapshotRatio > 1) {
setFixedWidth(WIDE_SNAPSHOT_DIALOG_WIDTH);
_ui.snapshotWidget->setFixedWidth(WIDE_SNAPSHOT_DIALOG_WIDTH);
}
float labelRatio = static_cast<float>(_ui.snapshotWidget->size().width()) / _ui.snapshotWidget->size().height();
// set the same aspect ratio of label as of snapshot
if (snapshotRatio > labelRatio) {
int oldHeight = _ui.snapshotWidget->size().height();
_ui.snapshotWidget->setFixedHeight((int) (_ui.snapshotWidget->size().width() / snapshotRatio));
// if height is less then original, resize the window as well
if (_ui.snapshotWidget->size().height() < NARROW_SNAPSHOT_DIALOG_SIZE) {
setFixedHeight(size().height() - (oldHeight - _ui.snapshotWidget->size().height()));
}
} else {
_ui.snapshotWidget->setFixedWidth((int) (_ui.snapshotWidget->size().height() * snapshotRatio));
}
_ui.snapshotWidget->setPixmap(snaphsotPixmap);
_ui.snapshotWidget->adjustSize();
@ -85,119 +43,5 @@ void SnapshotShareDialog::accept() {
uploadSnapshot();
}
void SnapshotShareDialog::uploadSnapshot() {
if (AccountManager::getInstance().getAccountInfo().getDiscourseApiKey().isEmpty()) {
OffscreenUi::warning(this, "",
"Your Discourse API key is missing, you cannot share snapshots. Please try to relog.");
return;
}
QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart apiKeyPart;
apiKeyPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"api_key\""));
apiKeyPart.setBody(AccountManager::getInstance().getAccountInfo().getDiscourseApiKey().toLatin1());
QFile* file = new QFile(_fileName);
file->open(QIODevice::ReadOnly);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"file\"; filename=\"" + file->fileName() +"\""));
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(apiKeyPart);
multiPart->append(imagePart);
QUrl url(FORUM_UPLOADS_URL);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
QNetworkReply* reply = NetworkAccessManager::getInstance().post(request, multiPart);
connect(reply, &QNetworkReply::finished, this, &SnapshotShareDialog::uploadRequestFinished);
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
}
void SnapshotShareDialog::sendForumPost(QString snapshotPath) {
// post to Discourse forum
QNetworkRequest request;
request.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT);
QUrl forumUrl(FORUM_POST_URL);
QUrlQuery query;
query.addQueryItem("api_key", AccountManager::getInstance().getAccountInfo().getDiscourseApiKey());
query.addQueryItem("topic_id", FORUM_REPLY_TO_TOPIC);
query.addQueryItem("raw", FORUM_POST_TEMPLATE.arg(snapshotPath, _ui.textEdit->toPlainText()));
forumUrl.setQuery(query);
QByteArray postData = forumUrl.toEncoded(QUrl::RemoveFragment);
request.setUrl(forumUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply* requestReply = NetworkAccessManager::getInstance().post(request, postData);
connect(requestReply, &QNetworkReply::finished, this, &SnapshotShareDialog::postRequestFinished);
QEventLoop loop;
connect(requestReply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
}
void SnapshotShareDialog::postRequestFinished() {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
QJsonDocument jsonResponse = QJsonDocument::fromJson(requestReply->readAll());
requestReply->deleteLater();
const QJsonObject& responseObject = jsonResponse.object();
if (responseObject.contains("id")) {
_ui.textEdit->setHtml("");
const QString urlTemplate = "%1/t/%2/%3/%4";
QString link = urlTemplate.arg(FORUM_URL,
responseObject["topic_slug"].toString(),
QString::number(responseObject["topic_id"].toDouble()),
QString::number(responseObject["post_number"].toDouble()));
_ui.successLabel->setText(SUCCESS_LABEL_TEMPLATE.arg(link));
_ui.successLabel->setFixedHeight(SUCCESS_LABEL_HEIGHT);
// hide input widgets
_ui.shareButton->hide();
_ui.textEdit->hide();
_ui.labelNotes->hide();
} else {
QString errorMessage(SHARE_DEFAULT_ERROR);
if (responseObject.contains("errors")) {
QJsonArray errorArray = responseObject["errors"].toArray();
if (!errorArray.first().toString().isEmpty()) {
errorMessage = errorArray.first().toString();
}
}
OffscreenUi::warning(this, "", errorMessage);
_ui.shareButton->setEnabled(true);
_ui.shareButton->setStyleSheet(SHARE_BUTTON_STYLE + SHARE_BUTTON_ENABLED_STYLE);
}
}
void SnapshotShareDialog::uploadRequestFinished() {
QNetworkReply* requestReply = reinterpret_cast<QNetworkReply*>(sender());
QJsonDocument jsonResponse = QJsonDocument::fromJson(requestReply->readAll());
const QJsonObject& responseObject = jsonResponse.object();
if (responseObject.contains("url")) {
sendForumPost(responseObject["url"].toString());
} else {
OffscreenUi::warning(this, "", SHARE_DEFAULT_ERROR);
_ui.shareButton->setEnabled(true);
_ui.shareButton->setStyleSheet(SHARE_BUTTON_STYLE + SHARE_BUTTON_ENABLED_STYLE);
}
delete requestReply;
}
#endif

View file

@ -1,39 +0,0 @@
//
// SnapshotShareDialog.h
// interface/src/ui
//
// Created by Stojce Slavkovski on 2/16/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_snapshotShareDialog
#define hifi_snapshotShareDialog
#include "ui_shareSnapshot.h"
#include <QNetworkReply>
#include <QUrl>
class SnapshotShareDialog : public QDialog {
Q_OBJECT
public:
SnapshotShareDialog(QString fileName, QWidget* parent = 0);
private:
QString _fileName;
Ui_SnapshotShareDialog _ui;
void uploadSnapshot();
void sendForumPost(QString snapshotPath);
private slots:
void uploadRequestFinished();
void postRequestFinished();
void accept();
};
#endif // hifi_snapshotShareDialog