kalarm/lib

checkbox.cpp

00001 /*
00002  *  checkbox.cpp  -  check box 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 "checkbox.moc"
00022 
00023 
00024 CheckBox::CheckBox(QWidget* parent, const char* name)
00025     : QCheckBox(parent, name),
00026       mFocusPolicy(focusPolicy()),
00027       mFocusWidget(0),
00028       mReadOnly(false)
00029 { }
00030 
00031 CheckBox::CheckBox(const QString& text, QWidget* parent, const char* name)
00032     : QCheckBox(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 checkbox can be toggled by the
00040 *  application, but not by the user.
00041 */
00042 void CheckBox::setReadOnly(bool ro)
00043 {
00044     if ((int)ro != (int)mReadOnly)
00045     {
00046         mReadOnly = ro;
00047         setFocusPolicy(ro ? QWidget::NoFocus : mFocusPolicy);
00048         if (ro)
00049             clearFocus();
00050     }
00051 }
00052 
00053 /******************************************************************************
00054 *  Specify a widget to receive focus when the checkbox is clicked on.
00055 */
00056 void CheckBox::setFocusWidget(QWidget* w, bool enable)
00057 {
00058     mFocusWidget = w;
00059     mFocusWidgetEnable = enable;
00060     if (w)
00061         connect(this, SIGNAL(clicked()), SLOT(slotClicked()));
00062     else
00063         disconnect(this, SIGNAL(clicked()), this, SLOT(slotClicked()));
00064 }
00065 
00066 /******************************************************************************
00067 *  Called when the checkbox is clicked.
00068 *  If it is now checked, focus is transferred to any specified focus widget.
00069 */
00070 void CheckBox::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 checkbox state are discarded.
00083 */
00084 void CheckBox::mousePressEvent(QMouseEvent* e)
00085 {
00086     if (mReadOnly)
00087     {
00088         // Swallow up the event if it's the left button
00089         if (e->button() == LeftButton)
00090             return;
00091     }
00092     QCheckBox::mousePressEvent(e);
00093 }
00094 
00095 void CheckBox::mouseReleaseEvent(QMouseEvent* e)
00096 {
00097     if (mReadOnly)
00098     {
00099         // Swallow up the event if it's the left button
00100         if (e->button() == LeftButton)
00101             return;
00102     }
00103     QCheckBox::mouseReleaseEvent(e);
00104 }
00105 
00106 void CheckBox::mouseMoveEvent(QMouseEvent* e)
00107 {
00108     if (!mReadOnly)
00109         QCheckBox::mouseMoveEvent(e);
00110 }
00111 
00112 void CheckBox::keyPressEvent(QKeyEvent* e)
00113 {
00114     if (mReadOnly)
00115         switch (e->key())
00116         {
00117             case Key_Up:
00118             case Key_Left:
00119             case Key_Right:
00120             case Key_Down:
00121                 // Process keys which shift the focus
00122                 break;
00123             default:
00124                 return;
00125         }
00126     QCheckBox::keyPressEvent(e);
00127 }
00128 
00129 void CheckBox::keyReleaseEvent(QKeyEvent* e)
00130 {
00131     if (!mReadOnly)
00132         QCheckBox::keyReleaseEvent(e);
00133 }
KDE Home | KDE Accessibility Home | Description of Access Keys