File indexing completed on 2024-05-12 04:19:35

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2022 Ilya Pominov <ipominov@astralinux.ru>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (at your option) any later version.
0009 
0010 This program is distributed in the hope that it will be useful,
0011 but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 GNU General Public License for more details.
0014 
0015 You should have received a copy of the GNU General Public License
0016 along with this program; if not, write to the Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018 
0019 */
0020 // Self
0021 #include "bcgtool.h"
0022 
0023 // Qt
0024 #include <QAbstractButton>
0025 #include <QDialogButtonBox>
0026 #include <QPainter>
0027 
0028 // KF
0029 
0030 // Local
0031 #include "bcgimageoperation.h"
0032 #include "bcgwidget.h"
0033 #include <lib/documentview/rasterimageview.h>
0034 
0035 namespace Gwenview
0036 {
0037 struct BCGToolPrivate {
0038     BCGImageOperation::BrightnessContrastGamma getBcg() const
0039     {
0040         BCGImageOperation::BrightnessContrastGamma bcg;
0041         bcg.brightness = mBCGWidget->brightness();
0042         bcg.contrast = mBCGWidget->contrast();
0043         bcg.gamma = mBCGWidget->gamma();
0044         return bcg;
0045     }
0046 
0047     BCGTool *q = nullptr;
0048     BCGWidget *mBCGWidget = nullptr;
0049     QImage mImage;
0050 };
0051 
0052 BCGTool::BCGTool(RasterImageView *view)
0053     : AbstractRasterImageViewTool(view)
0054     , d(new BCGToolPrivate)
0055 {
0056     d->q = this;
0057     d->mBCGWidget = new BCGWidget;
0058     connect(d->mBCGWidget, &BCGWidget::bcgChanged, this, &BCGTool::slotBCGRequested);
0059     connect(d->mBCGWidget, &BCGWidget::done, this, [this](bool accept) {
0060         if (accept) {
0061             auto op = new BCGImageOperation(d->getBcg());
0062             Q_EMIT imageOperationRequested(op);
0063         }
0064         Q_EMIT done();
0065     });
0066 
0067     d->mImage = view->document()->image();
0068 }
0069 
0070 BCGTool::~BCGTool()
0071 {
0072     // mBCGWidget is a child of its container not of us, so it is not deleted automatically
0073     delete d->mBCGWidget;
0074     delete d;
0075 }
0076 
0077 void BCGTool::paint(QPainter *painter)
0078 {
0079     const QRectF docRect = imageView()->mapToView(QRectF(QPointF(), imageView()->documentSize()));
0080     painter->eraseRect(docRect);
0081 
0082     painter->drawImage(docRect, d->mImage);
0083 }
0084 
0085 void BCGTool::keyPressEvent(QKeyEvent *event)
0086 {
0087     auto buttons = d->mBCGWidget->findChild<QDialogButtonBox *>();
0088     switch (event->key()) {
0089     case Qt::Key_Escape:
0090         event->accept();
0091         Q_EMIT buttons->rejected();
0092         break;
0093     case Qt::Key_Return:
0094     case Qt::Key_Enter: {
0095         event->accept();
0096         auto focusButton = static_cast<QAbstractButton *>(buttons->focusWidget());
0097         if (focusButton && buttons->buttonRole(focusButton) == QDialogButtonBox::RejectRole) {
0098             Q_EMIT buttons->rejected();
0099         } else {
0100             Q_EMIT buttons->accepted();
0101         }
0102         break;
0103     }
0104     default:
0105         break;
0106     }
0107 }
0108 
0109 QWidget *BCGTool::widget() const
0110 {
0111     return d->mBCGWidget;
0112 }
0113 
0114 void BCGTool::slotBCGRequested()
0115 {
0116     d->mImage = imageView()->document()->image();
0117     BCGImageOperation::apply(d->mImage, d->getBcg());
0118     imageView()->update();
0119 }
0120 
0121 } // namespace
0122 
0123 #include "moc_bcgtool.cpp"