Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 3233

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: mainwindow.cpp Example File (phonon/qmusicplayer/mainwindow.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">mainwindow.cpp Example File<br /><span class="small-subtitle">phonon/qmusicplayer/mainwindow.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ***************************************************************************/</span>

 #include &lt;QtGui&gt;

 #include &quot;mainwindow.h&quot;

 MainWindow::MainWindow()
 {
     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
     mediaObject = new Phonon::MediaObject(this);
     metaInformationResolver = new Phonon::MediaObject(this);

     mediaObject-&gt;setTickInterval(1000);
     connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
     connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
             this, SLOT(stateChanged(Phonon::State,Phonon::State)));
     connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
             this, SLOT(metaStateChanged(Phonon::State,Phonon::State)));
     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
             this, SLOT(sourceChanged(Phonon::MediaSource)));
     connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));

     Phonon::createPath(mediaObject, audioOutput);

     setupActions();
     setupMenus();
     setupUi();
     timeLcd-&gt;display(&quot;00:00&quot;);
 }

 void MainWindow::addFiles()
 {
     QStringList files = QFileDialog::getOpenFileNames(this, tr(&quot;Select Music Files&quot;),
         QDesktopServices::storageLocation(QDesktopServices::MusicLocation));

     if (files.isEmpty())
         return;

     int index = sources.size();
     foreach (QString string, files) {
             Phonon::MediaSource source(string);

         sources.append(source);
     }
     if (!sources.isEmpty())
         metaInformationResolver-&gt;setCurrentSource(sources.at(index));

 }

 void MainWindow::about()
 {
     QMessageBox::information(this, tr(&quot;About Music Player&quot;),
         tr(&quot;The Music Player example shows how to use Phonon - the multimedia&quot;
            &quot; framework that comes with Qt - to create a simple music player.&quot;));
 }

 void MainWindow::stateChanged(Phonon::State newState, Phonon::State <span class="comment">/* oldState */</span>)
 {
     switch (newState) {
         case Phonon::ErrorState:
             if (mediaObject-&gt;errorType() == Phonon::FatalError) {
                 QMessageBox::warning(this, tr(&quot;Fatal Error&quot;),
                 mediaObject-&gt;errorString());
             } else {
                 QMessageBox::warning(this, tr(&quot;Error&quot;),
                 mediaObject-&gt;errorString());
             }
             break;
         case Phonon::PlayingState:
                 playAction-&gt;setEnabled(false);
                 pauseAction-&gt;setEnabled(true);
                 stopAction-&gt;setEnabled(true);
                 break;
         case Phonon::StoppedState:
                 stopAction-&gt;setEnabled(false);
                 playAction-&gt;setEnabled(true);
                 pauseAction-&gt;setEnabled(false);
                 timeLcd-&gt;display(&quot;00:00&quot;);
                 break;
         case Phonon::PausedState:
                 pauseAction-&gt;setEnabled(false);
                 stopAction-&gt;setEnabled(true);
                 playAction-&gt;setEnabled(true);
                 break;
         case Phonon::BufferingState:
                 break;
         default:
             ;
     }
 }

 void MainWindow::tick(qint64 time)
 {
     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);

     timeLcd-&gt;display(displayTime.toString(&quot;mm:ss&quot;));
 }

 void MainWindow::tableClicked(int row, int <span class="comment">/* column */</span>)
 {
     bool wasPlaying = mediaObject-&gt;state() == Phonon::PlayingState;

     mediaObject-&gt;stop();
     mediaObject-&gt;clearQueue();

     if (row &gt;= sources.size())
         return;

     mediaObject-&gt;setCurrentSource(sources[row]);

     if (wasPlaying)
         mediaObject-&gt;play();
     else
         mediaObject-&gt;stop();
 }

 void MainWindow::sourceChanged(const Phonon::MediaSource &amp;source)
 {
     musicTable-&gt;selectRow(sources.indexOf(source));
     timeLcd-&gt;display(&quot;00:00&quot;);
 }

 void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State <span class="comment">/* oldState */</span>)
 {
     if (newState == Phonon::ErrorState) {
         QMessageBox::warning(this, tr(&quot;Error opening files&quot;),
             metaInformationResolver-&gt;errorString());
         while (!sources.isEmpty() &amp;&amp;
                !(sources.takeLast() == metaInformationResolver-&gt;currentSource())) {}  <span class="comment">/* loop */</span>;
         return;
     }

     if (newState != Phonon::StoppedState &amp;&amp; newState != Phonon::PausedState)
         return;

     if (metaInformationResolver-&gt;currentSource().type() == Phonon::MediaSource::Invalid)
             return;

     QMap&lt;QString, QString&gt; metaData = metaInformationResolver-&gt;metaData();

     QString title = metaData.value(&quot;TITLE&quot;);
     if (title == &quot;&quot;)
         title = metaInformationResolver-&gt;currentSource().fileName();

     QTableWidgetItem *titleItem = new QTableWidgetItem(title);
     titleItem-&gt;setFlags(titleItem-&gt;flags() ^ Qt::ItemIsEditable);
     QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value(&quot;ARTIST&quot;));
     artistItem-&gt;setFlags(artistItem-&gt;flags() ^ Qt::ItemIsEditable);
     QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value(&quot;ALBUM&quot;));
     albumItem-&gt;setFlags(albumItem-&gt;flags() ^ Qt::ItemIsEditable);
     QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value(&quot;DATE&quot;));
     yearItem-&gt;setFlags(yearItem-&gt;flags() ^ Qt::ItemIsEditable);

     int currentRow = musicTable-&gt;rowCount();
     musicTable-&gt;insertRow(currentRow);
     musicTable-&gt;setItem(currentRow, 0, titleItem);
     musicTable-&gt;setItem(currentRow, 1, artistItem);
     musicTable-&gt;setItem(currentRow, 2, albumItem);
     musicTable-&gt;setItem(currentRow, 3, yearItem);

     if (musicTable-&gt;selectedItems().isEmpty()) {
         musicTable-&gt;selectRow(0);
         mediaObject-&gt;setCurrentSource(metaInformationResolver-&gt;currentSource());
     }

     Phonon::MediaSource source = metaInformationResolver-&gt;currentSource();
     int index = sources.indexOf(metaInformationResolver-&gt;currentSource()) + 1;
     if (sources.size() &gt; index) {
         metaInformationResolver-&gt;setCurrentSource(sources.at(index));
     }
     else {
         musicTable-&gt;resizeColumnsToContents();
         if (musicTable-&gt;columnWidth(0) &gt; 300)
             musicTable-&gt;setColumnWidth(0, 300);
     }
 }

 void MainWindow::aboutToFinish()
 {
     int index = sources.indexOf(mediaObject-&gt;currentSource()) + 1;
     if (sources.size() &gt; index) {
         mediaObject-&gt;enqueue(sources.at(index));
     }
 }

 void MainWindow::setupActions()
 {
     playAction = new QAction(style()-&gt;standardIcon(QStyle::SP_MediaPlay), tr(&quot;Play&quot;), this);
     playAction-&gt;setShortcut(tr(&quot;Crl+P&quot;));
     playAction-&gt;setDisabled(true);
     pauseAction = new QAction(style()-&gt;standardIcon(QStyle::SP_MediaPause), tr(&quot;Pause&quot;), this);
     pauseAction-&gt;setShortcut(tr(&quot;Ctrl+A&quot;));
     pauseAction-&gt;setDisabled(true);
     stopAction = new QAction(style()-&gt;standardIcon(QStyle::SP_MediaStop), tr(&quot;Stop&quot;), this);
     stopAction-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
     stopAction-&gt;setDisabled(true);
     nextAction = new QAction(style()-&gt;standardIcon(QStyle::SP_MediaSkipForward), tr(&quot;Next&quot;), this);
     nextAction-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
     previousAction = new QAction(style()-&gt;standardIcon(QStyle::SP_MediaSkipBackward), tr(&quot;Previous&quot;), this);
     previousAction-&gt;setShortcut(tr(&quot;Ctrl+R&quot;));
     addFilesAction = new QAction(tr(&quot;Add &amp;Files&quot;), this);
     addFilesAction-&gt;setShortcut(tr(&quot;Ctrl+F&quot;));
     exitAction = new QAction(tr(&quot;E&amp;xit&quot;), this);
     exitAction-&gt;setShortcuts(QKeySequence::Quit);
     aboutAction = new QAction(tr(&quot;A&amp;bout&quot;), this);
     aboutAction-&gt;setShortcut(tr(&quot;Ctrl+B&quot;));
     aboutQtAction = new QAction(tr(&quot;About &amp;Qt&quot;), this);
     aboutQtAction-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));

     connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
     connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
     connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
     connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 }

 void MainWindow::setupMenus()
 {
     QMenu *fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
     fileMenu-&gt;addAction(addFilesAction);
     fileMenu-&gt;addSeparator();
     fileMenu-&gt;addAction(exitAction);

     QMenu *aboutMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
     aboutMenu-&gt;addAction(aboutAction);
     aboutMenu-&gt;addAction(aboutQtAction);
 }

 void MainWindow::setupUi()
 {
     QToolBar *bar = new QToolBar;

     bar-&gt;addAction(playAction);
     bar-&gt;addAction(pauseAction);
     bar-&gt;addAction(stopAction);

     seekSlider = new Phonon::SeekSlider(this);
     seekSlider-&gt;setMediaObject(mediaObject);

     volumeSlider = new Phonon::VolumeSlider(this);
     volumeSlider-&gt;setAudioOutput(audioOutput);
     volumeSlider-&gt;setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

     QLabel *volumeLabel = new QLabel;
     volumeLabel-&gt;setPixmap(QPixmap(&quot;images/volume.png&quot;));

     QPalette palette;
     palette.setBrush(QPalette::Light, Qt::darkGray);

     timeLcd = new QLCDNumber;
     timeLcd-&gt;setPalette(palette);

     QStringList headers;
     headers &lt;&lt; tr(&quot;Title&quot;) &lt;&lt; tr(&quot;Artist&quot;) &lt;&lt; tr(&quot;Album&quot;) &lt;&lt; tr(&quot;Year&quot;);

     musicTable = new QTableWidget(0, 4);
     musicTable-&gt;setHorizontalHeaderLabels(headers);
     musicTable-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
     musicTable-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
     connect(musicTable, SIGNAL(cellPressed(int,int)),
             this, SLOT(tableClicked(int,int)));

     QHBoxLayout *seekerLayout = new QHBoxLayout;
     seekerLayout-&gt;addWidget(seekSlider);
     seekerLayout-&gt;addWidget(timeLcd);

     QHBoxLayout *playbackLayout = new QHBoxLayout;
     playbackLayout-&gt;addWidget(bar);
     playbackLayout-&gt;addStretch();
     playbackLayout-&gt;addWidget(volumeLabel);
     playbackLayout-&gt;addWidget(volumeSlider);

     QVBoxLayout *mainLayout = new QVBoxLayout;
     mainLayout-&gt;addWidget(musicTable);
     mainLayout-&gt;addLayout(seekerLayout);
     mainLayout-&gt;addLayout(playbackLayout);

     QWidget *widget = new QWidget;
     widget-&gt;setLayout(mainLayout);

     setCentralWidget(widget);
     setWindowTitle(&quot;Phonon Music Player&quot;);
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>