radiobutton.cpp
00001 /* 00002 * radiobutton.cpp - radio button with read-only option 00003 * Program: kalarm 00004 * Copyright (c) 2002, 2003 by David Jarvie <software@astrojar.org.uk> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "radiobutton.moc" 00022 00023 00024 RadioButton::RadioButton(TQWidget* parent, const char* name) 00025 : TQRadioButton(parent, name), 00026 mFocusPolicy(focusPolicy()), 00027 mFocusWidget(0), 00028 mReadOnly(false) 00029 { } 00030 00031 RadioButton::RadioButton(const TQString& text, TQWidget* parent, const char* name) 00032 : TQRadioButton(text, parent, name), 00033 mFocusPolicy(focusPolicy()), 00034 mFocusWidget(0), 00035 mReadOnly(false) 00036 { } 00037 00038 /****************************************************************************** 00039 * Set the read-only status. If read-only, the button can be toggled by the 00040 * application, but not by the user. 00041 */ 00042 void RadioButton::setReadOnly(bool ro) 00043 { 00044 if ((int)ro != (int)mReadOnly) 00045 { 00046 mReadOnly = ro; 00047 setFocusPolicy(ro ? TQ_NoFocus : mFocusPolicy); 00048 if (ro) 00049 clearFocus(); 00050 } 00051 } 00052 00053 /****************************************************************************** 00054 * Specify a widget to receive focus when the button is clicked on. 00055 */ 00056 void RadioButton::setFocusWidget(TQWidget* w, bool enable) 00057 { 00058 mFocusWidget = w; 00059 mFocusWidgetEnable = enable; 00060 if (w) 00061 connect(this, TQT_SIGNAL(clicked()), TQT_SLOT(slotClicked())); 00062 else 00063 disconnect(this, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotClicked())); 00064 } 00065 00066 /****************************************************************************** 00067 * Called when the button is clicked. 00068 * If it is now checked, focus is transferred to any specified focus widget. 00069 */ 00070 void RadioButton::slotClicked() 00071 { 00072 if (mFocusWidget && isChecked()) 00073 { 00074 if (mFocusWidgetEnable) 00075 mFocusWidget->setEnabled(true); 00076 mFocusWidget->setFocus(); 00077 } 00078 } 00079 00080 /****************************************************************************** 00081 * Event handlers to intercept events if in read-only mode. 00082 * Any events which could change the button state are discarded. 00083 */ 00084 void RadioButton::mousePressEvent(TQMouseEvent* e) 00085 { 00086 if (mReadOnly) 00087 { 00088 // Swallow up the event if it's the left button 00089 if (e->button() == Qt::LeftButton) 00090 return; 00091 } 00092 TQRadioButton::mousePressEvent(e); 00093 } 00094 00095 void RadioButton::mouseReleaseEvent(TQMouseEvent* e) 00096 { 00097 if (mReadOnly) 00098 { 00099 // Swallow up the event if it's the left button 00100 if (e->button() == Qt::LeftButton) 00101 return; 00102 } 00103 TQRadioButton::mouseReleaseEvent(e); 00104 } 00105 00106 void RadioButton::mouseMoveEvent(TQMouseEvent* e) 00107 { 00108 if (!mReadOnly) 00109 TQRadioButton::mouseMoveEvent(e); 00110 } 00111 00112 void RadioButton::keyPressEvent(TQKeyEvent* e) 00113 { 00114 if (mReadOnly) 00115 switch (e->key()) 00116 { 00117 case TQt::Key_Up: 00118 case TQt::Key_Left: 00119 case TQt::Key_Right: 00120 case TQt::Key_Down: 00121 // Process keys which shift the focus 00122 case TQt::Key_Escape: 00123 break; 00124 default: 00125 return; 00126 } 00127 TQRadioButton::keyPressEvent(e); 00128 } 00129 00130 void RadioButton::keyReleaseEvent(TQKeyEvent* e) 00131 { 00132 if (!mReadOnly) 00133 TQRadioButton::keyReleaseEvent(e); 00134 }