25#include <QFontDatabase>
27#include <swflipdigit.h>
30SWFlipDigit::SWFlipDigit( QWidget *parent )
38 setAttribute( Qt::WA_NoSystemBackground,
true );
39 connect(&_animator, SIGNAL(frameChanged(
int)), SLOT(update()));
40 _animator.setFrameRange( 0, 100 );
41 _animator.setDuration( 600 );
42 _animator.setEasingCurve( ( QEasingCurve( QEasingCurve::InOutQuad ) ) );
47SWFlipDigit::~SWFlipDigit()
52void SWFlipDigit::setTransition( SWFlipMode tr)
58SWFlipDigit::SWFlipMode SWFlipDigit::transition()
const
64void SWFlipDigit::setRange(
int from,
int to )
70void SWFlipDigit::setNumber(
int n )
73 _number = qBound( _rangeFrom, n, _rangeTo );
79void SWFlipDigit::flipTo(
int n )
82 _number = qBound( _rangeFrom, n, _rangeTo );
83 _lastPixmap = _pixmap;
90void SWFlipDigit::drawFrame( QPainter *p,
const QRect &rect )
94 QLinearGradient gradient(rect.topLeft(), rect.bottomLeft());
95 gradient.setColorAt(0.00, QColor(245, 245, 245));
96 gradient.setColorAt(0.49, QColor(192, 192, 192));
97 gradient.setColorAt(0.51, QColor(245, 245, 245));
98 gradient.setColorAt(1.00, QColor(192, 192, 192));
99 p->setBrush( gradient );
102 p->drawRoundedRect( r, 15, 15, Qt::RelativeSize );
103 r.adjust( 1, 4, -1, -4 );
104 p->setPen (QColor( 181, 181, 181 ) );
105 p->setBrush( Qt::NoBrush );
106 p->drawRoundedRect( r, 15, 15, Qt::RelativeSize );
107 p->setPen( QColor( 159, 159, 159 ));
108 int y = rect.top() + rect.height() / 2 - 1;
109 p->drawLine( rect.left(), y, rect.right(), y );
113QPixmap SWFlipDigit::drawDigits(
int digit,
const QRect &rect )
116 QString strDigit = QString::number( digit );
117 if( strDigit.length() == 1 )
118 strDigit.prepend(
'0' );
124 font.setFamily(
"Grado Gradoo Nf" );
130 int fontHeight = SWSCALEFACTOR * SWFONTSCALE * rect.height();
131 font.setPixelSize( fontHeight );
134 QPixmap pixmap( rect.size() * SWSCALEFACTOR );
135 pixmap.fill( Qt::transparent );
137 QLinearGradient gradient( QPoint( 0, 0 ), QPoint( 0, pixmap.height() ) );
138 gradient.setColorAt( 0.00, QColor(128, 128, 128) );
139 gradient.setColorAt( 0.49, QColor(64, 64, 64) );
140 gradient.setColorAt( 0.51, QColor(128, 128, 128) );
141 gradient.setColorAt( 1.00, QColor(16, 16, 16) );
144 painter.begin( &pixmap );
145 painter.setFont( font );
147 pen.setBrush( QBrush( gradient ) );
148 painter.setPen( pen );
150 QRect pos = pixmap.rect();
151 pos.adjust( 0, -100, 0, 0 );
160 painter.drawText( pos, Qt::AlignCenter, strDigit );
163 return pixmap.scaledToWidth( width(), Qt::SmoothTransformation );
168void SWFlipDigit::preparePixmap()
170 _pixmap = QPixmap(size());
171 _pixmap.fill(Qt::transparent);
173 painter.begin(&_pixmap);
174 painter.drawPixmap( 0, 0, drawDigits( _number, rect() ) );
179void SWFlipDigit::resizeEvent(QResizeEvent*)
186void SWFlipDigit::paintStatic()
188 QPainter painter(
this );
189 painter.fillRect( rect(), Qt::black );
191 drawFrame( &painter, rect() );
192 painter.drawPixmap( 0, 0, _pixmap );
196void SWFlipDigit::paintSlide()
198 QPainter painter(
this );
199 painter.fillRect( rect(), Qt::black );
203 drawFrame( &painter, rect() );
204 painter.setClipRect( rect() );
206 int y = height() * _animator.currentFrame() / 100;
207 painter.drawPixmap( 0, y, _lastPixmap );
208 painter.drawPixmap( 0, y - height(), _pixmap );
212void SWFlipDigit::paintFlip()
214 QPainter painter(
this );
215 painter.setRenderHint(QPainter::SmoothPixmapTransform,
true);
216 painter.setRenderHint(QPainter::Antialiasing,
true);
217 painter.fillRect(rect(), Qt::black);
219 int hw = width() / 2;
220 int hh = height() / 2;
226 drawFrame( &painter, fr );
227 painter.drawPixmap( 0, 0, _pixmap );
229 int index = _animator.currentFrame();
235 int angle = -180 * index / 100;
236 QTransform transform;
237 transform.translate(hw, hh);
238 transform.rotate( angle, Qt::XAxis );
239 painter.setTransform(transform);
240 drawFrame( &painter, fr.adjusted(-hw, -hh, -hw, -hh ) );
241 painter.drawPixmap(-hw, -hh, _lastPixmap);
244 painter.resetTransform();
245 painter.setClipRect(0, hh, width(), hh);
246 drawFrame( &painter, fr );
247 painter.drawPixmap( 0, 0, _lastPixmap );
252 painter.setClipRect( 0, hh, width(), hh );
255 drawFrame( &painter, fr );
256 painter.drawPixmap(0, 0, _lastPixmap );
259 int angle = 180 - 180 * _animator.currentFrame() / 100;
260 QTransform transform;
261 transform.translate( hw, hh);
262 transform.rotate( angle, Qt::XAxis );
263 painter.setTransform( transform );
264 drawFrame( &painter, fr.adjusted( -hw, -hh, -hw, -hh ) );
265 painter.drawPixmap( -hw, -hh, _pixmap );
272void SWFlipDigit::paintRotate()
281 int angle1 = -180 * _animator.currentFrame() / 100;
282 int angle2 = 180 - 180 * _animator.currentFrame() / 100;
283 int angle = ( _animator.currentFrame() <= 50 ) ? angle1 : angle2;
284 QPixmap pix = ( _animator.currentFrame() <= 50 ) ? _lastPixmap : _pixmap;
286 QTransform transform;
287 transform.translate( width() / 2, height() / 2 );
288 transform.rotate( angle, Qt::XAxis );
290 p.setTransform(transform);
291 p.setRenderHint( QPainter::SmoothPixmapTransform,
true );
292 p.drawPixmap(-width() / 2, -height() / 2, pix);
296void SWFlipDigit::paintEvent( QPaintEvent *event )
300 if (_animator.state() == QTimeLine::Running)
302 if(_transition == Slide )
304 if(_transition == Flip )
306 if(_transition == Rotate )