libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQCheckBoxFrame.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       YQCheckBoxFrame.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #define YUILogComponent "qt-ui"
00027 #include <yui/YUILog.h>
00028 #include <qcheckbox.h>
00029 #include <QDebug>
00030 #include <QVBoxLayout>
00031 #include <qevent.h>
00032 #include "YQUI.h"
00033 #include <yui/YEvent.h>
00034 #include "utf8.h"
00035 
00036 using std::max;
00037 
00038 #include "YQCheckBoxFrame.h"
00039 
00040 #define TOP_MARGIN 6
00041 
00042 
00043 YQCheckBoxFrame::YQCheckBoxFrame( YWidget *             parent,
00044                                   const std::string &   label,
00045                                   bool                  checked )
00046     : QGroupBox( (QWidget *) parent->widgetRep() )
00047     , YCheckBoxFrame( parent, label, checked)
00048 {
00049     setWidgetRep ( this );
00050     QGroupBox::setTitle( fromUTF8( label ) );
00051     QGroupBox::setCheckable( true );
00052     setValue( checked );
00053 
00054     connect( this, SIGNAL( toggled     ( bool ) ),
00055              this, SLOT  ( stateChanged( bool ) ) );
00056 }
00057 
00058 
00059 void YQCheckBoxFrame::setLabel( const std::string & newLabel )
00060 {
00061     YCheckBoxFrame::setLabel( newLabel );
00062     QGroupBox::setTitle( fromUTF8( label() ) );
00063 }
00064 
00065 
00066 bool YQCheckBoxFrame::value()
00067 {
00068     return QGroupBox::isChecked();
00069 }
00070 
00071 
00072 void YQCheckBoxFrame::setValue( bool newValue )
00073 {
00074     setChecked( newValue );
00075     setEnabled( newValue );
00076 }
00077 
00078 
00079 void YQCheckBoxFrame::setEnabled( bool enabled )
00080 {
00081     if ( enabled )
00082     {
00083         QGroupBox::setEnabled( true );
00084         handleChildrenEnablement( value() );
00085     }
00086     else
00087     {
00088         QGroupBox::setEnabled( true );
00089         YWidget::setChildrenEnabled( false );
00090     }
00091 
00092     YWidget::setEnabled( enabled );
00093 }
00094 
00095 
00096 void YQCheckBoxFrame::stateChanged( bool newState )
00097 {
00098     if ( notify() )
00099         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
00100 }
00101 
00102 
00103 bool YQCheckBoxFrame::event( QEvent *e )
00104 {
00105     bool oldChildEnabled = true;
00106 
00107     if ( YCheckBoxFrame::hasChildren() )
00108         oldChildEnabled = YCheckBoxFrame::firstChild()->isEnabled();
00109 
00110     bool oldStatus = QGroupBox::isChecked();
00111     bool ret = QGroupBox::event( e );
00112     bool newStatus = QGroupBox::isChecked();
00113 
00114     if ( oldStatus != newStatus )
00115     {
00116         yuiDebug() << "Status change of " << this << " : now " << std::boolalpha << newStatus << std::endl;
00117 
00118         if ( autoEnable() )
00119         {
00120             handleChildrenEnablement( newStatus );
00121         }
00122         else
00123         {
00124             if ( YCheckBoxFrame::hasChildren() )
00125                 YCheckBoxFrame::firstChild()->setEnabled( oldChildEnabled );
00126         }
00127     }
00128 
00129     return ret;
00130 }
00131 
00132 
00133 void YQCheckBoxFrame::childEvent( QChildEvent * event )
00134 {
00135     if ( event->added() )
00136     {
00137         // yuiDebug() << "Child widget added" << std::endl;
00138 
00139         // Prevent parent class from disabling child widgets according to its
00140         // own policy: YCheckBoxFrame is much more flexible than QGroupBox.
00141     }
00142     else
00143         QGroupBox::childEvent( event );
00144 }
00145 
00146 
00147 void
00148 YQCheckBoxFrame::setSize( int newWidth, int newHeight )
00149 {
00150     resize ( newWidth, newHeight );
00151 
00152     if ( hasChildren() )
00153     {
00154         int left, top, right, bottom;
00155         getContentsMargins( &left, &top, &right, &bottom );
00156         int newChildWidth  = newWidth - left - right;
00157         int newChildHeight = newHeight - bottom - top;
00158 
00159         firstChild()->setSize( newChildWidth, newChildHeight );
00160 
00161         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
00162         qChild->move( left, top );
00163     }
00164 }
00165 
00166 
00167 int YQCheckBoxFrame::preferredWidth()
00168 {
00169     int preferredWidth = hasChildren() ? firstChild()->preferredWidth() : 0;
00170     int left, top, right, bottom;
00171     getContentsMargins( &left, &top, &right, &bottom );
00172 
00173     return preferredWidth + left + right;
00174 }
00175 
00176 
00177 int YQCheckBoxFrame::preferredHeight()
00178 {
00179     int preferredHeight = hasChildren() ? firstChild()->preferredHeight() : 0;
00180     int left, top, right, bottom;
00181     getContentsMargins( &left, &top, &right, &bottom );
00182 
00183     return preferredHeight + top + left;
00184 }
00185 
00186 
00187 bool YQCheckBoxFrame::setKeyboardFocus()
00188 {
00189     setFocus();
00190 
00191     return true;
00192 }
00193 
00194 
00195 
00196 
00197 #include "YQCheckBoxFrame.moc"
 All Classes Functions Variables