File indexing completed on 2025-01-19 04:01:38

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 "bcgwidget.h"
0022 
0023 // Qt
0024 #include <QDialogButtonBox>
0025 #include <QFormLayout>
0026 #include <QPushButton>
0027 #include <QSlider>
0028 #include <QSpinBox>
0029 #include <QTimer>
0030 #include <QVBoxLayout>
0031 
0032 // KF
0033 #include <KLocalizedString>
0034 
0035 // Local
0036 #include "gwenview_lib_debug.h"
0037 
0038 namespace Gwenview
0039 {
0040 struct BCGWidgetPrivate {
0041     void setupWidgets()
0042     {
0043         mCompressor = new QTimer(q);
0044         mCompressor->setInterval(10);
0045         mCompressor->setSingleShot(true);
0046         QObject::connect(mCompressor, &QTimer::timeout, q, [this]() {
0047             Q_EMIT q->bcgChanged();
0048         });
0049 
0050         auto sliderLayout = new QFormLayout;
0051         sliderLayout->addRow(i18n("Brightness:"), createSlider(&mBrightness));
0052         sliderLayout->addRow(i18n("Contrast:"), createSlider(&mContrast));
0053         sliderLayout->addRow(i18n("Gamma:"), createSlider(&mGamma));
0054 
0055         auto layout = new QVBoxLayout;
0056         layout->addLayout(sliderLayout);
0057 
0058         auto btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Reset);
0059         mOkBtn = btnBox->button(QDialogButtonBox::Ok);
0060         mOkBtn->setEnabled(false);
0061         mResetBtn = btnBox->button(QDialogButtonBox::Reset);
0062         mResetBtn->setEnabled(false);
0063         QObject::connect(btnBox, &QDialogButtonBox::rejected, q, [this]() {
0064             Q_EMIT q->done(false);
0065         });
0066         QObject::connect(btnBox, &QDialogButtonBox::accepted, q, [this]() {
0067             Q_EMIT q->done(true);
0068         });
0069         QObject::connect(btnBox->button(QDialogButtonBox::Reset), &QAbstractButton::clicked, q, &BCGWidget::reset);
0070         layout->addWidget(btnBox);
0071 
0072         q->setLayout(layout);
0073     }
0074 
0075     QLayout *createSlider(int *field)
0076     {
0077         auto slider = new QSlider(Qt::Horizontal);
0078         slider->setMinimum(-100);
0079         slider->setMaximum(100);
0080         auto spinBox = new QSpinBox;
0081         spinBox->setMinimum(-100);
0082         spinBox->setMaximum(100);
0083 
0084         QObject::connect(slider, &QSlider::valueChanged, spinBox, &QSpinBox::setValue);
0085         QObject::connect(spinBox, qOverload<int>(&QSpinBox::valueChanged), q, [this, slider, field](int value) {
0086             slider->blockSignals(true);
0087             slider->setValue(value);
0088             slider->blockSignals(false);
0089             *field = value;
0090             bool bcgChanged = mBrightness != 0 || mContrast != 0 || mGamma != 0;
0091             mOkBtn->setEnabled(bcgChanged);
0092             mResetBtn->setEnabled(bcgChanged);
0093             mCompressor->start();
0094         });
0095         QObject::connect(q, &BCGWidget::reset, q, [slider]() {
0096             slider->setValue(0);
0097         });
0098 
0099         auto layout = new QHBoxLayout;
0100         layout->addWidget(slider);
0101         layout->addWidget(spinBox);
0102         return layout;
0103     }
0104 
0105     BCGWidget *q = nullptr;
0106     QTimer *mCompressor = nullptr;
0107     QAbstractButton *mOkBtn = nullptr;
0108     QAbstractButton *mResetBtn = nullptr;
0109     int mBrightness = 0;
0110     int mContrast = 0;
0111     int mGamma = 0;
0112 };
0113 
0114 BCGWidget::BCGWidget()
0115     : QWidget(nullptr)
0116     , d(new BCGWidgetPrivate)
0117 {
0118     setWindowFlags(Qt::Tool);
0119 
0120     d->q = this;
0121     d->setupWidgets();
0122 }
0123 
0124 BCGWidget::~BCGWidget()
0125 {
0126     delete d;
0127 }
0128 
0129 int BCGWidget::brightness() const
0130 {
0131     return d->mBrightness;
0132 }
0133 
0134 int BCGWidget::contrast() const
0135 {
0136     return d->mContrast;
0137 }
0138 
0139 int BCGWidget::gamma() const
0140 {
0141     return d->mGamma;
0142 }
0143 
0144 } // namespace
0145 
0146 #include "moc_bcgwidget.cpp"