Added the ability to cache the last region (#2615)

* Added the ability to cache the last region

* adding cli option

* addressed typo comments and applied clang-format
This commit is contained in:
borgmanJeremy
2022-06-01 09:18:54 -05:00
committed by GitHub
parent 2582f063cc
commit 46801d933a
15 changed files with 144 additions and 93 deletions

View File

@@ -1,19 +1,20 @@
target_sources(
flameshot
PRIVATE buttonlistview.cpp
cacheutils.cpp
clickablelabel.cpp
configwindow.cpp
configresolver.cpp
colorpickereditmode.cpp
colorpickereditor.cpp
configerrordetails.cpp
configresolver.cpp
configwindow.cpp
extendedslider.cpp
filenameeditor.cpp
generalconf.cpp
setshortcutwidget.cpp
shortcutswidget.cpp
strftimechooserwidget.cpp
styleoverride.cpp
uicoloreditor.cpp
colorpickereditor.cpp
visualseditor.cpp
shortcutswidget.cpp
setshortcutwidget.cpp
colorpickereditmode.cpp
)

49
src/config/cacheutils.cpp Normal file
View File

@@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2021 Jeremy Borgman
#include "cacheutils.h"
#include <QDataStream>
#include <QDir>
#include <QFile>
#include <QRect>
#include <QStandardPaths>
#include <QString>
QString getCachePath()
{
auto cachePath =
QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
if (!QDir(cachePath).exists()) {
QDir().mkpath(cachePath);
}
return cachePath;
}
void setLastRegion(QRect const& newRegion)
{
auto cachePath = getCachePath() + "/region.txt";
QFile file(cachePath);
if (file.open(QIODevice::WriteOnly)) {
QDataStream out(&file);
out << newRegion;
file.close();
}
}
QRect getLastRegion()
{
auto cachePath = getCachePath() + "/region.txt";
QFile file(cachePath);
QRect lastRegion;
if (file.open(QIODevice::ReadOnly)) {
QDataStream input(&file);
input >> lastRegion;
file.close();
} else {
lastRegion = QRect(0, 0, 0, 0);
}
return lastRegion;
}

14
src/config/cacheutils.h Normal file
View File

@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2021 Jeremy Borgman
#ifndef FLAMESHOT_CACHEUTILS_H
#define FLAMESHOT_CACHEUTILS_H
class QString;
class QRect;
QString getCachePath();
QRect getLastRegion();
void setLastRegion(QRect const& newRegion);
#endif // FLAMESHOT_CACHEUTILS_H

View File

@@ -33,6 +33,7 @@ GeneralConf::GeneralConf(QWidget* parent)
initScrollArea();
initShowHelp();
initSaveLastRegion();
initShowSidePanelButton();
initShowDesktopNotification();
initShowTrayIcon();
@@ -84,6 +85,7 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
m_allowMultipleGuiInstances->setChecked(config.allowMultipleGuiInstances());
m_showMagnifier->setChecked(config.showMagnifier());
m_squareMagnifier->setChecked(config.squareMagnifier());
m_saveLastRegion->setChecked(config.saveLastRegion());
#if !defined(Q_OS_WIN)
m_autoCloseIdleDaemon->setChecked(config.autoCloseIdleDaemon());
@@ -109,6 +111,11 @@ void GeneralConf::updateComponents()
_updateComponents(false);
}
void GeneralConf::saveLastRegion(bool checked)
{
ConfigHandler().setSaveLastRegion(checked);
}
void GeneralConf::showHelpChanged(bool checked)
{
ConfigHandler().setShowHelp(checked);
@@ -235,6 +242,19 @@ void GeneralConf::initShowHelp()
m_helpMessage, &QCheckBox::clicked, this, &GeneralConf::showHelpChanged);
}
void GeneralConf::initSaveLastRegion()
{
m_saveLastRegion = new QCheckBox(tr("Use last region"), this);
m_saveLastRegion->setToolTip(tr("Uses the last region as the default "
"selection for the next screenshot"));
m_scrollAreaLayout->addWidget(m_saveLastRegion);
connect(m_saveLastRegion,
&QCheckBox::clicked,
this,
&GeneralConf::saveLastRegion);
}
void GeneralConf::initShowSidePanelButton()
{
m_sidePanelButton = new QCheckBox(tr("Show the side panel button"), this);

View File

@@ -25,6 +25,7 @@ public slots:
private slots:
void showHelpChanged(bool checked);
void saveLastRegion(bool checked);
void showSidePanelButtonChanged(bool checked);
void showDesktopNotificationChanged(bool checked);
void checkForUpdatesChanged(bool checked);
@@ -72,6 +73,7 @@ private:
void initUseJpgForClipboard();
void initUploadHistoryMax();
void initUploadClientSecret();
void initSaveLastRegion();
void _updateComponents(bool allowEmptySavePath);
@@ -91,6 +93,7 @@ private:
QCheckBox* m_copyAndCloseAfterUpload;
QCheckBox* m_copyPathAfterSave;
QCheckBox* m_antialiasingPinZoom;
QCheckBox* m_saveLastRegion;
QCheckBox* m_uploadWithoutConfirmation;
QPushButton* m_importButton;
QPushButton* m_exportButton;

View File

@@ -3,16 +3,13 @@
#include "capturerequest.h"
#include "confighandler.h"
#include "flameshot.h"
#include "imgupload/imguploadermanager.h"
#include "pinwidget.h"
#include "src/config/cacheutils.h"
#include "src/utils/screenshotsaver.h"
#include "src/widgets/imguploaddialog.h"
#include "systemnotification.h"
#include <QApplication>
#include <QClipboard>
#include <QDateTime>
#include <QVector>
#include <stdexcept>
#include <utility>
@@ -24,7 +21,13 @@ CaptureRequest::CaptureRequest(CaptureRequest::CaptureMode mode,
, m_delay(delay)
, m_tasks(tasks)
, m_data(std::move(data))
{}
{
ConfigHandler config;
if (config.saveLastRegion()) {
setInitialSelection(getLastRegion());
}
}
CaptureRequest::CaptureMode CaptureRequest::captureMode() const
{

View File

@@ -3,7 +3,6 @@
#include "flameshot.h"
#include "flameshotdaemon.h"
#if defined(Q_OS_MACOS)
#include "external/QHotkey/QHotkey"
#endif
@@ -25,7 +24,6 @@
#include <QApplication>
#include <QBuffer>
#include <QDebug>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QFile>
#include <QMessageBox>
@@ -115,8 +113,6 @@ CaptureWidget* Flameshot::gui(const CaptureRequest& req)
}
m_captureWindow = new CaptureWidget(req);
// m_captureWindow = new CaptureWidget(req, false); //
// debug
#ifdef Q_OS_WIN
m_captureWindow->show();
@@ -138,20 +134,16 @@ CaptureWidget* Flameshot::gui(const CaptureRequest& req)
void Flameshot::screen(CaptureRequest req, const int screenNumber)
{
if (!resolveAnyConfigErrors())
if (!resolveAnyConfigErrors()) {
return;
}
bool ok = true;
QScreen* screen;
if (screenNumber < 0) {
QPoint globalCursorPos = QCursor::pos();
#if QT_VERSION > QT_VERSION_CHECK(5, 10, 0)
screen = qApp->screenAt(globalCursorPos);
#else
screen =
qApp->screens()[qApp->desktop()->screenNumber(globalCursorPos)];
#endif
} else if (screenNumber >= qApp->screens().count()) {
AbstractLogger() << QObject::tr(
"Requested screen exceeds screen count");
@@ -184,8 +176,9 @@ void Flameshot::screen(CaptureRequest req, const int screenNumber)
void Flameshot::full(const CaptureRequest& req)
{
if (!resolveAnyConfigErrors())
if (!resolveAnyConfigErrors()) {
return;
}
bool ok = true;
QPixmap p(ScreenGrabber().grabEntireDesktop(ok));
@@ -203,10 +196,11 @@ void Flameshot::full(const CaptureRequest& req)
void Flameshot::launcher()
{
if (!resolveAnyConfigErrors())
if (!resolveAnyConfigErrors()) {
return;
}
if (!m_launcherWindow) {
if (m_launcherWindow == nullptr) {
m_launcherWindow = new CaptureLauncher();
}
m_launcherWindow->show();
@@ -218,10 +212,11 @@ void Flameshot::launcher()
void Flameshot::config()
{
if (!resolveAnyConfigErrors())
if (!resolveAnyConfigErrors()) {
return;
}
if (!m_configWindow) {
if (m_configWindow == nullptr) {
m_configWindow = new ConfigWindow();
m_configWindow->show();
#if defined(Q_OS_MACOS)
@@ -233,7 +228,7 @@ void Flameshot::config()
void Flameshot::info()
{
if (!m_infoWindow) {
if (m_infoWindow == nullptr) {
m_infoWindow = new InfoWindow();
#if defined(Q_OS_MACOS)
m_infoWindow->activateWindow();

View File

@@ -34,7 +34,7 @@ public:
public slots:
CaptureWidget* gui(
const CaptureRequest& req = CaptureRequest::GRAPHICAL_MODE);
void screen(CaptureRequest req, const int screenNumber = -1);
void screen(CaptureRequest req, int const screenNumber = -1);
void full(const CaptureRequest& req);
void launcher();
void config();

View File

@@ -9,6 +9,7 @@
#include "abstractlogger.h"
#include "src/cli/commandlineparser.h"
#include "src/config/cacheutils.h"
#include "src/config/styleoverride.h"
#include "src/core/capturerequest.h"
#include "src/core/flameshot.h"
@@ -23,7 +24,6 @@
#include <QSharedMemory>
#include <QTimer>
#include <QTranslator>
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
#include "abstractlogger.h"
#include "src/core/flameshotdbusadapter.h"
@@ -176,6 +176,11 @@ int main(int argc, char* argv[])
CommandOption delayOption({ "d", "delay" },
QObject::tr("Delay time in milliseconds"),
QStringLiteral("milliseconds"));
CommandOption useLastRegionOption(
"last-region",
QObject::tr("Repeat screenshot with previously selected region"));
CommandOption regionOption("region",
QObject::tr("Screenshot region to select"),
QStringLiteral("WxH+X+Y or string"));
@@ -272,6 +277,7 @@ int main(int argc, char* argv[])
mainColorOption.addChecker(colorChecker, colorErr);
delayOption.addChecker(numericChecker, delayErr);
regionOption.addChecker(regionChecker, regionErr);
useLastRegionOption.addChecker(booleanChecker, booleanErr);
pathOption.addChecker(pathChecker, pathErr);
trayOption.addChecker(booleanChecker, booleanErr);
autostartOption.addChecker(booleanChecker, booleanErr);
@@ -290,6 +296,7 @@ int main(int argc, char* argv[])
clipboardOption,
delayOption,
regionOption,
useLastRegionOption,
rawImageOption,
selectionOption,
uploadOption,
@@ -359,6 +366,7 @@ int main(int argc, char* argv[])
}
int delay = parser.value(delayOption).toInt();
QString region = parser.value(regionOption);
bool useLastRegion = parser.isSet(useLastRegionOption);
bool clipboard = parser.isSet(clipboardOption);
bool raw = parser.isSet(rawImageOption);
bool printGeometry = parser.isSet(selectionOption);
@@ -367,7 +375,10 @@ int main(int argc, char* argv[])
bool acceptOnSelect = parser.isSet(acceptOnSelectOption);
CaptureRequest req(CaptureRequest::GRAPHICAL_MODE, delay, path);
if (!region.isEmpty()) {
req.setInitialSelection(Region().value(region).toRect());
auto selectionRegion = Region().value(region).toRect();
req.setInitialSelection(selectionRegion);
} else if (useLastRegion) {
req.setInitialSelection(getLastRegion());
}
if (clipboard) {
req.addTask(CaptureRequest::COPY);
@@ -394,7 +405,6 @@ int main(int argc, char* argv[])
req.addSaveTask();
}
}
requestCaptureAndWait(req);
} else if (parser.isSet(fullArgument)) { // FULL
// Recreate the application as a QApplication

View File

@@ -101,9 +101,10 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
OPTION("savePath" ,ExistingDir ( )),
OPTION("savePathFixed" ,Bool ( false )),
OPTION("saveAsFileExtension" ,SaveFileExtension ( )),
OPTION("saveLastRegion" ,Bool (false )),
OPTION("uploadHistoryMax" ,LowerBoundedInt (0, 25 )),
OPTION("undoLimit" ,BoundedInt (0, 999, 100 )),
// Interface tab
// Interface tab
OPTION("uiColor" ,Color ( {116, 0, 150} )),
OPTION("contrastUiColor" ,Color ( {39, 0, 50} )),
OPTION("contrastOpacity" ,BoundedInt ( 0, 255, 190 )),

View File

@@ -119,7 +119,7 @@ public:
CONFIG_GETTER_SETTER(squareMagnifier, setSquareMagnifier, bool)
CONFIG_GETTER_SETTER(copyOnDoubleClick, setCopyOnDoubleClick, bool)
CONFIG_GETTER_SETTER(uploadClientSecret, setUploadClientSecret, QString)
CONFIG_GETTER_SETTER(saveLastRegion, setSaveLastRegion, bool)
// SPECIAL CASES
bool startupLaunch();
void setStartupLaunch(const bool);

View File

@@ -12,6 +12,7 @@
#include "capturewidget.h"
#include "abstractlogger.h"
#include "copytool.h"
#include "src/config/cacheutils.h"
#include "src/core/flameshot.h"
#include "src/core/qguiappcurrentscreen.h"
#include "src/tools/toolfactory.h"
@@ -259,6 +260,8 @@ CaptureWidget::~CaptureWidget()
}
#endif
if (m_captureDone) {
auto lastRegion = m_selection->geometry();
setLastRegion(lastRegion);
QRect geometry(m_context.selection);
geometry.setTopLeft(geometry.topLeft() + m_context.widgetOffset);
Flameshot::instance()->exportCapture(
@@ -1700,7 +1703,7 @@ void CaptureWidget::redo()
QRect CaptureWidget::extendedSelection() const
{
if (!m_selection->isVisible()) {
if (m_selection == nullptr) {
return {};
}
QRect r = m_selection->geometry();

View File

@@ -3,15 +3,14 @@
#include "capturelauncher.h"
#include "./ui_capturelauncher.h"
#include "src/config/cacheutils.h"
#include "src/core/flameshot.h"
#include "src/utils/globalvalues.h"
#include "src/utils/screengrabber.h"
#include "src/utils/screenshotsaver.h"
#include "src/widgets/imagelabel.h"
#include <QDir>
#include <QFile>
#include <QMimeData>
#include <QStandardPaths>
// https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSWidget.cpp
// https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSMainWindow.cpp
@@ -80,51 +79,18 @@ CaptureLauncher::CaptureLauncher(QDialog* parent)
}
});
auto last_region = getLastRegion();
ui->screenshotX->setText(QString::number(last_region.x()));
ui->screenshotY->setText(QString::number(last_region.y()));
ui->screenshotWidth->setText(QString::number(last_region.width()));
ui->screenshotHeight->setText(QString::number(last_region.height()));
auto lastRegion = getLastRegion();
ui->screenshotX->setText(QString::number(lastRegion.x()));
ui->screenshotY->setText(QString::number(lastRegion.y()));
ui->screenshotWidth->setText(QString::number(lastRegion.width()));
ui->screenshotHeight->setText(QString::number(lastRegion.height()));
show();
}
void CaptureLauncher::setLastRegion()
{
auto cachePath = getCachePath() + "/region.txt";
QFile file(cachePath);
if (file.open(QIODevice::WriteOnly)) {
auto newRegion = QRect(ui->screenshotX->text().toInt(),
ui->screenshotY->text().toInt(),
ui->screenshotWidth->text().toInt(),
ui->screenshotHeight->text().toInt());
QDataStream out(&file);
out << newRegion;
file.close();
}
}
QRect CaptureLauncher::getLastRegion()
{
auto cachePath = getCachePath() + "/region.txt";
QFile file(cachePath);
QRect lastRegion;
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file);
in >> lastRegion;
file.close();
} else {
lastRegion = QRect(0, 0, 0, 0);
}
return lastRegion;
}
// HACK:
// https://github.com/KDE/spectacle/blob/fa1e780b8bf3df3ac36c410b9ece4ace041f401b/src/Gui/KSMainWindow.cpp#L70
void CaptureLauncher::startCapture()
{
setLastRegion();
ui->launchButton->setEnabled(false);
hide();
@@ -205,13 +171,3 @@ CaptureLauncher::~CaptureLauncher()
{
delete ui;
}
QString getCachePath()
{
auto cachePath =
QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
if (!QDir(cachePath).exists()) {
QDir().mkpath(cachePath);
}
return cachePath;
}

View File

@@ -23,13 +23,9 @@ private:
Ui::CaptureLauncher* ui;
void connectCaptureSlots() const;
void disconnectCaptureSlots() const;
void setLastRegion();
QRect getLastRegion();
private slots:
void startCapture();
void onCaptureTaken(QPixmap const& p);
void onCaptureFailed();
};
QString getCachePath();

View File

@@ -95,7 +95,7 @@ void TrayIcon::initMenu()
{
m_menu = new QMenu();
QAction* captureAction = new QAction(tr("&Take Screenshot"), this);
auto* captureAction = new QAction(tr("&Take Screenshot"), this);
connect(captureAction, &QAction::triggered, this, [this]() {
#if defined(Q_OS_MACOS)
auto currentMacOsVersion = QOperatingSystemVersion::current();
@@ -113,17 +113,17 @@ void TrayIcon::initMenu()
});
#endif
});
QAction* launcherAction = new QAction(tr("&Open Launcher"), this);
auto* launcherAction = new QAction(tr("&Open Launcher"), this);
connect(launcherAction,
&QAction::triggered,
Flameshot::instance(),
&Flameshot::launcher);
QAction* configAction = new QAction(tr("&Configuration"), this);
auto* configAction = new QAction(tr("&Configuration"), this);
connect(configAction,
&QAction::triggered,
Flameshot::instance(),
&Flameshot::config);
QAction* infoAction = new QAction(tr("&About"), this);
auto* infoAction = new QAction(tr("&About"), this);
connect(
infoAction, &QAction::triggered, Flameshot::instance(), &Flameshot::info);