diff --git a/flameshot.pro b/flameshot.pro index 7572644a..cf0e8466 100644 --- a/flameshot.pro +++ b/flameshot.pro @@ -38,24 +38,23 @@ include(src/third-party/Qt-Color-Widgets//color_widgets.pri) DEFINES += QAPPLICATION_CLASS=QApplication SOURCES += src/main.cpp\ - src/capture/buttonhandler.cpp \ + src/capture/widget/buttonhandler.cpp \ src/infowindow.cpp \ src/config/configwindow.cpp \ src/capture/screenshot.cpp \ - src/capture/capturewidget.cpp \ + src/capture/widget/capturewidget.cpp \ src/capture/capturemodification.cpp \ - src/capture/colorpicker.cpp \ + src/capture/widget/colorpicker.cpp \ src/config/buttonlistview.cpp \ src/config/uicoloreditor.cpp \ src/config/geneneralconf.cpp \ src/core/flameshotdbusadapter.cpp \ + src/core/controller.cpp \ src/config/clickablelabel.cpp \ src/config/filenameeditor.cpp \ - src/utils/filenamehandler.cpp \ src/config/strftimechooserwidget.cpp \ - src/capture/screengrabber.cpp \ src/capture/tools/capturetool.cpp \ - src/capture/capturebutton.cpp \ + src/capture/widget/capturebutton.cpp \ src/capture/tools/penciltool.cpp \ src/capture/tools/undotool.cpp \ src/capture/tools/arrowtool.cpp \ @@ -71,21 +70,28 @@ SOURCES += src/main.cpp\ src/capture/tools/selectiontool.cpp \ src/capture/tools/sizeindicatortool.cpp \ src/capture/tools/toolfactory.cpp \ + src/utils/filenamehandler.cpp \ + src/utils/screengrabber.cpp \ src/utils/confighandler.cpp \ - src/core/controller.cpp \ src/utils/systemnotification.cpp \ src/cli/commandlineparser.cpp \ src/cli/commandoption.cpp \ - src/cli/commandargument.cpp + src/cli/commandargument.cpp \ + src/capture/workers/screenshotsaver.cpp \ + src/capture/workers/imgur/imguruploader.cpp \ + src/capture/workers/graphicalscreenshotsaver.cpp \ + src/capture/workers/imgur/loadspinner.cpp \ + src/capture/workers/imgur/imagelabel.cpp \ + src/capture/workers/imgur/notificationwidget.cpp HEADERS += \ - src/capture/buttonhandler.h \ + src/capture/widget/buttonhandler.h \ src/infowindow.h \ src/config/configwindow.h \ src/capture/screenshot.h \ - src/capture/capturewidget.h \ + src/capture/widget/capturewidget.h \ src/capture/capturemodification.h \ - src/capture/colorpicker.h \ + src/capture/widget/colorpicker.h \ src/config/buttonlistview.h \ src/config/uicoloreditor.h \ src/config/geneneralconf.h \ @@ -94,9 +100,9 @@ HEADERS += \ src/config/filenameeditor.h \ src/utils/filenamehandler.h \ src/config/strftimechooserwidget.h \ - src/capture/screengrabber.h \ + src/utils/screengrabber.h \ src/capture/tools/capturetool.h \ - src/capture/capturebutton.h \ + src/capture/widget/capturebutton.h \ src/capture/tools/penciltool.h \ src/capture/tools/undotool.h \ src/capture/tools/arrowtool.h \ @@ -117,7 +123,13 @@ HEADERS += \ src/utils/systemnotification.h \ src/cli/commandlineparser.h \ src/cli/commandoption.h \ - src/cli/commandargument.h + src/cli/commandargument.h \ + src/capture/workers/screenshotsaver.h \ + src/capture/workers/imgur/imguruploader.h \ + src/capture/workers/graphicalscreenshotsaver.h \ + src/capture/workers/imgur/loadspinner.h \ + src/capture/workers/imgur/imagelabel.h \ + src/capture/workers/imgur/notificationwidget.h RESOURCES += \ graphics.qrc diff --git a/src/capture/capturemodification.h b/src/capture/capturemodification.h index 5f7f3e92..620b563e 100644 --- a/src/capture/capturemodification.h +++ b/src/capture/capturemodification.h @@ -18,7 +18,7 @@ #ifndef CAPTURECHANGE_H #define CAPTURECHANGE_H -#include "capturebutton.h" +#include "src/capture/widget/capturebutton.h" #include #include diff --git a/src/capture/screenshot.cpp b/src/capture/screenshot.cpp index a5bc9915..4d6c7f01 100644 --- a/src/capture/screenshot.cpp +++ b/src/capture/screenshot.cpp @@ -15,8 +15,8 @@ // You should have received a copy of the GNU General Public License // along with Flameshot. If not, see . -#include "screenshot.h" -#include "capturebutton.h" +#include "src/capture/screenshot.h" +#include "src/capture/widget/capturebutton.h" #include "capturemodification.h" #include "src/capture/tools/capturetool.h" #include "src/utils/filenamehandler.h" @@ -57,65 +57,8 @@ QPixmap Screenshot::screenshot() const { return m_modifiedScreenshot; } -// graphicalSave generates a graphical window to ask about the save path and -// saves the screenshot with all the modifications in such directory -QString Screenshot::graphicalSave(bool &ok, - const QRect &selection, - QWidget *parent) const -{ - ok = false; // user quits the dialog case - QString savePath = FileNameHandler().absoluteSavePath(); - // setup window - QFileDialog fileDialog(parent, QObject::tr("Save As"), savePath); - fileDialog.setAcceptMode(QFileDialog::AcceptSave); - fileDialog.setFileMode(QFileDialog::AnyFile); - fileDialog.setDirectory(savePath); - QStringList mimeTypes; - for (const QByteArray &bf: QImageWriter::supportedMimeTypes()) - mimeTypes.append(QLatin1String(bf)); - fileDialog.setMimeTypeFilters(mimeTypes); - fileDialog.selectMimeTypeFilter("image/png"); - fileDialog.setDefaultSuffix("png"); - fileDialog.setWindowIcon(QIcon(":img/flameshot.png")); - - QString fileName; - do { - if (fileDialog.exec() != QDialog::Accepted) { return QString(); } - fileName = fileDialog.selectedFiles().first(); - - QString pathNoFile = fileName.left(fileName.lastIndexOf("/")); - ConfigHandler().setSavePath(pathNoFile); - - QPixmap pixToSave; - if (selection.isEmpty()) { - pixToSave = m_modifiedScreenshot; - } else { // save full screen when no selection - pixToSave = m_modifiedScreenshot.copy(selection); - } - ok = pixToSave.save(fileName); - if (!ok) { - QMessageBox saveErrBox( - QMessageBox::Warning, - QObject::tr("Save Error"), - QObject::tr("The image could not be saved to \"%1\".") - .arg(QDir::toNativeSeparators(fileName))); - saveErrBox.setWindowIcon(QIcon(":img/flameshot.png")); - saveErrBox.exec(); - } - } while(!ok); - return savePath; -} - -QString Screenshot::fileSave(bool &ok, const QRect &selection) const { - QString savePath = FileNameHandler().absoluteSavePath(); - QPixmap pixToSave; - if (selection.isEmpty()) { - pixToSave = m_modifiedScreenshot; - } else { // save full screen when no selection - pixToSave = m_modifiedScreenshot.copy(selection); - } - ok = pixToSave.save(savePath); - return savePath; +QPixmap Screenshot::croppedScreenshot(const QRect &selection) const { + return m_modifiedScreenshot.copy(selection); } // paintModification adds a new modification to the screenshot @@ -162,34 +105,4 @@ void Screenshot::paintInPainter(QPainter &painter, modification->tool()->processImage(painter, points, color); } -void Screenshot::uploadToImgur(QNetworkAccessManager *accessManager, - const QRect &selection) -{ - QString title ="flameshot_screenshot"; - QString description = FileNameHandler().parsedPattern(); - QPixmap pixToSave; - if (selection.isEmpty()) { - pixToSave = m_modifiedScreenshot; - } else { // save full screen when no selection - pixToSave = m_modifiedScreenshot.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/src/capture/screenshot.h b/src/capture/screenshot.h index 40a11459..3cc7968f 100644 --- a/src/capture/screenshot.h +++ b/src/capture/screenshot.h @@ -36,13 +36,8 @@ public: void setScreenshot(const QPixmap &); QPixmap baseScreenshot() const; QPixmap screenshot() const; + inline QPixmap croppedScreenshot(const QRect &selection) const; - QString graphicalSave(bool &ok, - const QRect &selection = QRect(), - QWidget *parent = 0) const; - QString fileSave(bool &ok, const QRect &selection = QRect()) const; - void uploadToImgur(QNetworkAccessManager *, - const QRect &selection = QRect()); QPixmap paintModification(const CaptureModification*); QPixmap paintTemporalModification(const CaptureModification*); QPixmap overrideModifications(const QVector &); @@ -50,7 +45,6 @@ public: private: QPixmap m_baseScreenshot; QPixmap m_modifiedScreenshot; - QPointer m_accessManager; inline void paintInPainter(QPainter &, const CaptureModification *); diff --git a/src/capture/tools/toolfactory.h b/src/capture/tools/toolfactory.h index 50dd0268..146d3c50 100644 --- a/src/capture/tools/toolfactory.h +++ b/src/capture/tools/toolfactory.h @@ -19,7 +19,7 @@ #define TOOLFACTORY_H #include -#include "src/capture/capturebutton.h" +#include "src/capture/widget/capturebutton.h" #include "src/capture/tools/capturetool.h" class CaptureTool; diff --git a/src/capture/buttonhandler.cpp b/src/capture/widget/buttonhandler.cpp similarity index 100% rename from src/capture/buttonhandler.cpp rename to src/capture/widget/buttonhandler.cpp diff --git a/src/capture/buttonhandler.h b/src/capture/widget/buttonhandler.h similarity index 100% rename from src/capture/buttonhandler.h rename to src/capture/widget/buttonhandler.h diff --git a/src/capture/capturebutton.cpp b/src/capture/widget/capturebutton.cpp similarity index 91% rename from src/capture/capturebutton.cpp rename to src/capture/widget/capturebutton.cpp index 7709aeb7..64885c66 100644 --- a/src/capture/capturebutton.cpp +++ b/src/capture/widget/capturebutton.cpp @@ -16,7 +16,7 @@ // along with Flameshot. If not, see . #include "capturebutton.h" -#include "src/capture/capturewidget.h" +#include "src/capture/widget/capturewidget.h" #include "src/utils/confighandler.h" #include "src/capture/tools/capturetool.h" #include "src/capture/tools/toolfactory.h" @@ -69,11 +69,11 @@ void CaptureButton::initButton() { setToolTip(m_tool->description()); - emergeAnimation = new QPropertyAnimation(this, "size", this); - emergeAnimation->setEasingCurve(QEasingCurve::InOutQuad); - emergeAnimation->setDuration(80); - emergeAnimation->setStartValue(QSize(0, 0)); - emergeAnimation->setEndValue(QSize(BUTTON_SIZE, BUTTON_SIZE)); + m_emergeAnimation = new QPropertyAnimation(this, "size", this); + m_emergeAnimation->setEasingCurve(QEasingCurve::InOutQuad); + m_emergeAnimation->setDuration(80); + m_emergeAnimation->setStartValue(QSize(0, 0)); + m_emergeAnimation->setEndValue(QSize(BUTTON_SIZE, BUTTON_SIZE)); } QVector CaptureButton::getIterableButtonTypes() { @@ -132,8 +132,8 @@ void CaptureButton::leaveEvent(QEvent *e) { } void CaptureButton::mouseReleaseEvent(QMouseEvent *e) { - CaptureWidget *parent = static_cast(this->parent()); - parent->mouseReleaseEvent(e); +// CaptureWidget *parent = static_cast(this->parent()); +// parent->mouseReleaseEvent(e); if (e->button() == Qt::LeftButton && m_pressed) { Q_EMIT pressedButton(this); } @@ -150,8 +150,8 @@ void CaptureButton::animatedShow() { if(!isVisible()) { setMouseTracking(false); show(); - emergeAnimation->start(); - connect(emergeAnimation, &QPropertyAnimation::finished, this, [this](){ + m_emergeAnimation->start(); + connect(m_emergeAnimation, &QPropertyAnimation::finished, this, [this](){ setMouseTracking(true); }); } diff --git a/src/capture/capturebutton.h b/src/capture/widget/capturebutton.h similarity index 98% rename from src/capture/capturebutton.h rename to src/capture/widget/capturebutton.h index c501783f..602bc537 100644 --- a/src/capture/capturebutton.h +++ b/src/capture/widget/capturebutton.h @@ -87,7 +87,7 @@ private: ButtonType m_buttonType; bool m_pressed; - QPropertyAnimation *emergeAnimation; + QPropertyAnimation *m_emergeAnimation; static QColor m_mainColor; diff --git a/src/capture/capturewidget.cpp b/src/capture/widget/capturewidget.cpp similarity index 86% rename from src/capture/capturewidget.cpp rename to src/capture/widget/capturewidget.cpp index 23d4c8fd..a6758489 100644 --- a/src/capture/capturewidget.cpp +++ b/src/capture/widget/capturewidget.cpp @@ -21,14 +21,15 @@ // Based on KDE's KSnapshot regiongrabber.cpp, revision 796531, Copyright 2007 Luca Gugelmann // released under the GNU LGPL -#include "screenshot.h" -#include "capturemodification.h" +#include "src/capture/screenshot.h" +#include "src/capture/capturemodification.h" #include "capturewidget.h" #include "capturebutton.h" -#include "src/capture/colorpicker.h" -#include "src/capture/screengrabber.h" +#include "src/capture/widget/colorpicker.h" +#include "src/utils/screengrabber.h" #include "src/utils/confighandler.h" #include "src/utils/systemnotification.h" +#include "src/core/controller.h" #include #include #include @@ -36,11 +37,6 @@ #include #include #include -#include -#include -#include -#include -#include // CaptureWidget is the main component used to capture the screen. It contains an // are of selection with its respective buttons. @@ -129,6 +125,14 @@ void CaptureWidget::updateButtons() { m_buttonHandler->setButtons(vectorButtons); } +QPixmap CaptureWidget::pixmap() { + if (m_selection.isNull()) { // copy full screen when no selection + return m_screenshot->screenshot(); + } else { + return m_screenshot->screenshot().copy(extendedSelection()); + } +} + void CaptureWidget::paintEvent(QPaintEvent *) { QPainter painter(this); @@ -378,91 +382,6 @@ void CaptureWidget::keyPressEvent(QKeyEvent *e) { } } -QString CaptureWidget::saveScreenshot(bool toClipboard) { - QString savePath, saveMessage; - SystemNotification notify; - if (toClipboard) { - if (m_selection.isNull()) { // copy full screen when no selection - QApplication::clipboard()->setPixmap(m_screenshot->screenshot()); - } else { - QApplication::clipboard()->setPixmap(m_screenshot->screenshot() - .copy(extendedSelection())); - } - } - bool ok = false; - if (m_forcedSavePath.isEmpty()) { - if (isVisible()) { - hide(); - } - savePath = m_screenshot->graphicalSave(ok, extendedSelection(), this); - } else { - ConfigHandler config; - config.setSavePath(m_forcedSavePath); - savePath = m_screenshot->fileSave(ok, extendedSelection()); - if(!ok || config.savePathValue() != m_forcedSavePath) { - saveMessage = tr("Error trying to save in ") + savePath; - notify.sendMessage(saveMessage); - } - } - if (ok) { - saveMessage = tr("Capture saved in ") + savePath; - notify.sendMessage(saveMessage); - } - close(); - return savePath; -} - -void CaptureWidget::copyScreenshot() { - if (m_selection.isNull()) { // copy full screen when no selection - QApplication::clipboard()->setPixmap(m_screenshot->screenshot()); - } else { - QApplication::clipboard()->setPixmap(m_screenshot->screenshot() - .copy(extendedSelection())); - } - 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); - bool successful = QDesktopServices::openUrl(url); - if (!successful) { - QMessageBox *openErrBox = - new QMessageBox(QMessageBox::Warning, - QObject::tr("Resource Error"), - QObject::tr("Unable to open the URL.")); - openErrBox->setModal(false); - openErrBox->setAttribute(Qt::WA_DeleteOnClose); - openErrBox->setWindowIcon(QIcon(":img/flameshot.png")); - openErrBox->show(); - } - } else { - QMessageBox *netErrBox = - new QMessageBox(QMessageBox::Warning, "Network Error", - reply->errorString()); - netErrBox->setModal(false); - netErrBox->setAttribute(Qt::WA_DeleteOnClose); - netErrBox->setWindowIcon(QIcon(":img/flameshot.png")); - netErrBox->show(); - } - close(); -} - -void CaptureWidget::uploadScreenshot() { - QNetworkAccessManager *am = new QNetworkAccessManager(this); - connect(am, &QNetworkAccessManager::finished, this, - &CaptureWidget::openURL); - if (m_selection.isNull()) { - m_screenshot->uploadToImgur(am); - } else { - m_screenshot->uploadToImgur(am, extendedSelection()); - } - hide(); - SystemNotification().sendMessage(tr("Uploading image...")); -} - bool CaptureWidget::undo() { bool itemRemoved = false; if (!m_modifications.isEmpty()) { @@ -520,7 +439,7 @@ void CaptureWidget::handleButtonSignal(CaptureTool::Request r) { undo(); break; case CaptureTool::REQ_UPLOAD_TO_IMGUR: - uploadScreenshot(); + uploadToImgur(); break; case CaptureTool::REQ_MOVE_MODE: m_state = CaptureButton::TYPE_MOVESELECTION; @@ -650,6 +569,25 @@ QRegion CaptureWidget::handleMask() const { return mask; } +void CaptureWidget::copyScreenshot() { + Controller::getInstance()->captureToClipboard(pixmap()); + close(); +} + +void CaptureWidget::saveScreenshot() { + if (m_forcedSavePath.isEmpty()) { + Controller::getInstance()->captureToFileUi(pixmap()); + } else { + Controller::getInstance()->captureToFile(pixmap(), m_forcedSavePath); + } + close(); +} + +void CaptureWidget::uploadToImgur() { + Controller::getInstance()->captureToImgur(pixmap()); + close(); +} + QRect CaptureWidget::extendedSelection() const { if (m_selection.isNull()) return QRect(); diff --git a/src/capture/capturewidget.h b/src/capture/widget/capturewidget.h similarity index 96% rename from src/capture/capturewidget.h rename to src/capture/widget/capturewidget.h index 31a0c38f..3c6340d1 100644 --- a/src/capture/capturewidget.h +++ b/src/capture/widget/capturewidget.h @@ -50,13 +50,12 @@ public: ~CaptureWidget(); void updateButtons(); -public slots: - QString saveScreenshot(bool toClipboard = false); - void handleButtonSignal(CaptureTool::Request r); + inline QPixmap pixmap(); private slots: void copyScreenshot(); - void openURL(QNetworkReply *reply); + void saveScreenshot(); + void uploadToImgur(); void leaveButton(); void enterButton(); bool undo(); @@ -67,6 +66,7 @@ private slots: void downResize(); void setState(CaptureButton *); + void handleButtonSignal(CaptureTool::Request r); protected: void paintEvent(QPaintEvent *); @@ -106,7 +106,6 @@ protected: QVector m_Handles; private: - void uploadScreenshot(); void initShortcuts(); void updateHandles(); void updateSizeIndicator(); diff --git a/src/capture/colorpicker.cpp b/src/capture/widget/colorpicker.cpp similarity index 100% rename from src/capture/colorpicker.cpp rename to src/capture/widget/colorpicker.cpp diff --git a/src/capture/colorpicker.h b/src/capture/widget/colorpicker.h similarity index 100% rename from src/capture/colorpicker.h rename to src/capture/widget/colorpicker.h diff --git a/src/capture/workers/graphicalscreenshotsaver.cpp b/src/capture/workers/graphicalscreenshotsaver.cpp new file mode 100644 index 00000000..7ff8d3b3 --- /dev/null +++ b/src/capture/workers/graphicalscreenshotsaver.cpp @@ -0,0 +1,94 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "graphicalscreenshotsaver.h" +#include "src/utils/confighandler.h" +#include "src/utils/systemnotification.h" +#include "src/utils/filenamehandler.h" +#include +#include +#include +#include +#include + +/* + * Añadir la captura de pantalla a la derecha y boton de copiar + * + */ + +GraphicalScreenshotSaver::GraphicalScreenshotSaver(const QPixmap &capture, + QWidget *parent) : + QWidget(parent), m_pixmap(capture) +{ + setAttribute(Qt::WA_DeleteOnClose); + setWindowTitle(QObject::tr("Save As")); + + new QShortcut(Qt::Key_Escape, this, SLOT(close())); + + m_layout = new QVBoxLayout(this); + m_fileDialog = new QFileDialog(); + initFileDialog(); + m_layout->addWidget(m_fileDialog); + +} + +void GraphicalScreenshotSaver::initFileDialog() { + m_fileDialog->setOption(QFileDialog::DontUseNativeDialog, true); + m_fileDialog->setFileMode(QFileDialog::AnyFile); + m_fileDialog->setAcceptMode(QFileDialog::AcceptSave); + QString fileName, directory; + FileNameHandler().absoluteSavePath(directory, fileName); + m_fileDialog->selectFile(fileName); + m_fileDialog->setDirectory(directory); + + QStringList mimeTypes; + for (const QByteArray &bf: QImageWriter::supportedMimeTypes()) + mimeTypes.append(QLatin1String(bf)); + m_fileDialog->setMimeTypeFilters(mimeTypes); + m_fileDialog->selectMimeTypeFilter("image/png"); + m_fileDialog->setDefaultSuffix("png"); + + connect(m_fileDialog, &QFileDialog::rejected, + this, &GraphicalScreenshotSaver::close); + connect(m_fileDialog, &QFileDialog::accepted, + this, &GraphicalScreenshotSaver::checkSaveAcepted); +} + +void GraphicalScreenshotSaver::showErrorMessage(const QString &msg) { + QMessageBox saveErrBox( + QMessageBox::Warning, + QObject::tr("Save Error"), + msg); + saveErrBox.setWindowIcon(QIcon(":img/flameshot.png")); + saveErrBox.exec(); +} + +void GraphicalScreenshotSaver::checkSaveAcepted() { + m_fileDialog->show(); + QString path = m_fileDialog->selectedFiles().first(); + bool ok = m_pixmap.save(path); + if (ok) { + QString pathNoFile = path.left(path.lastIndexOf("/")); + ConfigHandler().setSavePath(pathNoFile); + QString msg = QObject::tr("Capture saved as ") + path; + SystemNotification().sendMessage(msg); + close(); + } else { + QString msg = QObject::tr("Error trying to save as ") + path; + showErrorMessage(msg); + } +} diff --git a/src/capture/workers/graphicalscreenshotsaver.h b/src/capture/workers/graphicalscreenshotsaver.h new file mode 100644 index 00000000..3dd8234e --- /dev/null +++ b/src/capture/workers/graphicalscreenshotsaver.h @@ -0,0 +1,43 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef GRAPHICALSCREENSHOTSAVER_H +#define GRAPHICALSCREENSHOTSAVER_H + +#include + +class QFileDialog; +class QVBoxLayout; + +class GraphicalScreenshotSaver : public QWidget +{ + Q_OBJECT +public: + explicit GraphicalScreenshotSaver(const QPixmap &capture, + QWidget *parent = nullptr); + +private: + QPixmap m_pixmap; + QFileDialog *m_fileDialog; + QVBoxLayout *m_layout; + + void initFileDialog(); + void showErrorMessage(const QString &msg); + void checkSaveAcepted(); +}; + +#endif // GRAPHICALSCREENSHOTSAVER_H diff --git a/src/capture/workers/imgur/imagelabel.cpp b/src/capture/workers/imgur/imagelabel.cpp new file mode 100644 index 00000000..9aaaa26d --- /dev/null +++ b/src/capture/workers/imgur/imagelabel.cpp @@ -0,0 +1,87 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +// This code is a modified version of the KDE software Spectacle +// /src/Gui/KSImageWidget.cpp commit cbbd6d45f6426ccbf1a82b15fdf98613ccccbbe9 + +#include "imagelabel.h" + +ImageLabel::ImageLabel(QWidget *parent): + QLabel(parent), m_pixmap(QPixmap()) +{ + m_DSEffect = new QGraphicsDropShadowEffect(this); + + m_DSEffect->setBlurRadius(5); + m_DSEffect->setOffset(0); + m_DSEffect->setColor(QColor(Qt::black)); + + setGraphicsEffect(m_DSEffect); + setCursor(Qt::OpenHandCursor); + setAlignment(Qt::AlignCenter); + setMinimumSize(size()); +} + +void ImageLabel::setScreenshot(const QPixmap &pixmap) { + m_pixmap = pixmap; + const QString tooltip = QString("%1x%2 px").arg(m_pixmap.width()) + .arg(m_pixmap.height()); + setToolTip(tooltip); + setScaledPixmap(); +} + +void ImageLabel::setScaledPixmap() { + const qreal scale = qApp->devicePixelRatio(); + QPixmap scaledPixmap = m_pixmap.scaled(size() * scale, Qt::KeepAspectRatio, + Qt::SmoothTransformation); + scaledPixmap.setDevicePixelRatio(scale); + setPixmap(scaledPixmap); +} + +// drag handlers + +void ImageLabel::mousePressEvent(QMouseEvent *event) { + if (event->button() == Qt::LeftButton) { + m_dragStartPosition = event->pos(); + setCursor(Qt::ClosedHandCursor); + } +} + +void ImageLabel::mouseReleaseEvent(QMouseEvent *event) { + if (event->button() == Qt::LeftButton) { + setCursor(Qt::OpenHandCursor); + } +} + +void ImageLabel::mouseMoveEvent(QMouseEvent *event) +{ + if (!(event->buttons() & Qt::LeftButton)) { + return; + } + if ((event->pos() - m_dragStartPosition).manhattanLength() < + QGuiApplication::styleHints()->startDragDistance()) + { + return; + } + setCursor(Qt::OpenHandCursor); + emit dragInitiated(); +} + +// resize handler +void ImageLabel::resizeEvent(QResizeEvent *event) { + Q_UNUSED(event); + setScaledPixmap(); +} diff --git a/src/capture/workers/imgur/imagelabel.h b/src/capture/workers/imgur/imagelabel.h new file mode 100644 index 00000000..c949f766 --- /dev/null +++ b/src/capture/workers/imgur/imagelabel.h @@ -0,0 +1,59 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +// This code is a modified version of the KDE software Spectacle +// /src/Gui/KSImageWidget.h commit cbbd6d45f6426ccbf1a82b15fdf98613ccccbbe9 + +#ifndef IMAGELABEL_H +#define IMAGELABEL_H + +#include +#include +#include +#include +#include +#include +#include +#include + +class ImageLabel : public QLabel +{ + Q_OBJECT + +public: + explicit ImageLabel(QWidget *parent = 0); + void setScreenshot(const QPixmap &pixmap); + +signals: + void dragInitiated(); + +protected: + void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; + +private: + void setScaledPixmap(); + + QGraphicsDropShadowEffect *m_DSEffect; + QPixmap m_pixmap; + QPoint m_dragStartPosition; + +}; + +#endif // IMAGELABEL_H diff --git a/src/capture/workers/imgur/imguruploader.cpp b/src/capture/workers/imgur/imguruploader.cpp new file mode 100644 index 00000000..664b9b5c --- /dev/null +++ b/src/capture/workers/imgur/imguruploader.cpp @@ -0,0 +1,161 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "imguruploader.h" +#include "src/utils/filenamehandler.h" +#include "src/utils/systemnotification.h" +#include "src/capture/workers/imgur/loadspinner.h" +#include "src/capture/workers/imgur/imagelabel.h" +#include "src/capture/workers/imgur/notificationwidget.h" +#include "src/utils/confighandler.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +ImgurUploader::ImgurUploader(const QPixmap &capture, QWidget *parent) : + QWidget(parent), m_pixmap(capture) +{ + setWindowTitle(tr("Upload to Imgur")); + + m_spinner = new LoadSpinner(this); + m_spinner->setColor(ConfigHandler().uiMainColorValue()); + m_spinner->start(); + + m_infoLabel = new QLabel(tr("Uploading Image")); + + m_vLayout = new QVBoxLayout(); + setLayout(m_vLayout); + m_vLayout->addWidget(m_spinner, 0, Qt::AlignHCenter); + m_vLayout->addWidget(m_infoLabel); + + m_NetworkAM = new QNetworkAccessManager(this); + connect(m_NetworkAM, &QNetworkAccessManager::finished, this, + &ImgurUploader::handleReply); + + setAttribute(Qt::WA_DeleteOnClose); + + upload(); + // QTimer::singleShot(2000, this, &ImgurUploader::onUploadOk); // testing +} + +void ImgurUploader::handleReply(QNetworkReply *reply) { + m_spinner->deleteLater(); + 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); + m_imageURL.setUrl(url); + onUploadOk(); + } else { + m_infoLabel->setText(reply->errorString()); + } + new QShortcut(Qt::Key_Escape, this, SLOT(close())); +} + +void ImgurUploader::startDrag() { + QMimeData *mimeData = new QMimeData; + mimeData->setUrls(QList { m_imageURL }); + mimeData->setImageData(m_pixmap); + + QDrag *dragHandler = new QDrag(this); + dragHandler->setMimeData(mimeData); + dragHandler->setPixmap(m_pixmap.scaled(256, 256, Qt::KeepAspectRatioByExpanding, + Qt::SmoothTransformation)); + dragHandler->exec(); +} + +void ImgurUploader::upload() { + QByteArray byteArray; + QBuffer buffer(&byteArray); + m_pixmap.save(&buffer, "PNG"); + + QUrlQuery urlQuery; + urlQuery.addQueryItem("title", "flameshot_screenshot"); + QString description = FileNameHandler().parsedPattern(); + urlQuery.addQueryItem("description", description); + + QUrl url("https://api.imgur.com/3/image"); + url.setQuery(urlQuery); + QNetworkRequest request(url); + request.setHeader(QNetworkRequest::ContentTypeHeader, + "application/application/x-www-form-urlencoded"); + request.setRawHeader("Authorization", "Client-ID 313baf0c7b4d3ff"); + + m_NetworkAM->post(request, byteArray); +} + +void ImgurUploader::onUploadOk() { + m_infoLabel->deleteLater(); + + m_notification = new NotificationWidget(); + m_vLayout->addWidget(m_notification); + + ImageLabel *imageLabel = new ImageLabel(); + imageLabel->setScreenshot(m_pixmap); + imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + connect(imageLabel, &ImageLabel::dragInitiated, this, &ImgurUploader::startDrag); + m_vLayout->addWidget(imageLabel); + + m_hLayout = new QHBoxLayout(); + m_vLayout->addLayout(m_hLayout); + + m_copyUrlButton = new QPushButton(tr("Copy URL")); + m_openUrlButton = new QPushButton(tr("Open URL")); + m_toClipboardButton = new QPushButton(tr("Image to Clipboard.")); + m_hLayout->addWidget(m_copyUrlButton); + m_hLayout->addWidget(m_openUrlButton); + m_hLayout->addWidget(m_toClipboardButton); + + connect(m_copyUrlButton, &QPushButton::clicked, + this, &ImgurUploader::copyURL); + connect(m_openUrlButton, &QPushButton::clicked, + this, &ImgurUploader::openURL); + connect(m_toClipboardButton, &QPushButton::clicked, + this, &ImgurUploader::copyImage); + +} + +void ImgurUploader::openURL() { + bool successful = QDesktopServices::openUrl(m_imageURL); + if (!successful) { + m_notification->showMessage(tr("Unable to open the URL.")); + } +} + +void ImgurUploader::copyURL() { + QApplication::clipboard()->setText(m_imageURL.toString()); + m_notification->showMessage(tr("URL copied to clipboard.")); +} + +void ImgurUploader::copyImage() { + QApplication::clipboard()->setPixmap(m_pixmap); + m_notification->showMessage(tr("Screenshot copied to clipboard.")); +} + diff --git a/src/capture/workers/imgur/imguruploader.h b/src/capture/workers/imgur/imguruploader.h new file mode 100644 index 00000000..2387db90 --- /dev/null +++ b/src/capture/workers/imgur/imguruploader.h @@ -0,0 +1,68 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef IMGURUPLOADER_H +#define IMGURUPLOADER_H + +#include +#include + +class QNetworkReply; +class QNetworkAccessManager; +class QHBoxLayout; +class QVBoxLayout; +class QLabel; +class LoadSpinner; +class QPushButton; +class QUrl; +class NotificationWidget; + +class ImgurUploader : public QWidget +{ + Q_OBJECT +public: + explicit ImgurUploader(const QPixmap &p, QWidget *parent = nullptr); + +private slots: + void handleReply(QNetworkReply *reply); + void startDrag(); + + void openURL(); + void copyURL(); + void copyImage(); + +private: + QPixmap m_pixmap; + QNetworkAccessManager *m_NetworkAM; + + QVBoxLayout *m_vLayout; + QHBoxLayout *m_hLayout; + // loading + QLabel *m_infoLabel; + LoadSpinner *m_spinner; + // uploaded + QPushButton *m_openUrlButton; + QPushButton *m_copyUrlButton; + QPushButton *m_toClipboardButton; + QUrl m_imageURL; + NotificationWidget *m_notification; + + void upload(); + void onUploadOk(); +}; + +#endif // IMGURUPLOADER_H diff --git a/src/capture/workers/imgur/loadspinner.cpp b/src/capture/workers/imgur/loadspinner.cpp new file mode 100644 index 00000000..6dd95484 --- /dev/null +++ b/src/capture/workers/imgur/loadspinner.cpp @@ -0,0 +1,94 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "loadspinner.h" +#include +#include +#include + +#define OFFSET 5 + +LoadSpinner::LoadSpinner(QWidget *parent) : + QWidget(parent), m_startAngle(0), m_span(0), m_growing(true) +{ + setAttribute(Qt::WA_TranslucentBackground); + setFixedSize(100, 100); + updateFrame(); + // init timer + m_timer = new QTimer(this); + connect(m_timer, SIGNAL(timeout()), this, SLOT(rotate())); + m_timer->setInterval(30); +} + +void LoadSpinner::setColor(const QColor &c) { + m_color = c; +} + +void LoadSpinner::setWidth(int w) { + setFixedSize(w, w); + updateFrame(); +} + +void LoadSpinner::setHeight(int h) { + setFixedSize(h, h); + updateFrame(); +} + +void LoadSpinner::start() { + m_timer->start(); +} + +void LoadSpinner::stop() { + m_timer->stop(); +} + +void LoadSpinner::paintEvent(QPaintEvent *) { + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing, true); + auto pen = QPen(m_color); + + pen.setWidth(5); + painter.setPen(pen); + painter.setOpacity(0.2); + painter.drawArc(m_frame, 0, 5760); + painter.setOpacity(1.0); + painter.drawArc(m_frame, (m_startAngle * 16), (m_span * 16)); + +} + +void LoadSpinner::rotate() { + const int advance = 3; + const int grow = 8; + if (m_growing) { + m_startAngle = (m_startAngle + advance) % 360; + m_span += grow; + if(m_span > 260) { + m_growing = false; + } + } else { + m_startAngle = (m_startAngle + grow) % 360; + m_span = m_span + advance - grow; + if(m_span < 10) { + m_growing = true; + } + } + update(); +} + +void LoadSpinner::updateFrame() { + m_frame = QRect(OFFSET, OFFSET, width() - OFFSET*2, height() - OFFSET*2); +} diff --git a/src/capture/workers/imgur/loadspinner.h b/src/capture/workers/imgur/loadspinner.h new file mode 100644 index 00000000..bf884693 --- /dev/null +++ b/src/capture/workers/imgur/loadspinner.h @@ -0,0 +1,53 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef LOADSPINNER_H +#define LOADSPINNER_H + +#include + +class LoadSpinner : public QWidget +{ + Q_OBJECT +public: + explicit LoadSpinner(QWidget *parent = nullptr); + + void setColor(const QColor &c); + void setWidth(int w); + void setHeight(int h); + void start(); + void stop(); + +protected: + void paintEvent(QPaintEvent *); + +private slots: + void rotate(); + +private: + QColor m_color; + QTimer *m_timer; + + int m_startAngle = 0; + int m_span =180; + bool m_growing; + + QRect m_frame; + void updateFrame(); +}; + +#endif // LOADSPINNER_H diff --git a/src/capture/workers/imgur/notificationwidget.cpp b/src/capture/workers/imgur/notificationwidget.cpp new file mode 100644 index 00000000..b1177f36 --- /dev/null +++ b/src/capture/workers/imgur/notificationwidget.cpp @@ -0,0 +1,73 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "notificationwidget.h" +#include +#include +#include +#include +#include +#include +#include + +NotificationWidget::NotificationWidget(QWidget *parent) : QWidget(parent) +{ + m_timer = new QTimer(this); + m_timer->setSingleShot(true); + m_timer->setInterval(7000); + connect(m_timer, &QTimer::timeout, this, &NotificationWidget::animatedHide); + + m_content = new QFrame(); + m_layout = new QVBoxLayout(); + m_label = new QLabel(m_content); + m_label->hide(); + + m_showAnimation = new QPropertyAnimation(m_content, "geometry", this); + m_showAnimation->setDuration(300); + + m_hideAnimation = new QPropertyAnimation(m_content, "geometry", this); + m_hideAnimation->setDuration(300); + connect(m_hideAnimation, &QPropertyAnimation::finished, m_label, &QLabel::hide); + + auto mainLayout = new QVBoxLayout(); + setLayout(mainLayout); + + mainLayout->addWidget(m_content); + m_layout->addWidget(m_label, 0, Qt::AlignHCenter); + m_content->setLayout(m_layout); + + setFixedHeight(40); +} + +void NotificationWidget::showMessage(const QString &msg) { + m_label->setText(msg); + m_label->show(); + animatedShow(); +} + +void NotificationWidget::animatedShow() { + m_showAnimation->setStartValue(QRect(0, 0, width(), 0)); + m_showAnimation->setEndValue(QRect(0, 0, width(), height())); + m_showAnimation->start(); + m_timer->start(); +} + +void NotificationWidget::animatedHide() { + m_hideAnimation->setStartValue(QRect(0, 0, width(), height())); + m_hideAnimation->setEndValue(QRect(0, 0, width(), 0)); + m_hideAnimation->start(); +} diff --git a/src/capture/workers/imgur/notificationwidget.h b/src/capture/workers/imgur/notificationwidget.h new file mode 100644 index 00000000..bce68087 --- /dev/null +++ b/src/capture/workers/imgur/notificationwidget.h @@ -0,0 +1,50 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef NOTIFICATIONWIDGET_H +#define NOTIFICATIONWIDGET_H + +#include + +class QLabel; +class QTimer; +class QPropertyAnimation; +class QVBoxLayout; +class QFrame; + +class NotificationWidget : public QWidget +{ + Q_OBJECT +public: + explicit NotificationWidget(QWidget *parent = nullptr); + + void showMessage(const QString &msg); + +private: + QLabel *m_label; + QPropertyAnimation *m_showAnimation; + QPropertyAnimation *m_hideAnimation; + QVBoxLayout *m_layout; + QFrame *m_content; + QTimer *m_timer; + + void animatedShow(); + void animatedHide(); + +}; + +#endif // NOTIFICATIONWIDGET_H diff --git a/src/capture/workers/screenshotsaver.cpp b/src/capture/workers/screenshotsaver.cpp new file mode 100644 index 00000000..d746b2ac --- /dev/null +++ b/src/capture/workers/screenshotsaver.cpp @@ -0,0 +1,51 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#include "screenshotsaver.h" +#include "src/utils/systemnotification.h" +#include "src/utils/filenamehandler.h" +#include "src/utils/confighandler.h" +#include +#include +#include + +ScreenshotSaver::ScreenshotSaver() +{ + +} + +void ScreenshotSaver::saveToClipboard(const QPixmap &capture) { + QApplication::clipboard()->setPixmap(capture); +} + +void ScreenshotSaver::saveToFilesystem(const QPixmap &capture, + const QString &path) +{ + QString completePath = FileNameHandler().generateAbsolutePath(path); + completePath += ".png"; + bool ok = capture.save(completePath); + QString saveMessage; + if (ok) { + ConfigHandler().setSavePath(path); + saveMessage = QObject::tr("Capture saved as ") + completePath; + } else { + saveMessage = QObject::tr("Error trying to save as ") + completePath; + } + SystemNotification().sendMessage(saveMessage); +} + + diff --git a/src/capture/workers/screenshotsaver.h b/src/capture/workers/screenshotsaver.h new file mode 100644 index 00000000..7388da49 --- /dev/null +++ b/src/capture/workers/screenshotsaver.h @@ -0,0 +1,34 @@ +// Copyright 2017 Alejandro Sirgo Rica +// +// This file is part of Flameshot. +// +// Flameshot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Flameshot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Flameshot. If not, see . + +#ifndef SCREENSHOTSAVER_H +#define SCREENSHOTSAVER_H + +class QPixmap; +class QString; + +class ScreenshotSaver +{ +public: + ScreenshotSaver(); + + void saveToClipboard(const QPixmap &capture); + void saveToFilesystem(const QPixmap &capture, const QString &path); + +}; + +#endif // SCREENSHOTSAVER_H diff --git a/src/config/buttonlistview.h b/src/config/buttonlistview.h index e9586164..28caa3a2 100644 --- a/src/config/buttonlistview.h +++ b/src/config/buttonlistview.h @@ -18,8 +18,8 @@ #ifndef BUTTONLISTVIEW_H #define BUTTONLISTVIEW_H +#include "src/capture/widget/capturebutton.h" #include -#include "src/capture/capturebutton.h" class ButtonListView : public QListWidget { public: diff --git a/src/config/configwindow.cpp b/src/config/configwindow.cpp index d95541e6..c1e197fb 100644 --- a/src/config/configwindow.cpp +++ b/src/config/configwindow.cpp @@ -16,7 +16,7 @@ // along with Flameshot. If not, see . #include "configwindow.h" -#include "src/capture/capturebutton.h" +#include "src/capture/widget/capturebutton.h" #include "src/config/buttonlistview.h" #include "src/config/uicoloreditor.h" #include "src/config/geneneralconf.h" diff --git a/src/config/filenameeditor.cpp b/src/config/filenameeditor.cpp index d2380be4..5e8b8b1b 100644 --- a/src/config/filenameeditor.cpp +++ b/src/config/filenameeditor.cpp @@ -92,7 +92,7 @@ void FileNameEditor::initWidgets() { void FileNameEditor::savePattern() { QString pattern = m_nameEditor->text(); - m_nameHandler->savePattern(pattern); + m_nameHandler->setPattern(pattern); } void FileNameEditor::showParsedPattern(const QString &p) { diff --git a/src/config/uicoloreditor.h b/src/config/uicoloreditor.h index 3a7c342b..ea377a8c 100644 --- a/src/config/uicoloreditor.h +++ b/src/config/uicoloreditor.h @@ -19,7 +19,7 @@ #define UICOLORPICKER_H #include "color_wheel.hpp" -#include "src/capture/capturebutton.h" +#include "src/capture/widget/capturebutton.h" #include class QVBoxLayout; diff --git a/src/core/controller.cpp b/src/core/controller.cpp index 6df7ddd2..b1f07ec1 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -16,11 +16,15 @@ // along with Flameshot. If not, see . #include "controller.h" -#include "src/capture/capturewidget.h" +#include "src/capture/widget/capturewidget.h" #include "src/utils/confighandler.h" #include "src/infowindow.h" #include "src/config/configwindow.h" -#include "src/capture/capturebutton.h" +#include "src/capture/widget/capturebutton.h" +#include "src/utils/screengrabber.h" +#include "src/capture/workers/imgur/imguruploader.h" +#include "src/capture/workers/screenshotsaver.h" +#include "src/capture/workers/graphicalscreenshotsaver.h" #include #include #include @@ -51,10 +55,17 @@ Controller *Controller::getInstance() { return &c; } -QString Controller::saveScreenshot(const QString &path, +void Controller::saveFullScreenshot(const QString &path, bool const toClipboard) { - QPointer w = createCaptureWidget(path); - return w->saveScreenshot(toClipboard); + QPixmap p(ScreenGrabber().grabEntireDesktop()); + if(toClipboard) { + captureToClipboard(p); + } + if(path.isEmpty()) { + captureToFileUi(p); + } else { + captureToFile(p, path); + } } // initDefaults inits the global config in the first execution of the program @@ -66,16 +77,10 @@ void Controller::initDefaults() { } } -// creation of a new capture -QPointer Controller::createCaptureWidget(const QString &forcedSavePath) { - QPointer w = new CaptureWidget(forcedSavePath); - return w; -} - // creation of a new capture in GUI mode void Controller::createVisualCapture(const QString &forcedSavePath) { if (!m_captureWindow) { - m_captureWindow = createCaptureWidget(forcedSavePath); + m_captureWindow = new CaptureWidget(forcedSavePath); m_captureWindow->showFullScreen(); } } @@ -142,3 +147,21 @@ void Controller::updateConfigComponents() { m_configWindow->updateComponents(); } } + +void Controller::captureToClipboard(const QPixmap &p) { + ScreenshotSaver().saveToClipboard(p); +} + +void Controller::captureToFile(const QPixmap &p, const QString &path) { + ScreenshotSaver().saveToFilesystem(p, path); +} + +void Controller::captureToFileUi(const QPixmap &p) { + auto w = new GraphicalScreenshotSaver(p); + w->show(); +} + +void Controller::captureToImgur(const QPixmap &p) { + auto w = new ImgurUploader(p); + w->show(); +} diff --git a/src/core/controller.h b/src/core/controller.h index 83086d30..53b75a21 100644 --- a/src/core/controller.h +++ b/src/core/controller.h @@ -36,7 +36,7 @@ public: void operator =(const Controller&) = delete; public slots: - QString saveScreenshot(const QString &path = QString(), + void saveFullScreenshot(const QString &path = QString(), bool const toClipboard = false); void createVisualCapture(const QString &forcedSavePath = QString()); @@ -48,15 +48,18 @@ public slots: void updateConfigComponents(); + void captureToClipboard(const QPixmap &p); + void captureToFile(const QPixmap &p, const QString &path); + void captureToFileUi(const QPixmap &p); + void captureToImgur(const QPixmap &p); + + private slots: void initDefaults(); private: Controller(); - QPointer createCaptureWidget( - const QString &forcedSavePath = QString()); - QPointer m_captureWindow; QPointer m_infoWindow; QPointer m_configWindow; diff --git a/src/core/flameshotdbusadapter.cpp b/src/core/flameshotdbusadapter.cpp index 3ebaafce..d08baaa9 100644 --- a/src/core/flameshotdbusadapter.cpp +++ b/src/core/flameshotdbusadapter.cpp @@ -41,7 +41,7 @@ void FlameshotDBusAdapter::graphicCapture(QString path, int delay) { void FlameshotDBusAdapter::fullScreen(QString path, bool toClipboard, int delay) { auto controller = Controller::getInstance(); auto f = [controller, path, toClipboard, this]() { - controller->saveScreenshot(path, toClipboard); + controller->saveFullScreenshot(path, toClipboard); }; QTimer::singleShot(delay, controller, f); diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index 1161cc76..28d35503 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -18,7 +18,7 @@ #ifndef CONFIGHANDLER_H #define CONFIGHANDLER_H -#include "src/capture/capturebutton.h" +#include "src/capture/widget/capturebutton.h" #include #include diff --git a/src/utils/filenamehandler.cpp b/src/utils/filenamehandler.cpp index 22e46d09..cb6187b3 100644 --- a/src/utils/filenamehandler.cpp +++ b/src/utils/filenamehandler.cpp @@ -44,44 +44,32 @@ QString FileNameHandler::parseFilename(const QString &name) { res = QString::fromLocal8Bit(data, strlen(data)); free(tempData); } + // add the parsed pattern in a correct format for the filesystem + res = res.replace("/", "⁄"); return res; } -void FileNameHandler::savePattern(const QString &pattern) { +QString FileNameHandler::generateAbsolutePath(const QString &path) +{ + QString directory = path; + QString filename = parsedPattern(); + fixPath(directory, filename); + return directory + filename; +} +// path a images si no existe, add numeration +void FileNameHandler::setPattern(const QString &pattern) { ConfigHandler().setFilenamePattern(pattern); } -QString FileNameHandler::absoluteSavePath() { +QString FileNameHandler::absoluteSavePath(QString &directory, QString &filename) { ConfigHandler config; - QString savePath = config.savePathValue(); - bool changed = false; - if (savePath.isEmpty() || !QDir(savePath).exists() || !QFileInfo(savePath).isWritable()) { - changed = true; - savePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); + directory = config.savePathValue(); + if (directory.isEmpty() || !QDir(directory).exists() || !QFileInfo(directory).isWritable()) { + directory = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); } - if (changed) { - config.setSavePath(savePath); - } - // first add slash if needed - QString tempName = savePath.endsWith("/") ? "" : "/"; - // add the parsed pattern in a correct format for the filesystem - tempName += FileNameHandler().parsedPattern().replace("/", "⁄"); - // find unused name adding _n where n is a number - QFileInfo checkFile(savePath + tempName + ".png"); - if (checkFile.exists()) { - tempName += "_"; - int i = 1; - while (true) { - checkFile.setFile( - savePath + tempName + QString::number(i) + ".png"); - if (!checkFile.exists()) { - tempName += QString::number(i); - break; - } - ++i; - } - } - return savePath + tempName + ".png"; + filename = parsedPattern(); + fixPath(directory, filename); + return directory + filename; } QString FileNameHandler::charArrToQString(const char *c) { @@ -92,3 +80,26 @@ char * FileNameHandler::QStringTocharArr(const QString &s) { QByteArray ba = s.toLocal8Bit(); return const_cast(strdup(ba.constData())); } + +void FileNameHandler::fixPath(QString &directory, QString &filename) { + // add '/' at the end of the directory + if (!directory.endsWith("/")) { + directory += "/"; + } + // add numeration in case of repeated filename in the directory + // find unused name adding _n where n is a number + QFileInfo checkFile(directory + filename + ".png"); + if (checkFile.exists()) { + filename += "_"; + int i = 1; + while (true) { + checkFile.setFile( + directory + filename + QString::number(i) + ".png"); + if (!checkFile.exists()) { + filename += QString::number(i); + break; + } + ++i; + } + } +} diff --git a/src/utils/filenamehandler.h b/src/utils/filenamehandler.h index 7e8d5f29..c19752d4 100644 --- a/src/utils/filenamehandler.h +++ b/src/utils/filenamehandler.h @@ -29,18 +29,21 @@ public: QString parsedPattern(); QString parseFilename(const QString &name); + QString generateAbsolutePath(const QString &path); + QString absoluteSavePath(QString &directory, QString &filename); static const int MAX_CHARACTERS = 70; public slots: - void savePattern(const QString &pattern); - QString absoluteSavePath(); + void setPattern(const QString &pattern); private: //using charArr = char[MAX_CHARACTERS]; inline QString charArrToQString(const char *c); inline char * QStringTocharArr(const QString &s); + void fixPath(QString &directory, QString &filename); + }; #endif // FILENAMEHANDLER_H diff --git a/src/capture/screengrabber.cpp b/src/utils/screengrabber.cpp similarity index 100% rename from src/capture/screengrabber.cpp rename to src/utils/screengrabber.cpp diff --git a/src/capture/screengrabber.h b/src/utils/screengrabber.h similarity index 100% rename from src/capture/screengrabber.h rename to src/utils/screengrabber.h