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

@@ -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;
}