]> git.sur5r.net Git - minitube/blob - src/snapshotpreview.cpp
36626483a4f9a96953fcf7e0946b9185d952269d
[minitube] / src / snapshotpreview.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "snapshotpreview.h"
22 #include "mainwindow.h"
23
24 SnapshotPreview::SnapshotPreview(QWidget *parent) : QWidget(parent),
25 #ifdef APP_PHONON
26     mediaObject(0),
27     audioOutput(0)
28 #endif
29     {
30     setAttribute(Qt::WA_ShowWithoutActivating);
31     setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
32     setAttribute(Qt::WA_StaticContents);
33     setAttribute(Qt::WA_OpaquePaintEvent);
34     setAttribute(Qt::WA_NoSystemBackground);
35     setEnabled(false);
36     setFocusPolicy(Qt::NoFocus);
37
38     timeLine = new QTimeLine(300, this);
39     timeLine->setCurveShape(QTimeLine::LinearCurve);
40     timeLine->setFrameRange(0, 20);
41     connect(timeLine, SIGNAL(frameChanged(int)), SLOT(update()));
42
43     timer = new QTimer(this);
44     timer->setSingleShot(true);
45     timer->setInterval(1500);
46     connect(timer, SIGNAL(timeout()), SLOT(finish()));
47
48     hide();
49 }
50
51 void SnapshotPreview::start(QWidget *widget, const QPixmap &pixmap, bool soundOnly) {
52 #ifdef APP_PHONON
53     if (!mediaObject) {
54         mediaObject = new Phonon::MediaObject(this);
55         audioOutput = new Phonon::AudioOutput(Phonon::NotificationCategory, this);
56         Phonon::createPath(mediaObject, audioOutput);
57     }
58     mediaObject->setCurrentSource(QUrl("qrc:///sounds/snapshot.wav"));
59     mediaObject->play();
60 #endif
61     if (soundOnly) return;
62
63     resize(pixmap.size());
64 #if defined(APP_MAC) || defined(APP_WIN)
65     QPoint pos = widget->mapToGlobal(widget->pos());
66     pos.setY(pos.y() + ((widget->height() - pixmap.height()) / 2));
67     pos.setX(pos.x() + ((widget->width() - pixmap.width()) / 2));
68     move(pos);
69 #else
70     QPoint pos;
71     pos.setY((widget->height() - pixmap.height()) / 2);
72     pos.setX((widget->width() - pixmap.width()) / 2);
73     move(pos);
74     setParent(widget);
75 #endif
76
77     this->pixmap = pixmap;
78 #ifndef APP_MAC
79     timeLine->start();
80 #endif
81     timer->start();
82     if (isVisible()) update();
83     else show();
84 }
85
86 void SnapshotPreview::paintEvent(QPaintEvent *e) {
87     Q_UNUSED(e);
88     QPainter p(this);
89     if (timeLine->state() == QTimeLine::Running) {
90         p.fillRect(rect(), Qt::white);
91         const qreal opacity = timeLine->currentFrame() / 20.;
92         p.setOpacity(opacity);
93     }
94     p.drawPixmap(0, 0, pixmap);
95 }
96
97 void SnapshotPreview::finish() {
98     hide();
99     emit done();
100 }