Skip to content

Commit

Permalink
Add method of editing a pinned screenshot
Browse files Browse the repository at this point in the history
Add a method to allow editing a pinned screenshot by initiating a new
screenshot over top of the pinned image, and then closing the old pinned image
on success.

This adds a new context menu to pinned screenshots. When selected the
PinWidget will call the daemon to do the work. Thus this functionality only
works when flameshot is running in daemon mode. The context menu options don't
show up when not running in daemon mode (I don't know why they don't show up,
other ones like the rotate and opacity ones also don't show up).

This is not the ideal method of doing this, it doesn't actually edit the
screenshot at all. I call it the "bait and switch" method of editing. The only
previous attempt that I've soon to do this got caught up in dealing with
screens with different DPI (pull/1565). While it would be amazing to be able
to limit the whole "grab" region to be less than a screen, or even just one
screen, I think this method of editing a pin in the full desktop cature mode
is a step forward. It sure is for my workflow anyhow.

I'm not familiar with c++ or this codebase so if there looks to be anything
weird in this PR it's probably due to ignorance.

I'm calling FlameshotDaemon from PinWidget, I'm not sure if I should be doing
that or trying to access the Flameshot singleton directly, I just copied off
of another tool which called `FlameshotDaemon::copyToClipboard()`.

I'm not sure if doing `.geometry() - .layout()->contentsMargins()` is the
correct way to get the image geometry, I was guided by what attributes I could
see in GammaRay while inspecting a running flameshot.

The method of disconnecting from the signals from within the lambdas is from
here: https://stackoverflow.com/questions/14828678/disconnecting-lambda-functions-in-qt5

I contemplated calling the method in Flameshot "replacePin" or
"captureFromPin" or something more technically accurate. But I figured the
signature could stay a bit optimistic and if people would like the behaviour
to be firmed up to match the goal in the future that could be done. Eg add
functionality to the CaptureWidget to make it so you can't modify the capture
region, change the grab region to be less than the whole desktop etc.

Known issues:

* you can move/resize the selection region, eg not just edit a pin but create
  a completely different one of a different region of the screen

Relates to: flameshot-org#954
  • Loading branch information
toofar committed Dec 27, 2023
1 parent 3d21e49 commit 9228e46
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/core/flameshotdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QClipboard>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QLayout>
#include <QPixmap>
#include <QRect>

Expand Down Expand Up @@ -269,6 +270,45 @@ void FlameshotDaemon::attachPin(const QPixmap& pixmap, QRect geometry)
pinWidget->activateWindow();
}

/**
* @brief Initiate a new pin capture to replace `existing`.
*
* Starts a new screenshot capture with the selection region initialized to be
* the same as `existing`. When it is completed the selection will be pinned and
* the previous pin (the `existing` argument) will be closed.
*/
void FlameshotDaemon::editPin(PinWidget& existing)
{
CaptureRequest req(CaptureRequest::GRAPHICAL_MODE);
req.addTask(CaptureRequest::PIN);
QRect contentsRect =
existing.geometry() - existing.layout()->contentsMargins();
req.setInitialSelection(contentsRect);

Flameshot* flameshot = Flameshot::instance();
flameshot->gui(req);

// Connect to signals emitted when a capture (hopefully the one we just
// initiated!) is finished, both when it succeeds and when it is aborted.
auto successConn = std::make_shared<QMetaObject::Connection>();
auto failureConn = std::make_shared<QMetaObject::Connection>();
*successConn = QObject::connect(flameshot,
&Flameshot::captureTaken,
[&existing, successConn, failureConn]() {
// Only close the old pin on success.
existing.close();

QObject::disconnect(*successConn);
QObject::disconnect(*failureConn);
});
*failureConn = QObject::connect(flameshot,
&Flameshot::captureTaken,
[&existing, successConn, failureConn]() {
QObject::disconnect(*successConn);
QObject::disconnect(*failureConn);
});
}

void FlameshotDaemon::attachScreenshotToClipboard(const QPixmap& pixmap)
{
m_hostingClipboard = true;
Expand Down
2 changes: 2 additions & 0 deletions src/core/flameshotdaemon.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "pinwidget.h"
#include <QByteArray>
#include <QObject>
#include <QtDBus/QDBusAbstractAdaptor>
Expand Down Expand Up @@ -27,6 +28,7 @@ class FlameshotDaemon : public QObject
static void copyToClipboard(const QPixmap& capture);
static void copyToClipboard(const QString& text,
const QString& notification = "");
static void editPin(PinWidget& pinned);
static bool isThisInstanceHostingWidgets();

void sendTrayNotification(
Expand Down
11 changes: 11 additions & 0 deletions src/tools/pin/pinwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "pinwidget.h"
#include "qguiappcurrentscreen.h"
#include "screenshotsaver.h"
#include "src/core/flameshotdaemon.h"
#include "src/utils/confighandler.h"
#include "src/utils/globalvalues.h"

Expand Down Expand Up @@ -328,6 +329,10 @@ void PinWidget::showContextMenu(const QPoint& pos)
&PinWidget::decreaseOpacity);
contextMenu.addAction(&decreaseOpacityAction);

QAction editAction(tr("Edit Pin"), this);
connect(&editAction, &QAction::triggered, this, &PinWidget::edit);
contextMenu.addAction(&editAction);

QAction closePinAction(tr("Close"), this);
connect(&closePinAction, &QAction::triggered, this, &PinWidget::closePin);
contextMenu.addSeparator();
Expand All @@ -340,9 +345,15 @@ void PinWidget::copyToClipboard()
{
saveToClipboard(m_pixmap);
}

void PinWidget::saveToFile()
{
hide();
saveToFilesystemGUI(m_pixmap);
show();
}

void PinWidget::edit()
{
FlameshotDaemon::editPin(*this);
}
1 change: 1 addition & 0 deletions src/tools/pin/pinwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ private slots:
void showContextMenu(const QPoint& pos);
void copyToClipboard();
void saveToFile();
void edit();
};

0 comments on commit 9228e46

Please sign in to comment.