| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 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)));
|
|---|
| 57 | connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
|
|---|
| 58 | this, SLOT(stateChanged(Phonon::State, Phonon::State)));
|
|---|
| 59 | connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
|
|---|
| 60 | this, SLOT(metaStateChanged(Phonon::State, Phonon::State)));
|
|---|
| 61 | connect(mediaObject, SIGNAL(currentSourceChanged(const Phonon::MediaSource &)),
|
|---|
| 62 | this, SLOT(sourceChanged(const Phonon::MediaSource &)));
|
|---|
| 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 | mediaObject->setCurrentSource(sources[row]);
|
|---|
| 161 |
|
|---|
| 162 | if (wasPlaying)
|
|---|
| 163 | mediaObject->play();
|
|---|
| 164 | else
|
|---|
| 165 | mediaObject->stop();
|
|---|
| 166 | }
|
|---|
| 167 | //![12]
|
|---|
| 168 |
|
|---|
| 169 | //![13]
|
|---|
| 170 | void MainWindow::sourceChanged(const Phonon::MediaSource &source)
|
|---|
| 171 | {
|
|---|
| 172 | musicTable->selectRow(sources.indexOf(source));
|
|---|
| 173 | timeLcd->display("00:00");
|
|---|
| 174 | }
|
|---|
| 175 | //![13]
|
|---|
| 176 |
|
|---|
| 177 | //![14]
|
|---|
| 178 | void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
|
|---|
| 179 | {
|
|---|
| 180 | if (newState == Phonon::ErrorState) {
|
|---|
| 181 | QMessageBox::warning(this, tr("Error opening files"),
|
|---|
| 182 | metaInformationResolver->errorString());
|
|---|
| 183 | while (!sources.isEmpty() &&
|
|---|
| 184 | !(sources.takeLast() == metaInformationResolver->currentSource()));
|
|---|
| 185 | return;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
|
|---|
| 189 | return;
|
|---|
| 190 |
|
|---|
| 191 | if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
|
|---|
| 192 | return;
|
|---|
| 193 |
|
|---|
| 194 | QMap<QString, QString> metaData = metaInformationResolver->metaData();
|
|---|
| 195 |
|
|---|
| 196 | QString title = metaData.value("TITLE");
|
|---|
| 197 | if (title == "")
|
|---|
| 198 | title = metaInformationResolver->currentSource().fileName();
|
|---|
| 199 |
|
|---|
| 200 | QTableWidgetItem *titleItem = new QTableWidgetItem(title);
|
|---|
| 201 | titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
|
|---|
| 202 | QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
|
|---|
| 203 | artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
|
|---|
| 204 | QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
|
|---|
| 205 | albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
|
|---|
| 206 | QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value("DATE"));
|
|---|
| 207 | yearItem->setFlags(yearItem->flags() ^ Qt::ItemIsEditable);
|
|---|
| 208 | //![14]
|
|---|
| 209 |
|
|---|
| 210 | int currentRow = musicTable->rowCount();
|
|---|
| 211 | musicTable->insertRow(currentRow);
|
|---|
| 212 | musicTable->setItem(currentRow, 0, titleItem);
|
|---|
| 213 | musicTable->setItem(currentRow, 1, artistItem);
|
|---|
| 214 | musicTable->setItem(currentRow, 2, albumItem);
|
|---|
| 215 | musicTable->setItem(currentRow, 3, yearItem);
|
|---|
| 216 |
|
|---|
| 217 | //![15]
|
|---|
| 218 | if (musicTable->selectedItems().isEmpty()) {
|
|---|
| 219 | musicTable->selectRow(0);
|
|---|
| 220 | mediaObject->setCurrentSource(metaInformationResolver->currentSource());
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | Phonon::MediaSource source = metaInformationResolver->currentSource();
|
|---|
| 224 | int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
|
|---|
| 225 | if (sources.size() > index) {
|
|---|
| 226 | metaInformationResolver->setCurrentSource(sources.at(index));
|
|---|
| 227 | }
|
|---|
| 228 | else {
|
|---|
| 229 | musicTable->resizeColumnsToContents();
|
|---|
| 230 | if (musicTable->columnWidth(0) > 300)
|
|---|
| 231 | musicTable->setColumnWidth(0, 300);
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 | //![15]
|
|---|
| 235 |
|
|---|
| 236 | //![16]
|
|---|
| 237 | void MainWindow::aboutToFinish()
|
|---|
| 238 | {
|
|---|
| 239 | int index = sources.indexOf(mediaObject->currentSource()) + 1;
|
|---|
| 240 | if (sources.size() > index) {
|
|---|
| 241 | mediaObject->enqueue(sources.at(index));
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 | //![16]
|
|---|
| 245 |
|
|---|
| 246 | void MainWindow::setupActions()
|
|---|
| 247 | {
|
|---|
| 248 | playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this);
|
|---|
| 249 | playAction->setShortcut(tr("Crl+P"));
|
|---|
| 250 | playAction->setDisabled(true);
|
|---|
| 251 | pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this);
|
|---|
| 252 | pauseAction->setShortcut(tr("Ctrl+A"));
|
|---|
| 253 | pauseAction->setDisabled(true);
|
|---|
| 254 | stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this);
|
|---|
| 255 | stopAction->setShortcut(tr("Ctrl+S"));
|
|---|
| 256 | stopAction->setDisabled(true);
|
|---|
| 257 | nextAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipForward), tr("Next"), this);
|
|---|
| 258 | nextAction->setShortcut(tr("Ctrl+N"));
|
|---|
| 259 | previousAction = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), tr("Previous"), this);
|
|---|
| 260 | previousAction->setShortcut(tr("Ctrl+R"));
|
|---|
| 261 | addFilesAction = new QAction(tr("Add &Files"), this);
|
|---|
| 262 | addFilesAction->setShortcut(tr("Ctrl+F"));
|
|---|
| 263 | exitAction = new QAction(tr("E&xit"), this);
|
|---|
| 264 | exitAction->setShortcut(tr("Ctrl+X"));
|
|---|
| 265 | aboutAction = new QAction(tr("A&bout"), this);
|
|---|
| 266 | aboutAction->setShortcut(tr("Ctrl+B"));
|
|---|
| 267 | aboutQtAction = new QAction(tr("About &Qt"), this);
|
|---|
| 268 | aboutQtAction->setShortcut(tr("Ctrl+Q"));
|
|---|
| 269 |
|
|---|
| 270 | //![5]
|
|---|
| 271 | connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
|
|---|
| 272 | connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
|
|---|
| 273 | connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
|
|---|
| 274 | //![5]
|
|---|
| 275 | connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
|
|---|
| 276 | connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
|
|---|
| 277 | connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
|
|---|
| 278 | connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | void MainWindow::setupMenus()
|
|---|
| 282 | {
|
|---|
| 283 | QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
|---|
| 284 | fileMenu->addAction(addFilesAction);
|
|---|
| 285 | fileMenu->addSeparator();
|
|---|
| 286 | fileMenu->addAction(exitAction);
|
|---|
| 287 |
|
|---|
| 288 | QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
|
|---|
| 289 | aboutMenu->addAction(aboutAction);
|
|---|
| 290 | aboutMenu->addAction(aboutQtAction);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | //![3]
|
|---|
| 294 | void MainWindow::setupUi()
|
|---|
| 295 | {
|
|---|
| 296 | //![3]
|
|---|
| 297 | QToolBar *bar = new QToolBar;
|
|---|
| 298 |
|
|---|
| 299 | bar->addAction(playAction);
|
|---|
| 300 | bar->addAction(pauseAction);
|
|---|
| 301 | bar->addAction(stopAction);
|
|---|
| 302 |
|
|---|
| 303 | //![4]
|
|---|
| 304 | seekSlider = new Phonon::SeekSlider(this);
|
|---|
| 305 | seekSlider->setMediaObject(mediaObject);
|
|---|
| 306 |
|
|---|
| 307 | volumeSlider = new Phonon::VolumeSlider(this);
|
|---|
| 308 | volumeSlider->setAudioOutput(audioOutput);
|
|---|
| 309 | //![4]
|
|---|
| 310 | volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
|---|
| 311 |
|
|---|
| 312 | QLabel *volumeLabel = new QLabel;
|
|---|
| 313 | volumeLabel->setPixmap(QPixmap("images/volume.png"));
|
|---|
| 314 |
|
|---|
| 315 | QPalette palette;
|
|---|
| 316 | palette.setBrush(QPalette::Light, Qt::darkGray);
|
|---|
| 317 |
|
|---|
| 318 | timeLcd = new QLCDNumber;
|
|---|
| 319 | timeLcd->setPalette(palette);
|
|---|
| 320 |
|
|---|
| 321 | QStringList headers;
|
|---|
| 322 | headers << tr("Title") << tr("Artist") << tr("Album") << tr("Year");
|
|---|
| 323 |
|
|---|
| 324 | musicTable = new QTableWidget(0, 4);
|
|---|
| 325 | musicTable->setHorizontalHeaderLabels(headers);
|
|---|
| 326 | musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
|
|---|
| 327 | musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|---|
| 328 | connect(musicTable, SIGNAL(cellPressed(int, int)),
|
|---|
| 329 | this, SLOT(tableClicked(int, int)));
|
|---|
| 330 |
|
|---|
| 331 | QHBoxLayout *seekerLayout = new QHBoxLayout;
|
|---|
| 332 | seekerLayout->addWidget(seekSlider);
|
|---|
| 333 | seekerLayout->addWidget(timeLcd);
|
|---|
| 334 |
|
|---|
| 335 | QHBoxLayout *playbackLayout = new QHBoxLayout;
|
|---|
| 336 | playbackLayout->addWidget(bar);
|
|---|
| 337 | playbackLayout->addStretch();
|
|---|
| 338 | playbackLayout->addWidget(volumeLabel);
|
|---|
| 339 | playbackLayout->addWidget(volumeSlider);
|
|---|
| 340 |
|
|---|
| 341 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
|---|
| 342 | mainLayout->addWidget(musicTable);
|
|---|
| 343 | mainLayout->addLayout(seekerLayout);
|
|---|
| 344 | mainLayout->addLayout(playbackLayout);
|
|---|
| 345 |
|
|---|
| 346 | QWidget *widget = new QWidget;
|
|---|
| 347 | widget->setLayout(mainLayout);
|
|---|
| 348 |
|
|---|
| 349 | setCentralWidget(widget);
|
|---|
| 350 | setWindowTitle("Phonon Music Player");
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|