libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQRadioButton.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:       YQRadioButton.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #include <qradiobutton.h>
00026 #include <QMouseEvent>
00027 #include <QBoxLayout>
00028 
00029 #define YUILogComponent "qt-ui"
00030 #include <yui/YUILog.h>
00031 
00032 #include "utf8.h"
00033 #include "YQUI.h"
00034 #include "YQApplication.h"
00035 #include <yui/YEvent.h>
00036 #include "YQRadioButton.h"
00037 #include <yui/YRadioButtonGroup.h>
00038 #include "YQSignalBlocker.h"
00039 
00040 using std::string;
00041 
00042 #define SPACING 8
00043 
00044 // +----+----------------------------------+----+
00045 // |    |(o) RadioButtonlabel              |    |
00046 // +----+----------------------------------+----+
00047 // <----> SPACING                          <---->
00048 
00049 
00050 
00051 YQRadioButton::YQRadioButton( YWidget *         parent,
00052                               const std::string &       label,
00053                               bool              checked )
00054     : QRadioButton( fromUTF8( label ), ( QWidget *) (parent->widgetRep() ) )
00055     , YRadioButton( parent, label )
00056 {
00057     setWidgetRep( this );
00058 
00059     // QRadioButton uses its own logic by default to make sure that only one
00060     // button of a radio box is checked at any time, but this interferes with
00061     // YRadioButtonGroup. Let YRadioButtonGroup and YQRadioButton::changed()
00062     // handle this.
00063     setAutoExclusive( false );
00064 
00065     setChecked( checked );
00066 
00067     installEventFilter(this);
00068 
00069     connect ( this,     SIGNAL( toggled ( bool ) ),
00070               this,     SLOT  ( changed ( bool ) ) );
00071 }
00072 
00073 
00074 void
00075 YQRadioButton::setUseBoldFont( bool useBold )
00076 {
00077     setFont( useBold ?
00078              YQUI::yqApp()->boldFont() :
00079              YQUI::yqApp()->currentFont() );
00080 
00081     YRadioButton::setUseBoldFont( useBold );
00082 }
00083 
00084 
00085 int YQRadioButton::preferredWidth()
00086 {
00087     return sizeHint().width();
00088 }
00089 
00090 
00091 int YQRadioButton::preferredHeight()
00092 {
00093     return sizeHint().height();
00094 }
00095 
00096 
00097 void YQRadioButton::setSize( int newWidth, int newHeight )
00098 {
00099     resize( newWidth, newHeight );
00100 }
00101 
00102 
00103 bool YQRadioButton::value()
00104 {
00105     return isChecked();
00106 }
00107 
00108 
00109 void YQRadioButton::setValue( bool newValue )
00110 {
00111     YQSignalBlocker sigBlocker( this );
00112 
00113     // yuiDebug() << "Setting " << this << (newValue ? " on" : " off") << std::endl;
00114     setChecked( newValue );
00115 
00116     if ( newValue )
00117     {
00118         YRadioButtonGroup * group = buttonGroup();
00119 
00120         if ( group )
00121             group->uncheckOtherButtons( this );
00122     }
00123 }
00124 
00125 
00126 void YQRadioButton::setLabel( const std::string & label )
00127 {
00128     setText( fromUTF8( label ) );
00129     YRadioButton::setLabel( label );
00130 }
00131 
00132 
00133 void YQRadioButton::setEnabled( bool enabled )
00134 {
00135     QRadioButton::setEnabled( enabled );
00136     YWidget::setEnabled( enabled );
00137 }
00138 
00139 
00140 bool YQRadioButton::setKeyboardFocus()
00141 {
00142     setFocus();
00143 
00144     return true;
00145 }
00146 
00147 
00148 void YQRadioButton::changed( bool newState )
00149 {
00150     if ( newState )
00151     {
00152         yuiDebug() << "User set " << this << ( newState ? " on" : " off" ) << std::endl;
00153         YRadioButtonGroup * group = buttonGroup();
00154 
00155         if ( group )
00156             group->uncheckOtherButtons( this );
00157 
00158         if ( notify() )
00159             YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
00160     }
00161     else
00162     {
00163         // prevent that the user deselects all items
00164         setChecked( true );
00165     }
00166 }
00167 
00168 
00169 bool YQRadioButton::eventFilter( QObject * obj, QEvent * event )
00170 {
00171     if ( event && event->type() == QEvent::MouseButtonRelease )
00172     {
00173         QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
00174 
00175         if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
00176         {
00177             yuiMilestone() << "Right click on button detected" << std::endl;
00178             YQUI::yqApp()->maybeLeftHandedUser();
00179         }
00180     }
00181 
00182     return QObject::eventFilter( obj, event );
00183 }
00184 
00185 
00186 #include "YQRadioButton.moc"
 All Classes Functions Variables