libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQSlider.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:       YQSlider.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #include <QSlider>
00026 #include <QSpinBox>
00027 #include <QLabel>
00028 #include <QVBoxLayout>
00029 
00030 #define YUILogComponent "qt-ui"
00031 #include <yui/YUILog.h>
00032 
00033 #include "utf8.h"
00034 #include "YQUI.h"
00035 #include <yui/YEvent.h>
00036 #include "YQSlider.h"
00037 #include "YQSignalBlocker.h"
00038 #include "YQWidgetCaption.h"
00039 
00040 
00041 YQSlider::YQSlider( YWidget *           parent,
00042                     const std::string & label,
00043                     int                 minValue,
00044                     int                 maxValue,
00045                     int                 initialValue,
00046                     bool                reverseLayout )
00047 
00048     : QFrame( (QWidget *) parent->widgetRep() )
00049     , YSlider( parent, label, minValue, maxValue )
00050 {
00051     setWidgetRep( this );
00052 
00053     QVBoxLayout* toplayout = new QVBoxLayout( this );
00054     setLayout( toplayout );
00055 
00056     toplayout->setSpacing( YQWidgetSpacing );
00057     toplayout->setMargin ( YQWidgetMargin );
00058 
00059     _caption = new YQWidgetCaption( this, label );
00060     YUI_CHECK_NEW( _caption );
00061     toplayout->addWidget( _caption );
00062 
00063     _hbox = new QFrame( this );
00064     YUI_CHECK_NEW( _hbox );
00065     toplayout->addWidget( _hbox );
00066 
00067     QHBoxLayout *layout = new QHBoxLayout( _hbox );
00068     _hbox->setLayout( layout );
00069 
00070     layout->setMargin ( YQWidgetMargin );
00071     layout->setSpacing( YQWidgetSpacing );
00072 
00073     if ( reverseLayout )
00074     {
00075         _qt_spinBox = new QSpinBox( _hbox );
00076         _qt_spinBox->setMinimum(minValue);
00077         _qt_spinBox->setMaximum(maxValue);
00078         _qt_spinBox->setSingleStep(1);
00079         layout->addWidget( _qt_spinBox );
00080     }
00081     else
00082     {
00083         _caption->setAlignment( Qt::AlignRight );
00084     }
00085 
00086     _qt_slider = new QSlider( Qt::Horizontal, _hbox );
00087     _qt_slider->setMinimum(minValue);
00088     _qt_slider->setMaximum(maxValue);
00089     _qt_slider->setPageStep(1);
00090     YUI_CHECK_NEW( _qt_slider );
00091     layout->addWidget( _qt_slider );
00092 
00093     if ( ! reverseLayout )
00094     {
00095         _qt_spinBox = new QSpinBox( _hbox );
00096         _qt_spinBox->setMinimum(minValue);
00097         _qt_spinBox->setMaximum(maxValue);
00098         _qt_spinBox->setSingleStep(1);
00099 
00100         layout->addWidget( _qt_spinBox );
00101     }
00102     YUI_CHECK_NEW( _qt_spinBox );
00103 
00104     _qt_spinBox->setValue( initialValue );
00105     _caption->setBuddy( _qt_spinBox );
00106 
00107     setValue( initialValue );
00108 
00109     connect( _qt_spinBox, SIGNAL( valueChanged(int) ),
00110              _qt_slider,  SLOT  ( setValue    (int) ) );
00111 
00112     connect( _qt_slider,  SIGNAL( valueChanged(int) ),
00113              _qt_spinBox, SLOT  ( setValue    (int) ) );
00114 
00115     connect( _qt_spinBox, SIGNAL( valueChanged    (int) ),
00116              this,        SLOT  ( valueChangedSlot(int) ) );
00117 }
00118 
00119 
00120 YQSlider::~YQSlider()
00121 {
00122     // NOP
00123 }
00124 
00125 
00126 int
00127 YQSlider::value()
00128 {
00129     return _qt_spinBox->value();
00130 }
00131 
00132 
00133 void
00134 YQSlider::setValueInternal( int newValue )
00135 {
00136     YQSignalBlocker sigBlocker1( _qt_spinBox );
00137     YQSignalBlocker sigBlocker2( _qt_slider  );
00138     _qt_slider->setValue ( newValue );
00139     _qt_spinBox->setValue( newValue );
00140 }
00141 
00142 
00143 void
00144 YQSlider::valueChangedSlot( int newValue )
00145 {
00146     if ( notify() )
00147         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
00148 
00149     emit valueChanged( newValue );
00150 }
00151 
00152 
00153 void
00154 YQSlider::setEnabled( bool enabled )
00155 {
00156     _caption->setEnabled  ( enabled );
00157     _qt_slider->setEnabled ( enabled );
00158     _qt_spinBox->setEnabled( enabled );
00159     YWidget::setEnabled( enabled );
00160 }
00161 
00162 
00163 int
00164 YQSlider::preferredWidth()
00165 {
00166     int hintWidth = !_caption->isHidden() ? _caption->sizeHint().width() : 0;
00167 
00168     // Arbitrary value - there is no really good default
00169     return std::max( 200, hintWidth );
00170 }
00171 
00172 
00173 int
00174 YQSlider::preferredHeight()
00175 {
00176     return sizeHint().height();
00177 }
00178 
00179 
00180 void
00181 YQSlider::setSize( int newWidth, int newHeight )
00182 {
00183     resize( newWidth, newHeight );
00184 }
00185 
00186 
00187 void
00188 YQSlider::setLabel( const std::string & newLabel )
00189 {
00190     _caption->setText( newLabel );
00191     YSlider::setLabel( newLabel );
00192 }
00193 
00194 
00195 bool
00196 YQSlider::setKeyboardFocus()
00197 {
00198     _qt_spinBox->setFocus();
00199 
00200     return true;
00201 }
00202 
00203 
00204 #include "YQSlider.moc"
 All Classes Functions Variables