File indexing completed on 2024-11-24 04:42:26

0001 /*
0002  *  slider.cpp  -  slider control with read-only option
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2004-2021 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "slider.h"
0010 
0011 #include <QLabel>
0012 #include <QMouseEvent>
0013 #include <QLocale>
0014 
0015 
0016 Slider::Slider(QWidget* parent)
0017     : QSlider(parent)
0018 { }
0019 
0020 Slider::Slider(Qt::Orientation o, QWidget* parent)
0021     : QSlider(o, parent)
0022 { }
0023 
0024 Slider::Slider(int minval, int maxval, int pageStep, Qt::Orientation o, QWidget* parent)
0025     : QSlider(o, parent)
0026 {
0027     setRange(minval, maxval);
0028     setPageStep(pageStep);
0029 }
0030 
0031 /******************************************************************************
0032 * Set the read-only status. If read-only, the slider can be moved by the
0033 * application, but not by the user.
0034 */
0035 void Slider::setReadOnly(bool ro)
0036 {
0037     mReadOnly = ro;
0038 }
0039 
0040 /******************************************************************************
0041 * Set a label to contain the slider's value.
0042 */
0043 void Slider::setValueLabel(QLabel* label, const QString& format, bool hideIfDisabled)
0044 {
0045     if (label != mValueLabel)
0046         delete mValueLabel;    // delete any existing label
0047     mValueLabel = label;
0048     if (mValueLabel)
0049     {
0050         mValueLabel->setParent(this);
0051         mValueFormat = format;
0052         mValueLabelHide = hideIfDisabled;
0053         connect(this, &QAbstractSlider::valueChanged, this, &Slider::valueHasChanged);
0054         connect(mValueLabel, &QObject::destroyed, this, &Slider::valueLabelDestroyed);
0055 
0056         // Substitute any '%' character with the locale's percent symbol.
0057         for (int i = 0;  i < mValueFormat.size();  ++i)
0058         {
0059             if (mValueFormat.at(i) == QLatin1Char('%'))
0060             {
0061                 if (i < mValueFormat.size() - 1  &&  mValueFormat.at(i + 1) == QLatin1Char('1'))
0062                 {
0063                     // Convert "%1" to "%L1" to display the value in localised form.
0064                     mValueFormat.insert(++i, QLatin1Char('L'));
0065                     continue;
0066                 }
0067                 mValueFormat.replace(i, 1, QLocale().percent());
0068             }
0069         }
0070     }
0071     else
0072         disconnect(this, &QAbstractSlider::valueChanged, this, &Slider::valueHasChanged);
0073 }
0074 
0075 /******************************************************************************
0076 * Sets the visibility of the slider.
0077 * This also sets the visibility of the value label.
0078 */
0079 void Slider::setVisible(bool vis)
0080 {
0081     QSlider::setVisible(vis);
0082     if (mValueLabel)
0083     {
0084         if (mValueLabelHide  &&  !isEnabled())
0085             vis = false;
0086         mValueLabel->setVisible(vis);
0087     }
0088 }
0089 
0090 /******************************************************************************
0091 * Called when the slider's status has changed.
0092 * If it is enabled or disabled, show or hide the value label if required.
0093 */
0094 void Slider::changeEvent(QEvent* e)
0095 {
0096     if (e->type() == QEvent::EnabledChange)
0097     {
0098         if (mValueLabel  &&  mValueLabelHide  &&  isVisible())
0099             mValueLabel->setVisible(isEnabled());
0100     }
0101 }
0102 
0103 /******************************************************************************
0104 * Called when the slider's value has changed.
0105 */
0106 void Slider::valueHasChanged(int value)
0107 {
0108     if (mValueLabel)
0109         mValueLabel->setText(mValueFormat.arg(value));
0110 }
0111 
0112 /******************************************************************************
0113 * Called when the value label is destroyed.
0114 */
0115 void Slider::valueLabelDestroyed(QObject* obj)
0116 {
0117     if (obj == mValueLabel)
0118         mValueLabel = nullptr;
0119 }
0120 
0121 /******************************************************************************
0122 * Event handlers to intercept events if in read-only mode.
0123 * Any events which could change the slider value are discarded.
0124 */
0125 void Slider::mousePressEvent(QMouseEvent* e)
0126 {
0127     if (mReadOnly)
0128     {
0129         // Swallow up the event if it's the left button
0130         if (e->button() == Qt::LeftButton)
0131             return;
0132     }
0133     QSlider::mousePressEvent(e);
0134 }
0135 
0136 void Slider::mouseReleaseEvent(QMouseEvent* e)
0137 {
0138     if (!mReadOnly)
0139         QSlider::mouseReleaseEvent(e);
0140 }
0141 
0142 void Slider::mouseMoveEvent(QMouseEvent* e)
0143 {
0144     if (!mReadOnly)
0145         QSlider::mouseMoveEvent(e);
0146 }
0147 
0148 void Slider::keyPressEvent(QKeyEvent* e)
0149 {
0150     if (!mReadOnly  ||  e->key() == Qt::Key_Escape)
0151         QSlider::keyPressEvent(e);
0152 }
0153 
0154 void Slider::keyReleaseEvent(QKeyEvent* e)
0155 {
0156     if (!mReadOnly)
0157         QSlider::keyReleaseEvent(e);
0158 }
0159 
0160 #include "moc_slider.cpp"
0161 
0162 // vim: et sw=4: