1 | /* |
2 | * Copyright (C) 2007777777 Aaron J. Seigo <aseigo@kde.org> |
3 | * |
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. |
8 | * |
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. |
13 | * |
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. |
18 | */ |
19 | |
20 | #include "snapshottimer.h" |
21 | #include <QPainter> |
22 | #include <QPaintEvent> |
23 | #include <QToolTip> |
24 | #include <QApplication> |
25 | #include <QDesktopWidget> |
26 | |
27 | #include <KDebug> |
28 | #include <klocale.h> |
29 | |
30 | SnapshotTimer::SnapshotTimer() |
31 | : QWidget(0), |
32 | toggle(true) |
33 | { |
34 | setWindowFlags( Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); |
35 | // The text is copied from paintEvent and the maximum number is used as %1 argument + margins |
36 | resize(fontMetrics().width(i18np("Snapshot will be taken in 1 second" , |
37 | "Snapshot will be taken in %1 seconds" , 99) ) + 6, fontMetrics().height() + 4); |
38 | connect(&timer, SIGNAL(timeout()), this, SLOT(bell())); |
39 | } |
40 | |
41 | SnapshotTimer::~SnapshotTimer() |
42 | { |
43 | } |
44 | |
45 | void SnapshotTimer::start(int seconds) |
46 | { |
47 | const QRect screenGeom = qApp->desktop()->screenGeometry(); |
48 | move(screenGeom.width() / 2 - size().width() / 2, screenGeom.top()); |
49 | toggle = true; |
50 | time = 0; |
51 | length = seconds; |
52 | timer.start(1000); |
53 | show(); |
54 | } |
55 | |
56 | void SnapshotTimer::stop() |
57 | { |
58 | setVisible(false); |
59 | hide(); |
60 | timer.stop(); |
61 | } |
62 | |
63 | void SnapshotTimer::bell() |
64 | { |
65 | if (time == length - 1) { |
66 | hide(); |
67 | } |
68 | else { |
69 | if (time == length) { |
70 | emit timeout(); |
71 | timer.stop(); |
72 | } |
73 | } |
74 | ++time; |
75 | toggle = !toggle; |
76 | update(); |
77 | } |
78 | |
79 | void SnapshotTimer::enterEvent(QEvent *) |
80 | { |
81 | const QRect screenGeom = qApp->desktop()->screenGeometry(); |
82 | if (x() == screenGeom.left()) { |
83 | move(screenGeom.x() + (screenGeom.width() / 2 - size().width() / 2), screenGeom.top()); |
84 | } |
85 | else { |
86 | move(screenGeom.topLeft()); |
87 | } |
88 | } |
89 | |
90 | void SnapshotTimer::paintEvent( QPaintEvent* e ) |
91 | { |
92 | Q_UNUSED( e ); |
93 | |
94 | QPainter painter( this ); |
95 | |
96 | if (time < length) { |
97 | QPalette pal(QToolTip::palette()); |
98 | QColor handleColor = pal.color( QPalette::Active, QPalette::Highlight ); |
99 | handleColor.setAlpha( 160 ); |
100 | QColor overlayColor( 0, 0, 0, 160 ); |
101 | QColor textColor = pal.color( QPalette::Active, QPalette::Text ); |
102 | QColor textBackgroundColor = pal.color( QPalette::Active, QPalette::Base ); |
103 | if (toggle){ |
104 | textColor = pal.color( QPalette::Active, QPalette::Text); |
105 | } |
106 | else { |
107 | textColor = pal.color( QPalette::Active, QPalette::Base); |
108 | } |
109 | painter.setPen( textColor ); |
110 | painter.setBrush( textBackgroundColor ); |
111 | const QString helpText = i18np("Snapshot will be taken in 1 second" , |
112 | "Snapshot will be taken in %1 seconds" , length - time); |
113 | textRect = painter.boundingRect(rect().adjusted(2, 2, -2, -2), Qt::AlignHCenter | Qt::TextSingleLine, helpText); |
114 | painter.drawText(textRect, helpText); |
115 | } |
116 | } |
117 | |
118 | |