From 9763513a256bfbbb14a63dfa7952c9673c637315 Mon Sep 17 00:00:00 2001 From: borgmanJeremy <46930769+borgmanJeremy@users.noreply.github.com> Date: Tue, 3 May 2022 19:43:52 -0500 Subject: [PATCH] Added the ability to define a region for a screen shot in the launcher (#2208) --- src/widgets/capturelauncher.cpp | 83 ++++++++++++++++++++++++++++++++- src/widgets/capturelauncher.h | 6 ++- src/widgets/capturelauncher.ui | 83 ++++++++++++++++++++++++++++++--- 3 files changed, 162 insertions(+), 10 deletions(-) diff --git a/src/widgets/capturelauncher.cpp b/src/widgets/capturelauncher.cpp index 2993cc34..121bdfb8 100644 --- a/src/widgets/capturelauncher.cpp +++ b/src/widgets/capturelauncher.cpp @@ -8,8 +8,10 @@ #include "src/utils/screengrabber.h" #include "src/utils/screenshotsaver.h" #include "src/widgets/imagelabel.h" +#include +#include #include - +#include // https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSWidget.cpp // https://github.com/KDE/spectacle/blob/941c1a517be82bed25d1254ebd735c29b0d2951c/src/Gui/KSMainWindow.cpp @@ -57,13 +59,72 @@ CaptureLauncher::CaptureLauncher(QDialog* parent) this, &CaptureLauncher::startCapture); + connect(ui->captureType, + QOverload::of(&QComboBox::currentIndexChanged), + this, + [this]() { + auto mode = static_cast( + ui->captureType->currentData().toInt()); + if (mode == CaptureRequest::CaptureMode::GRAPHICAL_MODE) { + ui->sizeLabel->show(); + ui->screenshotX->show(); + ui->screenshotY->show(); + ui->screenshotWidth->show(); + ui->screenshotHeight->show(); + } else { + ui->sizeLabel->hide(); + ui->screenshotX->hide(); + ui->screenshotY->hide(); + ui->screenshotWidth->hide(); + ui->screenshotHeight->hide(); + } + }); + + 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())); 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(); @@ -74,6 +135,14 @@ void CaptureLauncher::startCapture() CaptureRequest req(mode, additionalDelayToHideUI + ui->delayTime->value() * secondsToMilliseconds); + + if (mode == CaptureRequest::CaptureMode::GRAPHICAL_MODE) { + req.setInitialSelection(QRect(ui->screenshotX->text().toInt(), + ui->screenshotY->text().toInt(), + ui->screenshotWidth->text().toInt(), + ui->screenshotHeight->text().toInt())); + } + connectCaptureSlots(); Flameshot::instance()->requestCapture(req); } @@ -107,7 +176,7 @@ void CaptureLauncher::disconnectCaptureSlots() const &CaptureLauncher::onCaptureFailed); } -void CaptureLauncher::onCaptureTaken(QPixmap screenshot) +void CaptureLauncher::onCaptureTaken(QPixmap const& screenshot) { // MacOS specific, more details in the function disconnectCaptureSlots() disconnectCaptureSlots(); @@ -136,3 +205,13 @@ CaptureLauncher::~CaptureLauncher() { delete ui; } + +QString getCachePath() +{ + auto cachePath = + QStandardPaths::writableLocation(QStandardPaths::CacheLocation); + if (!QDir(cachePath).exists()) { + QDir().mkpath(cachePath); + } + return cachePath; +} diff --git a/src/widgets/capturelauncher.h b/src/widgets/capturelauncher.h index 00467573..ce273483 100644 --- a/src/widgets/capturelauncher.h +++ b/src/widgets/capturelauncher.h @@ -23,9 +23,13 @@ private: Ui::CaptureLauncher* ui; void connectCaptureSlots() const; void disconnectCaptureSlots() const; + void setLastRegion(); + QRect getLastRegion(); private slots: void startCapture(); - void onCaptureTaken(QPixmap p); + void onCaptureTaken(QPixmap const& p); void onCaptureFailed(); }; + +QString getCachePath(); \ No newline at end of file diff --git a/src/widgets/capturelauncher.ui b/src/widgets/capturelauncher.ui index d228a6f7..ae26f27f 100644 --- a/src/widgets/capturelauncher.ui +++ b/src/widgets/capturelauncher.ui @@ -6,8 +6,8 @@ 0 0 - 619 - 148 + 807 + 213 @@ -18,6 +18,18 @@ + + + 0 + 0 + + + + + 420 + 0 + + TextLabel @@ -55,6 +67,13 @@ + + + + + + + @@ -62,10 +81,16 @@ - - - - + + + + + 0 + 0 + + + + WxH+x+y @@ -82,12 +107,56 @@ + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + + + + + + 0 + 0 + + + + + + - + 0 0