author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
***************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "mainwindow.h" |
|
45 |
||
46 |
//![0] |
|
47 |
MainWindow::MainWindow() |
|
48 |
{ |
|
49 |
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); |
|
50 |
mediaObject = new Phonon::MediaObject(this); |
|
51 |
metaInformationResolver = new Phonon::MediaObject(this); |
|
52 |
||
53 |
mediaObject->setTickInterval(1000); |
|
54 |
//![0] |
|
55 |
//![2] |
|
56 |
connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64))); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
this, SLOT(stateChanged(Phonon::State,Phonon::State))); |
0 | 59 |
connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)), |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
this, SLOT(metaStateChanged(Phonon::State,Phonon::State))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
61 |
connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
this, SLOT(sourceChanged(Phonon::MediaSource))); |
0 | 63 |
connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish())); |
64 |
//![2] |
|
65 |
||
66 |
//![1] |
|
67 |
Phonon::createPath(mediaObject, audioOutput); |
|
68 |
//![1] |
|
69 |
||
70 |
setupActions(); |
|
71 |
setupMenus(); |
|
72 |
setupUi(); |
|
73 |
timeLcd->display("00:00"); |
|
74 |
} |
|
75 |
||
76 |
//![6] |
|
77 |
void MainWindow::addFiles() |
|
78 |
{ |
|
79 |
QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"), |
|
80 |
QDesktopServices::storageLocation(QDesktopServices::MusicLocation)); |
|
81 |
||
82 |
if (files.isEmpty()) |
|
83 |
return; |
|
84 |
||
85 |
int index = sources.size(); |
|
86 |
foreach (QString string, files) { |
|
87 |
Phonon::MediaSource source(string); |
|
88 |
||
89 |
sources.append(source); |
|
90 |
} |
|
91 |
if (!sources.isEmpty()) |
|
92 |
metaInformationResolver->setCurrentSource(sources.at(index)); |
|
93 |
||
94 |
} |
|
95 |
//![6] |
|
96 |
||
97 |
void MainWindow::about() |
|
98 |
{ |
|
99 |
QMessageBox::information(this, tr("About Music Player"), |
|
100 |
tr("The Music Player example shows how to use Phonon - the multimedia" |
|
101 |
" framework that comes with Qt - to create a simple music player.")); |
|
102 |
} |
|
103 |
||
104 |
//![9] |
|
105 |
void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */) |
|
106 |
{ |
|
107 |
switch (newState) { |
|
108 |
case Phonon::ErrorState: |
|
109 |
if (mediaObject->errorType() == Phonon::FatalError) { |
|
110 |
QMessageBox::warning(this, tr("Fatal Error"), |
|
111 |
mediaObject->errorString()); |
|
112 |
} else { |
|
113 |
QMessageBox::warning(this, tr("Error"), |
|
114 |
mediaObject->errorString()); |
|
115 |
} |
|
116 |
break; |
|
117 |
//![9] |
|
118 |
//![10] |
|
119 |
case Phonon::PlayingState: |
|
120 |
playAction->setEnabled(false); |
|
121 |
pauseAction->setEnabled(true); |
|
122 |
stopAction->setEnabled(true); |
|
123 |
break; |
|
124 |
case Phonon::StoppedState: |
|
125 |
stopAction->setEnabled(false); |
|
126 |
playAction->setEnabled(true); |
|
127 |
pauseAction->setEnabled(false); |
|
128 |
timeLcd->display("00:00"); |
|
129 |
break; |
|
130 |
case Phonon::PausedState: |
|
131 |
pauseAction->setEnabled(false); |
|
132 |
stopAction->setEnabled(true); |
|
133 |
playAction->setEnabled(true); |
|
134 |
break; |
|
135 |
//![10] |
|
136 |
case Phonon::BufferingState: |
|
137 |
break; |
|
138 |
default: |
|
139 |
; |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
//![11] |
|
144 |
void MainWindow::tick(qint64 time) |
|
145 |
{ |
|
146 |
QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60); |
|
147 |
||
148 |
timeLcd->display(displayTime.toString("mm:ss")); |
|
149 |
} |
|
150 |
//![11] |
|
151 |
||
152 |
//![12] |
|
153 |
void MainWindow::tableClicked(int row, int /* column */) |
|
154 |
{ |
|
155 |
bool wasPlaying = mediaObject->state() == Phonon::PlayingState; |
|
156 |
||
157 |
mediaObject->stop(); |
|
158 |
mediaObject->clearQueue(); |
|
159 |
||
160 |
if (row >= sources.size()) |
|
161 |
return; |
|
162 |
||
163 |
mediaObject->setCurrentSource(sources[row]); |
|
164 |
||
165 |
if (wasPlaying) |
|
166 |
mediaObject->play(); |
|
167 |
else |
|
168 |
mediaObject->stop(); |
|
169 |
} |
|
170 |
//![12] |
|
171 |
||
172 |
//![13] |
|
173 |
void MainWindow::sourceChanged(const Phonon::MediaSource &source) |
|
174 |
{ |
|
175 |
musicTable->selectRow(sources.indexOf(source)); |
|
176 |
timeLcd->display("00:00"); |
|
177 |
} |
|
178 |
//![13] |
|
179 |
||
180 |
//![14] |
|
181 |
void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */) |
|
182 |
{ |
|
183 |
if (newState == Phonon::ErrorState) { |
|
184 |
QMessageBox::warning(this, tr("Error opening files"), |
|
185 |
metaInformationResolver->errorString()); |
|
186 |
while (!sources.isEmpty() && |
|
187 |
!(sources.takeLast() == metaInformationResolver->currentSource())) {} /* loop */; |
|
188 |
return; |
|
189 |
} |
|
190 |
||
191 |
if (newState != Phonon::StoppedState && newState != Phonon::PausedState) |
|
192 |
return; |
|
193 |
||
194 |
if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid) |
|
195 |
return; |
|
196 |
||
197 |
QMap<QString, QString> metaData = metaInformationResolver->metaData(); |
|
198 |
||
199 |
QString title = metaData.value("TITLE"); |
|
200 |
if (title == "") |
|
201 |
title = metaInformationResolver->currentSource().fileName(); |
|
202 |
||
203 |
QTableWidgetItem *titleItem = new QTableWidgetItem(title); |
|
204 |
titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable); |
|
205 |
QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST")); |
|
206 |
artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable); |
|
207 |
QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM")); |
|
208 |
albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable); |
|
209 |
QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value("DATE")); |
|
210 |
yearItem->setFlags(yearItem->flags() ^ Qt::ItemIsEditable); |
|
211 |
//![14] |
|
212 |
||
213 |
int currentRow = musicTable->rowCount(); |
|
214 |
musicTable->insertRow(currentRow); |
|
215 |
musicTable->setItem(currentRow, 0, titleItem); |
|
216 |
musicTable->setItem(currentRow, 1, artistItem); |
|
217 |
musicTable->setItem(currentRow, 2, albumItem); |
|
218 |
musicTable->setItem(currentRow, 3, yearItem); |
|
219 |
||
220 |
//![15] |
|
221 |
if (musicTable->selectedItems().isEmpty()) { |
|
222 |
musicTable->selectRow(0); |
|
223 |
mediaObject->setCurrentSource(metaInformationResolver->currentSource()); |
|
224 |
} |
|
225 |
||
226 |
Phonon::MediaSource source = metaInformationResolver->currentSource(); |
|
227 |
int index = sources.indexOf(metaInformationResolver->currentSource()) + 1; |
|
228 |
if (sources.size() > index) { |
|
229 |
metaInformationResolver->setCurrentSource(sources.at(index)); |
|
230 |
} |
|
231 |
else { |
|
232 |
musicTable->resizeColumnsToContents(); |
|
233 |
if (musicTable->columnWidth(0) > 300) |
|
234 |
musicTable->setColumnWidth(0, 300); |
|
235 |
} |
|
236 |
} |
|
237 |
//![15] |
|
238 |
||
239 |
//![16] |
|
240 |
void MainWindow::aboutToFinish() |
|
241 |
{ |
|
242 |
int index = sources.indexOf(mediaObject->currentSource()) + 1; |
|
243 |
if (sources.size() > index) { |
|
244 |
mediaObject->enqueue(sources.at(index)); |
|
245 |
} |
|
246 |
} |
|
247 |
//![16] |
|
248 |
||
249 |
void MainWindow::setupActions() |
|
250 |
{ |
|
251 |
playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this); |
|
252 |
playAction->setShortcut(tr("Crl+P")); |
|
253 |
playAction->setDisabled(true); |
|
254 |
pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this); |
|
255 |
pauseAction->setShortcut(tr("Ctrl+A")); |
|
256 |
pauseAction->setDisabled(true); |
|
257 |
stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this); |
|
258 |
stopAction->setShortcut(tr("Ctrl+S")); |
|
259 |
stopAction->setDisabled(true); |
|
260 |
nextAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipForward), tr("Next"), this); |
|
261 |
nextAction->setShortcut(tr("Ctrl+N")); |
|
262 |
previousAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), tr("Previous"), this); |
|
263 |
previousAction->setShortcut(tr("Ctrl+R")); |
|
264 |
addFilesAction = new QAction(tr("Add &Files"), this); |
|
265 |
addFilesAction->setShortcut(tr("Ctrl+F")); |
|
266 |
exitAction = new QAction(tr("E&xit"), this); |
|
267 |
exitAction->setShortcuts(QKeySequence::Quit); |
|
268 |
aboutAction = new QAction(tr("A&bout"), this); |
|
269 |
aboutAction->setShortcut(tr("Ctrl+B")); |
|
270 |
aboutQtAction = new QAction(tr("About &Qt"), this); |
|
271 |
aboutQtAction->setShortcut(tr("Ctrl+Q")); |
|
272 |
||
273 |
//![5] |
|
274 |
connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play())); |
|
275 |
connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) ); |
|
276 |
connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop())); |
|
277 |
//![5] |
|
278 |
connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles())); |
|
279 |
connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); |
|
280 |
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about())); |
|
281 |
connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
|
282 |
} |
|
283 |
||
284 |
void MainWindow::setupMenus() |
|
285 |
{ |
|
286 |
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
|
287 |
fileMenu->addAction(addFilesAction); |
|
288 |
fileMenu->addSeparator(); |
|
289 |
fileMenu->addAction(exitAction); |
|
290 |
||
291 |
QMenu *aboutMenu = menuBar()->addMenu(tr("&Help")); |
|
292 |
aboutMenu->addAction(aboutAction); |
|
293 |
aboutMenu->addAction(aboutQtAction); |
|
294 |
} |
|
295 |
||
296 |
//![3] |
|
297 |
void MainWindow::setupUi() |
|
298 |
{ |
|
299 |
//![3] |
|
300 |
QToolBar *bar = new QToolBar; |
|
301 |
||
302 |
bar->addAction(playAction); |
|
303 |
bar->addAction(pauseAction); |
|
304 |
bar->addAction(stopAction); |
|
305 |
||
306 |
//![4] |
|
307 |
seekSlider = new Phonon::SeekSlider(this); |
|
308 |
seekSlider->setMediaObject(mediaObject); |
|
309 |
||
310 |
volumeSlider = new Phonon::VolumeSlider(this); |
|
311 |
volumeSlider->setAudioOutput(audioOutput); |
|
312 |
//![4] |
|
313 |
volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); |
|
314 |
||
315 |
QLabel *volumeLabel = new QLabel; |
|
316 |
volumeLabel->setPixmap(QPixmap("images/volume.png")); |
|
317 |
||
318 |
QPalette palette; |
|
319 |
palette.setBrush(QPalette::Light, Qt::darkGray); |
|
320 |
||
321 |
timeLcd = new QLCDNumber; |
|
322 |
timeLcd->setPalette(palette); |
|
323 |
||
324 |
QStringList headers; |
|
325 |
headers << tr("Title") << tr("Artist") << tr("Album") << tr("Year"); |
|
326 |
||
327 |
musicTable = new QTableWidget(0, 4); |
|
328 |
musicTable->setHorizontalHeaderLabels(headers); |
|
329 |
musicTable->setSelectionMode(QAbstractItemView::SingleSelection); |
|
330 |
musicTable->setSelectionBehavior(QAbstractItemView::SelectRows); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
331 |
connect(musicTable, SIGNAL(cellPressed(int,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
332 |
this, SLOT(tableClicked(int,int))); |
0 | 333 |
|
334 |
QHBoxLayout *seekerLayout = new QHBoxLayout; |
|
335 |
seekerLayout->addWidget(seekSlider); |
|
336 |
seekerLayout->addWidget(timeLcd); |
|
337 |
||
338 |
QHBoxLayout *playbackLayout = new QHBoxLayout; |
|
339 |
playbackLayout->addWidget(bar); |
|
340 |
playbackLayout->addStretch(); |
|
341 |
playbackLayout->addWidget(volumeLabel); |
|
342 |
playbackLayout->addWidget(volumeSlider); |
|
343 |
||
344 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
345 |
mainLayout->addWidget(musicTable); |
|
346 |
mainLayout->addLayout(seekerLayout); |
|
347 |
mainLayout->addLayout(playbackLayout); |
|
348 |
||
349 |
QWidget *widget = new QWidget; |
|
350 |
widget->setLayout(mainLayout); |
|
351 |
||
352 |
setCentralWidget(widget); |
|
353 |
setWindowTitle("Phonon Music Player"); |
|
354 |
} |
|
355 |