]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQCheckBoxFrame.cc
unhide some virtual overloads - fixing Huha's only complaint :)
[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
107     _checkBox = dynamic_cast<QCheckBox *>( QObject::child( 0,           // objName
108                                                            "QCheckBox", // inheritsClass
109                                                            false ) );   // recursive
110
111     if ( ! _checkBox )
112     {
113         y2warning( "Can't find QCheckBox child" );
114
115         connect( this, SIGNAL( toggled     ( bool ) ),
116                  this, SLOT  ( stateChanged( bool ) ) );
117
118         return;
119     }
120
121     // Disconnect all signals to this object.
122     //
123     // In particular, disconnect the connection from the check box's
124     // 'toggled()' signal to this object's parent class's (private)
125     // setChildrenEnabled() method.
126
127     disconnect( _checkBox,      // sender
128                 0,              // signal
129                 this,           // receiver
130                 0 );            // slot (private method in parent class)
131
132     // Connect the check box directly to this class.
133
134     connect( _checkBox, SIGNAL( toggled     ( bool ) ),
135              this,      SLOT  ( stateChanged( bool ) ) );
136 }
137
138
139 void YQCheckBoxFrame::stateChanged( bool newState )
140 {
141     y2debug( "new state: %d", newState );
142     handleChildrenEnablement( newState );
143
144     if ( notify() )
145         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
146 }
147
148
149 void YQCheckBoxFrame::childEvent( QChildEvent * )
150 {
151     // Reimplemented to prevent the parent class disabling child widgets
152     // according to its default policy.
153
154     // y2debug( "ChildEvent" );
155 }
156
157
158 void
159 YQCheckBoxFrame::setSize( int newWidth, int newHeight )
160 {
161     resize ( newWidth, newHeight );
162
163     if ( hasChildren() )
164     {
165 #warning possibly this hack is not needed
166         /*
167         int newChildWidth  = max ( 0, newWidth  - 2 * frameWidth() - 1 );
168         int newChildHeight = max ( 0, newHeight - frameWidth() - fontMetrics().height() - TOP_MARGIN - 1 );
169
170         firstChild()->setSize( newChildWidth, newChildHeight );
171
172         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
173         qChild->move( frameWidth(), fontMetrics().height() + TOP_MARGIN );
174         */
175     }
176 }
177
178
179 int YQCheckBoxFrame::preferredWidth()
180 {
181     int preferredWidth;
182     int childPreferredWidth = hasChildren() ? firstChild()->preferredWidth() : 0;
183
184     preferredWidth = max( childPreferredWidth,
185                           (10 + fontMetrics().width( title() ) ) );
186     // preferredWidth += 2*frameWidth() + 1;
187
188     return preferredWidth;
189 }
190
191
192 int YQCheckBoxFrame::preferredHeight()
193 {
194     int preferredHeight = hasChildren() ? firstChild()->preferredHeight() : 0;
195     // preferredHeight += frameWidth() + fontMetrics().height() + TOP_MARGIN + 1;
196
197     return preferredHeight;
198 }
199
200
201 bool YQCheckBoxFrame::setKeyboardFocus()
202 {
203     setFocus();
204
205     return true;
206 }
207
208
209
210
211 #include "YQCheckBoxFrame.moc"