compile
[kdegraphics.git] / ksnapshot / snapshottimer.cpp
blob22f34034ecf08e587096dcd85511891711ac2838
1 /*
2 * Copyright (C) 2007777777 Aaron J. Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include <QTimerEvent>
21 #include <QPainter>
22 #include <QPaintEvent>
23 #include <QToolTip>
25 #include <KDebug>
26 #include <klocale.h>
29 #include "snapshottimer.h"
31 SnapshotTimer::SnapshotTimer() : QWidget(0)
33 setWindowFlags( Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
34 // The text is copied from paintEvent and the maximum number is used as %1 argument + margins
35 resize( fontMetrics().width( i18n("Snapshot will be taken in %1 seconds", 99) ) + 6 , fontMetrics().height() + 4 );
36 connect(&timer, SIGNAL(timeout()), this, SLOT(bell()));
39 SnapshotTimer::~SnapshotTimer()
43 void SnapshotTimer::start(int seconds)
45 show();
46 time = 0;
47 length = seconds;
48 timer.start(1000);
51 void SnapshotTimer::stop()
53 setVisible(false);
54 hide();
55 timer.stop();
58 void SnapshotTimer::bell()
60 if (time == length - 1) {
61 hide();
62 } else {
63 if (time == length) {
64 emit timeout();
65 timer.stop();
68 ++time;
69 update();
72 void SnapshotTimer::paintEvent( QPaintEvent* e )
74 Q_UNUSED( e );
76 QPainter painter( this );
78 if (time < length) {
79 QPalette pal(QToolTip::palette());
80 QColor handleColor = pal.color( QPalette::Active, QPalette::Highlight );
81 handleColor.setAlpha( 160 );
82 QColor overlayColor( 0, 0, 0, 160 );
83 QColor textColor = pal.color( QPalette::Active, QPalette::Text );
84 QColor textBackgroundColor = pal.color( QPalette::Active, QPalette::Base );
86 painter.setPen( textColor );
87 painter.setBrush( textBackgroundColor );
88 QString helpText = i18np( "Snapshot will be taken in 1 second",
89 "Snapshot will be taken in %1 seconds", ( length-time ) );
90 QRect textRect = painter.boundingRect( rect().adjusted( 2, 2, -2, -2 ), Qt::TextSingleLine, helpText );
91 textRect.adjust( -2, -2, 4, 2 );
92 painter.drawRect( rect().adjusted(0,0,-1,-1) );
93 textRect.moveTopLeft( QPoint( 3, 3 ) );
94 painter.drawText(textRect, helpText);