Skip to content

Commit

Permalink
Add auto save for new captures and store path for save captures #291 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirPorobic committed Apr 4, 2020
1 parent 35ed858 commit d0b6cc3
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 77 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@
* New: Pasting image or path to image from clipboard. ([#275](https://github.com/ksnip/ksnip/issues/275))
* New: Save to same file when editing existing image. ([#271](https://github.com/ksnip/ksnip/issues/271))
* New: Support for PrtScrn hotkey. ([#239](https://github.com/ksnip/ksnip/issues/239))
* New: Auto save new screenshot. ([#291](https://github.com/ksnip/ksnip/issues/291))
* New: Remember file for already saved images. ([#292](https://github.com/ksnip/ksnip/issues/292))
* Changed: Save As option was added and useInstantSave config was removed. ([#285](https://github.com/ksnip/ksnip/issues/285))
* Fixed: Compilation error with Qt 5.15. ([#279](https://github.com/ksnip/ksnip/issues/279))

Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -21,6 +21,7 @@ set(KSNIP_SRCS
${CMAKE_SOURCE_DIR}/src/common/loader/IconLoader.cpp
${CMAKE_SOURCE_DIR}/src/common/handler/DelayHandler.cpp
${CMAKE_SOURCE_DIR}/src/common/provider/ScaledSizeProvider.cpp
${CMAKE_SOURCE_DIR}/src/common/provider/ApplicationTitleProvider.cpp
${CMAKE_SOURCE_DIR}/src/widgets/CustomToolButton.cpp
${CMAKE_SOURCE_DIR}/src/widgets/CustomCursor.cpp
${CMAKE_SOURCE_DIR}/src/widgets/CursorFactory.cpp
Expand Down
23 changes: 18 additions & 5 deletions src/backend/config/KsnipConfig.cpp
Expand Up @@ -48,17 +48,30 @@ void KsnipConfig::setPromptSaveBeforeExit(bool enabled)
saveValue(KsnipConfigOptions::promptSaveBeforeExitString(), enabled);
}

bool KsnipConfig::alwaysCopyToClipboard() const
bool KsnipConfig::autoCopyToClipboardNewCaptures() const
{
return loadValue(KsnipConfigOptions::alwaysCopyToClipboardString(), false).toBool();
return loadValue(KsnipConfigOptions::autoCopyToClipboardNewCapturesString(), false).toBool();
}

void KsnipConfig::setAlwaysCopyToClipboard(bool enabled)
void KsnipConfig::setAutoCopyToClipboardNewCaptures(bool enabled)
{
if (alwaysCopyToClipboard() == enabled) {
if (autoCopyToClipboardNewCaptures() == enabled) {
return;
}
saveValue(KsnipConfigOptions::alwaysCopyToClipboardString(), enabled);
saveValue(KsnipConfigOptions::autoCopyToClipboardNewCapturesString(), enabled);
}

bool KsnipConfig::autoSaveNewCaptures() const
{
return loadValue(KsnipConfigOptions::autoSaveNewCapturesString(), false).toBool();
}

void KsnipConfig::setAutoSaveNewCaptures(bool enabled)
{
if (autoSaveNewCaptures() == enabled) {
return;
}
saveValue(KsnipConfigOptions::autoSaveNewCapturesString(), enabled);
}

bool KsnipConfig::saveToolSelection() const
Expand Down
7 changes: 5 additions & 2 deletions src/backend/config/KsnipConfig.h
Expand Up @@ -47,8 +47,11 @@ class KsnipConfig : public QObject
virtual bool promptSaveBeforeExit() const;
virtual void setPromptSaveBeforeExit(bool enabled);

virtual bool alwaysCopyToClipboard() const;
virtual void setAlwaysCopyToClipboard(bool enabled);
virtual bool autoCopyToClipboardNewCaptures() const;
virtual void setAutoCopyToClipboardNewCaptures(bool enabled);

virtual bool autoSaveNewCaptures() const;
virtual void setAutoSaveNewCaptures(bool enabled);

virtual bool saveToolSelection() const;
virtual void setSaveToolSelection(bool enabled);
Expand Down
9 changes: 7 additions & 2 deletions src/backend/config/KsnipConfigOptions.cpp
Expand Up @@ -29,9 +29,14 @@ QString KsnipConfigOptions::promptSaveBeforeExitString()
return applicationSectionString() + QStringLiteral("PromptSaveBeforeExit");
}

QString KsnipConfigOptions::alwaysCopyToClipboardString()
QString KsnipConfigOptions::autoCopyToClipboardNewCapturesString()
{
return applicationSectionString() + QStringLiteral("AlwaysCopyToClipboard");
return applicationSectionString() + QStringLiteral("AutoCopyToClipboardNewCaptures");
}

QString KsnipConfigOptions::autoSaveNewCapturesString()
{
return applicationSectionString() + QStringLiteral("AutoSaveNewCaptures");
}

QString KsnipConfigOptions::saveToolSelectionString()
Expand Down
3 changes: 2 additions & 1 deletion src/backend/config/KsnipConfigOptions.h
Expand Up @@ -27,7 +27,8 @@ class KsnipConfigOptions
public:
static QString savePositionString();
static QString promptSaveBeforeExitString();
static QString alwaysCopyToClipboardString();
static QString autoCopyToClipboardNewCapturesString();
static QString autoSaveNewCapturesString();
static QString saveToolSelectionString();
static QString captureOnStartupString();
static QString freezeImageWhileSnippingEnabledString();
Expand Down
5 changes: 2 additions & 3 deletions src/common/dtos/CaptureFromFileDto.h
Expand Up @@ -21,11 +21,10 @@
#define KSNIP_CAPTUREFROMFILEDTO_H

#include "CaptureDto.h"
#include "FilePathDto.h"

struct CaptureFromFileDto : CaptureDto
struct CaptureFromFileDto : public CaptureDto, public FilePathDto
{
QString path;

explicit CaptureFromFileDto(const QPixmap &screenshot, const QString &path) : CaptureDto(screenshot){
this->path = path;
}
Expand Down
28 changes: 28 additions & 0 deletions src/common/dtos/FilePathDto.h
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_ABSTRACTFILEPATH_H
#define KSNIP_ABSTRACTFILEPATH_H

struct FilePathDto
{
QString path;
};

#endif //KSNIP_ABSTRACTFILEPATH_H
35 changes: 35 additions & 0 deletions src/common/dtos/SaveResultDto.h
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_SAVERESULTDTO_H
#define KSNIP_SAVERESULTDTO_H

#include "FilePathDto.h"

struct SaveResultDto : public FilePathDto
{
bool isSuccessful;

explicit SaveResultDto(bool isSuccessful, const QString &path) {
this->isSuccessful = isSuccessful;
this->path = path;
}
};

#endif //KSNIP_SAVERESULTDTO_H
35 changes: 35 additions & 0 deletions src/common/provider/ApplicationTitleProvider.cpp
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include "ApplicationTitleProvider.h"

QString ApplicationTitleProvider::getApplicationTitle(const QString &applicationName, const QString &pathToImage, const QString &unsavedString, bool isUnsaved)
{
auto applicationTitle = applicationName;

if(!pathToImage.isEmpty()) {
applicationTitle = applicationTitle + QStringLiteral(" [") + pathToImage + QStringLiteral("]");
}

if (isUnsaved) {
applicationTitle = QStringLiteral("*") + applicationTitle + " - " + unsavedString;
}

return applicationTitle;
}
31 changes: 31 additions & 0 deletions src/common/provider/ApplicationTitleProvider.h
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef KSNIP_APPLICATIONTITLEPROVIDER_H
#define KSNIP_APPLICATIONTITLEPROVIDER_H

#include <QString>

class ApplicationTitleProvider
{
public:
static QString getApplicationTitle(const QString &applicationName, const QString &pathToImage, const QString &unsavedString, bool isUnsaved);
};

#endif //KSNIP_APPLICATIONTITLEPROVIDER_H
79 changes: 49 additions & 30 deletions src/gui/MainWindow.cpp
Expand Up @@ -64,7 +64,7 @@ MainWindow::MainWindow(AbstractImageGrabber *imageGrabber, RunMode mode) :

connect(mKImageAnnotator, &KImageAnnotator::imageChanged, this, &MainWindow::screenshotChanged);

connect(mImageGrabber, &AbstractImageGrabber::finished, this, &MainWindow::showCapture);
connect(mImageGrabber, &AbstractImageGrabber::finished, this, &MainWindow::processCapture);
connect(mImageGrabber, &AbstractImageGrabber::canceled, [this]()
{ setHidden(false); });

Expand Down Expand Up @@ -132,10 +132,6 @@ void MainWindow::processInstantCapture(const CaptureDto &capture)
void MainWindow::screenshotChanged()
{
setSaveable(true);

if (mConfig->alwaysCopyToClipboard()) {
copyCaptureToClipboard();
}
}

//
Expand All @@ -156,7 +152,7 @@ void MainWindow::quit()
QCoreApplication::exit(0);
}

void MainWindow::showCapture(const CaptureDto &capture)
void MainWindow::processCapture(const CaptureDto &capture)
{
if (!capture.isValid()) {
NotifyOperation operation(mTrayIcon, tr("Unable to show image"), tr("No image provided to but one was expected."), NotificationTypes::Critical);
Expand All @@ -165,25 +161,50 @@ void MainWindow::showCapture(const CaptureDto &capture)
return;
}

loadCapture(capture);
updatePathToImageSource(capture);
showImage(capture);
setSaveable(true);
capturePostProcessing();
}

if (mConfig->alwaysCopyToClipboard()) {
copyCaptureToClipboard();
}
void MainWindow::processImage(const CaptureDto &capture)
{
showImage(capture);
setSaveable(false);
}

void MainWindow::showImage(const CaptureDto &capture)
{
loadCapture(capture);
updatePathToImageFromCapture(capture);

setHidden(false);
setSaveable(true);
setEnablements(true);

adjustSize();
MainWindow::show();
show();
}

void MainWindow::capturePostProcessing()
{
if (mConfig->autoCopyToClipboardNewCaptures()) {
copyCaptureToClipboard();
}

if (mConfig->autoSaveNewCaptures()) {
saveCapture(true);
}
}

void MainWindow::updatePathToImageSource(const CaptureDto &capture)
void MainWindow::updatePathToImageFromCapture(const CaptureDto &capture)
{
auto captureFromFileDto = dynamic_cast<const CaptureFromFileDto*>(&capture);
mPathToImageSource = captureFromFileDto != nullptr ? captureFromFileDto->path : QString();
auto pathToImage = captureFromFileDto != nullptr ? captureFromFileDto->path : QString();
updatePathToImage(pathToImage);
}

void MainWindow::updatePathToImage(const QString &pathToImage)
{
mPathToImageSource = pathToImage;
}

void MainWindow::loadCapture(const CaptureDto &capture)
Expand Down Expand Up @@ -262,20 +283,17 @@ void MainWindow::changeEvent(QEvent *event)
// Private Functions
//

void MainWindow::setSaveable(bool enabled)
void MainWindow::setSaveable(bool isUnsaved)
{
setupApplicationName(enabled);
mIsUnsaved = enabled;
mToolBar->setSaveActionEnabled(enabled);
mIsUnsaved = isUnsaved;
mToolBar->setSaveActionEnabled(isUnsaved);
updateApplicationTitle();
}

void MainWindow::setupApplicationName(bool isUnsaved)
void MainWindow::updateApplicationTitle()
{
if (isUnsaved) {
setWindowTitle(QStringLiteral("*") + QApplication::applicationName() + " - " + tr("Unsaved"));
} else {
setWindowTitle(QApplication::applicationName());
}
auto applicationTitle = ApplicationTitleProvider::getApplicationTitle(QApplication::applicationName(), mPathToImageSource, tr("Unsaved"), mIsUnsaved);
setWindowTitle(applicationTitle);
}

void MainWindow::setEnablements(bool enabled)
Expand Down Expand Up @@ -446,9 +464,10 @@ void MainWindow::saveCapture(bool saveInstant)
{
auto image = mKImageAnnotator->image();
SaveOperation operation(this, image, saveInstant, mPathToImageSource, mTrayIcon);
bool successful = operation.execute();
auto saveResult = operation.execute();
updatePathToImage(saveResult.path);

setSaveable(!successful);
setSaveable(!saveResult.isSuccessful);
}

void MainWindow::copyCaptureToClipboard()
Expand Down Expand Up @@ -506,7 +525,7 @@ void MainWindow::loadImageFromFile()
if(!pixmap.isNull()) {
setHidden(false);
CaptureFromFileDto captureDto(pixmap, path);
showCapture(captureDto);
processImage(captureDto);
}
}

Expand Down Expand Up @@ -580,10 +599,10 @@ void MainWindow::pasteImageFromClipboard()
setHidden(false);
if(mClipboard->url().isNull()) {
CaptureDto captureDto(pixmap);
showCapture(captureDto);
processImage(captureDto);
} else {
CaptureFromFileDto captureDto(pixmap, mClipboard->url());
showCapture(captureDto);
processImage(captureDto);
}
}
}
Expand Down

0 comments on commit d0b6cc3

Please sign in to comment.