]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQCheckBoxFrame.cc
restart qt4 porting
[duncan/yast2-qt4.git] / src / YQCheckBoxFrame.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQCheckBoxFrame.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
22 #include <qcheckbox.h>
23 #include "YQUI.h"
24 #include "YEvent.h"
25 #include "utf8.h"
26
27 using std::max;
28
29 #include "YQCheckBoxFrame.h"
30
31 #define TOP_MARGIN 6
32
33
34 YQCheckBoxFrame::YQCheckBoxFrame( YWidget *             parent,
35                                   const string &        label,
36                                   bool                  checked )
37     : QGroupBox( (QWidget *) parent->widgetRep() )
38     , YCheckBoxFrame( parent, label, checked)
39 {
40     setWidgetRep ( this );
41     QGroupBox::setTitle( fromUTF8( label ) );
42     QGroupBox::setCheckable( true );
43     preventQGroupBoxAutoEnablement();
44                            
45     setValue( checked );
46 }
47
48
49 void YQCheckBoxFrame::setLabel( const string & newLabel )
50 {
51     YCheckBoxFrame::setLabel( newLabel );
52     QGroupBox::setTitle( fromUTF8( label() ) );
53 }
54
55
56 bool YQCheckBoxFrame::value()
57 {
58     return _checkBox ? _checkBox->isChecked() : QGroupBox::isChecked();
59 }
60
61
62 void YQCheckBoxFrame::setValue( bool newValue )
63 {
64     if ( _checkBox )
65         _checkBox->setChecked( newValue );
66     else
67         setChecked( newValue );
68 }
69
70
71 void YQCheckBoxFrame::setEnabled( bool enabled )
72 {
73     if ( enabled )
74     {
75         QFrame::setEnabled( true );
76         handleChildrenEnablement( value() );
77     }
78     else
79     {
80         QFrame::setEnabled( true );
81         YWidget::setChildrenEnabled( false );
82     }
83     
84     YWidget::setEnabled( enabled );
85 }
86
87
88 void YQCheckBoxFrame::preventQGroupBoxAutoEnablement()
89 {
90     /*
91      * This is a nasty hack. But it is necessary because QGroupBox handles its
92      * internal check box too nearsightedly: It forces all children to be
93      * enabled or disabled depending on the status of the check box. The
94      * behaviour cannot be inverted or suppressed.
95      *
96      * In some cases, however, it makes sense to let the application decide to
97      * handle that differently. Since the YaST2 UI is a toolkit, we leave this
98      * decision up to the application rather than forcing any specific behaviour.
99      */
100
101     // Find the check box in the child hierarchy (as a direct child)
102
103     _checkBox = dynamic_cast<QCheckBox *>( QObject::child( 0,           // objName
104                                                            "QCheckBox", // inheritsClass
105                                                            false ) );   // recursive
106
107     if ( ! _checkBox )
108     {
109         y2warning( "Can't find QCheckBox child" );
110
111         connect( this, SIGNAL( toggled     ( bool ) ),
112                  this, SLOT  ( stateChanged( bool ) ) );
113
114         return;
115     }
116
117     // Disconnect all signals to this object.
118     //
119     // In particular, disconnect the connection from the check box's
120     // 'toggled()' signal to this object's parent class's (private)
121     // setChildrenEnabled() method.
122
123     disconnect( _checkBox,      // sender
124                 0,              // signal
125                 this,           // receiver
126                 0 );            // slot (private method in parent class)
127
128     // Connect the check box directly to this class.
129
130     connect( _checkBox, SIGNAL( toggled     ( bool ) ),
131              this,      SLOT  ( stateChanged( bool ) ) );
132 }
133
134
135 void YQCheckBoxFrame::stateChanged( bool newState )
136 {
137     y2debug( "new state: %d", newState );
138     handleChildrenEnablement( newState );
139
140     if ( notify() )
141         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
142 }
143
144
145 void YQCheckBoxFrame::childEvent( QChildEvent * )
146 {
147     // Reimplemented to prevent the parent class disabling child widgets
148     // according to its default policy.
149
150     // y2debug( "ChildEvent" );
151 }
152
153
154 void
155 YQCheckBoxFrame::setSize( int newWidth, int newHeight )
156 {
157     resize ( newWidth, newHeight );
158
159     if ( hasChildren() )
160     {
161         int newChildWidth  = max ( 0, newWidth  - 2 * frameWidth() - 1 );
162         int newChildHeight = max ( 0, newHeight - frameWidth() - fontMetrics().height() - TOP_MARGIN - 1 );
163
164         firstChild()->setSize( newChildWidth, newChildHeight );
165         
166         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
167         qChild->move( frameWidth(), fontMetrics().height() + TOP_MARGIN );
168     }
169 }
170
171
172 int YQCheckBoxFrame::preferredWidth()
173 {
174     int preferredWidth;
175     int childPreferredWidth = hasChildren() ? firstChild()->preferredWidth() : 0;
176
177     preferredWidth = max( childPreferredWidth,
178                           (10 + fontMetrics().width( title() ) ) );
179     preferredWidth += 2*frameWidth() + 1;
180
181     return preferredWidth;
182 }
183
184
185 int YQCheckBoxFrame::preferredHeight()
186 {
187     int preferredHeight = hasChildren() ? firstChild()->preferredHeight() : 0;
188     preferredHeight += frameWidth() + fontMetrics().height() + TOP_MARGIN + 1;
189     
190     return preferredHeight;
191 }
192
193
194 bool YQCheckBoxFrame::setKeyboardFocus()
195 {
196     QGroupBox::setFocus();
197
198     return true;
199 }
200
201
202
203
204 #include "YQCheckBoxFrame.moc"