]> git.sur5r.net Git - minitube/blob - src/snapshotpreview.cpp
New upstream version 3.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 #ifdef MEDIA_QTAV
25 #include "mediaqtav.h"
26 #endif
27 #ifdef MEDIA_MPV
28 #include "mediampv.h"
29 #endif
30
31 SnapshotPreview::SnapshotPreview(QWidget *parent) : QWidget(parent), mediaObject(nullptr) {
32     setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
33                    Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus);
34     setAttribute(Qt::WA_ShowWithoutActivating);
35     setAttribute(Qt::WA_StaticContents);
36     setAttribute(Qt::WA_OpaquePaintEvent);
37     setAttribute(Qt::WA_NoSystemBackground);
38     setEnabled(false);
39     setFocusPolicy(Qt::NoFocus);
40
41     timeLine = new QTimeLine(300, this);
42     timeLine->setCurveShape(QTimeLine::LinearCurve);
43     timeLine->setFrameRange(0, 20);
44     connect(timeLine, SIGNAL(frameChanged(int)), SLOT(update()));
45
46     timer = new QTimer(this);
47     timer->setSingleShot(true);
48     timer->setInterval(1500);
49     connect(timer, SIGNAL(timeout()), SLOT(finish()));
50
51     hide();
52 }
53
54 void SnapshotPreview::start(QWidget *widget, const QPixmap &pixmap, bool soundOnly) {
55     if (!mediaObject) {
56 #ifdef MEDIA_QTAV
57         mediaObject = new MediaQtAV(this);
58 #elif defined MEDIA_MPV
59         mediaObject = new MediaMPV(this);
60 #else
61         qFatal("No media backend defined");
62 #endif
63         if (mediaObject) {
64             mediaObject->setAudioOnly(true);
65             mediaObject->init();
66         }
67     }
68
69 #ifdef APP_MAC
70     QString soundPath = QCoreApplication::applicationDirPath() + "/../Resources";
71 #elif defined PKGDATADIR
72     QString soundPath = QLatin1String(PKGDATADIR) + "/sounds";
73 #else
74     QString soundPath = QCoreApplication::applicationDirPath() + "/sounds";
75 #endif
76
77     if (mediaObject) mediaObject->play(soundPath + "/snapshot.wav");
78     if (soundOnly) return;
79
80     resize(pixmap.size());
81 #if defined(APP_MAC) || defined(APP_WIN)
82     QPoint pos = widget->mapToGlobal(widget->pos());
83     pos.setY(pos.y() + ((widget->height() - pixmap.height()) / 2));
84     pos.setX(pos.x() + ((widget->width() - pixmap.width()) / 2));
85     move(pos);
86 #else
87     QPoint pos;
88     pos.setY((widget->height() - pixmap.height()) / 2);
89     pos.setX((widget->width() - pixmap.width()) / 2);
90     move(pos);
91     setParent(widget);
92 #endif
93
94     this->pixmap = pixmap;
95 #ifndef APP_MAC
96     timeLine->start();
97 #endif
98     timer->start();
99     if (isVisible())
100         update();
101     else
102         show();
103 }
104
105 void SnapshotPreview::paintEvent(QPaintEvent *e) {
106     Q_UNUSED(e);
107     QPainter p(this);
108     if (timeLine->state() == QTimeLine::Running) {
109         p.fillRect(rect(), Qt::white);
110         const qreal opacity = timeLine->currentFrame() / 20.;
111         p.setOpacity(opacity);
112     }
113     p.drawPixmap(0, 0, pixmap);
114 }
115
116 void SnapshotPreview::finish() {
117     hide();
118     emit done();
119 }