]> git.sur5r.net Git - minitube/blob - src/snapshotpreview.cpp
Imported Upstream version 2.5.1
[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);
32 #if QT_VERSION >= 0x050000
33     setWindowFlags(windowFlags() | Qt::WindowDoesNotAcceptFocus);
34 #endif
35
36     setAttribute(Qt::WA_StaticContents);
37     setAttribute(Qt::WA_OpaquePaintEvent);
38     setAttribute(Qt::WA_NoSystemBackground);
39     setEnabled(false);
40     setFocusPolicy(Qt::NoFocus);
41
42     timeLine = new QTimeLine(300, this);
43     timeLine->setCurveShape(QTimeLine::LinearCurve);
44     timeLine->setFrameRange(0, 20);
45     connect(timeLine, SIGNAL(frameChanged(int)), SLOT(update()));
46
47     timer = new QTimer(this);
48     timer->setSingleShot(true);
49     timer->setInterval(1500);
50     connect(timer, SIGNAL(timeout()), SLOT(finish()));
51
52     hide();
53 }
54
55 void SnapshotPreview::start(QWidget *widget, const QPixmap &pixmap, bool soundOnly) {
56 #ifdef APP_PHONON
57     if (!mediaObject) {
58         mediaObject = new Phonon::MediaObject(this);
59         audioOutput = new Phonon::AudioOutput(Phonon::NotificationCategory, this);
60         Phonon::createPath(mediaObject, audioOutput);
61     }
62     mediaObject->setCurrentSource(QUrl("qrc:///sounds/snapshot.wav"));
63     mediaObject->play();
64 #endif
65     if (soundOnly) return;
66
67     resize(pixmap.size());
68 #if defined(APP_MAC) || defined(APP_WIN)
69     QPoint pos = widget->mapToGlobal(widget->pos());
70     pos.setY(pos.y() + ((widget->height() - pixmap.height()) / 2));
71     pos.setX(pos.x() + ((widget->width() - pixmap.width()) / 2));
72     move(pos);
73 #else
74     QPoint pos;
75     pos.setY((widget->height() - pixmap.height()) / 2);
76     pos.setX((widget->width() - pixmap.width()) / 2);
77     move(pos);
78     setParent(widget);
79 #endif
80
81     this->pixmap = pixmap;
82 #ifndef APP_MAC
83     timeLine->start();
84 #endif
85     timer->start();
86     if (isVisible()) update();
87     else show();
88 }
89
90 void SnapshotPreview::paintEvent(QPaintEvent *e) {
91     Q_UNUSED(e);
92     QPainter p(this);
93     if (timeLine->state() == QTimeLine::Running) {
94         p.fillRect(rect(), Qt::white);
95         const qreal opacity = timeLine->currentFrame() / 20.;
96         p.setOpacity(opacity);
97     }
98     p.drawPixmap(0, 0, pixmap);
99 }
100
101 void SnapshotPreview::finish() {
102     hide();
103     emit done();
104 }