Add support for Imgur anonymous uploads

This commit is contained in:
lupoDharkael
2017-05-14 18:45:57 +02:00
parent ae19e5f13e
commit 70fe8f1967
5 changed files with 80 additions and 8 deletions

View File

@@ -35,6 +35,8 @@
#include <QMouseEvent>
#include <QClipboard>
#include <QSettings>
#include <QNetworkReply>
#include <QDesktopServices>
#include <QDebug>
@@ -329,6 +331,30 @@ void CaptureWidget::copyScreenshot() {
close();
}
void CaptureWidget::openURL(QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NoError) {
QString data = QString::fromUtf8(reply->readAll());
QString imageID = data.split("\"").at(5);
QString url = QString("http://i.imgur.com/%1.png").arg(imageID);
QDesktopServices::openUrl(url);
}
close();
}
void CaptureWidget::uploadScreenshot() {
Screenshot s(m_screenshot);
s.paintModifications(m_modifications);
QNetworkAccessManager *am = new QNetworkAccessManager(this);
connect(am, &QNetworkAccessManager::finished, this,
&CaptureWidget::openURL);
if (m_selection.isNull()) {
s.uploadToImgur(am);
} else {
s.uploadToImgur(am, getExtendedSelection());
}
hide();
}
void CaptureWidget::undo() {
if (!m_modifications.isEmpty()) {
m_modifications.pop_back();
@@ -375,8 +401,7 @@ void CaptureWidget::downResize() {
void CaptureWidget::setState(Button::Type t) {
if(t == Button::Type::selectionIndicator ||
t == Button::Type::mouseVisibility ||
t == Button::Type::colorPicker ||
t == Button::Type::imageUploader) {
t == Button::Type::colorPicker) {
return;
}
Button::Type newState = t;
@@ -391,6 +416,8 @@ void CaptureWidget::setState(Button::Type t) {
close();
} else if (t == Button::Type::undo) {
undo();
} else if (t == Button::Type::imageUploader) {
uploadScreenshot();
} else {
m_state = newState;
}

View File

@@ -33,6 +33,8 @@ class QPaintEvent;
class QResizeEvent;
class QMouseEvent;
class CaptureModification;
class QNetworkAccessManager;
class QNetworkReply;
class CaptureWidget : public QWidget {
Q_OBJECT
@@ -45,6 +47,7 @@ public:
private slots:
void saveScreenshot();
void copyScreenshot();
void openURL(QNetworkReply *reply);
void leaveButton();
void enterButton();
void undo();
@@ -92,6 +95,7 @@ protected:
private:
void createCapture();
void uploadScreenshot();
void initShortcuts();
void updateHandles();
void updateSizeIndicator();

View File

@@ -27,6 +27,10 @@
#include <QImageWriter>
#include <QFileDialog>
#include <QPainter>
#include <QBuffer>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
// Screenshot is an extension of QPixmap which lets you manage specific tasks
@@ -34,6 +38,10 @@ Screenshot::Screenshot(const QPixmap &p) : m_screenshot(p) {
}
Screenshot::~Screenshot() {
if (m_accessManager) delete(m_accessManager);
}
void Screenshot::setScreenshot(const QPixmap &p) {
m_screenshot = p;
}
@@ -115,9 +123,8 @@ QPixmap Screenshot::paintModifications(const QVector<CaptureModification> v) {
switch (modification.getType()) {
case Button::Type::arrow:
painter.drawLine(points[0], points[1]);
// https://forum.qt.io/topic/27284/solved-trouble-creating-an-arrow-between-two-qgraphicsitems
// http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html
// https://forum.qt.io/topic/38928/code-to-create-a-arrow-graphics-item-in-qt
break;
case Button::Type::circle:
painter.drawEllipse(QRect(points[0], points[1]));
@@ -144,4 +151,33 @@ QPixmap Screenshot::paintModifications(const QVector<CaptureModification> v) {
return m_screenshot;
}
void Screenshot::uploadToImgur(QNetworkAccessManager *accessManager,
const QRect &selection) {
QString title ="asdasdf";
QString description = "test";
QPixmap pixToSave;
if (selection.isEmpty()) {
pixToSave = m_screenshot;
} else { // save full screen when no selection
pixToSave = m_screenshot.copy(selection);
}
QByteArray byteArray;
QBuffer buffer(&byteArray);
pixToSave.save(&buffer, "PNG");
QUrlQuery urlQuery;
urlQuery.addQueryItem("title", title);
urlQuery.addQueryItem("description", description);
QNetworkRequest request;
QUrl url("https://api.imgur.com/3/image");
url.setQuery(urlQuery);
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/application/x-www-form-urlencoded");
request.setRawHeader("Authorization", "Client-ID 313baf0c7b4d3ff");
accessManager->post(request, byteArray);
}

View File

@@ -20,22 +20,27 @@
#include <QPixmap>
#include <QRect>
#include <QPointer>
class QString;
class CaptureModification;
class QNetworkAccessManager;
class Screenshot
{
class Screenshot {
public:
Screenshot(const QPixmap &);
~Screenshot();
void setScreenshot(const QPixmap &);
QPixmap getScreenshot() const;
QString graphicalSave(const QRect &selection = QRect()) const;
void uploadToImgur(QNetworkAccessManager *,
const QRect &selection = QRect());
QPixmap paintModifications(const QVector<CaptureModification>);
private:
QPixmap m_screenshot;
QPointer<QNetworkAccessManager> m_accessManager;
};
#endif // SCREENSHOT_H

View File

@@ -56,7 +56,7 @@ void ConfigWindow::initButtonList() {
auto t = static_cast<Button::Type>(i);
// Blocked items
if (t ==Button::Type::mouseVisibility || t ==Button::Type::text ||
t ==Button::Type::imageUploader || t ==Button::Type::arrow) {
t ==Button::Type::arrow) {
continue;
}
QListWidgetItem *buttonItem = new QListWidgetItem(m_buttonListView);