picking up branches/tmp/sh/qt4-port/, merging it with trunk
[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.h>
21 #include <q3buttongroup.h>
22 #define y2log_component "qt-ui"
23 #include <ycp/y2log.h>
24
25 #include "YQRadioButtonGroup.h"
26 #include "YQRadioButton.h"
27
28
29 YQRadioButtonGroup::YQRadioButtonGroup( YWidget * parent )
30     : QWidget( (QWidget *) parent->widgetRep() )
31     , YRadioButtonGroup( parent )
32     , _recursive( false )
33 {
34     setWidgetRep( this );
35 }
36
37
38 YQRadioButtonGroup::~YQRadioButtonGroup()
39 {
40     // NOP
41 }
42
43
44 void
45 YQRadioButtonGroup::addRadioButton( YRadioButton * button )
46 {
47     YRadioButtonGroup::addRadioButton( button );
48
49     if ( button->value() )              // if this new button is active
50     {
51         uncheckOtherButtons( button );  // make it the only active
52     }
53
54     QRadioButton * radio_button = ( ( YQRadioButton * ) button )->getQtButton();
55
56     connect ( radio_button,     SIGNAL ( toggled           ( bool ) ),
57               this,             SLOT   ( radioButtonClicked( bool ) ) );
58 }
59
60
61 void
62 YQRadioButtonGroup::setEnabled( bool enabled )
63 {
64     QWidget::setEnabled( enabled );
65     YWidget::setEnabled( enabled );
66 }
67
68
69 void
70 YQRadioButtonGroup::setSize( int newWidth, int newHeight )
71 {
72     resize( newWidth, newHeight );
73     YRadioButtonGroup::setSize( newWidth, newHeight );
74 }
75
76
77 void
78 YQRadioButtonGroup::radioButtonClicked( bool newState )
79 {
80     // Prevent infinite recursion: YQRadioButton::setValue() might cause Qt
81     // signals that would cause recursion to this place.
82
83     if ( _recursive )
84         return;
85
86     _recursive = true;
87
88     QRadioButton * senderButton = (QRadioButton *) sender();
89
90     // Implement radio box behaviour: Uncheck all other radio buttons
91
92     for ( YRadioButtonListConstIterator it = radioButtonsBegin();
93           it != radioButtonsEnd();
94           ++it )
95     {
96         YQRadioButton * radioButton = dynamic_cast<YQRadioButton *> (*it);
97
98         if ( radioButton )
99         {
100             if ( radioButton->getQtButton() == senderButton )
101             {
102                 // If this button has been clicked, it is to be the RadioBox's
103                 // active button - regardless of newState. This is to avoid
104                 // RadioBoxes where no single button is active; otherwise the
105                 // second click would deactivate the only active button.
106
107                 radioButton->setValue( true );
108             }
109             else
110             {
111                 radioButton->setValue( false );
112             }
113         }
114     }
115
116     _recursive = false;
117 }
118
119
120 #include "YQRadioButtonGroup.moc"