libyui-qt  2.53.0
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 #include "utf8.h"
39 #include "YQUI.h"
40 #include "YQBusyIndicator.h"
41 #include "YQWidgetCaption.h"
42 
43 
44 #define REPAINT_INTERVAL 100
45 #define STEP_SIZE .05
46 #define MINIMUM_WITDH 100
47 #define MINIMUM_HEIGHT 24
48 
49 using std::string;
50 
51 
52 
53 BusyBar::BusyBar(QWidget *parent)
54  : QFrame(parent)
55  , _position(.5)
56  , _rightwards(true)
57  , _alive(true)
58 {
59  setMinimumSize(MINIMUM_WITDH, MINIMUM_HEIGHT);
60 
61  _timer = new QTimer(this);
62  connect(_timer, &pclass(_timer)::timeout, this, &pclass(this)::update);
63  _timer->start(REPAINT_INTERVAL);
64 
65  setFrameStyle (QFrame::Panel | QFrame::Sunken );
66  setLineWidth(2);
67  setMidLineWidth(2);
68 }
69 
70 void BusyBar::update()
71 {
72  if (!_alive)
73  return;
74 
75  if (_position > 1.0 - STEP_SIZE || _position < STEP_SIZE )
76  _rightwards = !_rightwards;
77 
78  if (_rightwards)
79  _position += STEP_SIZE;
80  else
81  _position -= STEP_SIZE;
82 
83  repaint();
84 }
85 
87 {
88  _alive=true;
89 }
90 
92 {
93  _alive=false;
94 }
95 
96 void BusyBar::paintEvent( QPaintEvent * e )
97 {
98 
99  QPalette palette = QApplication::palette();
100  QColor foreground = palette.color( QPalette::Active, QPalette::Highlight );
101  QColor background = palette.color( QPalette::Active, QPalette::Base );
102 
103  QPainter painter(this);
104  QLinearGradient gradient(0, 0, width()-1, 0 );
105 
106  gradient.setColorAt( 0.0, background );
107  gradient.setColorAt( _position, foreground );
108  gradient.setColorAt( 1.0, background );
109 
110  painter.setBrush( gradient );
111  painter.setPen( Qt::NoPen );
112  painter.drawRect( rect() );
113  painter.end();
114 
115  QFrame::paintEvent( e );
116 }
117 
118 
120  const string & label,
121  int timeout )
122  : QFrame( (QWidget *) parent->widgetRep() )
123  , YBusyIndicator( parent, label, timeout )
124  , _timeout (timeout)
125 {
126 
127  _timer = new QTimer(this);
128  connect(_timer, &pclass(_timer)::timeout, this, &pclass(this)::setStalled);
129  _timer->start(_timeout);
130 
131  QVBoxLayout* layout = new QVBoxLayout( this );
132  setLayout( layout );
133 
134  setWidgetRep( this );
135 
136  layout->setSpacing( YQWidgetSpacing );
137  layout->setMargin ( YQWidgetMargin );
138 
139  _caption = new YQWidgetCaption( this, label );
140  YUI_CHECK_NEW( _caption );
141  layout->addWidget( _caption );
142 
143  _bar = new BusyBar( this );
144  YUI_CHECK_NEW ( _bar );
145  layout->addWidget( _bar );
146  _caption->setBuddy( _bar );
147 
148 }
149 
150 
152 {
153  // NOP
154 }
155 
156 
157 void YQBusyIndicator::setLabel( const string & label )
158 {
159  _caption->setText( label );
160  YBusyIndicator::setLabel( label );
161 }
162 
163 
164 void YQBusyIndicator::setAlive( bool newAlive )
165 {
166  YBusyIndicator::setAlive( newAlive );
167  if (newAlive)
168  {
169  _bar->run();
170  _timer->stop();
171  _timer->start(_timeout);
172  }
173  else
174  {
175  _bar->stop();
176  _timer->stop();
177  }
178 }
179 
180 
181 void YQBusyIndicator::setStalled()
182 {
183  setAlive( false );
184 }
185 
186 
187 void YQBusyIndicator::setTimeout( int newTimeout )
188 {
189  _timeout = newTimeout;
190  YBusyIndicator::setTimeout( newTimeout );
191 }
192 
193 
194 void YQBusyIndicator::setEnabled( bool enabled )
195 {
196  _caption->setEnabled( enabled );
197  _bar->setEnabled( enabled );
198  YWidget::setEnabled( enabled );
199 }
200 
201 
203 {
204  int hintWidth = !_caption->isHidden() ?
205  _caption->sizeHint().width() + layout()->margin() : 0;
206 
207  return std::max( 200, hintWidth );
208 }
209 
210 
212 {
213  return sizeHint().height();
214 }
215 
216 
217 void YQBusyIndicator::setSize( int newWidth, int newHeight )
218 {
219  resize( newWidth, newHeight );
220 }
221 
222 
224 {
225  _bar->setFocus();
226 
227  return true;
228 }
229 
230 
231 
BusyBar::stop
void stop()
stop moving bar animation
Definition: YQBusyIndicator.cc:91
YQBusyIndicator::preferredWidth
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQBusyIndicator.cc:202
YQBusyIndicator::setAlive
virtual void setAlive(bool newAlive)
Send a keep allive message.
Definition: YQBusyIndicator.cc:164
YQBusyIndicator::~YQBusyIndicator
virtual ~YQBusyIndicator()
Destructor.
Definition: YQBusyIndicator.cc:151
YQBusyIndicator::setKeyboardFocus
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQBusyIndicator.cc:223
YQWidgetCaption::setText
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.
Definition: YQWidgetCaption.cc:59
YQBusyIndicator::preferredHeight
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQBusyIndicator.cc:211
YQBusyIndicator::setEnabled
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Definition: YQBusyIndicator.cc:194
YQBusyIndicator::setTimeout
virtual void setTimeout(int newTimeout)
Set the timeout is ms after that the widget shows 'stalled' when no new tick is received.
Definition: YQBusyIndicator.cc:187
YQBusyIndicator::timeout
int timeout() const
Return the timeout is ms after that the widget shows 'stalled' when no new tick is received.
Definition: YQBusyIndicator.h:76
YQWidgetCaption
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
Definition: YQWidgetCaption.h:39
YQBusyIndicator::setSize
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQBusyIndicator.cc:217
BusyBar
Definition: YQBusyIndicator.h:135
BusyBar::run
void run()
start moving bar animation
Definition: YQBusyIndicator.cc:86
YQBusyIndicator::YQBusyIndicator
YQBusyIndicator(YWidget *parent, const std::string &label, int timeout=1000)
Constructor.
Definition: YQBusyIndicator.cc:119
YQBusyIndicator::setLabel
virtual void setLabel(const std::string &label)
Set the label (the caption above the progress bar).
Definition: YQBusyIndicator.cc:157