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"
35 CustomTrackView::CustomTrackView(KUndoStack *commandStack, QGraphicsScene * scene, QWidget *parent)
36 : 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)
38 setMouseTracking(true);
42 void CustomTrackView::initView()
44 m_cursorLine = scene()->addLine(0, 0, 0, height());
48 void CustomTrackView::resizeEvent ( QResizeEvent * event )
50 if (m_cursorLine) m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), height());
54 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
57 if (event->modifiers() == Qt::ControlModifier)
58 setDragMode(QGraphicsView::ScrollHandDrag);
59 else if (event->modifiers() == Qt::ShiftModifier)
60 setDragMode(QGraphicsView::RubberBandDrag);
62 QList<QGraphicsItem *> itemList = items ( event->pos());
64 QGraphicsItem *item = NULL;
65 for (int i = 0; i < itemList.count(); i++) {
66 if (itemList.at(i)->type() == 70000) {
67 item = itemList.at(i);
72 int cursorPos = event->x();
73 QRectF itemRect = item->sceneBoundingRect();
74 int itemStart = mapFromScene(itemRect.x(), 0).x();
75 int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
76 if (abs(itemStart - cursorPos) < 6 || abs(itemEnd - cursorPos) < 6)
77 setCursor(Qt::SizeHorCursor);
78 else setCursor(Qt::OpenHandCursor);
82 setCursor(Qt::ArrowCursor);
85 QGraphicsView::mouseMoveEvent(event);
89 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
92 if (event->modifiers() == Qt::ControlModifier)
93 setDragMode(QGraphicsView::ScrollHandDrag);
94 else if (event->modifiers() == Qt::ShiftModifier)
95 setDragMode(QGraphicsView::RubberBandDrag);
97 QGraphicsItem * item = itemAt(event->pos());
98 if (item && item->type() != 70000) item = item->parentItem();
99 if (item && item->type() == 70000) {
100 m_dragItem = (ClipItem *) item;
101 m_operationMode = m_dragItem->operationMode(item->mapFromScene(mapToScene(event->pos())));
102 if (m_operationMode != 2) m_startPos = QPointF(m_dragItem->rect().x(), m_dragItem->rect().y());
103 else m_startPos = QPointF(m_dragItem->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y());
105 kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
106 /*while (item->parentItem())
107 item = item->parentItem();
109 int cursorPos = event->x();
110 QRectF itemRect = item->sceneBoundingRect();
111 int itemStart = mapFromScene(itemRect.x(), 0).x();
112 int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
113 if (abs(itemStart - cursorPos) < 6)
114 ((ClipItem *) item )->setResizeMode(1);
115 else if (abs(itemEnd - cursorPos) < 6)
116 ((ClipItem *) item )->setResizeMode(2);
119 kDebug()<<"//////// NO ITEM FOUND ON CLICK";
121 setCursor(Qt::ArrowCursor);
122 emit cursorMoved((int) mapToScene(event->x(), 0).x());
126 QGraphicsView::mousePressEvent(event);
129 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
131 if (event->mimeData()->hasText()) {
132 QString clip = event->mimeData()->text();
133 addItem(clip, event->pos());
134 event->acceptProposedAction();
139 void CustomTrackView::addItem(QString producer, QPoint pos)
142 doc.setContent(producer);
143 QDomElement elem = doc.documentElement();
144 int in = elem.attribute("in", 0).toInt();
145 int out = elem.attribute("duration", 0).toInt();
146 if (out == 0) out = elem.attribute("out", 0).toInt() - in;
147 kDebug()<<"ADDING CLIP: "<<producer<<", OUT: "<<out<<", POS: "<<mapToScene(pos);
148 int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
149 QString clipName = elem.attribute("name");
150 if (clipName.isEmpty()) clipName = KUrl(elem.attribute("resource")).fileName();
151 m_dropItem = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("id").toInt(), QRectF(mapToScene(pos).x(), trackTop, out, 49));
152 scene()->addItem(m_dropItem);
156 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
157 event->setDropAction(Qt::IgnoreAction);
159 int trackTop = ((int) mapToScene(event->pos()).y()/50) * 50 + 1;
160 m_dropItem->moveTo(mapToScene(event->pos()).x(), trackTop - m_dropItem->rect().y());
163 event->setDropAction(Qt::MoveAction);
164 if (event->mimeData()->hasText()) {
165 event->acceptProposedAction();
170 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
171 if (m_dropItem) delete m_dropItem;
175 void CustomTrackView::dropEvent ( QDropEvent * event ) {
180 QStringList CustomTrackView::mimeTypes () const
182 QStringList qstrList;
183 // list of accepted mime types for drop
184 qstrList.append("text/plain");
188 Qt::DropActions CustomTrackView::supportedDropActions () const
190 // returns what actions are supported when dropping
191 return Qt::MoveAction;
194 void CustomTrackView::addTrack ()
199 void CustomTrackView::removeTrack ()
204 void CustomTrackView::setCursorPos(int pos)
207 m_cursorLine->setPos(pos, 0);
210 int CustomTrackView::cursorPos()
215 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
217 QGraphicsView::mouseReleaseEvent(event);
218 setDragMode(QGraphicsView::NoDrag);
219 if (m_dragItem == NULL) return;
220 //kDebug()<<"/// MOVING CLIP: "<<m_startPos<<", END: "<<QPoint(m_dragItem->rect().x(),m_dragItem->rect().y());
221 if (m_operationMode == 0) {
223 MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), false);
224 m_commandStack->push(command);
226 else if (m_operationMode == 1) {
228 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), true, false);
229 m_commandStack->push(command);
231 else if (m_operationMode == 2) {
233 ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x() + m_dragItem->rect().width(), m_dragItem->rect().y()), false, false);
234 m_commandStack->push(command);
238 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
240 ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + 1, startPos.y() + 1);
242 kDebug()<<"---------------- ERROR, CANNOT find clip to move at: "<<startPos;
245 item->setRect(QRectF(endPos.x(), endPos.y(), item->rect().width(), item->rect().height()));
246 QList <QGraphicsItem *> childrenList = item->children();
247 for (int i = 0; i < childrenList.size(); ++i) {
248 childrenList.at(i)->moveBy(endPos.x() - startPos.x() , endPos.y() - startPos.y());
252 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
255 if (resizeClipStart) offset = 1;
257 ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + offset, startPos.y() + 1);
259 kDebug()<<"---------------- ERROR, CANNOT find clip to resize at: "<<startPos;
262 qreal diff = endPos.x() - startPos.x();
263 if (resizeClipStart) {
264 item->setRect(QRectF(endPos.x(), endPos.y(), item->rect().width() - diff, item->rect().height()));
265 QList <QGraphicsItem *> childrenList = item->children();
266 for (int i = 0; i < childrenList.size(); ++i) {
267 childrenList.at(i)->moveBy(diff / 2 , endPos.y() - startPos.y());
271 //kdDebug()<<"/////// RESIZE CLIP END: "<<item->rect().x()<<", "<<item->rect().width()<<", "<<startPos<<", "<<endPos;
272 item->setRect(QRectF(item->rect().x(), item->rect().y(), endPos.x() - item->rect().x(), item->rect().height()));
273 QList <QGraphicsItem *> childrenList = item->children();
274 for (int i = 0; i < childrenList.size(); ++i) {
275 childrenList.at(i)->moveBy(-diff/2, endPos.y() - startPos.y());
281 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )
283 //kDebug()<<"///// DRAWING BG: "<<rect.x()<<", width: "<<rect.width();
284 painter->drawLine(rect.x(), 0, rect.x() + rect.width(), 0);
285 for (uint i = 0; i < m_tracksCount;i++)
287 painter->drawLine(rect.x(), 50 * (i+1), rect.x() + rect.width(), 50 * (i+1));
288 painter->drawText(QRectF(10, 50 * i, 100, 50 * i + 49), Qt::AlignLeft, i18n(" Track ") + QString::number(i));
292 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
294 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
295 painter->fillRect(rect, QColor(50, rand() % 250,50,100));
296 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
299 #include "customtrackview.moc"