kalarm/lib

radiobutton.cpp
1 /*
2  * radiobutton.cpp - radio button with read-only option
3  * Program: kalarm
4  * Copyright (c) 2002, 2003 by David Jarvie <software@astrojar.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "radiobutton.moc"
22 
23 
24 RadioButton::RadioButton(TQWidget* parent, const char* name)
25  : TQRadioButton(parent, name),
26  mFocusPolicy(focusPolicy()),
27  mFocusWidget(0),
28  mReadOnly(false)
29 { }
30 
31 RadioButton::RadioButton(const TQString& text, TQWidget* parent, const char* name)
32  : TQRadioButton(text, parent, name),
33  mFocusPolicy(focusPolicy()),
34  mFocusWidget(0),
35  mReadOnly(false)
36 { }
37 
38 /******************************************************************************
39 * Set the read-only status. If read-only, the button can be toggled by the
40 * application, but not by the user.
41 */
43 {
44  if ((int)ro != (int)mReadOnly)
45  {
46  mReadOnly = ro;
47  setFocusPolicy(ro ? TQWidget::NoFocus : mFocusPolicy);
48  if (ro)
49  clearFocus();
50  }
51 }
52 
53 /******************************************************************************
54 * Specify a widget to receive focus when the button is clicked on.
55 */
56 void RadioButton::setFocusWidget(TQWidget* w, bool enable)
57 {
58  mFocusWidget = w;
59  mFocusWidgetEnable = enable;
60  if (w)
61  connect(this, TQ_SIGNAL(clicked()), TQ_SLOT(slotClicked()));
62  else
63  disconnect(this, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotClicked()));
64 }
65 
66 /******************************************************************************
67 * Called when the button is clicked.
68 * If it is now checked, focus is transferred to any specified focus widget.
69 */
70 void RadioButton::slotClicked()
71 {
72  if (mFocusWidget && isChecked())
73  {
74  if (mFocusWidgetEnable)
75  mFocusWidget->setEnabled(true);
76  mFocusWidget->setFocus();
77  }
78 }
79 
80 /******************************************************************************
81 * Event handlers to intercept events if in read-only mode.
82 * Any events which could change the button state are discarded.
83 */
84 void RadioButton::mousePressEvent(TQMouseEvent* e)
85 {
86  if (mReadOnly)
87  {
88  // Swallow up the event if it's the left button
89  if (e->button() == TQt::LeftButton)
90  return;
91  }
92  TQRadioButton::mousePressEvent(e);
93 }
94 
95 void RadioButton::mouseReleaseEvent(TQMouseEvent* e)
96 {
97  if (mReadOnly)
98  {
99  // Swallow up the event if it's the left button
100  if (e->button() == TQt::LeftButton)
101  return;
102  }
103  TQRadioButton::mouseReleaseEvent(e);
104 }
105 
106 void RadioButton::mouseMoveEvent(TQMouseEvent* e)
107 {
108  if (!mReadOnly)
109  TQRadioButton::mouseMoveEvent(e);
110 }
111 
112 void RadioButton::keyPressEvent(TQKeyEvent* e)
113 {
114  if (mReadOnly)
115  switch (e->key())
116  {
117  case TQt::Key_Up:
118  case TQt::Key_Left:
119  case TQt::Key_Right:
120  case TQt::Key_Down:
121  // Process keys which shift the focus
122  case TQt::Key_Escape:
123  break;
124  default:
125  return;
126  }
127  TQRadioButton::keyPressEvent(e);
128 }
129 
130 void RadioButton::keyReleaseEvent(TQKeyEvent* e)
131 {
132  if (!mReadOnly)
133  TQRadioButton::keyReleaseEvent(e);
134 }
virtual void setReadOnly(bool readOnly)
Sets whether the radio button is read-only for the user.
Definition: radiobutton.cpp:42
void setFocusWidget(TQWidget *widget, bool enable=true)
Specifies a widget to receive focus when the button is clicked.
Definition: radiobutton.cpp:56
RadioButton(TQWidget *parent, const char *name=0)
Constructor.
Definition: radiobutton.cpp:24