diff --git a/capture/capturewidget.cpp b/capture/capturewidget.cpp index fba520dd..d0d8828c 100644 --- a/capture/capturewidget.cpp +++ b/capture/capturewidget.cpp @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include @@ -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; } diff --git a/capture/capturewidget.h b/capture/capturewidget.h index 8a11b027..5c99fe67 100644 --- a/capture/capturewidget.h +++ b/capture/capturewidget.h @@ -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(); diff --git a/capture/screenshot.cpp b/capture/screenshot.cpp index 45ef4e70..1c629913 100644 --- a/capture/screenshot.cpp +++ b/capture/screenshot.cpp @@ -27,6 +27,10 @@ #include #include #include +#include +#include +#include +#include // 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 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 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); +} + diff --git a/capture/screenshot.h b/capture/screenshot.h index f88c7fe0..3469f1f3 100644 --- a/capture/screenshot.h +++ b/capture/screenshot.h @@ -20,22 +20,27 @@ #include #include +#include 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); private: QPixmap m_screenshot; + QPointer m_accessManager; }; #endif // SCREENSHOT_H diff --git a/configwindow.cpp b/configwindow.cpp index 8ee68ee7..93a166d4 100644 --- a/configwindow.cpp +++ b/configwindow.cpp @@ -56,7 +56,7 @@ void ConfigWindow::initButtonList() { auto t = static_cast(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);