diff --git a/src/config/geneneralconf.cpp b/src/config/geneneralconf.cpp index 92bcb0eb..ec480793 100644 --- a/src/config/geneneralconf.cpp +++ b/src/config/geneneralconf.cpp @@ -18,16 +18,18 @@ #include "geneneralconf.h" #include "src/core/controller.h" #include "src/utils/confighandler.h" +#include "src/utils/filenamehandler.h" #include #include #include #include #include +#include #include #include +#include #include #include - GeneneralConf::GeneneralConf(QWidget* parent) : QWidget(parent) { @@ -39,6 +41,7 @@ GeneneralConf::GeneneralConf(QWidget* parent) initAutostart(); initCloseAfterCapture(); initCopyAndCloseAfterUpload(); + initSaveAfterCopy(); // this has to be at the end initConfingButtons(); @@ -55,6 +58,14 @@ GeneneralConf::updateComponents() m_closeAfterCapture->setChecked(config.closeAfterScreenshotValue()); m_copyAndCloseAfterUpload->setChecked( config.copyAndCloseAfterUploadEnabled()); + m_saveAfterCopy->setChecked(config.saveAfterCopyValue()); + + if (!config.saveAfterCopyPathValue().isEmpty()) { + m_savePath->setText(config.saveAfterCopyPathValue()); + } else { + ConfigHandler().setSaveAfterCopyPath( + QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)); + } #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) m_showTray->setChecked(!config.disabledTrayIconValue()); @@ -281,4 +292,62 @@ GeneneralConf::initCopyAndCloseAfterUpload() connect(m_copyAndCloseAfterUpload, &QCheckBox::clicked, [](bool checked) { ConfigHandler().setCopyAndCloseAfterUploadEnabled(checked); }); -} \ No newline at end of file +} + +void +GeneneralConf::initSaveAfterCopy() +{ + m_saveAfterCopy = new QCheckBox(tr("Save image after copy"), this); + m_saveAfterCopy->setToolTip(tr("Save image file after copying it")); + m_layout->addWidget(m_saveAfterCopy); + connect(m_saveAfterCopy, + &QCheckBox::clicked, + this, + &GeneneralConf::saveAfterCopyChanged); + + QHBoxLayout* pathLayout = new QHBoxLayout(); + m_layout->addStretch(); + QGroupBox* box = new QGroupBox(tr("Save Path")); + box->setFlat(true); + box->setLayout(pathLayout); + m_layout->addWidget(box); + + m_savePath = new QLineEdit( + QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), this); + m_savePath->setDisabled(true); + QString foreground = this->palette().foreground().color().name(); + m_savePath->setStyleSheet(QStringLiteral("color: %1").arg(foreground)); + pathLayout->addWidget(m_savePath); + + m_changeSaveButton = new QPushButton(tr("Change..."), this); + pathLayout->addWidget(m_changeSaveButton); + connect(m_changeSaveButton, + &QPushButton::clicked, + this, + &GeneneralConf::changeSavePath); +} + +void +GeneneralConf::saveAfterCopyChanged(bool checked) +{ + ConfigHandler().setSaveAfterCopy(checked); +} + +void +GeneneralConf::changeSavePath() +{ + QString path = QFileDialog::getExistingDirectory( + this, + tr("Choose a Folder"), + QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), + QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); + if (path.isEmpty()) { + return; + } + if (!QFileInfo(path).isWritable()) { + QMessageBox::about(this, tr("Error"), tr("Unable to write to directory.")); + return; + } + m_savePath->setText(path); + ConfigHandler().setSaveAfterCopyPath(path); +} diff --git a/src/config/geneneralconf.h b/src/config/geneneralconf.h index 2bc6e436..7dfe47a1 100644 --- a/src/config/geneneralconf.h +++ b/src/config/geneneralconf.h @@ -22,6 +22,8 @@ class QVBoxLayout; class QCheckBox; class QPushButton; +class QLabel; +class QLineEdit; class GeneneralConf : public QWidget { @@ -38,6 +40,8 @@ private slots: void showTrayIconChanged(bool checked); void autostartChanged(bool checked); void closeAfterCaptureChanged(bool checked); + void saveAfterCopyChanged(bool checked); + void changeSavePath(); void importConfiguration(); void exportFileConfiguration(); void resetConfiguration(); @@ -53,6 +57,9 @@ private: QPushButton* m_importButton; QPushButton* m_exportButton; QPushButton* m_resetButton; + QCheckBox* m_saveAfterCopy; + QLineEdit* m_savePath; + QPushButton* m_changeSaveButton; void initShowHelp(); void initShowDesktopNotification(); @@ -61,4 +68,5 @@ private: void initAutostart(); void initCloseAfterCapture(); void initCopyAndCloseAfterUpload(); + void initSaveAfterCopy(); }; diff --git a/src/core/capturerequest.cpp b/src/core/capturerequest.cpp index 9aac9374..e493cb77 100644 --- a/src/core/capturerequest.cpp +++ b/src/core/capturerequest.cpp @@ -95,7 +95,7 @@ CaptureRequest::exportCapture(const QPixmap& p) if (m_path.isEmpty()) { ScreenshotSaver().saveToFilesystemGUI(p); } else { - ScreenshotSaver().saveToFilesystem(p, m_path); + ScreenshotSaver().saveToFilesystem(p, m_path, ""); } } diff --git a/src/tools/save/savetool.cpp b/src/tools/save/savetool.cpp index 28a5a629..80c6ceb5 100644 --- a/src/tools/save/savetool.cpp +++ b/src/tools/save/savetool.cpp @@ -71,7 +71,7 @@ SaveTool::pressed(const CaptureContext& context) } } else { bool ok = ScreenshotSaver().saveToFilesystem( - context.selectedScreenshotArea(), context.savePath); + context.selectedScreenshotArea(), context.savePath, ""); if (ok) { emit requestAction(REQ_CAPTURE_DONE_OK); } diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index 04f1a2ad..cc33fdd6 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -391,6 +391,29 @@ ConfigHandler::setCopyAndCloseAfterUploadEnabled(const bool value) { m_settings.setValue(QStringLiteral("copyAndCloseAfterUpload"), value); } +bool +ConfigHandler::saveAfterCopyValue() +{ + return m_settings.value(QStringLiteral("saveAfterCopy")).toBool(); +} + +void +ConfigHandler::setSaveAfterCopy(const bool save) +{ + m_settings.setValue(QStringLiteral("saveAfterCopy"), save); +} + +QString +ConfigHandler::saveAfterCopyPathValue() +{ + return m_settings.value(QStringLiteral("saveAfterCopyPath")).toString(); +} + +void +ConfigHandler::setSaveAfterCopyPath(const QString& path) +{ + m_settings.setValue(QStringLiteral("saveAfterCopyPath"), path); +} void ConfigHandler::setDefaults() diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index dc995762..1e58b0f4 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -74,6 +74,11 @@ public: bool copyAndCloseAfterUploadEnabled(); void setCopyAndCloseAfterUploadEnabled(const bool); + bool saveAfterCopyValue(); + void setSaveAfterCopy(const bool); + + QString saveAfterCopyPathValue(); + void setSaveAfterCopyPath(const QString&); void setDefaults(); void setAllTheButtons(); diff --git a/src/utils/screenshotsaver.cpp b/src/utils/screenshotsaver.cpp index 41615071..59470793 100644 --- a/src/utils/screenshotsaver.cpp +++ b/src/utils/screenshotsaver.cpp @@ -30,12 +30,27 @@ ScreenshotSaver::ScreenshotSaver() {} void ScreenshotSaver::saveToClipboard(const QPixmap& capture) { - SystemNotification().sendMessage(QObject::tr("Capture saved to clipboard")); - QApplication::clipboard()->setPixmap(capture); + + // If we are able to properly save the file, save the file and copy to + // clipboard. + if ((ConfigHandler().saveAfterCopyValue()) && + (!ConfigHandler().saveAfterCopyPathValue().isEmpty())) { + QApplication::clipboard()->setPixmap(capture); + saveToFilesystem(capture, + ConfigHandler().saveAfterCopyPathValue(), + QObject::tr("Capture saved to clipboard. ")); + } + // Otherwise only save to clipboard + else { + QApplication::clipboard()->setPixmap(capture); + SystemNotification().sendMessage(QObject::tr("Capture saved to clipboard")); + } } bool -ScreenshotSaver::saveToFilesystem(const QPixmap& capture, const QString& path) +ScreenshotSaver::saveToFilesystem(const QPixmap& capture, + const QString& path, + const QString& messagePrefix) { QString completePath = FileNameHandler().generateAbsolutePath(path); completePath += QLatin1String(".png"); @@ -45,9 +60,11 @@ ScreenshotSaver::saveToFilesystem(const QPixmap& capture, const QString& path) if (ok) { ConfigHandler().setSavePath(path); - saveMessage = QObject::tr("Capture saved as ") + completePath; + saveMessage = + messagePrefix + QObject::tr("Capture saved as ") + completePath; } else { - saveMessage = QObject::tr("Error trying to save as ") + completePath; + saveMessage = + messagePrefix + QObject::tr("Error trying to save as ") + completePath; notificationPath = ""; } diff --git a/src/utils/screenshotsaver.h b/src/utils/screenshotsaver.h index fdab15de..99410e94 100644 --- a/src/utils/screenshotsaver.h +++ b/src/utils/screenshotsaver.h @@ -26,6 +26,8 @@ public: ScreenshotSaver(); void saveToClipboard(const QPixmap& capture); - bool saveToFilesystem(const QPixmap& capture, const QString& path); + bool saveToFilesystem(const QPixmap& capture, + const QString& path, + const QString& messagePrefix); bool saveToFilesystemGUI(const QPixmap& capture); }; diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index 6a21e84a..52fd2207 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -1011,7 +1011,7 @@ CaptureWidget::saveScreenshot() if (m_context.savePath.isEmpty()) { ScreenshotSaver().saveToFilesystemGUI(pixmap()); } else { - ScreenshotSaver().saveToFilesystem(pixmap(), m_context.savePath); + ScreenshotSaver().saveToFilesystem(pixmap(), m_context.savePath, ""); } close(); }