kalarm/lib

label.cpp

00001 /*
00002  *  label.cpp  -  label with radiobutton buddy option
00003  *  Program:  kalarm
00004  *  Copyright (C) 2004 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 "kalarm.h"
00022 #include <qradiobutton.h>
00023 #include "label.moc"
00024 
00025 
00026 Label::Label(QWidget* parent, const char* name, WFlags f)
00027     : QLabel(parent, name, f),
00028       mRadioButton(0),
00029       mFocusWidget(0)
00030 { }
00031 
00032 Label::Label(const QString& text, QWidget* parent, const char* name, WFlags f)
00033     : QLabel(text, parent, name, f),
00034       mRadioButton(0),
00035       mFocusWidget(0)
00036 { }
00037 
00038 Label::Label(QWidget* buddy, const QString& text, QWidget* parent, const char* name, WFlags f)
00039     : QLabel(buddy, text, parent, name, f),
00040       mRadioButton(0),
00041       mFocusWidget(0)
00042 { }
00043 
00044 /******************************************************************************
00045 * Set a buddy widget.
00046 * If it (or its focus proxy) is a radio button, create a focus widget.
00047 * When the accelerator key is pressed, the focus widget then receives focus.
00048 * That event triggers the selection of the radio button.
00049 */
00050 void Label::setBuddy(QWidget* bud)
00051 {
00052     if (mRadioButton)
00053         disconnect(mRadioButton, SIGNAL(destroyed()), this, SLOT(buddyDead()));
00054     QWidget* w = bud;
00055     if (w)
00056     {
00057         while (w->focusProxy())
00058             w = w->focusProxy();
00059         if (!w->inherits("QRadioButton"))
00060             w = 0;
00061     }
00062     if (!w)
00063     {
00064         // The buddy widget isn't a radio button
00065         QLabel::setBuddy(bud);
00066         delete mFocusWidget;
00067         mFocusWidget = 0;
00068         mRadioButton = 0;
00069     }
00070     else
00071     {
00072         // The buddy widget is a radio button, so set a different buddy
00073         if (!mFocusWidget)
00074             mFocusWidget = new LabelFocusWidget(this);
00075         QLabel::setBuddy(mFocusWidget);
00076         mRadioButton = (QRadioButton*)bud;
00077         connect(mRadioButton, SIGNAL(destroyed()), this, SLOT(buddyDead()));
00078     }
00079 }
00080 
00081 void Label::buddyDead()
00082 {
00083     delete mFocusWidget;
00084     mFocusWidget = 0;
00085     mRadioButton = 0;
00086 }
00087 
00088 /******************************************************************************
00089 * Called when focus is transferred to the label's special focus widget.
00090 * Transfer focus to the radio button and select it.
00091 */
00092 void Label::activated()
00093 {
00094     if (mFocusWidget  &&  mRadioButton)
00095     {
00096         mRadioButton->setFocus();
00097         mRadioButton->setChecked(true);
00098     }
00099 }
00100 
00101 
00102 /*=============================================================================
00103 * Class: LabelFocusWidget
00104 =============================================================================*/
00105 
00106 LabelFocusWidget::LabelFocusWidget(QWidget* parent, const char* name)
00107     : QWidget(parent, name)
00108 {
00109     setFocusPolicy(ClickFocus);
00110     setFixedSize(QSize(1,1));
00111 }
00112 
00113 void LabelFocusWidget::focusInEvent(QFocusEvent*)
00114 {
00115     Label* parent = (Label*)parentWidget();
00116     parent->activated();
00117 
00118 }
KDE Home | KDE Accessibility Home | Description of Access Keys