Commit 3da2a238 authored by Mario Aichinger's avatar Mario Aichinger Committed by Nate Graham
Browse files

Add save and reset state functionality to the crop tool

Summary:
The state is saved after the crop tool emits CropTool::done and gets
recalled if a new CropTool is created. The state also gets reset by
hitting the newly added reset button in the QDialogButtonBox of the
CropTool. The state also gets reset if the saved crop rectangle is
not within the image which is about to be cropped.

The use case for this is cropping the multiple images to the same
area. Like removing boarders of a series of pictures taken in a
photo both.

Test Plan:
Select the first image. Use the crop tool to crop it to region.
Click "Crop".
Select an image with the same or a larger size.
Select the crop tool.
The selection area should now be the same as used on the first image.
Hitting the reset button should reset all parameters to the defaults.

Alternatively: instead of the second (equal or larger) image select a image smaller than the crop area.
Again use the crop tool.
The selection should now cover the whole image.

Reviewers: #gwenview, ngraham

Reviewed By: #gwenview, ngraham

Subscribers: ngraham

Tags: #gwenview

Differential Revision: https://phabricator.kde.org/D28480
parent 4f7d35d1
......@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// Qt
#include <QApplication>
#include <QAction>
#include <QRect>
#include "gwenview_app_debug.h"
// KDE
......@@ -66,6 +67,7 @@ struct ImageOpsContextManagerItem::Private
ImageOpsContextManagerItem* q;
MainWindow* mMainWindow;
SideBarGroup* mGroup;
QRect* mCropStateRect;
QAction * mRotateLeftAction;
QAction * mRotateRightAction;
......@@ -153,6 +155,7 @@ ImageOpsContextManagerItem::ImageOpsContextManagerItem(ContextManager* manager,
d->mGroup = new SideBarGroup(i18n("Image Operations"));
setWidget(d->mGroup);
EventWatcher::install(d->mGroup, QEvent::Show, this, SLOT(updateSideBarContent()));
d->mCropStateRect = new QRect;
d->setupActions();
updateActions();
connect(contextManager(), &ContextManager::selectionChanged,
......@@ -165,6 +168,7 @@ ImageOpsContextManagerItem::ImageOpsContextManagerItem(ContextManager* manager,
ImageOpsContextManagerItem::~ImageOpsContextManagerItem()
{
delete d->mCropStateRect;
delete d;
}
......@@ -256,14 +260,36 @@ void ImageOpsContextManagerItem::crop()
qCCritical(GWENVIEW_APP_LOG) << "No ImageView available!";
return;
}
CropTool* tool = new CropTool(imageView);
Document::Ptr doc = DocumentFactory::instance()->load(contextManager()->currentUrl());
QSize size = doc->size();
QRect sizeAsRect = QRect(0, 0, size.width(), size.height());
if (!d->mCropStateRect->isNull() && sizeAsRect.contains(*d->mCropStateRect)) {
tool->setRect(*d->mCropStateRect);
}
connect(tool, &CropTool::imageOperationRequested, this, &ImageOpsContextManagerItem::applyImageOperation);
connect(tool, &CropTool::done, this, &ImageOpsContextManagerItem::restoreDefaultImageViewTool);
connect(tool, &CropTool::rectReset, this, [this](){
this->resetCropState();
});
connect(tool, &CropTool::done, this, [this, tool]() {
this->d->mCropStateRect->setTopLeft(tool->rect().topLeft());
this->d->mCropStateRect->setSize(tool->rect().size());
this->restoreDefaultImageViewTool();
});
d->mMainWindow->setDistractionFreeMode(true);
imageView->setCurrentTool(tool);
}
void ImageOpsContextManagerItem::resetCropState()
{
// Set the rect to null (see QRect::isNull())
d->mCropStateRect->setRect(0, 0, -1, -1);
}
void ImageOpsContextManagerItem::startRedEyeReduction()
{
if (!d->ensureEditable()) {
......
......@@ -57,6 +57,7 @@ private Q_SLOTS:
private:
struct Private;
Private* const d;
void resetCropState();
};
} // namespace
......
......@@ -205,6 +205,8 @@ struct CropToolPrivate
q, SLOT(slotCropRequested()));
QObject::connect(mCropWidget, &CropWidget::done,
q, &CropTool::done);
QObject::connect(mCropWidget, &CropWidget::rectReset,
q, &CropTool::rectReset);
// This is needed when crop ratio set to Current Image, and the image is rotated
QObject::connect(view, &RasterImageView::imageRectUpdated, mCropWidget, &CropWidget::updateCropRatio);
......
......@@ -66,6 +66,7 @@ public:
Q_SIGNALS:
void rectUpdated(const QRect&);
void rectReset();
void done();
void imageOperationRequested(AbstractImageOperation*);
......
......@@ -275,6 +275,8 @@ struct CropWidgetPrivate : public QWidget
QObject::connect(dialogButtonBox, &QDialogButtonBox::accepted, q, &CropWidget::cropRequested);
QObject::connect(dialogButtonBox, &QDialogButtonBox::rejected, q, &CropWidget::done);
QPushButton *resetButton = dialogButtonBox->button(QDialogButtonBox::Reset);
connect(resetButton, &QPushButton::clicked, q, &CropWidget::reset);
}
QWidget* boxWidget(QWidget* parent = nullptr)
......@@ -368,7 +370,7 @@ struct CropWidgetPrivate : public QWidget
// (6) Dialog buttons
box = boxWidget(cropWidget);
dialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok, box);
dialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Reset | QDialogButtonBox::Ok, box);
box->layout()->addWidget(dialogButtonBox);
flowLayout->addWidget(box);
}
......@@ -453,6 +455,19 @@ QSizeF CropWidget::cropRatio() const
return d->chosenRatio();
}
void CropWidget::reset()
{
d->ratioComboBox->clearEditText();
d->ratioComboBox->setCurrentIndex(-1);
QSize size = d->mDocument->size();
d->leftSpinBox->setValue(0);
d->widthSpinBox->setValue(size.width());
d->topSpinBox->setValue(0);
d->heightSpinBox->setValue(size.height());
emit rectReset();
}
void CropWidget::setCropRatioIndex(int index)
{
d->ratioComboBox->setCurrentIndex(index);
......
......@@ -53,10 +53,12 @@ public:
int cropRatioIndex() const;
void setCropRatioIndex(int index);
QSizeF cropRatio() const;
void reset();
Q_SIGNALS:
void cropRequested();
void done();
void rectReset();
public Q_SLOTS:
void updateCropRatio();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment