]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQCheckBoxFrame.cc
reworked the hack to be more generic and less specific
[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 <QDebug>
24 #include <QVBoxLayout>
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
47     connect( this, SIGNAL( toggled( bool ) ),
48              SLOT( stateChanged( bool ) ) );
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 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::stateChanged( bool newState )
92 {
93     handleChildrenEnablement( newState );
94
95     if ( notify() )
96         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
97 }
98
99 bool YQCheckBoxFrame::event(QEvent *e)
100 {
101     /* now on to something very fishy. The purpose of this widget
102      * is for whatever reason to provide a checkbox with a groupbox
103      * without the children having any connection to it.
104      *
105      * So we use this trick to undo everything the base class did
106      */
107     QHash<QWidget*, bool> widgetState;
108
109     QObjectList childList = children();
110     for (int i = 0; i < childList.size(); ++i)
111     {
112         QObject *o = childList.at(i);
113         if (o->isWidgetType())
114         {
115             QWidget *w = static_cast<QWidget *>(o);
116             widgetState[w] = w->isEnabled();
117         }
118     }
119
120     bool ret = QGroupBox::event( e );
121
122     childList = children();
123     for (int i = 0; i < childList.size(); ++i)
124     {
125         QObject *o = childList.at(i);
126         if (o->isWidgetType())
127         {
128             QWidget *w = static_cast<QWidget *>(o);
129             if ( widgetState.contains( w ) )
130                 w->setEnabled( widgetState[w] );
131         }
132     }
133
134     return ret;
135 }
136
137 void YQCheckBoxFrame::childEvent( QChildEvent * )
138 {
139     // Reimplemented to prevent the parent class disabling child widgets
140     // according to its default policy.
141
142     // y2debug( "ChildEvent" );
143 }
144
145
146 void
147 YQCheckBoxFrame::setSize( int newWidth, int newHeight )
148 {
149     resize ( newWidth, newHeight );
150
151     if ( hasChildren() )
152     {
153         int left, top, right, bottom;
154         getContentsMargins( &left, &top, &right, &bottom );
155         int newChildWidth  = newWidth - left - right;
156         int newChildHeight = newHeight - bottom - top;
157
158         firstChild()->setSize( newChildWidth, newChildHeight );
159
160         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
161         qChild->move( left, top );
162     }
163 }
164
165
166 int YQCheckBoxFrame::preferredWidth()
167 {
168     int preferredWidth = hasChildren() ? firstChild()->preferredWidth() : 0;
169     int left, top, right, bottom;
170     getContentsMargins( &left, &top, &right, &bottom );
171
172     preferredWidth += left + right;
173
174     return preferredWidth;
175 }
176
177
178 int YQCheckBoxFrame::preferredHeight()
179 {
180     int preferredHeight = hasChildren() ? firstChild()->preferredHeight() : 0;
181     int left, top, right, bottom;
182     getContentsMargins( &left, &top, &right, &bottom );
183
184     preferredHeight += top + left;
185
186     return preferredHeight;
187 }
188
189
190 bool YQCheckBoxFrame::setKeyboardFocus()
191 {
192     setFocus();
193
194     return true;
195 }
196
197
198
199
200 #include "YQCheckBoxFrame.moc"