Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to cache the last region #2615

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions src/config/cacheutils.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions src/config/cacheutils.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ GeneralConf::GeneralConf(QWidget* parent)
initScrollArea();

initShowHelp();
initSaveLastRegion();
initShowSidePanelButton();
initShowDesktopNotification();
initShowTrayIcon();
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/config/generalconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -72,6 +73,7 @@ private slots:
void initUseJpgForClipboard();
void initUploadHistoryMax();
void initUploadClientSecret();
void initSaveLastRegion();

void _updateComponents(bool allowEmptySavePath);

Expand All @@ -91,6 +93,7 @@ private slots:
QCheckBox* m_copyAndCloseAfterUpload;
QCheckBox* m_copyPathAfterSave;
QCheckBox* m_antialiasingPinZoom;
QCheckBox* m_saveLastRegion;
QCheckBox* m_uploadWithoutConfirmation;
QPushButton* m_importButton;
QPushButton* m_exportButton;
Expand Down
13 changes: 8 additions & 5 deletions src/core/capturerequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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
{
Expand Down
27 changes: 11 additions & 16 deletions src/core/flameshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "flameshot.h"
#include "flameshotdaemon.h"

#if defined(Q_OS_MACOS)
#include "external/QHotkey/QHotkey"
#endif
Expand All @@ -25,7 +24,6 @@
#include <QApplication>
#include <QBuffer>
#include <QDebug>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QFile>
#include <QMessageBox>
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -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));
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/core/flameshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Flameshot : public QObject
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();
Expand Down
16 changes: 13 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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);
Expand All @@ -290,6 +296,7 @@ int main(int argc, char* argv[])
clipboardOption,
delayOption,
regionOption,
useLastRegionOption,
rawImageOption,
selectionOption,
uploadOption,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )),
Expand Down
2 changes: 1 addition & 1 deletion src/utils/confighandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ConfigHandler : public QObject
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);
Expand Down
Loading