[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 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 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[556] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[556] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[556] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ***************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "mainwindow.h"
|
---|
| 44 |
|
---|
| 45 | //![0]
|
---|
| 46 | MainWindow::MainWindow()
|
---|
| 47 | {
|
---|
| 48 | audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
|
---|
| 49 | mediaObject = new Phonon::MediaObject(this);
|
---|
| 50 | metaInformationResolver = new Phonon::MediaObject(this);
|
---|
| 51 |
|
---|
| 52 | mediaObject->setTickInterval(1000);
|
---|
| 53 | //![0]
|
---|
| 54 | //![2]
|
---|
| 55 | connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
|
---|
| 56 | connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
|
---|
| 57 | this, SLOT(stateChanged(Phonon::State,Phonon::State)));
|
---|
| 58 | connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
|
---|
| 59 | this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
|
---|
| 60 | connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
|
---|
| 61 | this, SLOT(sourceChanged(Phonon::MediaSource)));
|
---|
| 62 | connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
|
---|
| 63 | //![2]
|
---|
| 64 |
|
---|
| 65 | //![1]
|
---|
| 66 | Phonon::createPath(mediaObject, audioOutput);
|
---|
| 67 | //![1]
|
---|
| 68 |
|
---|
| 69 | setupActions();
|
---|
| 70 | setupMenus();
|
---|
| 71 | setupUi();
|
---|
| 72 | timeLcd->display("00:00");
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | //![6]
|
---|
| 76 | void MainWindow::addFiles()
|
---|
| 77 | {
|
---|
| 78 | QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"),
|
---|
| 79 | QDesktopServices::storageLocation(QDesktopServices::MusicLocation));
|
---|
| 80 |
|
---|
| 81 | if (files.isEmpty())
|
---|
| 82 | return;
|
---|
| 83 |
|
---|
| 84 | int index = sources.size();
|
---|
| 85 | foreach (QString string, files) {
|
---|
| 86 | Phonon::MediaSource source(string);
|
---|
| 87 |
|
---|
| 88 | sources.append(source);
|
---|
| 89 | }
|
---|
| 90 | if (!sources.isEmpty())
|
---|
| 91 | metaInformationResolver->setCurrentSource(sources.at(index));
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 | //![6]
|
---|
| 95 |
|
---|
| 96 | void MainWindow::about()
|
---|
| 97 | {
|
---|
| 98 | QMessageBox::information(this, tr("About Music Player"),
|
---|
| 99 | tr("The Music Player example shows how to use Phonon - the multimedia"
|
---|
| 100 | " framework that comes with Qt - to create a simple music player."));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | //![9]
|
---|
| 104 | void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
|
---|
| 105 | {
|
---|
| 106 | switch (newState) {
|
---|
| 107 | case Phonon::ErrorState:
|
---|
| 108 | if (mediaObject->errorType() == Phonon::FatalError) {
|
---|
| 109 | QMessageBox::warning(this, tr("Fatal Error"),
|
---|
| 110 | mediaObject->errorString());
|
---|
| 111 | } else {
|
---|
| 112 | QMessageBox::warning(this, tr("Error"),
|
---|
| 113 | mediaObject->errorString());
|
---|
| 114 | }
|
---|
| 115 | break;
|
---|
| 116 | //![9]
|
---|
| 117 | //![10]
|
---|
| 118 | case Phonon::PlayingState:
|
---|
| 119 | playAction->setEnabled(false);
|
---|
| 120 | pauseAction->setEnabled(true);
|
---|
| 121 | stopAction->setEnabled(true);
|
---|
| 122 | break;
|
---|
| 123 | case Phonon::StoppedState:
|
---|
| 124 | stopAction->setEnabled(false);
|
---|
| 125 | playAction->setEnabled(true);
|
---|
| 126 | pauseAction->setEnabled(false);
|
---|
| 127 | timeLcd->display("00:00");
|
---|
| 128 | break;
|
---|
| 129 | case Phonon::PausedState:
|
---|
| 130 | pauseAction->setEnabled(false);
|
---|
| 131 | stopAction->setEnabled(true);
|
---|
| 132 | playAction->setEnabled(true);
|
---|
| 133 | break;
|
---|
| 134 | //![10]
|
---|
| 135 | case Phonon::BufferingState:
|
---|
| 136 | break;
|
---|
| 137 | default:
|
---|
| 138 | ;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | //![11]
|
---|
| 143 | void MainWindow::tick(qint64 time)
|
---|
| 144 | {
|
---|
| 145 | QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
|
---|
| 146 |
|
---|
| 147 | timeLcd->display(displayTime.toString("mm:ss"));
|
---|
| 148 | }
|
---|
| 149 | //![11]
|
---|
| 150 |
|
---|
| 151 | //![12]
|
---|
| 152 | void MainWindow::tableClicked(int row, int /* column */)
|
---|
| 153 | {
|
---|
| 154 | bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
|
---|
| 155 |
|
---|
| 156 | mediaObject->stop();
|
---|
| 157 | mediaObject->clearQueue();
|
---|
| 158 |
|
---|
| 159 | if (row >= sources.size())
|
---|
| 160 | return;
|
---|
| 161 |
|
---|
| 162 | mediaObject->setCurrentSource(sources[row]);
|
---|
| 163 |
|
---|
| 164 | if (wasPlaying)
|
---|
| 165 | mediaObject->play();
|
---|
| 166 | else
|
---|
| 167 | mediaObject->stop();
|
---|
| 168 | }
|
---|
| 169 | //![12]
|
---|
| 170 |
|
---|
| 171 | //![13]
|
---|
| 172 | void MainWindow::sourceChanged(const Phonon::MediaSource &source)
|
---|
| 173 | {
|
---|
| 174 | musicTable->selectRow(sources.indexOf(source));
|
---|
| 175 | timeLcd->display("00:00");
|
---|
| 176 | }
|
---|
| 177 | //![13]
|
---|
| 178 |
|
---|
| 179 | //![14]
|
---|
| 180 | void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
|
---|
| 181 | {
|
---|
| 182 | if (newState == Phonon::ErrorState) {
|
---|
| 183 | QMessageBox::warning(this, tr("Error opening files"),
|
---|
| 184 | metaInformationResolver->errorString());
|
---|
| 185 | while (!sources.isEmpty() &&
|
---|
| 186 | !(sources.takeLast() == metaInformationResolver->currentSource())) {} /* loop */;
|
---|
| 187 | return;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
|
---|
| 191 | return;
|
---|
| 192 |
|
---|
| 193 | if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
|
---|
| 194 | return;
|
---|
| 195 |
|
---|
| 196 | QMap<QString, QString> metaData = metaInformationResolver->metaData();
|
---|
| 197 |
|
---|
| 198 | QString title = metaData.value("TITLE");
|
---|
| 199 | if (title == "")
|
---|
| 200 | title = metaInformationResolver->currentSource().fileName();
|
---|
| 201 |
|
---|
| 202 | QTableWidgetItem *titleItem = new QTableWidgetItem(title);
|
---|
| 203 | titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
|
---|
| 204 | QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
|
---|
| 205 | artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
|
---|
| 206 | QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
|
---|
| 207 | albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
|
---|
| 208 | QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value("DATE"));
|
---|
| 209 | yearItem->setFlags(yearItem->flags() ^ Qt::ItemIsEditable);
|
---|
| 210 | //![14]
|
---|
| 211 |
|
---|
| 212 | int currentRow = musicTable->rowCount();
|
---|
| 213 | musicTable->insertRow(currentRow);
|
---|
| 214 | musicTable->setItem(currentRow, 0, titleItem);
|
---|
| 215 | musicTable->setItem(currentRow, 1, artistItem);
|
---|
| 216 | musicTable->setItem(currentRow, 2, albumItem);
|
---|
| 217 | musicTable->setItem(currentRow, 3, yearItem);
|
---|
| 218 |
|
---|
| 219 | //![15]
|
---|
| 220 | if (musicTable->selectedItems().isEmpty()) {
|
---|
| 221 | musicTable->selectRow(0);
|
---|
| 222 | mediaObject->setCurrentSource(metaInformationResolver->currentSource());
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | Phonon::MediaSource source = metaInformationResolver->currentSource();
|
---|
| 226 | int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
|
---|
| 227 | if (sources.size() > index) {
|
---|
| 228 | metaInformationResolver->setCurrentSource(sources.at(index));
|
---|
| 229 | }
|
---|
| 230 | else {
|
---|
| 231 | musicTable->resizeColumnsToContents();
|
---|
| 232 | if (musicTable->columnWidth(0) > 300)
|
---|
| 233 | musicTable->setColumnWidth(0, 300);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | //![15]
|
---|
| 237 |
|
---|
| 238 | //![16]
|
---|
| 239 | void MainWindow::aboutToFinish()
|
---|
| 240 | {
|
---|
| 241 | int index = sources.indexOf(mediaObject->currentSource()) + 1;
|
---|
| 242 | if (sources.size() > index) {
|
---|
| 243 | mediaObject->enqueue(sources.at(index));
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | //![16]
|
---|
| 247 |
|
---|
| 248 | void MainWindow::setupActions()
|
---|
| 249 | {
|
---|
| 250 | playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this);
|
---|
[846] | 251 | playAction->setShortcut(tr("Ctrl+P"));
|
---|
[556] | 252 | playAction->setDisabled(true);
|
---|
| 253 | pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this);
|
---|
| 254 | pauseAction->setShortcut(tr("Ctrl+A"));
|
---|
| 255 | pauseAction->setDisabled(true);
|
---|
| 256 | stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this);
|
---|
| 257 | stopAction->setShortcut(tr("Ctrl+S"));
|
---|
| 258 | stopAction->setDisabled(true);
|
---|
| 259 | nextAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipForward), tr("Next"), this);
|
---|
| 260 | nextAction->setShortcut(tr("Ctrl+N"));
|
---|
| 261 | previousAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), tr("Previous"), this);
|
---|
| 262 | previousAction->setShortcut(tr("Ctrl+R"));
|
---|
| 263 | addFilesAction = new QAction(tr("Add &Files"), this);
|
---|
| 264 | addFilesAction->setShortcut(tr("Ctrl+F"));
|
---|
| 265 | exitAction = new QAction(tr("E&xit"), this);
|
---|
| 266 | exitAction->setShortcuts(QKeySequence::Quit);
|
---|
| 267 | aboutAction = new QAction(tr("A&bout"), this);
|
---|
| 268 | aboutAction->setShortcut(tr("Ctrl+B"));
|
---|
| 269 | aboutQtAction = new QAction(tr("About &Qt"), this);
|
---|
| 270 | aboutQtAction->setShortcut(tr("Ctrl+Q"));
|
---|
| 271 |
|
---|
| 272 | //![5]
|
---|
| 273 | connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
|
---|
| 274 | connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
|
---|
| 275 | connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
|
---|
| 276 | //![5]
|
---|
| 277 | connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
|
---|
| 278 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
|
---|
| 279 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
|
---|
| 280 | connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | void MainWindow::setupMenus()
|
---|
| 284 | {
|
---|
| 285 | QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
| 286 | fileMenu->addAction(addFilesAction);
|
---|
| 287 | fileMenu->addSeparator();
|
---|
| 288 | fileMenu->addAction(exitAction);
|
---|
| 289 |
|
---|
| 290 | QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
|
---|
| 291 | aboutMenu->addAction(aboutAction);
|
---|
| 292 | aboutMenu->addAction(aboutQtAction);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | //![3]
|
---|
| 296 | void MainWindow::setupUi()
|
---|
| 297 | {
|
---|
| 298 | //![3]
|
---|
| 299 | QToolBar *bar = new QToolBar;
|
---|
| 300 |
|
---|
| 301 | bar->addAction(playAction);
|
---|
| 302 | bar->addAction(pauseAction);
|
---|
| 303 | bar->addAction(stopAction);
|
---|
| 304 |
|
---|
| 305 | //![4]
|
---|
| 306 | seekSlider = new Phonon::SeekSlider(this);
|
---|
| 307 | seekSlider->setMediaObject(mediaObject);
|
---|
| 308 |
|
---|
| 309 | volumeSlider = new Phonon::VolumeSlider(this);
|
---|
| 310 | volumeSlider->setAudioOutput(audioOutput);
|
---|
| 311 | //![4]
|
---|
| 312 | volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
---|
| 313 |
|
---|
| 314 | QLabel *volumeLabel = new QLabel;
|
---|
| 315 | volumeLabel->setPixmap(QPixmap("images/volume.png"));
|
---|
| 316 |
|
---|
| 317 | QPalette palette;
|
---|
| 318 | palette.setBrush(QPalette::Light, Qt::darkGray);
|
---|
| 319 |
|
---|
| 320 | timeLcd = new QLCDNumber;
|
---|
| 321 | timeLcd->setPalette(palette);
|
---|
| 322 |
|
---|
| 323 | QStringList headers;
|
---|
| 324 | headers << tr("Title") << tr("Artist") << tr("Album") << tr("Year");
|
---|
| 325 |
|
---|
| 326 | musicTable = new QTableWidget(0, 4);
|
---|
| 327 | musicTable->setHorizontalHeaderLabels(headers);
|
---|
| 328 | musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
|
---|
| 329 | musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
---|
| 330 | connect(musicTable, SIGNAL(cellPressed(int,int)),
|
---|
| 331 | this, SLOT(tableClicked(int,int)));
|
---|
| 332 |
|
---|
| 333 | QHBoxLayout *seekerLayout = new QHBoxLayout;
|
---|
| 334 | seekerLayout->addWidget(seekSlider);
|
---|
| 335 | seekerLayout->addWidget(timeLcd);
|
---|
| 336 |
|
---|
| 337 | QHBoxLayout *playbackLayout = new QHBoxLayout;
|
---|
| 338 | playbackLayout->addWidget(bar);
|
---|
| 339 | playbackLayout->addStretch();
|
---|
| 340 | playbackLayout->addWidget(volumeLabel);
|
---|
| 341 | playbackLayout->addWidget(volumeSlider);
|
---|
| 342 |
|
---|
| 343 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
---|
| 344 | mainLayout->addWidget(musicTable);
|
---|
| 345 | mainLayout->addLayout(seekerLayout);
|
---|
| 346 | mainLayout->addLayout(playbackLayout);
|
---|
| 347 |
|
---|
| 348 | QWidget *widget = new QWidget;
|
---|
| 349 | widget->setLayout(mainLayout);
|
---|
| 350 |
|
---|
| 351 | setCentralWidget(widget);
|
---|
| 352 | setWindowTitle("Phonon Music Player");
|
---|
| 353 | }
|
---|
| 354 |
|
---|