libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQBusyIndicator.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:       YQBusyIndicator.cc
00020 
00021   Author:     Thomas Goettlicher <tgoettlicher@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 //#include <qprogressbar.h>
00027 #include <QLabel>
00028 #include <QPalette>
00029 #include <QTimer>
00030 #include <QVBoxLayout>
00031 #include <QFrame>
00032 #include <QPainter>
00033 #include <math.h>
00034 
00035 #define YUILogComponent "qt-ui"
00036 #include <yui/YUILog.h>
00037 
00038 using std::max;
00039 
00040 #include "utf8.h"
00041 #include "YQUI.h"
00042 #include "YQBusyIndicator.h"
00043 #include "YQWidgetCaption.h"
00044 
00045 
00046 #define REPAINT_INTERVAL        100
00047 #define STEP_SIZE               .05
00048 #define MINIMUM_WITDH           100
00049 #define MINIMUM_HEIGHT          24
00050 
00051 
00052 BusyBar::BusyBar(QWidget *parent)
00053         : QFrame(parent)
00054         , _position(.5)
00055         , _rightwards(true)
00056         , _alive(true)
00057 {
00058     setMinimumSize(MINIMUM_WITDH, MINIMUM_HEIGHT);
00059 
00060     _timer = new QTimer(this);
00061     connect(_timer, SIGNAL(timeout()), this, SLOT(update()));
00062     _timer->start(REPAINT_INTERVAL);
00063 
00064     setFrameStyle (QFrame::Panel |  QFrame::Sunken );
00065     setLineWidth(2);
00066     setMidLineWidth(2);
00067 }
00068 
00069 void BusyBar::update()
00070 {
00071     if (!_alive)
00072         return;
00073 
00074     if (_position > 1.0 - STEP_SIZE || _position < STEP_SIZE )
00075         _rightwards = !_rightwards;
00076 
00077     if (_rightwards)
00078         _position += STEP_SIZE;
00079     else
00080         _position -= STEP_SIZE;
00081 
00082     repaint();
00083 }
00084 
00085 void BusyBar::run()
00086 {
00087     _alive=true;
00088 }
00089 
00090 void BusyBar::stop()
00091 {
00092     _alive=false;
00093 }
00094 
00095 void BusyBar::paintEvent( QPaintEvent * e )
00096 {
00097 
00098     QPalette palette = QApplication::palette();
00099     QColor foreground = palette.color( QPalette::Active, QPalette::Highlight );
00100     QColor background = palette.color( QPalette::Active, QPalette::Base );
00101 
00102     QPainter painter(this);
00103     QLinearGradient gradient(0, 0, width()-1, 0 );
00104 
00105     gradient.setColorAt( 0.0, background );
00106     gradient.setColorAt( _position, foreground );
00107     gradient.setColorAt( 1.0, background );
00108 
00109     painter.setBrush( gradient );
00110     painter.setPen( Qt::NoPen );
00111     painter.drawRect( rect() );
00112     painter.end();
00113 
00114     QFrame::paintEvent( e );
00115 }
00116 
00117 
00118 YQBusyIndicator::YQBusyIndicator( YWidget *     parent,
00119                               const std::string &       label,
00120                               int               timeout )
00121     : QFrame( (QWidget *) parent->widgetRep() )
00122     , YBusyIndicator( parent, label, timeout )
00123     , _timeout (timeout)
00124 {
00125 
00126     _timer = new QTimer(this);
00127     connect(_timer, SIGNAL(timeout()), this, SLOT(setStalled()));
00128     _timer->start(_timeout);
00129 
00130     QVBoxLayout* layout = new QVBoxLayout( this );
00131     setLayout( layout );
00132 
00133     setWidgetRep( this );
00134 
00135     layout->setSpacing( YQWidgetSpacing );
00136     layout->setMargin ( YQWidgetMargin  );
00137 
00138     _caption = new YQWidgetCaption( this, label );
00139     YUI_CHECK_NEW( _caption );
00140     layout->addWidget( _caption );
00141 
00142     _bar = new BusyBar( this );
00143     YUI_CHECK_NEW ( _bar );
00144     layout->addWidget( _bar );
00145     _caption->setBuddy( _bar );
00146 
00147 }
00148 
00149 
00150 YQBusyIndicator::~YQBusyIndicator()
00151 {
00152     // NOP
00153 }
00154 
00155 
00156 void YQBusyIndicator::setLabel( const std::string & label )
00157 {
00158     _caption->setText( label );
00159     YBusyIndicator::setLabel( label );
00160 }
00161 
00162 
00163 void YQBusyIndicator::setAlive( bool newAlive )
00164 {
00165     YBusyIndicator::setAlive( newAlive );
00166     if (newAlive)
00167     {
00168         _bar->run();
00169         _timer->stop();
00170         _timer->start(_timeout);
00171     }
00172     else
00173     {
00174         _bar->stop();
00175         _timer->stop();
00176     }
00177 }
00178 
00179 
00180 void YQBusyIndicator::setStalled()
00181 {
00182     setAlive( false );
00183 }
00184 
00185 
00186 void YQBusyIndicator::setTimeout( int newTimeout )
00187 {
00188     _timeout = newTimeout;
00189     YBusyIndicator::setTimeout( newTimeout );
00190 }
00191 
00192 
00193 void YQBusyIndicator::setEnabled( bool enabled )
00194 {
00195     _caption->setEnabled( enabled );
00196     _bar->setEnabled( enabled );
00197     YWidget::setEnabled( enabled );
00198 }
00199 
00200 
00201 int YQBusyIndicator::preferredWidth()
00202 {
00203     int hintWidth = !_caption->isHidden() ?
00204       _caption->sizeHint().width() + layout()->margin() : 0;
00205 
00206     return max( 200, hintWidth );
00207 }
00208 
00209 
00210 int YQBusyIndicator::preferredHeight()
00211 {
00212     return sizeHint().height();
00213 }
00214 
00215 
00216 void YQBusyIndicator::setSize( int newWidth, int newHeight )
00217 {
00218     resize( newWidth, newHeight );
00219 }
00220 
00221 
00222 bool YQBusyIndicator::setKeyboardFocus()
00223 {
00224     _bar->setFocus();
00225 
00226     return true;
00227 }
00228 
00229 
00230 #include "YQBusyIndicator.moc"
 All Classes Functions Variables