diff options
author | J-P Nurmi <jpnurmi@digia.com> | 2013-09-30 23:02:58 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-04 16:21:24 +0200 |
commit | a8699963b98fe2d1dcc33658ac006100b69444b1 (patch) | |
tree | 49889bb003740d573afd9c311b7ac96a39e0e099 /examples | |
parent | f3bc479f070dbe4ee3e784480101dad1c2e84296 (diff) |
A music player example
Change-Id: Ie28baa6373a8852b31137ed3c076ab4309d1c98f
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Diffstat (limited to 'examples')
16 files changed, 907 insertions, 0 deletions
diff --git a/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-composited.png b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-composited.png Binary files differnew file mode 100644 index 0000000..3ae91f9 --- /dev/null +++ b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-composited.png diff --git a/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-non-composited.png b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-non-composited.png Binary files differnew file mode 100644 index 0000000..682449b --- /dev/null +++ b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-non-composited.png diff --git a/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-taskbar.png b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-taskbar.png Binary files differnew file mode 100644 index 0000000..b03d192 --- /dev/null +++ b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-taskbar.png diff --git a/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-thumbnail.png b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-thumbnail.png Binary files differnew file mode 100644 index 0000000..2198ec8 --- /dev/null +++ b/examples/winextras/musicplayer/doc/images/qtwinextras-musicplayer-thumbnail.png diff --git a/examples/winextras/musicplayer/doc/src/qtwinextras-musicplayer.qdoc b/examples/winextras/musicplayer/doc/src/qtwinextras-musicplayer.qdoc new file mode 100644 index 0000000..d6b088b --- /dev/null +++ b/examples/winextras/musicplayer/doc/src/qtwinextras-musicplayer.qdoc @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! + \example musicplayer + \title Music Player + \ingroup examples-qtwinextras + \brief Demonstrates managing Windows DWM features, jump lists, + taskbar button overlay, and thumbnail toolbar. + + \image qtwinextras-musicplayer-composited.png Screenshot of the Music Player example + + The Music Player example demonstrates how to use the various features + provided by the QtWinExtras module. + + \note The example uses QMediaPlayer from the QtMultimedia module to play + the music, but this article focuses on the parts where QtWinExtras features + are used. + + \section1 DWM Features + + The example uses Windows DWM (Desktop Window Manager) features to visually + integrate the window content to the window frame and to make the main window + and the volume popup translucent and blurred. + + The example applies a different look based on whether composition is enabled + or not. When composition is enabled, the main window is made translucent and + the window frame is extended to the client area to make the window content + integrate seamlessly to the window frame as shown above. When composition + is disabled, the colorization color is used as a background color. The + following screenshot illustrates how the Music Player example looks when + composition is disabled. + + \image qtwinextras-musicplayer-non-composited.png Screenshot of the Music Player example + + The respective code is a combination of extending or resetting the system + frame, adjusting the necessary QWidget attributes, and setting an appropriate + style sheet to achieve the desired look. + + \snippet musicplayer/musicplayer.cpp 1 + + The volume popup does not have a window frame so it is enough to make the + popup blurred when composition is enabled. Additionally, a style sheet is + applied to get a border that follows the colorization color. Just like for + the main window, when composition is disabled, the colorization color is + used as a background color. + + \snippet musicplayer/volumebutton.cpp 0 + + The example application respects the user's composition settings, reacts + to dynamic composition changes, and looks solid regardless of whether + composition is enabled or not. It accomplishes this by catching + QWinEvent::CompositionChange and QWinEvent::ColorizationChange events + and adjusting its looks accordingly. + + \snippet musicplayer/musicplayer.cpp 0 + + \section1 Jump List + + The example creates a custom jump list to provide the user with fast + access to recently played music files. + + \snippet musicplayer/musicplayer.cpp 4 + + To enable the application jump list to show the desired recent files, + the corresponding file types are registered using the following helper + function. + + \snippet musicplayer/main.cpp 0 + + \section1 Taskbar Overlay and Progress + + The example uses Windows Taskbar for two things; it sets an overlay icon + that represents the current music playback state, and it indicates the + playback progress in the taskbar button. + + \image qtwinextras-musicplayer-taskbar.png Screenshot of the Music Player taskbar + + The following snippet shows how the taskbar button is prepared. The playback + progress is wired directly to the taskbar progress indicator by using signals + and slots. + + \snippet musicplayer/musicplayer.cpp 5 + + The overlay icon and the progress indicator are updated whenever the state + of the music playback changes. + + \snippet musicplayer/musicplayer.cpp 2 + + \section1 Thumbnail Toolbar + + \image qtwinextras-musicplayer-thumbnail.png Screenshot of the Music Player thumbnail + + The Windows Thumbnail Toolbar is used for providing basic music playback + controls. These controls can be used to control the application without + having to activate the application. + + \snippet musicplayer/musicplayer.cpp 6 + + The buttons in the thumbnail toolbar are updated accordingly whenever the state + of the music playback changes. + + \snippet musicplayer/musicplayer.cpp 3 +*/ diff --git a/examples/winextras/musicplayer/images/musicplayer.ico b/examples/winextras/musicplayer/images/musicplayer.ico Binary files differnew file mode 100644 index 0000000..fef1dee --- /dev/null +++ b/examples/winextras/musicplayer/images/musicplayer.ico diff --git a/examples/winextras/musicplayer/images/musicplayer.png b/examples/winextras/musicplayer/images/musicplayer.png Binary files differnew file mode 100644 index 0000000..7d3e97e --- /dev/null +++ b/examples/winextras/musicplayer/images/musicplayer.png diff --git a/examples/winextras/musicplayer/main.cpp b/examples/winextras/musicplayer/main.cpp new file mode 100644 index 0000000..cc3771a --- /dev/null +++ b/examples/winextras/musicplayer/main.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "musicplayer.h" + +#include <QApplication> +#include <QFileInfo> +#include <QSettings> +#include <QIcon> +#include <QDir> + +//! [0] +static void associateFileTypes(const QStringList &fileTypes) +{ + QString displayName = QGuiApplication::applicationDisplayName(); + QString filePath = QCoreApplication::applicationFilePath(); + QString fileName = QFileInfo(filePath).fileName(); + + QSettings settings("HKEY_CURRENT_USER\\Software\\Classes\\Applications\\" + fileName, QSettings::NativeFormat); + settings.setValue("FriendlyAppName", displayName); + + settings.beginGroup("SupportedTypes"); + foreach (const QString& fileType, fileTypes) + settings.setValue(fileType, QString()); + settings.endGroup(); + + settings.beginGroup("shell"); + settings.beginGroup("open"); + settings.setValue("FriendlyAppName", displayName); + settings.beginGroup("Command"); + settings.setValue(".", QChar('"') + QDir::toNativeSeparators(filePath) + QString("\" \"%1\"")); +} +//! [0] + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + app.setApplicationName("MusicPlayer"); + app.setOrganizationName("QtWinExtras"); + app.setOrganizationDomain("qt-project.org"); + app.setApplicationDisplayName("QtWinExtras Music Player"); + app.setWindowIcon(QIcon(":/logo.png")); + + associateFileTypes(QStringList(".mp3")); + + MusicPlayer player; + if (argc > 1) + player.playFile(app.arguments().value(1)); + player.resize(300, 60); + player.show(); + + return app.exec(); +} diff --git a/examples/winextras/musicplayer/musicplayer.cpp b/examples/winextras/musicplayer/musicplayer.cpp new file mode 100644 index 0000000..f9bc0ec --- /dev/null +++ b/examples/winextras/musicplayer/musicplayer.cpp @@ -0,0 +1,362 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "musicplayer.h" +#include "volumebutton.h" + +#include <QtWidgets> +#include <QtWinExtras> + +MusicPlayer::MusicPlayer(QWidget *parent) : QWidget(parent), + taskbarButton(0), taskbarProgress(0), thumbnailToolBar(0), + playToolButton(0), forwardToolButton(0), backwardToolButton(0), + mediaPlayer(0), playButton(0), volumeButton(0), + positionSlider(0), positionLabel(0), infoLabel(0) +{ + createWidgets(); + createShortcuts(); + createJumpList(); + createTaskbar(); + createThumbnailToolBar(); + + connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(updatePosition(qint64))); + connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(updateDuration(qint64))); + connect(&mediaPlayer, SIGNAL(metaDataAvailableChanged(bool)), this, SLOT(updateInfo())); + connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError())); + connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), + this, SLOT(updateState(QMediaPlayer::State))); + + stylize(); +} + +void MusicPlayer::openFile() +{ + static QString path = QDir::homePath(); + QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"), path, tr("MP3 files (*.mp3);;All files (*.*)")); + if (!filePath.isEmpty()) { + path = filePath; + playFile(filePath); + } +} + +void MusicPlayer::playFile(const QString &filePath) +{ + playButton->setEnabled(true); + infoLabel->setText(QFileInfo(filePath).fileName()); + + mediaPlayer.setMedia(QUrl::fromLocalFile(filePath)); + mediaPlayer.play(); +} + +void MusicPlayer::togglePlayback() +{ + if (mediaPlayer.mediaStatus() == QMediaPlayer::NoMedia) + openFile(); + else if (mediaPlayer.state() == QMediaPlayer::PlayingState) + mediaPlayer.pause(); + else + mediaPlayer.play(); +} + +void MusicPlayer::seekForward() +{ + positionSlider->triggerAction(QSlider::SliderPageStepAdd); +} + +void MusicPlayer::seekBackward() +{ + positionSlider->triggerAction(QSlider::SliderPageStepSub); +} + +//! [0] +bool MusicPlayer::event(QEvent *event) +{ + if (event->type() == QWinEvent::CompositionChange || event->type() == QWinEvent::ColorizationChange) + stylize(); + return QWidget::event(event); +} +//! [0] + +void MusicPlayer::mousePressEvent(QMouseEvent *event) +{ + offset = event->globalPos() - pos(); + event->accept(); +} + +void MusicPlayer::mouseMoveEvent(QMouseEvent *event) +{ + move(event->globalPos() - offset); + event->accept(); +} + +void MusicPlayer::mouseReleaseEvent(QMouseEvent *event) +{ + offset = QPoint(); + event->accept(); +} + +//! [1] +void MusicPlayer::stylize() +{ + if (QtWin::isCompositionEnabled()) { + QtWin::extendFrameIntoClientArea(this, -1, -1, -1, -1); + setAttribute(Qt::WA_TranslucentBackground, true); + setAttribute(Qt::WA_NoSystemBackground, false); + setStyleSheet("MusicPlayer { background: transparent; }"); + } else { + QtWin::resetExtendedFrame(this); + setAttribute(Qt::WA_TranslucentBackground, false); + setStyleSheet(QString("MusicPlayer { background: %1; }").arg(QtWin::realColorizationColor().name())); + } + volumeButton->stylize(); +} +//! [1] + +void MusicPlayer::updateState(QMediaPlayer::State state) +{ + if (state == QMediaPlayer::PlayingState) { + playButton->setToolTip(tr("Pause")); + playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); + } else { + playButton->setToolTip(tr("Play")); + playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + } +} + +void MusicPlayer::updatePosition(qint64 position) +{ + positionSlider->setValue(position); + + QTime duration(0, position / 60000, qRound((position % 60000) / 1000.0)); + positionLabel->setText(duration.toString(tr("mm:ss"))); +} + +void MusicPlayer::updateDuration(qint64 duration) +{ + positionSlider->setRange(0, duration); + positionSlider->setEnabled(duration > 0); + positionSlider->setPageStep(duration / 10); +} + +void MusicPlayer::setPosition(int position) +{ + // avoid seeking when the slider value change is triggered from updatePosition() + if (qAbs(mediaPlayer.position() - position) > 99) + mediaPlayer.setPosition(position); +} + +void MusicPlayer::updateInfo() +{ + QStringList info; + QString author = mediaPlayer.metaData("Author").toString(); + if (!author.isEmpty()) + info += author; + QString title = mediaPlayer.metaData("Title").toString(); + if (!title.isEmpty()) + info += title; + if (!info.isEmpty()) + infoLabel->setText(info.join(tr(" - "))); +} + +void MusicPlayer::handleError() +{ + playButton->setEnabled(false); + infoLabel->setText(tr("Error: %1").arg(mediaPlayer.errorString())); +} + +//! [2] +void MusicPlayer::updateTaskbar() +{ + switch (mediaPlayer.state()) { + case QMediaPlayer::PlayingState: + taskbarButton->setOverlayIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + taskbarProgress->show(); + taskbarProgress->resume(); + break; + case QMediaPlayer::PausedState: + taskbarButton->setOverlayIcon(style()->standardIcon(QStyle::SP_MediaPause)); + taskbarProgress->show(); + taskbarProgress->pause(); + break; + case QMediaPlayer::StoppedState: + taskbarButton->setOverlayIcon(style()->standardIcon(QStyle::SP_MediaStop)); + taskbarProgress->hide(); + break; + } +} +//! [2] + +//! [3] +void MusicPlayer::updateThumbnailToolBar() +{ + playToolButton->setEnabled(mediaPlayer.duration() > 0); + backwardToolButton->setEnabled(mediaPlayer.position() > 0); + forwardToolButton->setEnabled(mediaPlayer.position() < mediaPlayer.duration()); + + if (mediaPlayer.state() == QMediaPlayer::PlayingState) { + playToolButton->setToolTip(tr("Pause")); + playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); + } else { + playToolButton->setToolTip(tr("Play")); + playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + } +} +//! [3] + +void MusicPlayer::createWidgets() +{ + playButton = new QToolButton(this); + playButton->setEnabled(false); + playButton->setToolTip(tr("Play")); + playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + connect(playButton, SIGNAL(clicked()), this, SLOT(togglePlayback())); + + QAbstractButton *openButton = new QToolButton(this); + openButton->setText(tr("...")); + openButton->setToolTip(tr("Open a file...")); + openButton->setFixedSize(playButton->sizeHint()); + connect(openButton, SIGNAL(clicked()), this, SLOT(openFile())); + + volumeButton = new VolumeButton(this); + volumeButton->setToolTip(tr("Adjust volume")); + volumeButton->setVolume(mediaPlayer.volume()); + connect(volumeButton, SIGNAL(volumeChanged(int)), &mediaPlayer, SLOT(setVolume(int))); + + positionSlider = new QSlider(Qt::Horizontal, this); + positionSlider->setEnabled(false); + positionSlider->setToolTip(tr("Seek")); + connect(positionSlider, SIGNAL(valueChanged(int)), this, SLOT(setPosition(int))); + + infoLabel = new QLabel(this); + positionLabel = new QLabel(tr("00:00"), this); + positionLabel->setMinimumWidth(positionLabel->sizeHint().width()); + + QBoxLayout *controlLayout = new QHBoxLayout; + controlLayout->setMargin(0); + controlLayout->addWidget(openButton); + controlLayout->addWidget(playButton); + controlLayout->addWidget(positionSlider); + controlLayout->addWidget(positionLabel); + controlLayout->addWidget(volumeButton); + + QBoxLayout *mainLayout = new QVBoxLayout(this); + mainLayout->addWidget(infoLabel); + mainLayout->addLayout(controlLayout); +} + +void MusicPlayer::createShortcuts() +{ + QShortcut *quitShortcut = new QShortcut(QKeySequence::Quit, this); + connect(quitShortcut, SIGNAL(activated()), qApp, SLOT(quit())); + + QShortcut *openShortcut = new QShortcut(QKeySequence::Open, this); + connect(openShortcut, SIGNAL(activated()), this, SLOT(openFile())); + + QShortcut *toggleShortcut = new QShortcut(Qt::Key_Space, this); + connect(toggleShortcut, SIGNAL(activated()), this, SLOT(togglePlayback())); + + QShortcut *forwardShortcut = new QShortcut(Qt::Key_Right, this); + connect(forwardShortcut, SIGNAL(activated()), this, SLOT(seekForward())); + + QShortcut *backwardShortcut = new QShortcut(Qt::Key_Left, this); + connect(backwardShortcut, SIGNAL(activated()), this, SLOT(seekBackward())); + + QShortcut *increaseShortcut = new QShortcut(Qt::Key_Up, this); + connect(increaseShortcut, SIGNAL(activated()), volumeButton, SLOT(increaseVolume())); + + QShortcut *decreaseShortcut = new QShortcut(Qt::Key_Down, this); + connect(decreaseShortcut, SIGNAL(activated()), volumeButton, SLOT(descreaseVolume())); +} + +//! [4] +void MusicPlayer::createJumpList() +{ + QWinJumpList jumplist; + jumplist.recent()->setVisible(true); +} +//! [4] + +//! [5] +void MusicPlayer::createTaskbar() +{ + taskbarButton = new QWinTaskbarButton(this); + taskbarButton->setWindow(windowHandle()); + + taskbarProgress = taskbarButton->progress(); + connect(positionSlider, SIGNAL(valueChanged(int)), taskbarProgress, SLOT(setValue(int))); + connect(positionSlider, SIGNAL(rangeChanged(int, int)), taskbarProgress, SLOT(setRange(int, int))); + + connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(updateTaskbar())); +} +//! [5] + +//! [6] +void MusicPlayer::createThumbnailToolBar() +{ + thumbnailToolBar = new QWinThumbnailToolBar(this); + thumbnailToolBar->setWindow(windowHandle()); + + playToolButton = new QWinThumbnailToolButton(thumbnailToolBar); + playToolButton->setEnabled(false); + playToolButton->setToolTip(tr("Play")); + playToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); + connect(playToolButton, SIGNAL(clicked()), this, SLOT(togglePlayback())); + + forwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar); + forwardToolButton->setEnabled(false); + forwardToolButton->setToolTip(tr("Fast forward")); + forwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekForward)); + connect(forwardToolButton, SIGNAL(clicked()), this, SLOT(seekForward())); + + backwardToolButton = new QWinThumbnailToolButton(thumbnailToolBar); + backwardToolButton->setEnabled(false); + backwardToolButton->setToolTip(tr("Rewind")); + backwardToolButton->setIcon(style()->standardIcon(QStyle::SP_MediaSeekBackward)); + connect(backwardToolButton, SIGNAL(clicked()), this, SLOT(seekBackward())); + + thumbnailToolBar->addButton(backwardToolButton); + thumbnailToolBar->addButton(playToolButton); + thumbnailToolBar->addButton(forwardToolButton); + + connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(updateThumbnailToolBar())); + connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(updateThumbnailToolBar())); + connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(updateThumbnailToolBar())); +} +//! [6] diff --git a/examples/winextras/musicplayer/musicplayer.h b/examples/winextras/musicplayer/musicplayer.h new file mode 100644 index 0000000..aae8781 --- /dev/null +++ b/examples/winextras/musicplayer/musicplayer.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MUSICPLAYER_H +#define MUSICPLAYER_H + +#include <QWidget> +#include <QMediaPlayer> + +class VolumeButton; + +QT_FORWARD_DECLARE_CLASS(QLabel) +QT_FORWARD_DECLARE_CLASS(QSlider) +QT_FORWARD_DECLARE_CLASS(QAbstractButton) +QT_FORWARD_DECLARE_CLASS(QWinTaskbarButton) +QT_FORWARD_DECLARE_CLASS(QWinTaskbarProgress) +QT_FORWARD_DECLARE_CLASS(QWinThumbnailToolBar) +QT_FORWARD_DECLARE_CLASS(QWinThumbnailToolButton) + +class MusicPlayer : public QWidget +{ + Q_OBJECT +public: + MusicPlayer(QWidget *parent = 0); + +public slots: + void openFile(); + void playFile(const QString& filePath); + void togglePlayback(); + void seekForward(); + void seekBackward(); + +protected: + bool event(QEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + +private slots: + void stylize(); + void updateState(QMediaPlayer::State state); + void updatePosition(qint64 position); + void updateDuration(qint64 duration); + void setPosition(int position); + void updateInfo(); + void handleError(); + + void updateTaskbar(); + void updateThumbnailToolBar(); + +private: + void createWidgets(); + void createShortcuts(); + void createJumpList(); + void createTaskbar(); + void createThumbnailToolBar(); + + QWinTaskbarButton* taskbarButton; + QWinTaskbarProgress* taskbarProgress; + QWinThumbnailToolBar* thumbnailToolBar; + QWinThumbnailToolButton *playToolButton; + QWinThumbnailToolButton *forwardToolButton; + QWinThumbnailToolButton *backwardToolButton; + + QMediaPlayer mediaPlayer; + QAbstractButton *playButton; + VolumeButton *volumeButton; + QSlider *positionSlider; + QLabel *positionLabel; + QLabel *infoLabel; + QPoint offset; +}; + +#endif // MUSICPLAYER_H diff --git a/examples/winextras/musicplayer/musicplayer.pro b/examples/winextras/musicplayer/musicplayer.pro new file mode 100644 index 0000000..73968cf --- /dev/null +++ b/examples/winextras/musicplayer/musicplayer.pro @@ -0,0 +1,21 @@ +TEMPLATE = app +TARGET = musicplayer + +QT += widgets multimedia winextras + +HEADERS = \ + musicplayer.h \ + volumebutton.h + +SOURCES = \ + main.cpp \ + musicplayer.cpp \ + volumebutton.cpp + +RESOURCES += \ + musicplayer.qrc + +RC_ICONS = images/musicplayer.ico + +target.path = $$[QT_INSTALL_EXAMPLES]/winextras/musicplayer +INSTALLS += target diff --git a/examples/winextras/musicplayer/musicplayer.qrc b/examples/winextras/musicplayer/musicplayer.qrc new file mode 100644 index 0000000..b1a0029 --- /dev/null +++ b/examples/winextras/musicplayer/musicplayer.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource> + <file alias="musicplayer.png">images/musicplayer.png</file> +</qresource> +</RCC> diff --git a/examples/winextras/musicplayer/musicplayer.rc b/examples/winextras/musicplayer/musicplayer.rc new file mode 100644 index 0000000..00a51ee --- /dev/null +++ b/examples/winextras/musicplayer/musicplayer.rc @@ -0,0 +1 @@ +IDI_ICON1 ICON DISCARDABLE "images\\musicplayer.ico" diff --git a/examples/winextras/musicplayer/volumebutton.cpp b/examples/winextras/musicplayer/volumebutton.cpp new file mode 100644 index 0000000..5fe940c --- /dev/null +++ b/examples/winextras/musicplayer/volumebutton.cpp @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "volumebutton.h" + +#include <QtWidgets> +#include <QtWinExtras> + +VolumeButton::VolumeButton(QWidget *parent) : + QToolButton(parent), menu(0), label(0), slider(0) +{ + setIcon(style()->standardIcon(QStyle::SP_MediaVolume)); + setPopupMode(QToolButton::InstantPopup); + + QWidget *popup = new QWidget(this); + + slider = new QSlider(Qt::Horizontal, popup); + slider->setRange(0, 100); + connect(slider, SIGNAL(valueChanged(int)), this, SIGNAL(volumeChanged(int))); + + label = new QLabel(popup); + label->setAlignment(Qt::AlignCenter); + label->setNum(100); + label->setMinimumWidth(label->sizeHint().width()); + connect(slider, SIGNAL(valueChanged(int)), label, SLOT(setNum(int))); + + QBoxLayout *popupLayout = new QHBoxLayout(popup); + popupLayout->setMargin(2); + popupLayout->addWidget(slider); + popupLayout->addWidget(label); + + QWidgetAction *action = new QWidgetAction(this); + action->setDefaultWidget(popup); + + menu = new QMenu(this); + menu->addAction(action); + setMenu(menu); + + stylize(); +} + +void VolumeButton::increaseVolume() +{ + slider->triggerAction(QSlider::SliderPageStepAdd); +} + +void VolumeButton::descreaseVolume() +{ + slider->triggerAction(QSlider::SliderPageStepSub); +} + +int VolumeButton::volume() const +{ + return slider->value(); +} + +void VolumeButton::setVolume(int volume) +{ + slider->setValue(volume); +} + +//! [0] +void VolumeButton::stylize() +{ + if (QtWin::isCompositionEnabled()) { + QtWin::enableBlurBehindWindow(menu); + QString css("QMenu { border: 1px solid %1; border-radius: 2px; background: transparent; }"); + menu->setStyleSheet(css.arg(QtWin::realColorizationColor().name())); + } else { + QtWin::disableBlurBehindWindow(menu); + QString css("QMenu { border: 1px solid black; background: %1; }"); + menu->setStyleSheet(css.arg(QtWin::realColorizationColor().name())); + } +} +//! [0] diff --git a/examples/winextras/musicplayer/volumebutton.h b/examples/winextras/musicplayer/volumebutton.h new file mode 100644 index 0000000..4267273 --- /dev/null +++ b/examples/winextras/musicplayer/volumebutton.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef VOLUMEBUTTON_H +#define VOLUMEBUTTON_H + +#include <QToolButton> + +QT_FORWARD_DECLARE_CLASS(QMenu) +QT_FORWARD_DECLARE_CLASS(QLabel) +QT_FORWARD_DECLARE_CLASS(QSlider) + +class VolumeButton : public QToolButton +{ + Q_OBJECT + Q_PROPERTY(int volume READ volume WRITE setVolume NOTIFY volumeChanged) + +public: + VolumeButton(QWidget *parent = 0); + + int volume() const; + +public slots: + void increaseVolume(); + void descreaseVolume(); + void setVolume(int volume); + void stylize(); + +signals: + void volumeChanged(int volume); + +private: + QMenu *menu; + QLabel *label; + QSlider *slider; +}; + +#endif // VOLUMEBUTTON_H diff --git a/examples/winextras/winextras.pro b/examples/winextras/winextras.pro index faa2909..17d9cf3 100644 --- a/examples/winextras/winextras.pro +++ b/examples/winextras/winextras.pro @@ -5,4 +5,6 @@ win32:{ dwmfeatures \ jumplistexample EXAMPLE_FILES += quickdwm + + qtHaveModule(widgets):qtHaveModule(multimedia): SUBDIRS += musicplayer } |