File indexing completed on 2024-04-21 04:43:12

0001 /*
0002     Copyright (C) 2011 Harald Sitter <sitter@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), Nokia Corporation
0010     (or its successors, if any) and the KDE Free Qt Foundation, which shall
0011     act as a proxy defined in Section 6 of version 3 of the license.
0012 
0013     This library is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016     Lesser General Public License for more details.
0017 
0018     You should have received a copy of the GNU Lesser General Public
0019     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020 */
0021 
0022 /****************************************************************************
0023 **
0024 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
0025 ** All rights reserved.
0026 ** Contact: Nokia Corporation (qt-info@nokia.com)
0027 **
0028 ** This file is part of the Qt Designer of the Qt Toolkit.
0029 **
0030 ** $QT_BEGIN_LICENSE:LGPL$
0031 ** Commercial Usage
0032 ** Licensees holding valid Qt Commercial licenses may use this file in
0033 ** accordance with the Qt Commercial License Agreement provided with the
0034 ** Software or, alternatively, in accordance with the terms contained in
0035 ** a written agreement between you and Nokia.
0036 **
0037 ** GNU Lesser General Public License Usage
0038 ** Alternatively, this file may be used under the terms of the GNU Lesser
0039 ** General Public License version 2.1 as published by the Free Software
0040 ** Foundation and appearing in the file LICENSE.LGPL included in the
0041 ** packaging of this file.  Please review the following information to
0042 ** ensure the GNU Lesser General Public License version 2.1 requirements
0043 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
0044 **
0045 ** In addition, as a special exception, Nokia gives you certain additional
0046 ** rights.  These rights are described in the Nokia Qt LGPL Exception
0047 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
0048 **
0049 ** GNU General Public License Usage
0050 ** Alternatively, this file may be used under the terms of the GNU
0051 ** General Public License version 3.0 as published by the Free Software
0052 ** Foundation and appearing in the file LICENSE.GPL included in the
0053 ** packaging of this file.  Please review the following information to
0054 ** ensure the GNU General Public License version 3.0 requirements will be
0055 ** met: http://www.gnu.org/copyleft/gpl.html.
0056 **
0057 ** If you have questions regarding the use of this file, please contact
0058 ** Nokia at qt-info@nokia.com.
0059 ** $QT_END_LICENSE$
0060 **
0061 ****************************************************************************/
0062 
0063 #include "videoplayertaskmenu.h"
0064 
0065 #include <QtDesigner/QDesignerFormWindowInterface>
0066 #include <QtDesigner/QDesignerFormWindowCursorInterface>
0067 #include <QtDesigner/QDesignerFormEditorInterface>
0068 #include <QtDesigner/QExtensionManager>
0069 
0070 #include <phonon/backendcapabilities.h>
0071 #include <phonon/mediaobject.h>
0072 #include <phonon/videoplayer.h>
0073 
0074 #include <QPlainTextEdit>
0075 #include <QDialogButtonBox>
0076 #include <QAction>
0077 #include <QVBoxLayout>
0078 #include <QFileDialog>
0079 #include <QMessageBox>
0080 
0081 // -----------------  MimeTypeDialog: Display mime types in scrollable text
0082 
0083 class MimeTypeDialog : public QDialog {
0084     Q_DISABLE_COPY(MimeTypeDialog)
0085 public:
0086     explicit MimeTypeDialog(QWidget *parent = nullptr);
0087 
0088     void setMimeTypes(const QStringList &);
0089 
0090 private:
0091     QPlainTextEdit *m_plainTextEdit;
0092 };
0093 
0094 MimeTypeDialog::MimeTypeDialog(QWidget *parent) :
0095     QDialog(parent),
0096     m_plainTextEdit(new QPlainTextEdit)
0097 {
0098     setModal(true);
0099     setWindowTitle(VideoPlayerTaskMenu::tr("Available Mime Types"));
0100     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
0101 
0102     QVBoxLayout *layout = new QVBoxLayout;
0103     m_plainTextEdit->setReadOnly(true);
0104     layout->addWidget(m_plainTextEdit);
0105 
0106     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
0107     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0108     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0109     layout->addWidget(buttonBox);
0110 
0111     setLayout(layout);
0112 }
0113 
0114 void MimeTypeDialog::setMimeTypes(const QStringList &l)
0115 {
0116     m_plainTextEdit->setPlainText(l.join(QString(1, QLatin1Char('\n'))));
0117 }
0118 
0119 // ----------------- VideoPlayerTaskMenu
0120 VideoPlayerTaskMenu::VideoPlayerTaskMenu(Phonon::VideoPlayer *object, QObject *parent) :
0121     QObject(parent),
0122     m_widget(object),
0123     m_displayMimeTypesAction(new QAction(tr("Display supported mime types..."), this)),
0124     m_loadAction(new QAction(tr("Load..."), this)),
0125     m_playAction(new QAction(tr("Play"), this)),
0126     m_pauseAction(new QAction(tr("Pause"), this)),
0127     m_stopAction(new QAction(tr("Stop"), this))
0128 {
0129     m_taskActions << m_displayMimeTypesAction << m_loadAction << m_playAction << m_pauseAction << m_stopAction;
0130 
0131     connect(m_widget->mediaObject(), SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(mediaObjectStateChanged(Phonon::State,Phonon::State)));
0132     connect(m_displayMimeTypesAction, SIGNAL(triggered()), this, SLOT(slotMimeTypes()));
0133     connect(m_loadAction, SIGNAL(triggered()), this, SLOT(slotLoad()));
0134     connect(m_playAction, SIGNAL(triggered()), object, SLOT(play()));
0135     connect(m_pauseAction, SIGNAL(triggered()), object, SLOT(pause()));
0136     connect(m_stopAction, SIGNAL(triggered()), object, SLOT(stop()));
0137 }
0138 
0139 QList<QAction*> VideoPlayerTaskMenu::taskActions() const
0140 {
0141     const bool isPlaying = m_widget->isPlaying();
0142     const bool isPaused = m_widget->isPlaying();
0143     m_loadAction->setEnabled(!isPlaying && !isPaused);
0144     m_playAction->setEnabled(!isPlaying);
0145     m_pauseAction->setEnabled(isPlaying);
0146     m_stopAction->setEnabled(isPlaying || isPaused);
0147     return m_taskActions;
0148 }
0149 
0150 void VideoPlayerTaskMenu::slotMimeTypes()
0151 {
0152     MimeTypeDialog mimeTypeDialog(m_widget->window());
0153     mimeTypeDialog.setMimeTypes(Phonon::BackendCapabilities::availableMimeTypes());
0154     mimeTypeDialog.exec();
0155 }
0156 
0157 void VideoPlayerTaskMenu::slotLoad()
0158 {
0159     const QUrl fileName = QUrl::fromLocalFile(QFileDialog::getOpenFileName(m_widget->window(), tr("Choose Video Player Media Source")));
0160     if (fileName.isEmpty())
0161         return;
0162     m_widget->load(Phonon::MediaSource(fileName));
0163 
0164 }
0165 
0166 void VideoPlayerTaskMenu::mediaObjectStateChanged(Phonon::State newstate, Phonon::State /* oldstate */)
0167 {
0168     if (newstate == Phonon::ErrorState) {
0169         const QString msg = tr("An error has occurred in '%1': %2").arg(m_widget->objectName(), m_widget->mediaObject()->errorString());
0170         QMessageBox::warning(m_widget->window(), tr("Video Player Error"), msg);
0171     }
0172 }
0173 
0174 // ----------------- VideoPlayerTaskMenuFactory
0175 VideoPlayerTaskMenuFactory::VideoPlayerTaskMenuFactory(QExtensionManager *parent) :
0176     QExtensionFactory(parent)
0177 {
0178 }
0179 
0180 QObject *VideoPlayerTaskMenuFactory::createExtension(QObject *object,
0181                          const QString &iid, QObject *parent) const
0182 {
0183     if (iid != Q_TYPEID(QDesignerTaskMenuExtension))
0184         return nullptr;
0185 
0186     if (Phonon::VideoPlayer *widget = qobject_cast<Phonon::VideoPlayer *>(object))
0187         return new VideoPlayerTaskMenu(widget, parent);
0188 
0189     return nullptr;
0190 }