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