File indexing completed on 2024-10-06 04:26:03

0001 /* 
0002     SPDX-FileCopyrightText: 1998-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3btimeoutwidget.h"
0007 #include "k3bthememanager.h"
0008 #include "k3bapplication.h"
0009 
0010 #include <QTimer>
0011 #include <QResizeEvent>
0012 #include <QPainter>
0013 #include <QPaintEvent>
0014 
0015 
0016 class K3b::TimeoutWidget::Private
0017 {
0018 public:
0019   int timeout;
0020   int padding;
0021   int margin;
0022 
0023   QTimer timer;
0024   int currentTime;
0025 
0026   bool paused;
0027 };
0028 
0029 
0030 K3b::TimeoutWidget::TimeoutWidget( QWidget* parent )
0031   : QWidget( parent )
0032 {
0033   d = new Private;
0034   d->timeout = 10000;
0035   d->padding = 4;
0036   d->margin = 1;
0037   d->paused = false;
0038   connect( &d->timer, SIGNAL(timeout()), this, SLOT(timeStep()) );
0039 }
0040 
0041 
0042 K3b::TimeoutWidget::~TimeoutWidget()
0043 {
0044   delete d;
0045 }
0046 
0047 
0048 void K3b::TimeoutWidget::start()
0049 {
0050   d->paused = false;
0051   d->currentTime = 0;
0052   startTimer();
0053 }
0054 
0055 
0056 void K3b::TimeoutWidget::stop()
0057 {
0058   d->paused = false;
0059   d->timer.stop();
0060   d->currentTime = 0;
0061 }
0062 
0063 
0064 void K3b::TimeoutWidget::pause()
0065 {
0066   d->paused = true;
0067   d->timer.stop();
0068 }
0069 
0070 
0071 void K3b::TimeoutWidget::resume()
0072 {
0073   d->paused = false;
0074   startTimer();
0075 }
0076 
0077 
0078 void K3b::TimeoutWidget::timeStep()
0079 {
0080   d->currentTime += 100;
0081   update();
0082   if( d->currentTime >= d->timeout ) {
0083     stop();
0084     emit timeout();
0085   }
0086 }
0087 
0088 
0089 QSize K3b::TimeoutWidget::sizeHint() const
0090 {
0091   return minimumSizeHint();
0092 }
0093 
0094 
0095 QSize K3b::TimeoutWidget::minimumSizeHint() const
0096 {
0097   int fw = fontMetrics().horizontalAdvance( QString::number( d->timeout/1000 ) );
0098   int fh = fontMetrics().height();
0099 
0100   int diam = qMax( fw, fh ) + 2*d->padding + 2*d->margin;
0101 
0102   return QSize( diam, diam );
0103 }
0104 
0105 
0106 void K3b::TimeoutWidget::setTimeout( int msecs )
0107 {
0108   d->timeout = msecs;
0109 }
0110 
0111 
0112 void K3b::TimeoutWidget::startTimer()
0113 {
0114   d->timer.start( 100 );
0115 }
0116 
0117 
0118 void K3b::TimeoutWidget::paintEvent( QPaintEvent* )
0119 {
0120   if( d->timer.isActive() || d->paused ) {
0121     QPainter p(this);
0122     p.setRenderHint( QPainter::Antialiasing );
0123     
0124     if( K3b::Theme* theme = k3bappcore->themeManager()->currentTheme() ) {
0125       p.setBrush( theme->backgroundColor() );
0126       p.setPen( theme->backgroundColor() );
0127     }
0128 
0129     QRect r;
0130     r.setSize( minimumSizeHint() - QSize(2*d->margin,2*d->margin) );
0131     r.moveCenter( rect().center() );
0132 
0133     p.drawArc( r, 0, 360*16 );
0134     p.drawPie( r, 90*16, 360*16*d->currentTime/d->timeout );
0135     
0136     p.setPen( Qt::black );
0137     p.drawText( rect(), Qt::AlignCenter, QString::number( (d->timeout - d->currentTime + 500)/1000 ) );
0138   }
0139 }
0140 
0141 
0142 void K3b::TimeoutWidget::resizeEvent( QResizeEvent* e )
0143 {
0144   QWidget::resizeEvent( e );
0145 }
0146 
0147 #include "moc_k3btimeoutwidget.cpp"