1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <QMouseEvent>
21 #include <QStylePainter>
22 #include <QGraphicsItem>
23 #include <QDomDocument>
29 #include "customtrackview.h"
31 #include "definitions.h"
32 #include "moveclipcommand.h"
33 #include "resizeclipcommand.h"
34 #include "addtimelineclipcommand.h"
36 CustomTrackView::CustomTrackView(KUndoStack *commandStack, QGraphicsScene * scene, QWidget *parent)
37 : QGraphicsView(scene, parent), m_commandStack(commandStack), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL), m_cursorLine(NULL), m_operationMode(0), m_startPos(QPointF()), m_dragItem(NULL)
39 setMouseTracking(true);
43 void CustomTrackView::initView()
45 m_cursorLine = scene()->addLine(0, 0, 0, height());
49 void CustomTrackView::resizeEvent ( QResizeEvent * event )
51 if (m_cursorLine) m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), height());
55 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
58 if (event->modifiers() == Qt::ControlModifier)
59 setDragMode(QGraphicsView::ScrollHandDrag);
60 else if (event->modifiers() == Qt::ShiftModifier)
61 setDragMode(QGraphicsView::RubberBandDrag);
63 QList<QGraphicsItem *> itemList = items ( event->pos());
65 QGraphicsItem *item = NULL;
66 for (int i = 0; i < itemList.count(); i++) {
67 if (itemList.at(i)->type() == 70000) {
68 item = itemList.at(i);
73 int cursorPos = event->x();
74 QRectF itemRect = item->sceneBoundingRect();
75 int itemStart = mapFromScene(itemRect.x(), 0).x();
76 int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
77 if (abs(itemStart - cursorPos) < 6 || abs(itemEnd - cursorPos) < 6)
78 setCursor(Qt::SizeHorCursor);
79 else setCursor(Qt::OpenHandCursor);
83 setCursor(Qt::ArrowCursor);
86 QGraphicsView::mouseMoveEvent(event);
90 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
93 if (event->modifiers() == Qt::ControlModifier)
94 setDragMode(QGraphicsView::ScrollHandDrag);
95 else if (event->modifiers() == Qt::ShiftModifier)
96 setDragMode(QGraphicsView::RubberBandDrag);
98 QGraphicsItem * item = itemAt(event->pos());
99 if (item && item->type() != 70000) item = item->parentItem();
100 if (item && item->type() == 70000) {
101 m_dragItem = (ClipItem *) item;
102 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())));
103 if (m_operationMode != 2) m_startPos = QPointF(m_dragItem->rect().x(), m_dragItem->rect().y());
104 else m_startPos = QPointF(m_dragItem->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y());
106 kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
107 /*while (item->parentItem())
108 item = item->parentItem();
110 int cursorPos = event->x();
111 QRectF itemRect = item->sceneBoundingRect();
112 int itemStart = mapFromScene(itemRect.x(), 0).x();
113 int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
114 if (abs(itemStart - cursorPos) < 6)
115 ((ClipItem *) item )->setResizeMode(1);
116 else if (abs(itemEnd - cursorPos) < 6)
117 ((ClipItem *) item )->setResizeMode(2);
120 kDebug()<<"//////// NO ITEM FOUND ON CLICK";
122 setCursor(Qt::ArrowCursor);
123 emit cursorMoved((int) mapToScene(event->x(), 0).x());
127 QGraphicsView::mousePressEvent(event);
130 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
132 if (event->mimeData()->hasText()) {
133 QString clip = event->mimeData()->text();
134 addItem(clip, event->pos());
135 event->acceptProposedAction();
140 void CustomTrackView::addItem(QString producer, QPoint pos)
143 doc.setContent(producer);
144 QDomElement elem = doc.documentElement();
145 int in = elem.attribute("in", 0).toInt();
146 int out = elem.attribute("duration", 0).toInt();
147 if (out == 0) out = elem.attribute("out", 0).toInt() - in;
148 kDebug()<<"ADDING CLIP: "<<producer<<", OUT: "<<out<<", POS: "<<mapToScene(pos);
149 int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
150 QString clipName = elem.attribute("name");
151 if (clipName.isEmpty()) clipName = KUrl(elem.attribute("resource")).fileName();
152 m_dropItem = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("id").toInt(), QRectF(mapToScene(pos).x(), trackTop, out, 49));
153 scene()->addItem(m_dropItem);
157 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
158 event->setDropAction(Qt::IgnoreAction);
160 int trackTop = ((int) mapToScene(event->pos()).y()/50) * 50 + 1;
161 m_dropItem->moveTo(mapToScene(event->pos()).x(), trackTop - m_dropItem->rect().y());
164 event->setDropAction(Qt::MoveAction);
165 if (event->mimeData()->hasText()) {
166 event->acceptProposedAction();
171 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
178 void CustomTrackView::dropEvent ( QDropEvent * event ) {
180 AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->clipType(), m_dropItem->clipName(), m_dropItem->clipProducer(), m_dropItem->rect(), false);
181 m_commandStack->push(command);
187 QStringList CustomTrackView::mimeTypes () const
189 QStringList qstrList;
190 // list of accepted mime types for drop
191 qstrList.append("text/plain");
195 Qt::DropActions CustomTrackView::supportedDropActions () const
197 // returns what actions are supported when dropping
198 return Qt::MoveAction;
201 void CustomTrackView::addTrack ()
206 void CustomTrackView::removeTrack ()
211 void CustomTrackView::setCursorPos(int pos)
214 m_cursorLine->setPos(pos, 0);
217 int CustomTrackView::cursorPos()
222 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
224 QGraphicsView::mouseReleaseEvent(event);
225 setDragMode(QGraphicsView::NoDrag);
226 if (m_dragItem == NULL) return;
227 //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
228 if (m_operationMode == 0) {
230 MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), false);
231 m_commandStack->push(command);
233 else if (m_operationMode == 1) {
235 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), true, false);
236 m_commandStack->push(command);
238 else if (m_operationMode == 2) {
240 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y()), false, false);
241 m_commandStack->push(command);
245 void CustomTrackView::deleteClip ( const QRectF &rect )
247 ClipItem *item = (ClipItem *) scene()->itemAt(rect.x() + 1, rect.y() + 1);
249 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<rect.x();
255 void CustomTrackView::addClip ( int clipType, QString clipName, int clipProducer, const QRectF &rect )
257 ClipItem *item = new ClipItem(clipType, clipName, clipProducer, rect);
258 scene()->addItem(item);
261 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
263 ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + 1, startPos.y() + 1);
265 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<startPos;
268 item->setRect(QRectF(endPos.x(), endPos.y(), item->rect().width(), item->rect().height()));
269 QList <QGraphicsItem *> childrenList = item->children();
270 for (int i = 0; i < childrenList.size(); ++i) {
271 childrenList.at(i)->moveBy(endPos.x() - startPos.x() , endPos.y() - startPos.y());
275 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
278 if (resizeClipStart) offset = 1;
280 ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + offset, startPos.y() + 1);
282 kDebug()<<"---------------- ERROR, CANNOT find clip to resize at: "<<startPos;
285 qreal diff = endPos.x() - startPos.x();
286 if (resizeClipStart) {
287 item->setRect(QRectF(endPos.x(), endPos.y(), item->rect().width() - diff, item->rect().height()));
288 QList <QGraphicsItem *> childrenList = item->children();
289 for (int i = 0; i < childrenList.size(); ++i) {
290 childrenList.at(i)->moveBy(diff / 2 , endPos.y() - startPos.y());
294 //kdDebug()<<"/////// RESIZE CLIP END: "<<item->rect().x()<<", "<<item->rect().width()<<", "<<startPos<<", "<<endPos;
295 item->setRect(QRectF(item->rect().x(), item->rect().y(), endPos.x() - item->rect().x(), item->rect().height()));
296 QList <QGraphicsItem *> childrenList = item->children();
297 for (int i = 0; i < childrenList.size(); ++i) {
298 childrenList.at(i)->moveBy(-diff/2, endPos.y() - startPos.y());
304 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )
306 //kDebug()<<"///// DRAWING BG: "<<rect.x()<<", width: "<<rect.width();
307 painter->drawLine(rect.x(), 0, rect.x() + rect.width(), 0);
308 for (uint i = 0; i < m_tracksCount;i++)
310 painter->drawLine(rect.x(), 50 * (i+1), rect.x() + rect.width(), 50 * (i+1));
311 painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i));
315 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
317 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
318 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
319 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
322 #include "customtrackview.moc"