Skip to content

Commit 46801d9

Browse files
Added the ability to cache the last region (flameshot-org#2615)
* Added the ability to cache the last region * adding cli option * addressed typo comments and applied clang-format
1 parent 2582f06 commit 46801d9

15 files changed

+144
-93
lines changed

src/config/CMakeLists.txt

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
target_sources(
22
flameshot
33
PRIVATE buttonlistview.cpp
4+
cacheutils.cpp
45
clickablelabel.cpp
5-
configwindow.cpp
6-
configresolver.cpp
6+
colorpickereditmode.cpp
7+
colorpickereditor.cpp
78
configerrordetails.cpp
9+
configresolver.cpp
10+
configwindow.cpp
811
extendedslider.cpp
912
filenameeditor.cpp
1013
generalconf.cpp
14+
setshortcutwidget.cpp
15+
shortcutswidget.cpp
1116
strftimechooserwidget.cpp
1217
styleoverride.cpp
1318
uicoloreditor.cpp
14-
colorpickereditor.cpp
1519
visualseditor.cpp
16-
shortcutswidget.cpp
17-
setshortcutwidget.cpp
18-
colorpickereditmode.cpp
1920
)

src/config/cacheutils.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
// SPDX-FileCopyrightText: 2021 Jeremy Borgman
3+
4+
#include "cacheutils.h"
5+
#include <QDataStream>
6+
#include <QDir>
7+
#include <QFile>
8+
#include <QRect>
9+
#include <QStandardPaths>
10+
#include <QString>
11+
12+
QString getCachePath()
13+
{
14+
auto cachePath =
15+
QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
16+
if (!QDir(cachePath).exists()) {
17+
QDir().mkpath(cachePath);
18+
}
19+
return cachePath;
20+
}
21+
22+
void setLastRegion(QRect const& newRegion)
23+
{
24+
auto cachePath = getCachePath() + "/region.txt";
25+
26+
QFile file(cachePath);
27+
if (file.open(QIODevice::WriteOnly)) {
28+
QDataStream out(&file);
29+
out << newRegion;
30+
file.close();
31+
}
32+
}
33+
34+
QRect getLastRegion()
35+
{
36+
auto cachePath = getCachePath() + "/region.txt";
37+
QFile file(cachePath);
38+
39+
QRect lastRegion;
40+
if (file.open(QIODevice::ReadOnly)) {
41+
QDataStream input(&file);
42+
input >> lastRegion;
43+
file.close();
44+
} else {
45+
lastRegion = QRect(0, 0, 0, 0);
46+
}
47+
48+
return lastRegion;
49+
}

src/config/cacheutils.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
// SPDX-FileCopyrightText: 2021 Jeremy Borgman
3+
4+
#ifndef FLAMESHOT_CACHEUTILS_H
5+
#define FLAMESHOT_CACHEUTILS_H
6+
7+
class QString;
8+
class QRect;
9+
10+
QString getCachePath();
11+
QRect getLastRegion();
12+
void setLastRegion(QRect const& newRegion);
13+
14+
#endif // FLAMESHOT_CACHEUTILS_H

src/config/generalconf.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ GeneralConf::GeneralConf(QWidget* parent)
3333
initScrollArea();
3434

3535
initShowHelp();
36+
initSaveLastRegion();
3637
initShowSidePanelButton();
3738
initShowDesktopNotification();
3839
initShowTrayIcon();
@@ -84,6 +85,7 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
8485
m_allowMultipleGuiInstances->setChecked(config.allowMultipleGuiInstances());
8586
m_showMagnifier->setChecked(config.showMagnifier());
8687
m_squareMagnifier->setChecked(config.squareMagnifier());
88+
m_saveLastRegion->setChecked(config.saveLastRegion());
8789

8890
#if !defined(Q_OS_WIN)
8991
m_autoCloseIdleDaemon->setChecked(config.autoCloseIdleDaemon());
@@ -109,6 +111,11 @@ void GeneralConf::updateComponents()
109111
_updateComponents(false);
110112
}
111113

