- Don't create layouts with parent. Qt 4.x automatically reparents
[duncan/yast2-qt4.git] / src / YQRadioButtonGroup.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQRadioButtonGroup.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <QRadioButton>
21 #define y2log_component "qt-ui"
22 #include <ycp/y2log.h>
23
24 #include "YQRadioButtonGroup.h"
25 #include "YQRadioButton.h"
26
27
28 YQRadioButtonGroup::YQRadioButtonGroup( YWidget * parent )
29     : QWidget( (QWidget *) parent->widgetRep() )
30     , YRadioButtonGroup( parent )
31     , _recursive( false )
32 {
33     setWidgetRep( this );
34 }
35
36
37 YQRadioButtonGroup::~YQRadioButtonGroup()
38 {
39     // NOP
40 }
41
42
43 void
44 YQRadioButtonGroup::addRadioButton( YRadioButton * button )
45 {
46     YRadioButtonGroup::addRadioButton( button );
47
48     if ( button->value() )              // if this new button is active
49     {
50         uncheckOtherButtons( button );  // make it the only active
51     }
52
53     QRadioButton * radio_button = dynamic_cast<YQRadioButton *>( button );
54
55     connect ( radio_button,     SIGNAL ( toggled           ( bool ) ),
56               this,             SLOT   ( radioButtonClicked( bool ) ) );
57 }
58
59
60 void
61 YQRadioButtonGroup::setEnabled( bool enabled )
62 {
63     QWidget::setEnabled( enabled );
64     YWidget::setEnabled( enabled );
65 }
66
67
68 void
69 YQRadioButtonGroup::setSize( int newWidth, int newHeight )
70 {
71     resize( newWidth, newHeight );
72     YRadioButtonGroup::setSize( newWidth, newHeight );
73 }
74
75
76 void
77 YQRadioButtonGroup::radioButtonClicked( bool newState )
78 {
79     // Prevent infinite recursion: YQRadioButton::setValue() might cause Qt
80     // signals that would cause recursion to this place.
81
82     if ( _recursive )
83         return;
84
85     _recursive = true;
86
87     QRadioButton * senderButton = (QRadioButton *) sender();
88
89     // Implement radio box behaviour: Uncheck all other radio buttons
90
91     for ( YRadioButtonListConstIterator it = radioButtonsBegin();
92           it != radioButtonsEnd();
93           ++it )
94     {
95         YQRadioButton * radioButton = dynamic_cast<YQRadioButton *> (*it);
96
97         if ( radioButton )
98         {
99             if ( radioButton == senderButton )
100             {
101                 // If this button has been clicked, it is to be the RadioBox's
102                 // active button - regardless of newState. This is to avoid
103                 // RadioBoxes where no single button is active; otherwise the
104                 // second click would deactivate the only active button.
105
106                 radioButton->setValue( true );
107             }
108             else
109             {
110                 radioButton->setValue( false );
111             }
112         }
113     }
114
115     _recursive = false;
116 }
117
118
119 #include "YQRadioButtonGroup.moc"