initial import
[vuplus_webkit] / Source / WebKit / qt / symbian / platformplugin / OverlayWidget.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "OverlayWidget.h"
22
23 #include <QFile>
24 #include <QGridLayout>
25 #include <QHBoxLayout>
26 #include <QStyle>
27 #include <QTime>
28
29 OverlayWidget::OverlayWidget(QWidget *parent)
30     : QWidget(0, Qt::FramelessWindowHint)
31     , m_isPaused(false)
32     , m_isMuted(false)
33 {
34     applyStyleSheet();
35
36     m_playIcon = QIcon(":/images/button_play.png");
37     m_pauseIcon = QIcon(":/images/button_pause.png");
38     m_soundOnIcon = QIcon(":/images/button_sound_on.png");
39     m_soundOffIcon = QIcon(":/images/button_sound_off.png");
40
41     m_controlButton = new PlayerButton(this);
42     m_controlButton->setIcon(m_pauseIcon);
43     connect(m_controlButton, SIGNAL(clicked()), this, SLOT(onControlClicked()));
44
45     m_progressSlider = new QSlider(Qt::Horizontal, this);
46     connect(m_progressSlider, SIGNAL(sliderMoved(int)), this, SLOT(onSliderMoved(int)));
47
48     m_positionLabel = new QLabel(this);
49     m_positionLabel->setText(timeToString(0));
50     m_durationLabel = new QLabel(this);
51
52     m_soundButton = new PlayerButton(this);
53     m_soundButton->setIcon(m_soundOnIcon);
54     connect(m_soundButton, SIGNAL(clicked()), this, SLOT(onSoundClicked()));
55
56     m_closeButton = new PlayerButton(this);
57     m_closeButton->setIcon(QIcon(":/images/button_close.png"));
58     connect(m_closeButton, SIGNAL(clicked()), this, SLOT(onCloseClicked()));
59
60     m_volumeSlider = new QSlider(Qt::Vertical, this);
61     m_volumeSlider->setRange(0, 100);
62     connect(m_soundButton, SIGNAL(tapAndHold()), m_volumeSlider, SLOT(show()));
63     connect(m_volumeSlider, SIGNAL(sliderReleased()), this, SLOT(onVolumeSliderReleased()));
64     connect(m_volumeSlider, SIGNAL(sliderMoved(int)), this, SLOT(onVolumeSliderMoved(int)));
65     m_volumeSlider->hide();
66
67     m_playerLabel = new PlayerLabel(this);
68     m_playerLabel->hide();
69
70     QBoxLayout* m_controlLayout = new QHBoxLayout;
71     m_controlLayout->addWidget(m_controlButton);
72     m_controlLayout->addWidget(m_positionLabel);
73     m_controlLayout->addWidget(m_progressSlider);
74     m_controlLayout->addWidget(m_durationLabel);
75
76     QGridLayout* layout = new QGridLayout;
77     layout->addWidget(m_closeButton, 0, 0, 1, 2, Qt::AlignRight | Qt::AlignTop);
78     layout->addWidget(m_playerLabel, 1, 0, Qt::AlignCenter);
79     layout->addWidget(m_volumeSlider, 1, 1);
80     layout->addLayout(m_controlLayout, 2, 0, Qt::AlignBottom);
81     layout->addWidget(m_soundButton, 2, 1, Qt::AlignBottom);
82     setLayout(layout);
83
84     m_hideWidgetTimer = new QTimer(this);
85     m_hideWidgetTimer->setSingleShot(true);
86     m_hideWidgetTimer->setInterval(3000);
87     connect(m_hideWidgetTimer, SIGNAL(timeout()), this, SLOT(onTimerTimeout()));
88     connect(m_progressSlider, SIGNAL(sliderPressed()), m_hideWidgetTimer, SLOT(stop()));
89     connect(m_progressSlider, SIGNAL(sliderReleased()), m_hideWidgetTimer, SLOT(start()));
90     connect(m_volumeSlider, SIGNAL(sliderPressed()), m_hideWidgetTimer, SLOT(stop()));
91
92     setAttribute(Qt::WA_TranslucentBackground);
93 }
94
95 OverlayWidget::~OverlayWidget()
96 {
97 }
98
99 void OverlayWidget::setDuration(int duration)
100 {
101     m_progressSlider->setMaximum(duration);
102     m_durationLabel->setText(timeToString(duration));
103 }
104
105 void OverlayWidget::setPosition(int position)
106 {
107     if (!m_progressSlider->isSliderDown()) {
108         m_progressSlider->setValue(position);
109         m_positionLabel->setText(timeToString(position));
110     }
111 }
112
113 void OverlayWidget::setVolume(int volume)
114 {
115     m_volumeSlider->setValue(volume);
116 }
117
118 void OverlayWidget::mousePressEvent(QMouseEvent *)
119 {
120     m_hideWidgetTimer->stop();
121     hide();
122 }
123
124 void OverlayWidget::onPlayerStopped()
125 {
126     m_controlButton->setIcon(m_playIcon);
127 }
128
129 void OverlayWidget::onPlayerError()
130 {
131     m_controlButton->setDisabled(true);
132     m_progressSlider->setDisabled(true);
133     m_playerLabel->onPlayerError();
134     showFullScreen();
135 }
136
137 void OverlayWidget::onEndOfMedia()
138 {
139     QWidget::showFullScreen();
140 }
141
142 void OverlayWidget::onBufferingMedia()
143 {
144     m_playerLabel->startBufferingAnimation();
145 }
146
147 void OverlayWidget::onBufferedMedia()
148 {
149     m_playerLabel->stopBufferingAnimation();
150 }
151
152 QString OverlayWidget::timeToString(int seconds)
153 {
154     QTime initTime(0, 0, 0);
155     QTime time = initTime.addSecs(seconds);
156     return time.hour() ? time.toString("h:mm:ss") : time.toString("mm:ss");
157 }
158
159 void OverlayWidget::applyStyleSheet()
160 {
161     QFile file(":/qss/OverlayWidget.qss");
162     file.open(QFile::ReadOnly);
163     setStyleSheet(QLatin1String(file.readAll()));
164 }
165
166 void OverlayWidget::onControlClicked()
167 {
168     m_isPaused = !m_isPaused;
169     if (m_isPaused) {
170         m_controlButton->setIcon(m_playIcon);
171         m_hideWidgetTimer->stop();
172     } else {
173         m_controlButton->setIcon(m_pauseIcon);
174         m_hideWidgetTimer->start();
175     }
176     emit controlClicked(m_isPaused);
177 }
178
179 void OverlayWidget::onSliderMoved(int value)
180 {
181     emit sliderMoved(value);
182 }
183
184 void OverlayWidget::onSoundClicked()
185 {
186     if (m_volumeSlider->isVisible())
187         m_isMuted = false;
188     else
189         m_isMuted = !m_isMuted;
190
191     if (m_isMuted)
192         m_soundButton->setIcon(m_soundOffIcon);
193     else
194         m_soundButton->setIcon(m_soundOnIcon);
195
196     m_hideWidgetTimer->start();
197     emit muted(m_isMuted);
198 }
199
200 void OverlayWidget::onCloseClicked()
201 {
202     m_hideWidgetTimer->stop();
203     hide();
204     emit closeClicked();
205 }
206
207 void OverlayWidget::onVolumeSliderReleased()
208 {
209     m_hideWidgetTimer->start();
210     emit volumeChanged(m_volumeSlider->value());
211     m_volumeSlider->hide();
212 }
213
214 void OverlayWidget::onVolumeSliderMoved(int value)
215 {
216     emit volumeChanged(value);
217 }
218
219 void OverlayWidget::onTimerTimeout()
220 {
221     if (!m_isPaused)
222         hide();
223 }
224
225 void OverlayWidget::showFullScreen()
226 {
227     m_hideWidgetTimer->start();
228     QWidget::showFullScreen();
229 }