114+
void GeneralConf::saveLastRegion(bool checked)
115+
{
116+
ConfigHandler().setSaveLastRegion(checked);
117+
}
118+
112119
void GeneralConf::showHelpChanged(bool checked)
113120
{
114121
ConfigHandler().setShowHelp(checked);
@@ -235,6 +242,19 @@ void GeneralConf::initShowHelp()
235242
m_helpMessage, &QCheckBox::clicked, this, &GeneralConf::showHelpChanged);
236243
}
237244

245+
void GeneralConf::initSaveLastRegion()
246+
{
247+
m_saveLastRegion = new QCheckBox(tr("Use last region"), this);
248+
m_saveLastRegion->setToolTip(tr("Uses the last region as the default "
249+
"selection for the next screenshot"));
250+
m_scrollAreaLayout->addWidget(m_saveLastRegion);
251+
252+
connect(m_saveLastRegion,
253+
&QCheckBox::clicked,
254+
this,
255+
&GeneralConf::saveLastRegion);
256+
}
257+
238258
void GeneralConf::initShowSidePanelButton()
239259
{
240260
m_sidePanelButton = new QCheckBox(tr("Show the side panel button"), this);

src/config/generalconf.h

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public slots:
2525

2626
private slots:
2727
void showHelpChanged(bool checked);
28+
void saveLastRegion(bool checked);
2829
void showSidePanelButtonChanged(bool checked);
2930
void showDesktopNotificationChanged(bool checked);
3031
void checkForUpdatesChanged(bool checked);
@@ -72,6 +73,7 @@ private slots:
7273
void initUseJpgForClipboard();
7374
void initUploadHistoryMax();
7475
void initUploadClientSecret();
76+
void initSaveLastRegion();
7577

7678
void _updateComponents(bool allowEmptySavePath);
7779

@@ -91,6 +93,7 @@ private slots:
9193
QCheckBox* m_copyAndCloseAfterUpload;
9294
QCheckBox* m_copyPathAfterSave;
9395
QCheckBox* m_antialiasingPinZoom;
96+
QCheckBox* m_saveLastRegion;
9497
QCheckBox* m_uploadWithoutConfirmation;
9598
QPushButton* m_importButton;
9699
QPushButton* m_exportButton;

src/core/capturerequest.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44
#include "capturerequest.h"
55
#include "confighandler.h"
6-
#include "flameshot.h"
76
#include "imgupload/imguploadermanager.h"
87
#include "pinwidget.h"
8+
#include "src/config/cacheutils.h"
99
#include "src/utils/screenshotsaver.h"
10-
#include "src/widgets/imguploaddialog.h"
11-
#include "systemnotification.h"
1210
#include <QApplication>
1311
#include <QClipboard>
1412
#include <QDateTime>
15-
#include <QVector>
1613
#include <stdexcept>
1714
#include <utility>
1815

@@ -24,7 +21,13 @@ CaptureRequest::CaptureRequest(CaptureRequest::CaptureMode mode,
2421
, m_delay(delay)
2522
, m_tasks(tasks)
2623
, m_data(std::move(data))
27-
{}
24+
{
25+
26+
ConfigHandler config;
27+
if (config.saveLastRegion()) {
28+
setInitialSelection(getLastRegion());
29+
}
30+
}
2831

2932
CaptureRequest::CaptureMode CaptureRequest::captureMode() const
3033
{

src/core/flameshot.cpp

+11-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "flameshot.h"
55
#include "flameshotdaemon.h"
6-
76
#if defined(Q_OS_MACOS)
87
#include "external/QHotkey/QHotkey"
98
#endif
@@ -25,7 +24,6 @@
2524
#include <QApplication>
2625
#include <QBuffer>
2726
#include <QDebug>
28-
#include <QDesktopServices>
2927
#include <QDesktopWidget>
3028
#include <QFile>
3129
#include <QMessageBox>
@@ -115,8 +113,6 @@ CaptureWidget* Flameshot::gui(const CaptureRequest& req)
115113
}
116114

117115
m_captureWindow = new CaptureWidget(req);
118-
// m_captureWindow = new CaptureWidget(req, false); //
119-
// debug
120116

121117
#ifdef Q_OS_WIN
122118
m_captureWindow->show();
@@ -138,20 +134,16 @@ CaptureWidget* Flameshot::gui(const CaptureRequest& req)
138134

139135
void Flameshot::screen(CaptureRequest req, const int screenNumber)
140136
{
141-
if (!resolveAnyConfigErrors())
137+
if (!resolveAnyConfigErrors()) {
142138
return;
139+
}
143140

144141
bool ok = true;
145142
QScreen* screen;
146143

147144
if (screenNumber < 0) {
148145
QPoint globalCursorPos = QCursor::pos();
149-
#if QT_VERSION > QT_VERSION_CHECK(5, 10, 0)
150146
screen = qApp->screenAt(globalCursorPos);
151-
#else
152-
screen =
153-
qApp->screens()[qApp->desktop()->screenNumber(globalCursorPos)];
154-
#endif
155147
} else if (screenNumber >= qApp->screens().count()) {
156148
AbstractLogger() << QObject::tr(
157149
"Requested screen exceeds screen count");
@@ -184,8 +176,9 @@ void Flameshot::screen(CaptureRequest req, const int screenNumber)
184176

185177
void Flameshot::full(const CaptureRequest& req)
186178
{
187-
if (!resolveAnyConfigErrors())
179+
if (!resolveAnyConfigErrors()) {
188180
return;
181+
}
189182

190183
bool ok = true;
191184
QPixmap p(ScreenGrabber().grabEntireDesktop(ok));
@@ -203,10 +196,11 @@ void Flameshot::full(const CaptureRequest& req)
203196

204197
void Flameshot::launcher()
205198
{
206-
if (!resolveAnyConfigErrors())
199+
if (!resolveAnyConfigErrors()) {
207200
return;
201+
}
208202

209-
if (!m_launcherWindow) {
203+
if (m_launcherWindow == nullptr) {
210204
m_launcherWindow = new CaptureLauncher();
211205
}
212206
m_launcherWindow->show();
@@ -218,10 +212,11 @@ void Flameshot::launcher()
218212

219213
void Flameshot::config()
220214
{
221-
if (!resolveAnyConfigErrors())
215+
if (!resolveAnyConfigErrors()) {
222216
return;
217+
}
223218

224-
if (!m_configWindow) {
219+
if (m_configWindow == nullptr) {
225220
m_configWindow = new ConfigWindow();
226221
m_configWindow->show();
227222
#if defined(Q_OS_MACOS)
@@ -233,7 +228,7 @@ void Flameshot::config()
233228

234229
void Flameshot::info()
235230
{
236-
if (!m_infoWindow) {
231+
if (m_infoWindow == nullptr) {
237232
m_infoWindow = new InfoWindow();
238233
#if defined(Q_OS_MACOS)
239234
m_infoWindow->activateWindow();

src/core/flameshot.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Flameshot : public QObject
3434
public slots:
3535
CaptureWidget* gui(
3636
const CaptureRequest& req = CaptureRequest::GRAPHICAL_MODE);
37-
void screen(CaptureRequest req, const int screenNumber = -1);
37+
void screen(CaptureRequest req, int const screenNumber = -1);
3838
void full(const CaptureRequest& req);
3939
void launcher();
4040
void config();

src/main.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "abstractlogger.h"
1111
#include "src/cli/commandlineparser.h"
12+
#include "src/config/cacheutils.h"
1213
#include "src/config/styleoverride.h"
1314
#include "src/core/capturerequest.h"
1415
#include "src/core/flameshot.h"
@@ -23,7 +24,6 @@
2324
#include <QSharedMemory>
2425
#include <QTimer>
2526
#include <QTranslator>
26-
2727
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
2828
#include "abstractlogger.h"
2929
#include "src/core/flameshotdbusadapter.h"
@@ -176,6 +176,11 @@ int main(int argc, char* argv[])
176176
CommandOption delayOption({ "d", "delay" },
177177
QObject::tr("Delay time in milliseconds"),
178178
QStringLiteral("milliseconds"));
179+
180+
CommandOption useLastRegionOption(
181+
"last-region",
182+
QObject::tr("Repeat screenshot with previously selected region"));
183+
179184
CommandOption regionOption("region",
180185
QObject::tr("Screenshot region to select"),
181186
QStringLiteral("WxH+X+Y or string"));
@@ -272,6 +277,7 @@ int main(int argc, char* argv[])
272277
mainColorOption.addChecker(colorChecker, colorErr);
273278
delayOption.addChecker(numericChecker, delayErr);
274279
regionOption.addChecker(regionChecker, regionErr);
280+
useLastRegionOption.addChecker(booleanChecker, booleanErr);
275281
pathOption.addChecker(pathChecker, pathErr);
276282
trayOption.addChecker(booleanChecker, booleanErr);
277283
autostartOption.addChecker(booleanChecker, booleanErr);
@@ -290,6 +296,7 @@ int main(int argc, char* argv[])
290296
clipboardOption,
291297
delayOption,
292298
regionOption,
299+
useLastRegionOption,
293300
rawImageOption,
294301
selectionOption,
295302
uploadOption,
@@ -359,6 +366,7 @@ int main(int argc, char* argv[])
359366
}
360367
int delay = parser.value(delayOption).toInt();
361368
QString region = parser.value(regionOption);
369+
bool useLastRegion = parser.isSet(useLastRegionOption);
362370
bool clipboard = parser.isSet(clipboardOption);
363371
bool raw = parser.isSet(rawImageOption);
364372
bool printGeometry = parser.isSet(selectionOption);
@@ -367,7 +375,10 @@ int main(int argc, char* argv[])
367375
bool acceptOnSelect = parser.isSet(acceptOnSelectOption);
368376
CaptureRequest req(CaptureRequest::GRAPHICAL_MODE, delay, path);
369377
if (!region.isEmpty()) {
370-
req.setInitialSelection(Region().value(region).toRect());
378+
auto selectionRegion = Region().value(region).toRect();
379+
req.setInitialSelection(selectionRegion);
380+
} else if (useLastRegion) {
381+
req.setInitialSelection(getLastRegion());
371382
}
372383
if (clipboard) {
373384
req.addTask(CaptureRequest::COPY);
@@ -394,7 +405,6 @@ int main(int argc, char* argv[])
394405
req.addSaveTask();
395406
}
396407
}
397-
398408
requestCaptureAndWait(req);
399409
} else if (parser.isSet(fullArgument)) { // FULL
400410
// Recreate the application as a QApplication

src/utils/confighandler.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
101101
OPTION("savePath" ,ExistingDir ( )),
102102
OPTION("savePathFixed" ,Bool ( false )),
103103
OPTION("saveAsFileExtension" ,SaveFileExtension ( )),
104+
OPTION("saveLastRegion" ,Bool (false )),
104105
OPTION("uploadHistoryMax" ,LowerBoundedInt (0, 25 )),
105106
OPTION("undoLimit" ,BoundedInt (0, 999, 100 )),
106-
// Interface tab
107+
// Interface tab
107108
OPTION("uiColor" ,Color ( {116, 0, 150} )),
108109
OPTION("contrastUiColor" ,Color ( {39, 0, 50} )),
109110
OPTION("contrastOpacity" ,BoundedInt ( 0, 255, 190 )),

src/utils/confighandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ConfigHandler : public QObject
119119
CONFIG_GETTER_SETTER(squareMagnifier, setSquareMagnifier, bool)
120120
CONFIG_GETTER_SETTER(copyOnDoubleClick, setCopyOnDoubleClick, bool)
121121
CONFIG_GETTER_SETTER(uploadClientSecret, setUploadClientSecret, QString)
122-
122+
CONFIG_GETTER_SETTER(saveLastRegion, setSaveLastRegion, bool)
123123
// SPECIAL CASES
124124
bool startupLaunch();
125125
void setStartupLaunch(const bool);

0 commit comments

Comments
 (0)