1 /* This file is part of the KDE project
2 Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include <QtCore/QPair>
21 #include <QtOpenGL/QGLWidget>
22 #include <QtCore/QSignalMapper>
23 #include <QtGui/QAction>
24 #include <QtGui/QGraphicsProxyWidget>
25 #include <QtGui/QGraphicsView>
26 #include <QtGui/QMainWindow>
27 #include <QtGui/QMenu>
28 #include "mediaobjectitem.h"
29 #include "mygraphicsscene.h"
30 #include <kaboutdata.h>
31 #include <kapplication.h>
32 #include <kcmdlineargs.h>
34 #include "audiooutputitem.h"
35 #include "videowidgetitem.h"
37 #include "effectitem.h"
38 #include <Phonon/BackendCapabilities>
40 class MainWindow
: public QMainWindow
48 void addMediaObject();
50 void addAudioOutput();
51 void addVideoWidget();
54 QGraphicsView
*m_view
;
55 MyGraphicsScene
*m_scene
;
59 PathWidget::PathWidget(QWidget *parent)
62 setFrameShape(QFrame::Box);
63 setFrameShadow(QFrame::Raised);
65 QVBoxLayout *layout = new QVBoxLayout(this);
67 m_effectComboBox = new QComboBox(this);
68 layout->addWidget(m_effectComboBox);
69 QList<EffectDescription> effectList = BackendCapabilities::availableAudioEffects();
70 m_effectComboBox->setModel(new AudioEffectDescriptionModel(effectList, m_effectComboBox));
72 QPushButton *addButton = new QPushButton(this);
73 layout->addWidget(addButton);
74 addButton->setText("add effect");
75 connect(addButton, SIGNAL(clicked()), SLOT(addEffect()));
77 QPushButton *button = new QPushButton(this);
78 layout->addWidget(button);
79 button->setText("add VolumeFader");
80 connect(button, SIGNAL(clicked()), SLOT(addVolumeFader()));
83 void PathWidget::addEffect()
85 int current = m_effectComboBox->currentIndex();
89 QList<EffectDescription> effectList = BackendCapabilities::availableAudioEffects();
90 if (current < effectList.size()) {
91 Effect *effect = m_path.insertEffect(effectList[current]);
92 QGroupBox *gb = new QGroupBox(effectList[current].name(), this);
93 layout()->addWidget(gb);
95 gb->setCheckable(true);
97 (new QHBoxLayout(gb))->addWidget(new EffectWidget(effect, gb));
98 gb->setProperty("AudioEffect", QVariant::fromValue(static_cast<QObject *>(effect)));
99 connect(gb, SIGNAL(toggled(bool)), SLOT(effectToggled(bool)));
103 void PathWidget::effectToggled(bool checked)
108 QVariant v = sender()->property("AudioEffect");
112 QObject *effect = v.value<QObject *>();
117 sender()->deleteLater();
120 void PathWidget::addVolumeFader()
122 VolumeFaderEffect *effect = new VolumeFaderEffect(this);
123 QGroupBox *gb = new QGroupBox("VolumeFader", this);
124 layout()->addWidget(gb);
126 gb->setCheckable(true);
127 gb->setChecked(true);
128 (new QHBoxLayout(gb))->addWidget(new EffectWidget(effect, gb));
129 m_path.insertEffect(effect);
130 gb->setProperty("AudioEffect", QVariant::fromValue(static_cast<QObject *>(effect)));
131 connect(gb, SIGNAL(toggled(bool)), SLOT(effectToggled(bool)));
134 bool PathWidget::connectOutput(OutputWidget *w)
136 if (m_sink && m_path.isValid()) {
139 m_sink = w->output();
141 m_path = createPath(m_source, m_sink);
142 return m_path.isValid();
147 bool PathWidget::connectInput(MediaObject *m)
153 MainWindow::MainWindow()
155 m_scene
= new MyGraphicsScene(this);
156 m_view
= new QGraphicsView(m_scene
);
157 m_view
->setViewport(new QGLWidget
);
158 m_scene
->setView(m_view
);
159 m_view
->setRenderHints(QPainter::Antialiasing
);
160 setCentralWidget(m_view
);
162 action
= new QAction(i18n("add MediaObject"), m_view
);
163 connect(action
, SIGNAL(triggered()), SLOT(addMediaObject()));
164 m_view
->addAction(action
);
166 action
= new QAction(i18n("add Effect"), m_view
);
167 QMenu
*menu
= new QMenu("Title", m_view
);
168 QList
<EffectDescription
> effectList
= Phonon::BackendCapabilities::availableAudioEffects();
169 QSignalMapper
*mapper
= new QSignalMapper(menu
);
170 connect(mapper
, SIGNAL(mapped(int)), SLOT(addEffect(int)));
171 foreach (const EffectDescription
&d
, effectList
) {
172 QAction
*subAction
= menu
->addAction(d
.name(), mapper
, SLOT(map()));
173 mapper
->setMapping(subAction
, d
.index());
175 action
->setMenu(menu
);
176 m_view
->addAction(action
);
178 action
= new QAction(i18n("add AudioOutput"), m_view
);
179 connect(action
, SIGNAL(triggered()), SLOT(addAudioOutput()));
180 m_view
->addAction(action
);
181 action
= new QAction(i18n("add VideoWidget"), m_view
);
182 connect(action
, SIGNAL(triggered()), SLOT(addVideoWidget()));
183 m_view
->addAction(action
);
184 m_view
->setContextMenuPolicy(Qt::ActionsContextMenu
);
188 QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection
);
191 typedef QPair
<MediaObjectItem
*, WidgetRectItem
*> MediaItemPair
;
192 typedef QPair
<AudioOutputItem
*, WidgetRectItem
*> AudioItemPair
;
194 static MediaItemPair
addMediaObject(const QPoint
&pos
, QGraphicsView
*view
, QGraphicsScene
*scene
)
196 WidgetRectItem
*rect
= new WidgetRectItem(view
->mapToScene(pos
),
197 QColor(255, 100, 100, 150), QLatin1String("Media Object"));
198 scene
->addItem(rect
);
199 QGraphicsProxyWidget
*proxy
= new QGraphicsProxyWidget(rect
);
200 MediaObjectItem
*m
= new MediaObjectItem
;
202 proxy
->setPos(16.0, 17.0);
203 return MediaItemPair(m
, rect
);
206 static AudioItemPair
addAudioOutput(const QPoint
&pos
, QGraphicsView
*view
, QGraphicsScene
*scene
)
208 WidgetRectItem
*rect
= new WidgetRectItem(view
->mapToScene(pos
),
209 QColor(100, 255, 100, 150), QLatin1String("Audio Output"));
210 scene
->addItem(rect
);
211 QGraphicsProxyWidget
*proxy
= new QGraphicsProxyWidget(rect
);
212 AudioOutputItem
*a
= new AudioOutputItem
;
214 proxy
->setPos(16.0, 17.0);
215 return AudioItemPair(a
, rect
);
218 void MainWindow::init()
220 MediaItemPair source
= ::addMediaObject(QPoint(200, 50), m_view
, m_scene
);
221 AudioItemPair sink
= ::addAudioOutput(QPoint(700, 350), m_view
, m_scene
);
222 Phonon::Path p
= Phonon::createPath(source
.first
->mediaNode(), sink
.first
->mediaNode());
224 m_scene
->addItem(new PathItem(source
.second
, sink
.second
, p
));
228 void MainWindow::addMediaObject()
230 ::addMediaObject(QCursor::pos(), m_view
, m_scene
);
233 void MainWindow::addEffect(int effectIndex
)
235 const EffectDescription
&desc
= EffectDescription::fromIndex(effectIndex
);
236 QGraphicsRectItem
*rect
= new WidgetRectItem(m_view
->mapToScene(QCursor::pos()),
237 QColor(255, 200, 0, 150), desc
.name());
238 m_scene
->addItem(rect
);
239 QGraphicsProxyWidget
*proxy
= new QGraphicsProxyWidget(rect
);
240 proxy
->setWidget(new EffectItem(desc
));
241 proxy
->setPos(16.0, 17.0);
244 void MainWindow::addAudioOutput()
246 ::addAudioOutput(QCursor::pos(), m_view
, m_scene
);
249 void MainWindow::addVideoWidget()
251 QGraphicsRectItem
*rect
= new WidgetRectItem(m_view
->mapToScene(QCursor::pos()),
252 QColor(100, 100, 255, 150), "Video Widget");
253 m_scene
->addItem(rect
);
254 QGraphicsProxyWidget
*proxy
= new QGraphicsProxyWidget(rect
);
255 proxy
->setWidget(new VideoWidgetItem
);
256 proxy
->setPos(16.0, 17.0);
259 int main(int argc
, char **argv
)
261 KAboutData
about("phonontester", 0, ki18n("KDE Multimedia Test"),
262 "0.2", KLocalizedString(),
263 KAboutData::License_LGPL
);
264 about
.setProgramIconName("phonon");
265 about
.addAuthor(ki18n("Matthias Kretz"), KLocalizedString(), "kretz@kde.org");
266 KCmdLineArgs::init(argc
, argv
, &about
);