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