libyui-qt  2.47.1.1
YQBusyIndicator.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQBusyIndicator.cc
20 
21  Author: Thomas Goettlicher <tgoettlicher@suse.de>
22 
23 /-*/
24 
25 
26 //#include <qprogressbar.h>
27 #include <QLabel>
28 #include <QPalette>
29 #include <QTimer>
30 #include <QVBoxLayout>
31 #include <QFrame>
32 #include <QPainter>
33 #include <math.h>
34 
35 #define YUILogComponent "qt-ui"
36 #include <yui/YUILog.h>
37 
38 using std::max;
39 
40 #include "utf8.h"
41 #include "YQUI.h"
42 #include "YQBusyIndicator.h"
43 #include "YQWidgetCaption.h"
44 
45 
46 #define REPAINT_INTERVAL 100
47 #define STEP_SIZE .05
48 #define MINIMUM_WITDH 100
49 #define MINIMUM_HEIGHT 24
50 
51 
52 BusyBar::BusyBar(QWidget *parent)
53  : QFrame(parent)
54  , _position(.5)
55  , _rightwards(true)
56  , _alive(true)
57 {
58  setMinimumSize(MINIMUM_WITDH, MINIMUM_HEIGHT);
59 
60  _timer = new QTimer(this);
61  connect(_timer, &pclass(_timer)::timeout, this, &pclass(this)::update);
62  _timer->start(REPAINT_INTERVAL);
63 
64  setFrameStyle (QFrame::Panel | QFrame::Sunken );
65  setLineWidth(2);
66  setMidLineWidth(2);
67 }
68 
69 void BusyBar::update()
70 {
71  if (!_alive)
72  return;
73 
74  if (_position > 1.0 - STEP_SIZE || _position < STEP_SIZE )
75  _rightwards = !_rightwards;
76 
77  if (_rightwards)
78  _position += STEP_SIZE;
79  else
80  _position -= STEP_SIZE;
81 
82  repaint();
83 }
84 
86 {
87  _alive=true;
88 }
89 
91 {
92  _alive=false;
93 }
94 
95 void BusyBar::paintEvent( QPaintEvent * e )
96 {
97 
98  QPalette palette = QApplication::palette();
99  QColor foreground = palette.color( QPalette::Active, QPalette::Highlight );
100  QColor background = palette.color( QPalette::Active, QPalette::Base );
101 
102  QPainter painter(this);
103  QLinearGradient gradient(0, 0, width()-1, 0 );
104 
105  gradient.setColorAt( 0.0, background );
106  gradient.setColorAt( _position, foreground );
107  gradient.setColorAt( 1.0, background );
108 
109  painter.setBrush( gradient );
110  painter.setPen( Qt::NoPen );
111  painter.drawRect( rect() );
112  painter.end();
113 
114  QFrame::paintEvent( e );
115 }
116 
117 
119  const std::string & label,
120  int timeout )
121  : QFrame( (QWidget *) parent->widgetRep() )
122  , YBusyIndicator( parent, label, timeout )
123  , _timeout (timeout)
124 {
125 
126  _timer = new QTimer(this);
127  connect(_timer, &pclass(_timer)::timeout, this, &pclass(this)::setStalled);
128  _timer->start(_timeout);
129 
130  QVBoxLayout* layout = new QVBoxLayout( this );
131  setLayout( layout );
132 
133  setWidgetRep( this );
134 
135  layout->setSpacing( YQWidgetSpacing );
136  layout->setMargin ( YQWidgetMargin );
137 
138  _caption = new YQWidgetCaption( this, label );
139  YUI_CHECK_NEW( _caption );
140  layout->addWidget( _caption );
141 
142  _bar = new BusyBar( this );
143  YUI_CHECK_NEW ( _bar );
144  layout->addWidget( _bar );
145  _caption->setBuddy( _bar );
146 
147 }
148 
149 
151 {
152  // NOP
153 }
154 
155 
156 void YQBusyIndicator::setLabel( const std::string & label )
157 {
158  _caption->setText( label );
159  YBusyIndicator::setLabel( label );
160 }
161 
162 
163 void YQBusyIndicator::setAlive( bool newAlive )
164 {
165  YBusyIndicator::setAlive( newAlive );
166  if (newAlive)
167  {
168  _bar->run();
169  _timer->stop();
170  _timer->start(_timeout);
171  }
172  else
173  {
174  _bar->stop();
175  _timer->stop();
176  }
177 }
178 
179 
180 void YQBusyIndicator::setStalled()
181 {
182  setAlive( false );
183 }
184 
185 
186 void YQBusyIndicator::setTimeout( int newTimeout )
187 {
188  _timeout = newTimeout;
189  YBusyIndicator::setTimeout( newTimeout );
190 }
191 
192 
193 void YQBusyIndicator::setEnabled( bool enabled )
194 {
195  _caption->setEnabled( enabled );
196  _bar->setEnabled( enabled );
197  YWidget::setEnabled( enabled );
198 }
199 
200 
202 {
203  int hintWidth = !_caption->isHidden() ?
204  _caption->sizeHint().width() + layout()->margin() : 0;
205 
206  return max( 200, hintWidth );
207 }
208 
209 
211 {
212  return sizeHint().height();
213 }
214 
215 
216 void YQBusyIndicator::setSize( int newWidth, int newHeight )
217 {
218  resize( newWidth, newHeight );
219 }
220 
221 
223 {
224  _bar->setFocus();
225 
226  return true;
227 }
228 
229 
230 #include "YQBusyIndicator.moc"
virtual void setLabel(const std::string &label)
Set the label (the caption above the progress bar).
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.
virtual int preferredHeight()
Preferred height of the widget.
void run()
start moving bar animation
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
virtual int preferredWidth()
Preferred width of the widget.
void stop()
stop moving bar animation
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void setAlive(bool newAlive)
Send a keep allive message.
virtual void setTimeout(int newTimeout)
Set the timeout is ms after that the widget shows &#39;stalled&#39; when no new tick is received.
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
virtual bool setKeyboardFocus()
Accept the keyboard focus.
YQBusyIndicator(YWidget *parent, const std::string &label, int timeout=1000)
Constructor.
virtual ~YQBusyIndicator()
Destructor.