kalarm/lib

label.cpp
1/*
2 * label.cpp - label with radiobutton buddy option
3 * Program: kalarm
4 * Copyright (C) 2004 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 "kalarm.h"
22#include <tqradiobutton.h>
23#include "label.moc"
24
25
26Label::Label(TQWidget* parent, const char* name, WFlags f)
27 : TQLabel(parent, name, f),
28 mRadioButton(0),
29 mFocusWidget(0)
30{ }
31
32Label::Label(const TQString& text, TQWidget* parent, const char* name, WFlags f)
33 : TQLabel(text, parent, name, f),
34 mRadioButton(0),
35 mFocusWidget(0)
36{ }
37
38Label::Label(TQWidget* buddy, const TQString& text, TQWidget* parent, const char* name, WFlags f)
39 : TQLabel(buddy, text, parent, name, f),
40 mRadioButton(0),
41 mFocusWidget(0)
42{ }
43
44/******************************************************************************
45* Set a buddy widget.
46* If it (or its focus proxy) is a radio button, create a focus widget.
47* When the accelerator key is pressed, the focus widget then receives focus.
48* That event triggers the selection of the radio button.
49*/
50void Label::setBuddy(TQWidget* bud)
51{
52 if (mRadioButton)
53 disconnect(mRadioButton, TQ_SIGNAL(destroyed()), this, TQ_SLOT(buddyDead()));
54 TQWidget* w = bud;
55 if (w)
56 {
57 while (w->focusProxy())
58 w = w->focusProxy();
59 if (!w->inherits("TQRadioButton"))
60 w = 0;
61 }
62 if (!w)
63 {
64 // The buddy widget isn't a radio button
65 TQLabel::setBuddy(bud);
66 delete mFocusWidget;
67 mFocusWidget = 0;
68 mRadioButton = 0;
69 }
70 else
71 {
72 // The buddy widget is a radio button, so set a different buddy
73 if (!mFocusWidget)
74 mFocusWidget = new LabelFocusWidget(this);
75 TQLabel::setBuddy(mFocusWidget);
76 mRadioButton = (TQRadioButton*)bud;
77 connect(mRadioButton, TQ_SIGNAL(destroyed()), this, TQ_SLOT(buddyDead()));
78 }
79}
80
81void Label::buddyDead()
82{
83 delete mFocusWidget;
84 mFocusWidget = 0;
85 mRadioButton = 0;
86}
87
88/******************************************************************************
89* Called when focus is transferred to the label's special focus widget.
90* Transfer focus to the radio button and select it.
91*/
92void Label::activated()
93{
94 if (mFocusWidget && mRadioButton)
95 {
96 mRadioButton->setFocus();
97 mRadioButton->setChecked(true);
98 }
99}
100
101
102/*=============================================================================
103* Class: LabelFocusWidget
104=============================================================================*/
105
106LabelFocusWidget::LabelFocusWidget(TQWidget* parent, const char* name)
107 : TQWidget(parent, name)
108{
109 setFocusPolicy(TQWidget::ClickFocus);
110 setFixedSize(TQSize(1,1));
111}
112
113void LabelFocusWidget::focusInEvent(TQFocusEvent*)
114{
115 Label* parent = (Label*)parentWidget();
116 parent->activated();
117
118}
A TQLabel with option for a buddy radio button.
Definition: label.h:42
Label(TQWidget *parent, const char *name=0, WFlags f=0)
Constructs an empty label.
Definition: label.cpp:26
virtual void setBuddy(TQWidget *buddy)
Sets the label's buddy widget which receives the keyboard focus when the label's accelerator key is p...
Definition: label.cpp:50