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

0001 /*
0002  *  radiobutton.h  -  radio button with focus widget and read-only options
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2002-2019 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include <QRadioButton>
0012 class QMouseEvent;
0013 class QKeyEvent;
0014 
0015 
0016 /**
0017  *  @short A QRadioButton with focus widget and read-only options.
0018  *
0019  *  The RadioButton class is a QRadioButton with the ability to transfer focus to
0020  *  another widget when checked, and with a read-only option.
0021  *
0022  *  Another widget may be specified as the focus widget for the radio button. Whenever
0023  *  the user clicks on the radio button so as to set its state to checked, focus is
0024  *  automatically transferred to the focus widget.
0025  *
0026  *  The widget may be set as read-only. This has the same effect as disabling it, except
0027  *  that its appearance is unchanged.
0028  *
0029  *  @author David Jarvie <djarvie@kde.org>
0030  */
0031 class RadioButton : public QRadioButton
0032 {
0033     Q_OBJECT
0034 public:
0035     /** Constructor.
0036      *  @param parent The parent object of this widget.
0037      */
0038     explicit RadioButton(QWidget* parent = nullptr);
0039 
0040     /** Constructor.
0041      *  @param text Text to display.
0042      *  @param parent The parent object of this widget.
0043      */
0044     explicit RadioButton(const QString& text, QWidget* parent = nullptr);
0045 
0046     /** Returns true if the widget is read only. */
0047     bool isReadOnly() const          { return mReadOnly; }
0048 
0049     /** Sets whether the radio button is read-only for the user. If read-only,
0050      *  its state cannot be changed by the user.
0051      *  @param readOnly True to set the widget read-only, false to set it read-write.
0052      */
0053     virtual void setReadOnly(bool readOnly);
0054 
0055     /** Returns the widget which receives focus when the button is clicked. */
0056     QWidget* focusWidget() const         { return mFocusWidget; }
0057 
0058     /** Specifies a widget to receive focus when the button is clicked.
0059      *  @param widget Widget to receive focus.
0060      *  @param enable If true, @p widget will be enabled before receiving focus. If
0061      *                false, the enabled state of @p widget will be left unchanged when
0062      *                the radio button is clicked.
0063      */
0064     void setFocusWidget(QWidget* widget, bool enable = true);
0065 
0066 protected:
0067     void mousePressEvent(QMouseEvent*) override;
0068     void mouseReleaseEvent(QMouseEvent*) override;
0069     void mouseMoveEvent(QMouseEvent*) override;
0070     void keyPressEvent(QKeyEvent*) override;
0071     void keyReleaseEvent(QKeyEvent*) override;
0072 
0073 protected Q_SLOTS:
0074     void     slotClicked();
0075 
0076 private:
0077     Qt::FocusPolicy mFocusPolicy;           // default focus policy for the QRadioButton
0078     QWidget*        mFocusWidget {nullptr}; // widget to receive focus when button is clicked on
0079     bool            mFocusWidgetEnable;     // enable focus widget before setting focus
0080     bool            mReadOnly {false};      // value cannot be changed
0081 };
0082 
0083 // vim: et sw=4: