diff --git a/app/imageopscontextmanageritem.h b/app/imageopscontextmanageritem.h --- a/app/imageopscontextmanageritem.h +++ b/app/imageopscontextmanageritem.h @@ -57,6 +57,7 @@ private: struct Private; Private* const d; + void resetCropState(); }; } // namespace diff --git a/app/imageopscontextmanageritem.cpp b/app/imageopscontextmanageritem.cpp --- a/app/imageopscontextmanageritem.cpp +++ b/app/imageopscontextmanageritem.cpp @@ -25,6 +25,7 @@ // Qt #include #include +#include #include "gwenview_app_debug.h" // KDE @@ -66,6 +67,7 @@ ImageOpsContextManagerItem* q; MainWindow* mMainWindow; SideBarGroup* mGroup; + QRect* mCropStateRect; QAction * mRotateLeftAction; QAction * mRotateRightAction; @@ -153,6 +155,7 @@ 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() { + delete d->mCropStateRect; delete d; } @@ -256,14 +260,36 @@ 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()) { diff --git a/lib/crop/croptool.h b/lib/crop/croptool.h --- a/lib/crop/croptool.h +++ b/lib/crop/croptool.h @@ -66,6 +66,7 @@ Q_SIGNALS: void rectUpdated(const QRect&); + void rectReset(); void done(); void imageOperationRequested(AbstractImageOperation*); diff --git a/lib/crop/croptool.cpp b/lib/crop/croptool.cpp --- a/lib/crop/croptool.cpp +++ b/lib/crop/croptool.cpp @@ -205,6 +205,8 @@ 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); diff --git a/lib/crop/cropwidget.h b/lib/crop/cropwidget.h --- a/lib/crop/cropwidget.h +++ b/lib/crop/cropwidget.h @@ -53,10 +53,12 @@ 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(); diff --git a/lib/crop/cropwidget.cpp b/lib/crop/cropwidget.cpp --- a/lib/crop/cropwidget.cpp +++ b/lib/crop/cropwidget.cpp @@ -275,6 +275,8 @@ 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 @@ // (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); } @@ -457,6 +459,19 @@ 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);