Commit 00dd4730 authored by Srevin Saju's avatar Srevin Saju 🎲 Committed by Méven Car
Browse files

feat: add support for copying saved screenshot file path to clipboard

this feature aims to enable 'Copy File Location' to clipboard,
this allows users to get a direct local file path to the image if
the user has enabled 'Auto Save' option. If 'Auto Save' is disabled, a
temporary screenshot file will be created, and that file path would
be copied to clipboard.

This feature is particularly useful for users, who would directly like to
upload their screenshot by file path using a FileChooser, eg: Browser,
or to upload the screenshot to non-KServices, like a private cloud instance, etc.
parent f532c20e
......@@ -542,6 +542,26 @@ void ExportManager::doCopyToClipboard(bool notify)
}
}
void ExportManager::doCopyLocationToClipboard(bool notify)
{
QString localFile;
if (Settings::self()->autoSaveImage()) {
// The auto save has been enabled, we need to choose that file path
localFile = Settings::self()->lastSaveLocation().toLocalFile();
} else {
// use a temporary save path, and copy that to clipboard instead
localFile = ExportManager::instance()->tempSave().toLocalFile();
}
QApplication::clipboard()->setText(localFile);
Q_EMIT imageLocationCopied(QUrl::fromLocalFile(localFile));
if (notify) {
Q_EMIT forceNotify(QUrl::fromLocalFile(localFile));
}
}
void ExportManager::doPrint(QPrinter *printer)
{
QPainter painter;
......
......@@ -58,6 +58,7 @@ class ExportManager: public QObject
void errorMessage(const QString &str);
void imageSaved(const QUrl &savedAt);
void imageCopied();
void imageLocationCopied(const QUrl &savedAt);
void imageSavedAndCopied(const QUrl &savedAt);
void forceNotify(const QUrl &savedAt);
......@@ -71,6 +72,7 @@ class ExportManager: public QObject
bool doSaveAs(QWidget *parentWindow = nullptr, bool notify = false);
void doSaveAndCopy(const QUrl &url = QUrl());
void doCopyToClipboard(bool notify = false);
void doCopyLocationToClipboard(bool notify = false);
void doPrint(QPrinter *printer);
private:
......
......@@ -55,6 +55,9 @@ KSMainWindow::KSMainWindow(Platform::GrabModes theGrabModes, Platform::ShutterMo
mToolsButton(new QPushButton(this)),
mSendToButton(new QPushButton(this)),
mClipboardButton(new QToolButton(this)),
mClipboardMenu(new QMenu(this)),
mClipboardLocationAction(new QAction(this)),
mClipboardImageAction(new QAction(this)),
mSaveButton(new QToolButton(this)),
mSaveMenu(new QMenu(this)),
mSaveAsAction(new QAction(this)),
......@@ -122,6 +125,7 @@ void KSMainWindow::init()
connect(ExportManager::instance(), &ExportManager::imageSaved, this, &KSMainWindow::imageSaved);
connect(ExportManager::instance(), &ExportManager::imageCopied, this, &KSMainWindow::imageCopied);
connect(ExportManager::instance(), &ExportManager::imageLocationCopied, this, &KSMainWindow::imageSavedAndLocationCopied);
connect(ExportManager::instance(), &ExportManager::imageSavedAndCopied, this, &KSMainWindow::imageSavedAndCopied);
// the KSGWidget
......@@ -178,9 +182,8 @@ void KSMainWindow::init()
mSendToButton->setAutoDefault(false);
mDialogButtonBox->addButton(mSendToButton, QDialogButtonBox::ActionRole);
mClipboardButton->setDefaultAction(KStandardAction::copy(this, &KSMainWindow::copy, this));
mClipboardButton->setText(i18n("Copy to Clipboard"));
mClipboardButton->setToolTip(i18n("Copy the current screenshot image to the clipboard."));
mClipboardButton->setMenu(mClipboardMenu);
mClipboardButton->setPopupMode(QToolButton::MenuButtonPopup);
mClipboardButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
mDialogButtonBox->addButton(mClipboardButton, QDialogButtonBox::ActionRole);
......@@ -216,6 +219,18 @@ void KSMainWindow::init()
mSaveMenu->addAction(mSaveAction);
setDefaultSaveAction();
// the clipboard menu
mClipboardImageAction = KStandardAction::copy(this, &KSMainWindow::copyImage, this);
mClipboardImageAction->setText(i18n("Copy Image to Clipboard"));
mClipboardLocationAction = new QAction(
QIcon::fromTheme(QStringLiteral("clipboard")),
i18n("Copy Location to Clipboard"), this);
connect(mClipboardLocationAction, &QAction::triggered, this, &KSMainWindow::copyLocation);
mClipboardMenu->addAction(mClipboardImageAction);
mClipboardMenu->addAction(mClipboardLocationAction);
setDefaultCopyAction();
// message widget
connect(mMessageWidget, &KMessageWidget::linkActivated, this, [](const QString &str) { QDesktopServices::openUrl(QUrl(str)); } );
......@@ -286,6 +301,20 @@ void KSMainWindow::setDefaultSaveAction()
}
}
void KSMainWindow::setDefaultCopyAction()
{
switch (Settings::lastUsedCopyMode()) {
case Settings::CopyImage:
mClipboardButton->setText(i18n("Copy Image to Clipboard"));
mClipboardButton->setDefaultAction(mClipboardImageAction);
break;
case Settings::CopyLocation:
mClipboardButton->setText(i18n("Copy Location to Clipboard"));
mClipboardButton->setDefaultAction(mClipboardLocationAction);
break;
}
}
// overrides
void KSMainWindow::moveEvent(QMoveEvent *event)
......@@ -459,8 +488,23 @@ void KSMainWindow::showImageSharedFeedback(bool error, const QString &message)
}
}
void KSMainWindow::copy()
void KSMainWindow::copyLocation()
{
Settings::setLastUsedCopyMode(Settings::CopyLocation);
setDefaultCopyAction();
const bool quitChecked = Settings::quitAfterSaveCopyExport();
ExportManager::instance()->doCopyLocationToClipboard();
if (quitChecked) {
quit(QuitBehavior::QuitExternally);
}
}
void KSMainWindow::copyImage()
{
Settings::setLastUsedCopyMode(Settings::CopyImage);
setDefaultCopyAction();
const bool quitChecked = Settings::quitAfterSaveCopyExport();
ExportManager::instance()->doCopyToClipboard();
if (quitChecked) {
......@@ -474,6 +518,13 @@ void KSMainWindow::imageCopied()
KMessageWidget::Information);
}
void KSMainWindow::imageSavedAndLocationCopied(const QUrl &location)
{
showInlineMessage(i18n("The screenshot has been saved as <a href=\"%1\">%2</a> and its location has been copied to clipboard",
location.toString(), location.fileName()), KMessageWidget::Positive,
MessageDuration::AutoHide, {mOpenContaining});
}
void KSMainWindow::screenshotFailed()
{
showInlineMessage(i18n("Could not take a screenshot. Please report this bug here: <a href=\"https://bugs.kde.org/enter_bug.cgi?product=Spectacle\">create a spectacle bug</a>"),
......
......@@ -59,10 +59,14 @@ class KSMainWindow: public QDialog
void showPreferencesDialog();
void showImageSharedFeedback(bool error, const QString &message);
void imageCopied();
void imageSavedAndLocationCopied(const QUrl &location);
void init();
void setDefaultSaveAction();
void setDefaultCopyAction();
void save();
void saveAs();
void copyImage();
void copyLocation();
void restoreWindowTitle();
public Q_SLOTS:
......@@ -84,7 +88,6 @@ class KSMainWindow: public QDialog
private:
void keyPressEvent(QKeyEvent *event) override;
void copy();
KSWidget *mKSWidget;
QFrame *mDivider;
......@@ -93,6 +96,9 @@ class KSMainWindow: public QDialog
QPushButton *mToolsButton;
QPushButton *mSendToButton;
QToolButton *mClipboardButton;
QMenu *mClipboardMenu;
QAction *mClipboardLocationAction;
QAction *mClipboardImageAction;
QToolButton *mSaveButton;
QMenu *mSaveMenu;
QAction *mSaveAsAction;
......
......@@ -119,5 +119,13 @@
</choices>
<default>SaveAs</default>
</entry>
<entry name="lastUsedCopyMode" type="Enum">
<label>The last used copy mode</label>
<choices>
<choice name="CopyLocation"></choice>
<choice name="CopyImage"></choice>
</choices>
<default>CopyImage</default>
</entry>
</group>
</kcfg>
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