add more spacing
[personal-kdebase.git] / runtime / phonon / tests / guitest / mygraphicsscene.cpp
blobf714f823ccb22e5d88b1fdd39ef8af4a7a45cfac
1 /* This file is part of the KDE project
2 Copyright (C) 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 "mygraphicsscene.h"
22 #include "mediaobjectitem.h"
23 #include "redrectitem.h"
24 #include "sinkitem.h"
25 #include "pathitem.h"
26 #include "effectitem.h"
28 #include <QtGui/QGraphicsProxyWidget>
29 #include <QtGui/QMenu>
31 #include <Phonon/BackendCapabilities>
33 #include <kdebug.h>
35 using Phonon::Path;
37 void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
39 if (m_lineItem) {
40 QLineF line = m_lineItem->line();
41 m_lineItem->setLine(QLineF(line.p1(), mouseEvent->scenePos()));
43 QGraphicsScene::mouseMoveEvent(mouseEvent);
46 static inline MediaObjectItem *mediaObjectItem(WidgetRectItem *item)
48 if (item && item->childItems().size() == 1) {
49 QGraphicsProxyWidget *proxy = qgraphicsitem_cast<QGraphicsProxyWidget *>(
50 item->childItems().first());
51 return qobject_cast<MediaObjectItem *>(proxy->widget());
53 return 0;
56 static inline EffectItem *effectItem(WidgetRectItem *item)
58 if (item && item->childItems().size() == 1) {
59 QGraphicsProxyWidget *proxy = qgraphicsitem_cast<QGraphicsProxyWidget *>(
60 item->childItems().first());
61 return qobject_cast<EffectItem *>(proxy->widget());
63 return 0;
66 static inline SinkItem *sinkItem(WidgetRectItem *item)
68 if (item && item->childItems().size() == 1) {
69 QGraphicsProxyWidget *proxy = qgraphicsitem_cast<QGraphicsProxyWidget *>(
70 item->childItems().first());
71 return qobject_cast<SinkItem *>(proxy->widget());
73 return 0;
76 //X static inline AudioOutputItem *audioOutputItem(WidgetRectItem *item)
77 //X {
78 //X if (item && item->childItems().size() == 1) {
79 //X QGraphicsProxyWidget *proxy = qgraphicsitem_cast<QGraphicsProxyWidget *>(
80 //X item->childItems().first());
81 //X return qobject_cast<AudioOutputItem *>(proxy->widget());
82 //X }
83 //X return 0;
84 //X }
85 //X
86 //X static inline VideoWidgetItem *videoWidgetItem(WidgetRectItem *item)
87 //X {
88 //X if (item && item->childItems().size() == 1) {
89 //X QGraphicsProxyWidget *proxy = qgraphicsitem_cast<QGraphicsProxyWidget *>(
90 //X item->childItems().first());
91 //X return qobject_cast<VideoWidgetItem *>(proxy->widget());
92 //X }
93 //X return 0;
94 //X }
96 void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
98 kDebug() << mouseEvent->button() << mouseEvent->scenePos();
99 if (mouseEvent->button() == Qt::LeftButton) {
100 QGraphicsItem *itemUnderMouse = itemAt(mouseEvent->scenePos());
101 PathItem *pathItem = qgraphicsitem_cast<PathItem *>(itemUnderMouse);
102 if (!pathItem && !itemUnderMouse) {
103 const QPointF offset(12.0, 12.0);
104 QList<QGraphicsItem *> startItems = items(QRectF(mouseEvent->scenePos() - offset, mouseEvent->scenePos() + offset));
105 if (startItems.isEmpty()) {
106 const QPointF offset(25.0, 25.0);
107 startItems = items(QRectF(mouseEvent->scenePos() - offset, mouseEvent->scenePos() + offset));
109 if (startItems.size() == 1) {
110 m_startItem = qgraphicsitem_cast<WidgetRectItem *>(startItems.first());
111 if (mediaObjectItem(m_startItem) || effectItem(m_startItem)) {
112 m_lineItem = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(), mouseEvent->scenePos()));
113 addItem(m_lineItem);
114 return;
116 pathItem = qgraphicsitem_cast<PathItem *>(startItems.first());
118 if (!pathItem) {
119 addItem(new RedRectItem(mouseEvent->scenePos()));
120 return;
123 if (pathItem) {
124 // allow to disconnect or add an effect
125 QMenu popupMenu;
126 QAction *disconnectAction = popupMenu.addAction("disconnect");
127 popupMenu.addSeparator();
129 //QMenu *effectMenu = popupMenu.addMenu("add Effect");
130 QList<EffectDescription> effectList = Phonon::BackendCapabilities::availableAudioEffects();
131 QHash<QAction *, EffectDescription> actionHash;
132 foreach (const EffectDescription &d, effectList) {
133 QAction *subAction = popupMenu.addAction("add " + d.name());
134 actionHash.insert(subAction, d);
137 QAction *triggeredAction = popupMenu.exec(mouseEvent->screenPos());
138 if (triggeredAction) {
139 kDebug() << "popupMenu exec triggered action" << triggeredAction->metaObject()->className();
140 if (actionHash.contains(triggeredAction)) {
141 EffectDescription d = actionHash[triggeredAction];
142 Q_ASSERT(m_view);
143 new EffectItem(d, pathItem);
144 } else {
145 // disconnect
146 Q_ASSERT(triggeredAction == disconnectAction); Q_UNUSED(disconnectAction);
147 pathItem->path().disconnect();
148 delete pathItem;
150 return;
151 } else {
152 kDebug() << "popupMenu no action";
156 QGraphicsScene::mousePressEvent(mouseEvent);
159 void MyGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
161 if (m_lineItem && mouseEvent->button() == Qt::LeftButton) {
162 delete m_lineItem;
163 m_lineItem = 0;
165 QList<QGraphicsItem *> endItems = items(mouseEvent->scenePos());
166 if (endItems.isEmpty()) {
167 const QPointF offset(12.0, 12.0);
168 endItems = items(QRectF(mouseEvent->scenePos() - offset, mouseEvent->scenePos() + offset));
169 if (endItems.isEmpty()) {
170 const QPointF offset(25.0, 25.0);
171 endItems = items(QRectF(mouseEvent->scenePos() - offset, mouseEvent->scenePos() + offset));
174 if (endItems.size() == 1 && endItems.first() != m_startItem) {
175 WidgetRectItem *endItem = qgraphicsitem_cast<WidgetRectItem *>(endItems.first());
176 MediaNode *sourceNode = 0;
177 MediaNode *sinkNode = 0;
179 MediaObjectItem *source = mediaObjectItem(m_startItem);
180 if (source) {
181 sourceNode = source->mediaNode();
182 } else {
183 EffectItem *source= effectItem(m_startItem);
184 if (source) {
185 sourceNode = source->mediaNode();
188 if (sourceNode) {
189 SinkItem *sink = sinkItem(endItem);
190 if (sink) {
191 sinkNode = sink->mediaNode();
192 } else {
193 EffectItem *sink = effectItem(endItem);
194 if (sink) {
195 sinkNode = sink->mediaNode();
198 if (sinkNode) {
199 Path p = Phonon::createPath(sourceNode, sinkNode);
200 if (p.isValid()) {
201 addItem(new PathItem(m_startItem, endItem, p));
202 m_startItem = 0;
203 return;
208 m_startItem = 0;
209 addItem(new RedRectItem(mouseEvent->scenePos()));
210 return;
212 QGraphicsScene::mouseReleaseEvent(mouseEvent);
215 #include "moc_mygraphicsscene.cpp"
216 #include "moc_redrectitem.cpp"