1 /*
2  *  checkbox.h  -  check box with focus widget and read-only options
3  *  Program:  kalarm
4  *  SPDX-FileCopyrightText: 2002, 2003, 2005, 2006 David Jarvie <djarvie@kde.org>
5  *
6  *  SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #pragma once
10 
11 #include <QCheckBox>
12 class QMouseEvent;
13 class QKeyEvent;
14 
15 
16 /**
17  *  @short A QCheckBox with focus widget and read-only options.
18  *
19  *  The CheckBox class is a QCheckBox with the ability to transfer focus to another
20  *  widget when checked, and with a read-only option.
21  *
22  *  Another widget may be specified as the focus widget for the check box. Whenever
23  *  the user clicks on the check box so as to set its state to checked, focus is
24  *  automatically transferred to the focus widget.
25  *
26  *  The widget may be set as read-only. This has the same effect as disabling it, except
27  *  that its appearance is unchanged.
28  *
29  *  @author David Jarvie <djarvie@kde.org>
30  */
31 class CheckBox : public QCheckBox
32 {
33     Q_OBJECT
34 public:
35     /** Constructor.
36      *  @param parent The parent object of this widget.
37      */
38     explicit CheckBox(QWidget* parent);
39 
40     /** Constructor.
41      *  @param text Text to display.
42      *  @param parent The parent object of this widget.
43      */
44     CheckBox(const QString& text, QWidget* parent);
45 
46     /** Returns true if the widget is read only. */
isReadOnly()47     bool isReadOnly() const          { return mReadOnly; }
48 
49     /** Sets whether the check box is read-only for the user. If read-only,
50      *  its state cannot be changed by the user.
51      *  @param readOnly True to set the widget read-only, false to set it read-write.
52      */
53     virtual void setReadOnly(bool readOnly);
54 
55     /** Returns the widget which receives focus when the user selects the check box by clicking on it. */
focusWidget()56     QWidget* focusWidget() const         { return mFocusWidget; }
57 
58     /** Specifies a widget to receive focus when the user selects the check box by clicking on it.
59      *  @param widget Widget to receive focus.
60      *  @param enable If true, @p widget will be enabled before receiving focus. If
61      *                false, the enabled state of @p widget will be left unchanged when
62      *                the check box is clicked.
63      */
64     void setFocusWidget(QWidget* widget, bool enable = true);
65 
66     /** Return the indentation from the left of a checkbox widget to the start
67      *  of the checkbox text.
68      *  @param widget  Checkbox to use, or parent widget.
69      *  @return  Indentation to start of text.
70      */
71     static int textIndent(QWidget* cb);
72 
73 protected:
74     void         mousePressEvent(QMouseEvent*) override;
75     void         mouseReleaseEvent(QMouseEvent*) override;
76     void         mouseMoveEvent(QMouseEvent*) override;
77     void         keyPressEvent(QKeyEvent*) override;
78     void         keyReleaseEvent(QKeyEvent*) override;
79 protected Q_SLOTS:
80     void         slotClicked();
81 private:
82     Qt::FocusPolicy mFocusPolicy;           // default focus policy for the QCheckBox
83     QWidget*        mFocusWidget {nullptr}; // widget to receive focus when button is clicked on
84     bool            mFocusWidgetEnable;     // enable focus widget before setting focus
85     bool            mReadOnly {false};      // value cannot be changed
86 };
87 
88 
89 // vim: et sw=4:
90