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 <QMouseEvent>
#include <QClipboard> #include <QClipboard>
#include <QSettings> #include <QSettings>
#include <QNetworkReply>
#include <QDesktopServices>
#include <QDebug> #include <QDebug>
@@ -329,6 +331,30 @@ void CaptureWidget::copyScreenshot() {
close(); 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() { void CaptureWidget::undo() {
if (!m_modifications.isEmpty()) { if (!m_modifications.isEmpty()) {
m_modifications.pop_back(); m_modifications.pop_back();
@@ -375,8 +401,7 @@ void CaptureWidget::downResize() {
void CaptureWidget::setState(Button::Type t) { void CaptureWidget::setState(Button::Type t) {
if(t == Button::Type::selectionIndicator || if(t == Button::Type::selectionIndicator ||
t == Button::Type::mouseVisibility || t == Button::Type::mouseVisibility ||
t == Button::Type::colorPicker || t == Button::Type::colorPicker) {
t == Button::Type::imageUploader) {
return; return;
} }
Button::Type newState = t; Button::Type newState = t;
@@ -391,6 +416,8 @@ void CaptureWidget::setState(Button::Type t) {
close(); close();
} else if (t == Button::Type::undo) { } else if (t == Button::Type::undo) {
undo(); undo();
} else if (t == Button::Type::imageUploader) {
uploadScreenshot();
} else { } else {
m_state = newState; m_state = newState;
} }

View File

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

View File

@@ -27,6 +27,10 @@
#include <QImageWriter> #include <QImageWriter>
#include <QFileDialog> #include <QFileDialog>
#include <QPainter> #include <QPainter>
#include <QBuffer>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
// Screenshot is an extension of QPixmap which lets you manage specific tasks // 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) { void Screenshot::setScreenshot(const QPixmap &p) {
m_screenshot = p; m_screenshot = p;
} }
@@ -115,9 +123,8 @@ QPixmap Screenshot::paintModifications(const QVector<CaptureModification> v) {
switch (modification.getType()) { switch (modification.getType()) {
case Button::Type::arrow: case Button::Type::arrow:
painter.drawLine(points[0], points[1]); 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; break;
case Button::Type::circle: case Button::Type::circle:
painter.drawEllipse(QRect(points[0], points[1])); painter.drawEllipse(QRect(points[0], points[1]));
@@ -144,4 +151,33 @@ QPixmap Screenshot::paintModifications(const QVector<CaptureModification> v) {
return m_screenshot; 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 <QPixmap>
#include <QRect> #include <QRect>
#include <QPointer>
class QString; class QString;
class CaptureModification; class CaptureModification;
class QNetworkAccessManager;
class Screenshot class Screenshot {
{
public: public:
Screenshot(const QPixmap &); Screenshot(const QPixmap &);
~Screenshot();
void setScreenshot(const QPixmap &); void setScreenshot(const QPixmap &);
QPixmap getScreenshot() const; QPixmap getScreenshot() const;
QString graphicalSave(const QRect &selection = QRect()) const; QString graphicalSave(const QRect &selection = QRect()) const;
void uploadToImgur(QNetworkAccessManager *,
const QRect &selection = QRect());
QPixmap paintModifications(const QVector<CaptureModification>); QPixmap paintModifications(const QVector<CaptureModification>);
private: private:
QPixmap m_screenshot; QPixmap m_screenshot;
QPointer<QNetworkAccessManager> m_accessManager;
}; };
#endif // SCREENSHOT_H #endif // SCREENSHOT_H

View File

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