1/*
2 Copyright (C) 2011 Harald Sitter <sitter@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), Nokia Corporation
10 (or its successors, if any) and the KDE Free Qt Foundation, which shall
11 act as a proxy defined in Section 6 of version 3 of the license.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22/****************************************************************************
23**
24** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
25** All rights reserved.
26** Contact: Nokia Corporation (qt-info@nokia.com)
27**
28** This file is part of the Qt Designer of the Qt Toolkit.
29**
30** $QT_BEGIN_LICENSE:LGPL$
31** Commercial Usage
32** Licensees holding valid Qt Commercial licenses may use this file in
33** accordance with the Qt Commercial License Agreement provided with the
34** Software or, alternatively, in accordance with the terms contained in
35** a written agreement between you and Nokia.
36**
37** GNU Lesser General Public License Usage
38** Alternatively, this file may be used under the terms of the GNU Lesser
39** General Public License version 2.1 as published by the Free Software
40** Foundation and appearing in the file LICENSE.LGPL included in the
41** packaging of this file. Please review the following information to
42** ensure the GNU Lesser General Public License version 2.1 requirements
43** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
44**
45** In addition, as a special exception, Nokia gives you certain additional
46** rights. These rights are described in the Nokia Qt LGPL Exception
47** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
48**
49** GNU General Public License Usage
50** Alternatively, this file may be used under the terms of the GNU
51** General Public License version 3.0 as published by the Free Software
52** Foundation and appearing in the file LICENSE.GPL included in the
53** packaging of this file. Please review the following information to
54** ensure the GNU General Public License version 3.0 requirements will be
55** met: http://www.gnu.org/copyleft/gpl.html.
56**
57** If you have questions regarding the use of this file, please contact
58** Nokia at qt-info@nokia.com.
59** $QT_END_LICENSE$
60**
61****************************************************************************/
62
63#include "videoplayertaskmenu.h"
64
65#include <QtDesigner/QDesignerFormWindowInterface>
66#include <QtDesigner/QDesignerFormWindowCursorInterface>
67#include <QtDesigner/QDesignerFormEditorInterface>
68#include <QtDesigner/QExtensionManager>
69
70#include <phonon/backendcapabilities.h>
71#include <phonon/mediaobject.h>
72#include <phonon/videoplayer.h>
73
74#include <QPlainTextEdit>
75#include <QDialogButtonBox>
76#include <QAction>
77#include <QVBoxLayout>
78#include <QFileDialog>
79#include <QMessageBox>
80
81// ----------------- MimeTypeDialog: Display mime types in scrollable text
82
83class MimeTypeDialog : public QDialog {
84 Q_DISABLE_COPY(MimeTypeDialog)
85public:
86 explicit MimeTypeDialog(QWidget *parent = 0);
87
88 void setMimeTypes(const QStringList &);
89
90private:
91 QPlainTextEdit *m_plainTextEdit;
92};
93
94MimeTypeDialog::MimeTypeDialog(QWidget *parent) :
95 QDialog(parent),
96 m_plainTextEdit(new QPlainTextEdit)
97{
98 setModal(true);
99 setWindowTitle(VideoPlayerTaskMenu::tr("Available Mime Types"));
100 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
101
102 QVBoxLayout *layout = new QVBoxLayout;
103 m_plainTextEdit->setReadOnly(true);
104 layout->addWidget(m_plainTextEdit);
105
106 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
107 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
108 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
109 layout->addWidget(buttonBox);
110
111 setLayout(layout);
112}
113
114void MimeTypeDialog::setMimeTypes(const QStringList &l)
115{
116 m_plainTextEdit->setPlainText(l.join(QString(1, QLatin1Char('\n'))));
117}
118
119// ----------------- VideoPlayerTaskMenu
120VideoPlayerTaskMenu::VideoPlayerTaskMenu(Phonon::VideoPlayer *object, QObject *parent) :
121 QObject(parent),
122 m_widget(object),
123 m_displayMimeTypesAction(new QAction(tr("Display supported mime types..."), this)),
124 m_loadAction(new QAction(tr("Load..."), this)),
125 m_playAction(new QAction(tr("Play"), this)),
126 m_pauseAction(new QAction(tr("Pause"), this)),
127 m_stopAction(new QAction(tr("Stop"), this))
128{
129 m_taskActions << m_displayMimeTypesAction << m_loadAction << m_playAction << m_pauseAction << m_stopAction;
130
131 connect(m_widget->mediaObject(), SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(mediaObjectStateChanged(Phonon::State,Phonon::State)));
132 connect(m_displayMimeTypesAction, SIGNAL(triggered()), this, SLOT(slotMimeTypes()));
133 connect(m_loadAction, SIGNAL(triggered()), this, SLOT(slotLoad()));
134 connect(m_playAction, SIGNAL(triggered()), object, SLOT(play()));
135 connect(m_pauseAction, SIGNAL(triggered()), object, SLOT(pause()));
136 connect(m_stopAction, SIGNAL(triggered()), object, SLOT(stop()));
137}
138
139QList<QAction*> VideoPlayerTaskMenu::taskActions() const
140{
141 const bool isPlaying = m_widget->isPlaying();
142 const bool isPaused = m_widget->isPlaying();
143 m_loadAction->setEnabled(!isPlaying && !isPaused);
144 m_playAction->setEnabled(!isPlaying);
145 m_pauseAction->setEnabled(isPlaying);
146 m_stopAction->setEnabled(isPlaying || isPaused);
147 return m_taskActions;
148}
149
150void VideoPlayerTaskMenu::slotMimeTypes()
151{
152 MimeTypeDialog mimeTypeDialog(m_widget->window());
153 mimeTypeDialog.setMimeTypes(Phonon::BackendCapabilities::availableMimeTypes());
154 mimeTypeDialog.exec();
155}
156
157void VideoPlayerTaskMenu::slotLoad()
158{
159 const QUrl fileName = QUrl::fromLocalFile(QFileDialog::getOpenFileName(m_widget->window(), tr("Choose Video Player Media Source")));
160 if (fileName.isEmpty())
161 return;
162 m_widget->load(Phonon::MediaSource(fileName));
163
164}
165
166void VideoPlayerTaskMenu::mediaObjectStateChanged(Phonon::State newstate, Phonon::State /* oldstate */)
167{
168 if (newstate == Phonon::ErrorState) {
169 const QString msg = tr("An error has occurred in '%1': %2").arg(m_widget->objectName(), m_widget->mediaObject()->errorString());
170 QMessageBox::warning(m_widget->window(), tr("Video Player Error"), msg);
171 }
172}
173
174// ----------------- VideoPlayerTaskMenuFactory
175VideoPlayerTaskMenuFactory::VideoPlayerTaskMenuFactory(QExtensionManager *parent) :
176 QExtensionFactory(parent)
177{
178}
179
180QObject *VideoPlayerTaskMenuFactory::createExtension(QObject *object,
181 const QString &iid, QObject *parent) const
182{
183 if (iid != Q_TYPEID(QDesignerTaskMenuExtension))
184 return 0;
185
186 if (Phonon::VideoPlayer *widget = qobject_cast<Phonon::VideoPlayer *>(object))
187 return new VideoPlayerTaskMenu(widget, parent);
188
189 return 0;
190}
191