File indexing completed on 2024-04-28 04:18:53

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2009 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "zoomslider.h"
0023 
0024 // Qt
0025 #include <QAction>
0026 #include <QHBoxLayout>
0027 #include <QIcon>
0028 #include <QProxyStyle>
0029 #include <QSlider>
0030 #include <QToolButton>
0031 
0032 // KF
0033 
0034 // Local
0035 
0036 namespace Gwenview
0037 {
0038 class ZoomSliderStyle : public QProxyStyle
0039 {
0040 public:
0041     explicit ZoomSliderStyle(QObject *parent = nullptr)
0042         : QProxyStyle()
0043     {
0044         setParent(parent);
0045     }
0046 
0047     int styleHint(QStyle::StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const override
0048     {
0049         int styleHint = QProxyStyle::styleHint(hint, option, widget, returnData);
0050         if (hint == QStyle::SH_Slider_AbsoluteSetButtons) {
0051             styleHint |= Qt::LeftButton;
0052         }
0053 
0054         return styleHint;
0055     }
0056 };
0057 
0058 struct ZoomSliderPrivate {
0059     QToolButton *mZoomOutButton = nullptr;
0060     QToolButton *mZoomInButton = nullptr;
0061     QSlider *mSlider = nullptr;
0062 
0063     void updateButtons()
0064     {
0065         // Use QSlider::sliderPosition(), not QSlider::value() because when we are
0066         // called from slotZoomSliderActionTriggered(), QSlider::value() has not
0067         // been updated yet.
0068         mZoomOutButton->setEnabled(mSlider->sliderPosition() > mSlider->minimum());
0069         mZoomInButton->setEnabled(mSlider->sliderPosition() < mSlider->maximum());
0070     }
0071 };
0072 
0073 static QToolButton *createZoomButton(const QString &iconName)
0074 {
0075     auto button = new QToolButton;
0076     button->setIcon(QIcon::fromTheme(iconName));
0077     button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
0078     button->setAutoRaise(true);
0079     button->setAutoRepeat(true);
0080     return button;
0081 }
0082 
0083 ZoomSlider::ZoomSlider(QWidget *parent)
0084     : QWidget(parent)
0085     , d(new ZoomSliderPrivate)
0086 {
0087     d->mZoomInButton = createZoomButton(QStringLiteral("zoom-in"));
0088     d->mZoomOutButton = createZoomButton(QStringLiteral("zoom-out"));
0089 
0090     d->mSlider = new QSlider;
0091     d->mSlider->setOrientation(Qt::Horizontal);
0092     d->mSlider->setStyle(new ZoomSliderStyle(this));
0093 
0094     auto layout = new QHBoxLayout(this);
0095     layout->setContentsMargins(0, 0, 0, 0);
0096     layout->setSpacing(0);
0097     layout->addWidget(d->mZoomOutButton);
0098     layout->addWidget(d->mSlider);
0099     layout->addWidget(d->mZoomInButton);
0100 
0101     connect(d->mSlider, &QSlider::actionTriggered, this, &ZoomSlider::slotActionTriggered);
0102     connect(d->mSlider, &QSlider::valueChanged, this, &ZoomSlider::valueChanged);
0103 
0104     connect(d->mZoomOutButton, &QToolButton::clicked, this, &ZoomSlider::zoomOut);
0105     connect(d->mZoomInButton, &QToolButton::clicked, this, &ZoomSlider::zoomIn);
0106 }
0107 
0108 ZoomSlider::~ZoomSlider()
0109 {
0110     delete d;
0111 }
0112 
0113 int ZoomSlider::value() const
0114 {
0115     return d->mSlider->value();
0116 }
0117 
0118 void ZoomSlider::setValue(int value)
0119 {
0120     d->mSlider->setValue(value);
0121     d->updateButtons();
0122 }
0123 
0124 void ZoomSlider::setMinimum(int value)
0125 {
0126     d->mSlider->setMinimum(value);
0127     d->updateButtons();
0128 }
0129 
0130 void ZoomSlider::setMaximum(int value)
0131 {
0132     d->mSlider->setMaximum(value);
0133     d->updateButtons();
0134 }
0135 
0136 void ZoomSlider::setZoomInAction(QAction *action)
0137 {
0138     disconnect(d->mZoomInButton, &QToolButton::clicked, this, &ZoomSlider::zoomIn);
0139     d->mZoomInButton->setDefaultAction(action);
0140 }
0141 
0142 void ZoomSlider::setZoomOutAction(QAction *action)
0143 {
0144     disconnect(d->mZoomOutButton, &QToolButton::clicked, this, &ZoomSlider::zoomOut);
0145     d->mZoomOutButton->setDefaultAction(action);
0146 }
0147 
0148 void ZoomSlider::slotActionTriggered(int)
0149 {
0150     d->updateButtons();
0151 }
0152 
0153 void ZoomSlider::zoomOut()
0154 {
0155     if (!d->mZoomOutButton->defaultAction()) {
0156         d->mSlider->triggerAction(QAbstractSlider::SliderPageStepSub);
0157     }
0158 }
0159 
0160 void ZoomSlider::zoomIn()
0161 {
0162     if (!d->mZoomInButton->defaultAction()) {
0163         d->mSlider->triggerAction(QAbstractSlider::SliderPageStepAdd);
0164     }
0165 }
0166 
0167 QSlider *ZoomSlider::slider() const
0168 {
0169     return d->mSlider;
0170 }
0171 
0172 } // namespace
0173 
0174 #include "moc_zoomslider.cpp"