libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQGenericButton.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:       YQGenericButton.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <qpushbutton.h>
00027 #include <qsize.h>
00028 #include <qevent.h>
00029 #include <qpixmap.h>
00030 #include <qevent.h>
00031 #define YUILogComponent "qt-ui"
00032 #include <yui/YUILog.h>
00033 
00034 #include "utf8.h"
00035 #include "YQUI.h"
00036 #include "YQApplication.h"
00037 #include <yui/YEvent.h>
00038 #include "YQGenericButton.h"
00039 #include "YQDialog.h"
00040 
00041 
00042 YQGenericButton::YQGenericButton( YWidget *             parent,
00043                                   const std::string &   label )
00044     : QWidget( (QWidget *) parent->widgetRep() )
00045     , YPushButton( parent, label )
00046     , _dialog( 0 )
00047     , _qPushButton( 0 )
00048 {
00049     setWidgetRep( 0 );
00050 }
00051 
00052 
00053 void YQGenericButton::setQPushButton( QPushButton * pb )
00054 {
00055     _qPushButton = pb;
00056     _qPushButton->installEventFilter( this );
00057     _qPushButton->setAutoDefault( true );
00058 
00059     YPushButton::setLabel( toUTF8 ( _qPushButton->text() ) );
00060 }
00061 
00062 
00063 YQGenericButton::~YQGenericButton()
00064 {
00065     if ( _dialog ) // If we don't have one any more, don't bother
00066     {
00067         if ( _dialog->focusButton() == this )
00068             _dialog->losingFocus( this );
00069 
00070         if ( _dialog->defaultButton() == this )
00071             _dialog->setDefaultButton(0);
00072     }
00073 }
00074 
00075 
00076 void YQGenericButton::forgetDialog()
00077 {
00078    _dialog = 0;
00079 }
00080 
00081 
00082 YQDialog *
00083 YQGenericButton::dialog()
00084 {
00085     if ( ! _dialog )
00086     {
00087         YDialog * yDialog = findDialog();
00088 
00089         if ( yDialog )
00090             _dialog = dynamic_cast<YQDialog *> (yDialog);
00091 
00092         YUI_CHECK_PTR( _dialog );
00093     }
00094 
00095     return _dialog;
00096 }
00097 
00098 
00099 void YQGenericButton::setEnabled( bool enabled )
00100 {
00101     if ( _qPushButton )
00102         _qPushButton->setEnabled( enabled );
00103 
00104     YWidget::setEnabled( enabled );
00105 }
00106 
00107 
00108 bool YQGenericButton::isEnabled() const
00109 {
00110     return _qPushButton ? _qPushButton->isEnabled() : false;
00111 }
00112 
00113 
00114 void YQGenericButton::setIcon( const std::string & iconName )
00115 {
00116     if ( ! _qPushButton )
00117     {
00118         yuiError() << "NULL button (icon " << iconName << ")" << std::endl;
00119         return;
00120     }
00121 
00122     QString qIconName = fromUTF8( iconName );
00123 
00124     if ( qIconName.isEmpty() )
00125     {
00126         _qPushButton->setIcon( QIcon() );
00127         return;
00128     }
00129 
00130     // Search for the icon - FaTE #306356
00131     qIconName =  fromUTF8( YQUI::yqApp()->iconLoader()->findIcon( iconName ) );
00132     QPixmap icon( qIconName );
00133 
00134     if ( icon.isNull() )
00135         yuiWarning() << "Can't load icon \"" << qIconName << "\"" << std::endl;
00136     else
00137         _qPushButton->setIcon( icon );
00138 }
00139 
00140 
00141 void YQGenericButton::setLabel( const QString & label )
00142 {
00143     if ( _qPushButton )
00144         _qPushButton->setText( label );
00145     else
00146         yuiError() << "NULL button \"" << label << "\"" << std::endl;
00147 
00148     YPushButton::setLabel( toUTF8( label ) );
00149 }
00150 
00151 
00152 void YQGenericButton::setLabel( const std::string & label )
00153 {
00154     if ( _qPushButton )
00155         _qPushButton->setText( fromUTF8( label ) );
00156     else
00157         yuiError() << "NULL button \"" << label << "\"" << std::endl;
00158 
00159     YPushButton::setLabel( label );
00160 }
00161 
00162 
00163 void YQGenericButton::showAsDefault( bool show )
00164 {
00165     if ( _qPushButton )
00166     {
00167         _qPushButton->setAutoDefault( !show );
00168         _qPushButton->setDefault( show );
00169         _qPushButton->update();
00170     }
00171 }
00172 
00173 
00174 bool YQGenericButton::isShownAsDefault() const
00175 {
00176     return _qPushButton ? _qPushButton->isDefault() : false;
00177 }
00178 
00179 
00180 QString
00181 YQGenericButton::text() const
00182 {
00183     return _qPushButton ? _qPushButton->text() : "";
00184 }
00185 
00186 
00187 void YQGenericButton::activate()
00188 {
00189     if ( _qPushButton )
00190         _qPushButton->animateClick();
00191 }
00192 
00193 
00194 bool YQGenericButton::eventFilter( QObject * obj, QEvent * event )
00195 {
00196     if ( event )
00197     {
00198         if ( event->type() == QEvent::FocusIn )
00199         {
00200             dialog()->gettingFocus( this );
00201             return false;       // event processed?
00202         }
00203         else if ( event->type() == QEvent::FocusOut )
00204         {
00205             dialog()->losingFocus( this );
00206             return false;       // event processed?
00207         }
00208         else if ( event->type() == QEvent::MouseButtonRelease )
00209         {
00210             QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
00211 
00212             if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
00213             {
00214                 yuiMilestone() << "Right click on button detected" << std::endl;
00215                 YQUI::yqApp()->maybeLeftHandedUser();
00216             }
00217         }
00218     }
00219 
00220 
00221     return QObject::eventFilter( obj, event );
00222 }
00223 
00224 
00225 bool YQGenericButton::setKeyboardFocus()
00226 {
00227     if ( ! _qPushButton )
00228         return false;
00229 
00230     dialog()->gettingFocus( this );
00231     _qPushButton->setFocus();
00232 
00233     return true;
00234 }
00235 
00236 
00237 #include "YQGenericButton.moc"
 All Classes Functions Variables