Commit ec5e9187 authored by Karlo Dautović's avatar Karlo Dautović Committed by Nate Graham

Add option to copy screenshot to clipboard in background mode

Summary:
FEATURE: 385559
BUG: 393708
FIXED-IN: 19.04.0

Reviewers: #spectacle, ngraham

Reviewed By: #spectacle, ngraham

Subscribers: ngraham, davidre, #spectacle

Tags: #spectacle

Differential Revision: https://phabricator.kde.org/D19715
parent d023ae5e
......@@ -31,6 +31,8 @@
#include <QBuffer>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QMimeData>
#include <QString>
#include <KSharedConfig>
#include <KConfigGroup>
......@@ -491,9 +493,16 @@ bool ExportManager::doSaveAs(QWidget *parentWindow, bool notify)
// misc helpers
void ExportManager::doCopyToClipboard()
void ExportManager::doCopyToClipboard(bool notify)
{
QApplication::clipboard()->setPixmap(mSavePixmap, QClipboard::Clipboard);
auto data = new QMimeData();
data->setImageData(mSavePixmap.toImage());
data->setData(QStringLiteral("x-kde-force-image-copy"), QByteArray());
QApplication::clipboard()->setMimeData(data, QClipboard::Clipboard);
if (notify) {
emit forceNotify(QUrl());
}
}
void ExportManager::doPrint(QPrinter *printer)
......
......@@ -87,7 +87,7 @@ class ExportManager : public QObject
void doSave(const QUrl &url = QUrl(), bool notify = false);
bool doSaveAs(QWidget *parentWindow = nullptr, bool notify = false);
void doCopyToClipboard();
void doCopyToClipboard(bool notify);
void doPrint(QPrinter *printer);
private:
......
......@@ -389,7 +389,8 @@ void KSMainWindow::showImageSharedFeedback(bool error, const QString &message)
void KSMainWindow::sendToClipboard()
{
ExportManager::instance()->doCopyToClipboard();
bool notify = false;
ExportManager::instance()->doCopyToClipboard(notify);
SpectacleConfig::instance()->quitAfterSaveOrCopyChecked()
? quit()
......
......@@ -68,6 +68,7 @@ int main(int argc, char **argv)
{{QStringLiteral("n"), QStringLiteral("nonotify")}, i18n("In background mode, do not pop up a notification when the screenshot is taken")},
{{QStringLiteral("o"), QStringLiteral("output")}, i18n("In background mode, save image to specified file"), QStringLiteral("fileName")},
{{QStringLiteral("d"), QStringLiteral("delay")}, i18n("In background mode, delay before taking the shot (in milliseconds)"), QStringLiteral("delayMsec")},
{{QStringLiteral("c"), QStringLiteral("clipboard")}, i18n("In background mode, copy screenshot to clipboard")},
{{QStringLiteral("w"), QStringLiteral("onclick")}, i18n("Wait for a click before taking screenshot. Invalidates delay")}
});
......@@ -93,6 +94,7 @@ int main(int argc, char **argv)
SpectacleCore::StartMode startMode = SpectacleCore::GuiMode;
bool notify = true;
bool copyToClipboard = false;
qint64 delayMsec = 0;
QString fileName = QString();
......@@ -124,6 +126,10 @@ int main(int argc, char **argv)
delayMsec = -1;
}
if (parser.isSet(QStringLiteral("clipboard"))) {
copyToClipboard = true;
}
app.setQuitOnLastWindowClosed(false);
break;
......@@ -137,7 +143,7 @@ int main(int argc, char **argv)
// release the kraken
SpectacleCore core(startMode, grabMode, fileName, delayMsec, notify);
SpectacleCore core(startMode, grabMode, fileName, delayMsec, notify, copyToClipboard);
QObject::connect(&core, &SpectacleCore::allDone, qApp, &QApplication::quit);
// create the dbus connections
......
......@@ -42,14 +42,15 @@
#include <QTimer>
SpectacleCore::SpectacleCore(StartMode startMode, ImageGrabber::GrabMode grabMode, QString &saveFileName,
qint64 delayMsec, bool notifyOnGrab, QObject *parent) :
qint64 delayMsec, bool notifyOnGrab, bool copyToClipboard, QObject *parent) :
QObject(parent),
mExportManager(ExportManager::instance()),
mStartMode(startMode),
mNotify(notifyOnGrab),
mImageGrabber(nullptr),
mMainWindow(nullptr),
isGuiInited(false)
isGuiInited(false),
copyToClipboard(copyToClipboard)
{
KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("spectaclerc"));
KConfigGroup guiConfig(config, "GuiConfig");
......@@ -217,9 +218,13 @@ void SpectacleCore::screenshotUpdated(const QPixmap &pixmap)
connect(mExportManager, &ExportManager::imageSaved, this, &SpectacleCore::doNotify);
}
QUrl savePath = (mStartMode == BackgroundMode && mFileNameUrl.isValid() && mFileNameUrl.isLocalFile()) ?
if (copyToClipboard) {
mExportManager->doCopyToClipboard(mNotify);
} else {
QUrl savePath = (mStartMode == BackgroundMode && mFileNameUrl.isValid() && mFileNameUrl.isLocalFile()) ?
mFileNameUrl : QUrl();
mExportManager->doSave(savePath);
mExportManager->doSave(savePath);
}
// if we notify, we emit allDone only if the user either dismissed the notification or pressed
// the "Open" button, otherwise the app closes before it can react to it.
......@@ -278,15 +283,19 @@ void SpectacleCore::doNotify(const QUrl &savedAt)
const QString &path = savedAt.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash).path();
// a speaking message is prettier than a URL, special case for the default pictures location
if (path == QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)) {
// a speaking message is prettier than a URL, special case for copy to clipboard and the default pictures location
if (copyToClipboard) {
notify->setText(i18n("A screenshot was saved to your clipboard."));
} else if (path == QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)) {
notify->setText(i18nc("Placeholder is filename", "A screenshot was saved as '%1' to your Pictures folder.", savedAt.fileName()));
} else {
notify->setText(i18n("A screenshot was saved as '%1' to '%2'.", savedAt.fileName(), path));
}
notify->setActions({i18nc("Open the screenshot we just saved", "Open")});
notify->setUrls({savedAt});
if (!copyToClipboard) {
notify->setActions({i18nc("Open the screenshot we just saved", "Open")});
notify->setUrls({savedAt});
}
connect(notify, &KNotification::action1Activated, this, [this, savedAt] {
new KRun(savedAt, nullptr);
......
......@@ -42,7 +42,7 @@ class SpectacleCore : public QObject
};
explicit SpectacleCore(StartMode startMode, ImageGrabber::GrabMode grabMode, QString &saveFileName,
qint64 delayMsec, bool notifyOnGrab, QObject *parent = nullptr);
qint64 delayMsec, bool notifyOnGrab, bool copyToClipboard, QObject *parent = nullptr);
~SpectacleCore();
QString filename() const;
......@@ -81,6 +81,7 @@ class SpectacleCore : public QObject
ImageGrabber *mImageGrabber;
KSMainWindow *mMainWindow;
bool isGuiInited;
bool copyToClipboard;
};
#endif // KSCORE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment