diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt index 6e0c25070e..eb9ae90197 100644 --- a/src/config/CMakeLists.txt +++ b/src/config/CMakeLists.txt @@ -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 ) diff --git a/src/config/cacheutils.cpp b/src/config/cacheutils.cpp new file mode 100644 index 0000000000..b84ead9c74 --- /dev/null +++ b/src/config/cacheutils.cpp @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// SPDX-FileCopyrightText: 2021 Jeremy Borgman + +#include "cacheutils.h" +#include +#include +#include +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/src/config/cacheutils.h b/src/config/cacheutils.h new file mode 100644 index 0000000000..ad82ca4a97 --- /dev/null +++ b/src/config/cacheutils.h @@ -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 \ No newline at end of file diff --git a/src/config/generalconf.cpp b/src/config/generalconf.cpp index 696136892f..448f297971 100644 --- a/src/config/generalconf.cpp +++ b/src/config/generalconf.cpp @@ -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); diff --git a/src/config/generalconf.h b/src/config/generalconf.h index a24498817d..a1178cf576 100644 --- a/src/config/generalconf.h +++ b/src/config/generalconf.h @@ -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 slots: void initUseJpgForClipboard(); void initUploadHistoryMax(); void initUploadClientSecret(); + void initSaveLastRegion(); void _updateComponents(bool allowEmptySavePath); @@ -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; diff --git a/src/core/capturerequest.cpp b/src/core/capturerequest.cpp index eec0ceb087..2c58c5b0ba 100644 --- a/src/core/capturerequest.cpp +++ b/src/core/capturerequest.cpp @@ -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 #include #include -#include #include #include @@ -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 { diff --git a/src/core/flameshot.cpp b/src/core/flameshot.cpp index 196e878932..26e95b948a 100644 --- a/src/core/flameshot.cpp +++ b/src/core/flameshot.cpp @@ -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 #include #include -#include #include #include #include @@ -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(); diff --git a/src/core/flameshot.h b/src/core/flameshot.h index c738a73730..ec8ce98c6c 100644 --- a/src/core/flameshot.h +++ b/src/core/flameshot.h @@ -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(); diff --git a/src/main.cpp b/src/main.cpp index 1d4ddd1b03..86d7a1c5b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 #include #include - #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 diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index 2e7a59875a..8f53cba926 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -101,9 +101,10 @@ static QMap> 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 )), diff --git a/src/utils/confighandler.h b/src/utils/confighandler.h index 3e8322d29c..3be5953a85 100644 --- a/src/utils/confighandler.h +++ b/src/utils/confighandler.h @@ -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); diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index 8dfbe03eeb..e436dc9906 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -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(); diff --git a/src/widgets/capturelauncher.cpp b/src/widgets/capturelauncher.cpp index 121bdfb888..39a6d7c0f5 100644 --- a/src/widgets/capturelauncher.cpp +++ b/src/widgets/capturelauncher.cpp @@ -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 -#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 @@ -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; -} diff --git a/src/widgets/capturelauncher.h b/src/widgets/capturelauncher.h index ce273483e9..0e0baf0355 100644 --- a/src/widgets/capturelauncher.h +++ b/src/widgets/capturelauncher.h @@ -23,13 +23,9 @@ class CaptureLauncher : public QDialog 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(); \ No newline at end of file diff --git a/src/widgets/trayicon.cpp b/src/widgets/trayicon.cpp index 9d1137a775..3221118f99 100644 --- a/src/widgets/trayicon.cpp +++ b/src/widgets/trayicon.cpp @@ -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);