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"
26 #include "effectitem.h"
28 #include <QtGui/QGraphicsProxyWidget>
29 #include <QtGui/QMenu>
31 #include <Phonon/BackendCapabilities>
37 void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent
*mouseEvent
)
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());
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());
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());
76 //X static inline AudioOutputItem *audioOutputItem(WidgetRectItem *item)
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());
86 //X static inline VideoWidgetItem *videoWidgetItem(WidgetRectItem *item)
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());
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()));
116 pathItem
= qgraphicsitem_cast
<PathItem
*>(startItems
.first());
119 addItem(new RedRectItem(mouseEvent
->scenePos()));
124 // allow to disconnect or add an effect
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
];
143 new EffectItem(d
, pathItem
);
146 Q_ASSERT(triggeredAction
== disconnectAction
); Q_UNUSED(disconnectAction
);
147 pathItem
->path().disconnect();
152 kDebug() << "popupMenu no action";
156 QGraphicsScene::mousePressEvent(mouseEvent
);
159 void MyGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent
*mouseEvent
)
161 if (m_lineItem
&& mouseEvent
->button() == Qt::LeftButton
) {
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
);
181 sourceNode
= source
->mediaNode();
183 EffectItem
*source
= effectItem(m_startItem
);
185 sourceNode
= source
->mediaNode();
189 SinkItem
*sink
= sinkItem(endItem
);
191 sinkNode
= sink
->mediaNode();
193 EffectItem
*sink
= effectItem(endItem
);
195 sinkNode
= sink
->mediaNode();
199 Path p
= Phonon::createPath(sourceNode
, sinkNode
);
201 addItem(new PathItem(m_startItem
, endItem
, p
));
209 addItem(new RedRectItem(mouseEvent
->scenePos()));
212 QGraphicsScene::mouseReleaseEvent(mouseEvent
);
215 #include "moc_mygraphicsscene.cpp"
216 #include "moc_redrectitem.cpp"