File indexing completed on 2024-06-02 05:19:15
0001 /* 0002 * radiobutton.cpp - radio button with read-only option 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 #include "radiobutton.h" 0010 0011 #include <QMouseEvent> 0012 #include <QKeyEvent> 0013 0014 0015 RadioButton::RadioButton(QWidget* parent) 0016 : QRadioButton(parent) 0017 , mFocusPolicy(focusPolicy()) 0018 { } 0019 0020 RadioButton::RadioButton(const QString& text, QWidget* parent) 0021 : QRadioButton(text, parent) 0022 , mFocusPolicy(focusPolicy()) 0023 { } 0024 0025 /****************************************************************************** 0026 * Set the read-only status. If read-only, the button can be toggled by the 0027 * application, but not by the user. 0028 */ 0029 void RadioButton::setReadOnly(bool ro) 0030 { 0031 if (ro != mReadOnly) 0032 { 0033 mReadOnly = ro; 0034 setFocusPolicy(ro ? Qt::NoFocus : mFocusPolicy); 0035 if (ro) 0036 clearFocus(); 0037 } 0038 } 0039 0040 /****************************************************************************** 0041 * Specify a widget to receive focus when the button is clicked on. 0042 */ 0043 void RadioButton::setFocusWidget(QWidget* w, bool enable) 0044 { 0045 mFocusWidget = w; 0046 mFocusWidgetEnable = enable; 0047 if (w) 0048 connect(this, &RadioButton::clicked, this, &RadioButton::slotClicked); 0049 else 0050 disconnect(this, &RadioButton::clicked, this, &RadioButton::slotClicked); 0051 } 0052 0053 /****************************************************************************** 0054 * Called when the button is clicked. 0055 * If it is now checked, focus is transferred to any specified focus widget. 0056 */ 0057 void RadioButton::slotClicked() 0058 { 0059 if (mFocusWidget && isChecked()) 0060 { 0061 if (mFocusWidgetEnable) 0062 mFocusWidget->setEnabled(true); 0063 mFocusWidget->setFocus(); 0064 } 0065 } 0066 0067 /****************************************************************************** 0068 * Event handlers to intercept events if in read-only mode. 0069 * Any events which could change the button state are discarded. 0070 */ 0071 void RadioButton::mousePressEvent(QMouseEvent* e) 0072 { 0073 if (mReadOnly) 0074 { 0075 // Swallow up the event if it's the left button 0076 if (e->button() == Qt::LeftButton) 0077 return; 0078 } 0079 QRadioButton::mousePressEvent(e); 0080 } 0081 0082 void RadioButton::mouseReleaseEvent(QMouseEvent* e) 0083 { 0084 if (mReadOnly) 0085 { 0086 // Swallow up the event if it's the left button 0087 if (e->button() == Qt::LeftButton) 0088 return; 0089 } 0090 QRadioButton::mouseReleaseEvent(e); 0091 } 0092 0093 void RadioButton::mouseMoveEvent(QMouseEvent* e) 0094 { 0095 if (!mReadOnly) 0096 QRadioButton::mouseMoveEvent(e); 0097 } 0098 0099 void RadioButton::keyPressEvent(QKeyEvent* e) 0100 { 0101 if (mReadOnly) 0102 { 0103 switch (e->key()) 0104 { 0105 case Qt::Key_Up: 0106 case Qt::Key_Left: 0107 case Qt::Key_Right: 0108 case Qt::Key_Down: 0109 // Process keys which shift the focus 0110 case Qt::Key_Escape: 0111 break; 0112 default: 0113 return; 0114 } 0115 } 0116 QRadioButton::keyPressEvent(e); 0117 } 0118 0119 void RadioButton::keyReleaseEvent(QKeyEvent* e) 0120 { 0121 if (!mReadOnly) 0122 QRadioButton::keyReleaseEvent(e); 0123 } 0124 0125 #include "moc_radiobutton.cpp" 0126 0127 // vim: et sw=4: