label.cpp 3.41 KB
Newer Older
1 2 3
/*
 *  label.cpp  -  label with radiobutton buddy option
 *  Program:  kalarm
4
 *  Copyright © 2004,2005 by David Jarvie <djarvie@kde.org>
5 6 7 8 9 10 11 12 13 14 15
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
David Jarvie's avatar
David Jarvie committed
16 17 18
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 20 21
 */

#include "kalarm.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "label.h"
23
#include <QRadioButton>
24 25


26
Label::Label(QWidget* parent, Qt::WindowFlags f)
27 28 29
    : QLabel(parent, f),
      mRadioButton(0),
      mFocusWidget(0)
30 31
{ }

32
Label::Label(const QString& text, QWidget* parent, Qt::WindowFlags f)
33 34 35
    : QLabel(text, parent, f),
      mRadioButton(0),
      mFocusWidget(0)
36 37
{ }

38
Label::Label(QWidget* buddy, const QString& text, QWidget* parent, Qt::WindowFlags f)
39 40 41
    : QLabel(text, parent, f),
      mRadioButton(0),
      mFocusWidget(0)
David Jarvie's avatar
David Jarvie committed
42
{
43
    setBuddy(buddy);
David Jarvie's avatar
David Jarvie committed
44
}
45 46 47 48 49 50 51 52 53

/******************************************************************************
* Set a buddy widget.
* If it (or its focus proxy) is a radio button, create a focus widget.
* When the accelerator key is pressed, the focus widget then receives focus.
* That event triggers the selection of the radio button.
*/
void Label::setBuddy(QWidget* bud)
{
54
    if (mRadioButton)
Laurent Montel's avatar
Laurent Montel committed
55
        disconnect(mRadioButton, &QRadioButton::destroyed, this, &Label::buddyDead);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    QWidget* w = bud;
    if (w)
    {
        while (w->focusProxy())
            w = w->focusProxy();
        if (!qobject_cast<QRadioButton*>(w))
            w = 0;
    }
    if (!w)
    {
        // The buddy widget isn't a radio button
        QLabel::setBuddy(bud);
        delete mFocusWidget;
        mFocusWidget = 0;
        mRadioButton = 0;
    }
    else
    {
        // The buddy widget is a radio button, so set a different buddy
        if (!mFocusWidget)
            mFocusWidget = new LabelFocusWidget(this);
        QLabel::setBuddy(mFocusWidget);
        mRadioButton = (QRadioButton*)bud;
Laurent Montel's avatar
Laurent Montel committed
79
        connect(mRadioButton, &QRadioButton::destroyed, this, &Label::buddyDead);
80
    }
81 82 83 84
}

void Label::buddyDead()
{
85 86 87
    delete mFocusWidget;
    mFocusWidget = 0;
    mRadioButton = 0;
88 89 90 91 92 93 94 95
}

/******************************************************************************
* Called when focus is transferred to the label's special focus widget.
* Transfer focus to the radio button and select it.
*/
void Label::activated()
{
96 97 98 99 100
    if (mFocusWidget  &&  mRadioButton)
    {
        mRadioButton->setFocus();
        mRadioButton->setChecked(true);
    }
101 102 103 104 105 106 107
}


/*=============================================================================
* Class: LabelFocusWidget
=============================================================================*/

David Jarvie's avatar
David Jarvie committed
108
LabelFocusWidget::LabelFocusWidget(QWidget* parent)
109
    : QWidget(parent)
110
{
111 112
    setFocusPolicy(Qt::ClickFocus);
    setFixedSize(QSize(1,1));
113 114 115 116
}

void LabelFocusWidget::focusInEvent(QFocusEvent*)
{
117 118
    Label* parent = (Label*)parentWidget();
    parent->activated();
119 120

}
121

122
// vim: et sw=4: