kalarm/lib

checkbox.cpp
1 /*
2  * checkbox.cpp - check box 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 "checkbox.moc"
22 
23 
24 CheckBox::CheckBox(TQWidget* parent, const char* name)
25  : TQCheckBox(parent, name),
26  mFocusPolicy(focusPolicy()),
27  mFocusWidget(0),
28  mReadOnly(false)
29 { }
30 
31 CheckBox::CheckBox(const TQString& text, TQWidget* parent, const char* name)
32  : TQCheckBox(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 checkbox can be toggled by the
40 * application, but not by the user.
41 */
42 void CheckBox::setReadOnly(bool ro)
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 checkbox is clicked on.
55 */
56 void CheckBox::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 checkbox is clicked.
68 * If it is now checked, focus is transferred to any specified focus widget.
69 */
70 void CheckBox::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 checkbox state are discarded.
83 */
84 void CheckBox::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  TQCheckBox::mousePressEvent(e);
93 }
94 
95 void CheckBox::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  TQCheckBox::mouseReleaseEvent(e);
104 }
105 
106 void CheckBox::mouseMoveEvent(TQMouseEvent* e)
107 {
108  if (!mReadOnly)
109  TQCheckBox::mouseMoveEvent(e);
110 }
111 
112 void CheckBox::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  break;
123  default:
124  return;
125  }
126  TQCheckBox::keyPressEvent(e);
127 }
128 
129 void CheckBox::keyReleaseEvent(TQKeyEvent* e)
130 {
131  if (!mReadOnly)
132  TQCheckBox::keyReleaseEvent(e);
133 }
CheckBox(TQWidget *parent, const char *name=0)
Constructor.
Definition: checkbox.cpp:24
void setFocusWidget(TQWidget *widget, bool enable=true)
Specifies a widget to receive focus when the user selects the check box by clicking on it.
Definition: checkbox.cpp:56
virtual void setReadOnly(bool readOnly)
Sets whether the check box is read-only for the user.
Definition: checkbox.cpp:42