Commit 0956df98 authored by David Jarvie's avatar David Jarvie
Browse files

Optionally show slider value in a QLabel

parent 2a0ea788
Pipeline #47396 passed with stage
in 18 minutes and 26 seconds
/*
* slider.cpp - slider control with read-only option
* Program: kalarm
* SPDX-FileCopyrightText: 2004-2019 David Jarvie <djarvie@kde.org>
* SPDX-FileCopyrightText: 2004-2021 David Jarvie <djarvie@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "slider.h"
#include <QLabel>
#include <QMouseEvent>
#include <QDebug>
Slider::Slider(QWidget* parent)
: QSlider(parent)
......@@ -27,8 +29,8 @@ Slider::Slider(int minval, int maxval, int pageStep, Qt::Orientation o, QWidget*
}
/******************************************************************************
* Set the read-only status. If read-only, the slider can be moved by the
* application, but not by the user.
* Set the read-only status. If read-only, the slider can be moved by the
* application, but not by the user.
*/
void Slider::setReadOnly(bool ro)
{
......@@ -36,8 +38,74 @@ void Slider::setReadOnly(bool ro)
}
/******************************************************************************
* Event handlers to intercept events if in read-only mode.
* Any events which could change the slider value are discarded.
* Set a label to contain the slider's value.
*/
void Slider::setValueLabel(QLabel* label, const QString& format, bool hideIfDisabled)
{
if (label != mValueLabel)
delete mValueLabel; // delete any existing label
mValueLabel = label;
if (mValueLabel)
{
mValueLabel->setParent(this);
mValueFormat = format;
mValueLabelHide = hideIfDisabled;
connect(this, &QAbstractSlider::valueChanged, this, &Slider::valueHasChanged);
connect(mValueLabel, &QObject::destroyed, this, &Slider::valueLabelDestroyed);
}
else
disconnect(this, &QAbstractSlider::valueChanged, this, &Slider::valueHasChanged);
}
/******************************************************************************
* Sets the visibility of the slider.
* This also sets the visibility of the value label.
*/
void Slider::setVisible(bool vis)
{
QSlider::setVisible(vis);
if (mValueLabel)
{
if (mValueLabelHide && !isEnabled())
vis = false;
mValueLabel->setVisible(vis);
}
}
/******************************************************************************
* Called when the slider's status has changed.
* If it is enabled or disabled, show or hide the value label if required.
*/
void Slider::changeEvent(QEvent* e)
{
if (e->type() == QEvent::EnabledChange)
{
if (mValueLabel && mValueLabelHide && isVisible())
mValueLabel->setVisible(isEnabled());
}
}
/******************************************************************************
* Called when the slider's value has changed.
*/
void Slider::valueHasChanged(int value)
{
if (mValueLabel)
mValueLabel->setText(mValueFormat.arg(value));
}
/******************************************************************************
* Called when the value label is destroyed.
*/
void Slider::valueLabelDestroyed(QObject* obj)
{
if (obj == mValueLabel)
mValueLabel = nullptr;
}
/******************************************************************************
* Event handlers to intercept events if in read-only mode.
* Any events which could change the slider value are discarded.
*/
void Slider::mousePressEvent(QMouseEvent* e)
{
......
/*
* slider.h - slider control with read-only option
* Program: kalarm
* SPDX-FileCopyrightText: 2004-2019 David Jarvie <djarvie@kde.org>
* SPDX-FileCopyrightText: 2004-2021 David Jarvie <djarvie@kde.org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
......@@ -10,12 +10,13 @@
#define SLIDER_H
#include <QSlider>
class QLabel;
class QMouseEvent;
class QKeyEvent;
/**
* @short A QSlider with read-only option.
* @short A QSlider with read-only option and connection to a value label.
*
* The Slider class is a QSlider with a read-only option.
*
......@@ -26,43 +27,65 @@ class QKeyEvent;
*/
class Slider : public QSlider
{
Q_OBJECT
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
public:
/** Constructor.
* @param parent The parent object of this widget.
*/
explicit Slider(QWidget* parent = nullptr);
/** Constructor.
* @param orient The orientation of the slider, either Qt::Horizonal or Qt::Vertical.
* @param parent The parent object of this widget.
*/
explicit Slider(Qt::Orientation orient, QWidget* parent = nullptr);
/** Constructor.
* @param minValue The minimum value which the slider can have.
* @param maxValue The maximum value which the slider can have.
* @param pageStep The page step increment.
* @param orient The orientation of the slider, either Qt::Horizonal or Qt::Vertical.
* @param parent The parent object of this widget.
*/
Slider(int minValue, int maxValue, int pageStep, Qt::Orientation orient,
QWidget* parent = nullptr);
/** Returns true if the slider is read only. */
bool isReadOnly() const { return mReadOnly; }
/** Sets whether the slider is read-only for the user.
* @param readOnly True to set the widget read-only, false to set it read-write.
*/
virtual void setReadOnly(bool readOnly);
Q_OBJECT
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
public:
/** Constructor.
* @param parent The parent object of this widget.
*/
explicit Slider(QWidget* parent = nullptr);
protected:
void mousePressEvent(QMouseEvent*) override;
void mouseReleaseEvent(QMouseEvent*) override;
void mouseMoveEvent(QMouseEvent*) override;
void keyPressEvent(QKeyEvent*) override;
void keyReleaseEvent(QKeyEvent*) override;
/** Constructor.
* @param orient The orientation of the slider, either Qt::Horizonal or Qt::Vertical.
* @param parent The parent object of this widget.
*/
explicit Slider(Qt::Orientation orient, QWidget* parent = nullptr);
private:
bool mReadOnly {false}; // value cannot be changed by the user
/** Constructor.
* @param minValue The minimum value which the slider can have.
* @param maxValue The maximum value which the slider can have.
* @param pageStep The page step increment.
* @param orient The orientation of the slider, either Qt::Horizonal or Qt::Vertical.
* @param parent The parent object of this widget.
*/
Slider(int minValue, int maxValue, int pageStep, Qt::Orientation orient,
QWidget* parent = nullptr);
/** Returns true if the slider is read only. */
bool isReadOnly() const { return mReadOnly; }
/** Sets whether the slider is read-only for the user.
* @param readOnly True to set the widget read-only, false to set it read-write.
*/
virtual void setReadOnly(bool readOnly);
/** Set a label to display the slider's value.
* The label will be updated whenever the slider value changes.
* @param label The label to display the slider's value.
* @param format Format string for the value to display.
* @param hideIfDisabled Hide the label if the slider is disabled.
*/
void setValueLabel(QLabel* label, const QString& format = QStringLiteral("%1"), bool hideIfDisabled = false);
void setVisible(bool) override;
protected:
void mousePressEvent(QMouseEvent*) override;
void mouseReleaseEvent(QMouseEvent*) override;
void mouseMoveEvent(QMouseEvent*) override;
void keyPressEvent(QKeyEvent*) override;
void keyReleaseEvent(QKeyEvent*) override;
void changeEvent(QEvent*) override;
private Q_SLOTS:
void valueHasChanged(int value);
void valueLabelDestroyed(QObject*);
private:
QLabel* mValueLabel {nullptr}; // label to display the slider's value
QString mValueFormat; // format used for mValueLabel
bool mValueLabelHide; // hide mValueLabel if slider is disabled
bool mReadOnly {false}; // value cannot be changed by the user
};
#endif // SLIDER_H
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment