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>
28 #include "customtrackview.h"
30 #include "definitions.h"
32 CustomTrackView::CustomTrackView(QGraphicsScene * scene, QWidget *parent)
33 : QGraphicsView(scene, parent), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL)
35 setMouseTracking(true);
40 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
43 if (event->modifiers() == Qt::ControlModifier)
44 setDragMode(QGraphicsView::ScrollHandDrag);
45 else if (event->modifiers() == Qt::ShiftModifier)
46 setDragMode(QGraphicsView::RubberBandDrag);
48 QGraphicsItem * item = itemAt(event->pos());
50 while (item->parentItem())
51 item = item->parentItem();
52 int cursorPos = event->x();
53 QRectF itemRect = item->sceneBoundingRect();
54 int itemStart = mapFromScene(itemRect.x(), 0).x();
55 int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
56 if (abs(itemStart - cursorPos) < 6 || abs(itemEnd - cursorPos) < 6)
57 setCursor(Qt::SizeHorCursor);
58 else setCursor(Qt::OpenHandCursor);
62 setCursor(Qt::ArrowCursor);
65 QGraphicsView::mouseMoveEvent(event);
69 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
72 if (event->modifiers() == Qt::ControlModifier)
73 setDragMode(QGraphicsView::ScrollHandDrag);
74 else if (event->modifiers() == Qt::ShiftModifier)
75 setDragMode(QGraphicsView::RubberBandDrag);
77 QGraphicsItem * item = itemAt(event->pos());
79 /*while (item->parentItem())
80 item = item->parentItem();
82 int cursorPos = event->x();
83 QRectF itemRect = item->sceneBoundingRect();
84 int itemStart = mapFromScene(itemRect.x(), 0).x();
85 int itemEnd = mapFromScene(itemRect.x() + itemRect.width(), 0).x();
86 if (abs(itemStart - cursorPos) < 6)
87 ((ClipItem *) item )->setResizeMode(1);
88 else if (abs(itemEnd - cursorPos) < 6)
89 ((ClipItem *) item )->setResizeMode(2);
92 setCursor(Qt::ArrowCursor);
93 emit cursorMoved((int) mapToScene(event->x(), 0).x());
97 QGraphicsView::mousePressEvent(event);
100 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
102 if (event->mimeData()->hasText()) {
103 QString clip = event->mimeData()->text();
104 addItem(clip, event->pos());
105 event->acceptProposedAction();
110 void CustomTrackView::addItem(QString producer, QPoint pos)
113 doc.setContent(producer);
114 QDomElement elem = doc.documentElement();
115 int in = elem.attribute("in", 0).toInt();
116 int out = elem.attribute("duration", 0).toInt();
117 if (out == 0) out = elem.attribute("out", 0).toInt() - in;
118 kDebug()<<"ADDING CLIP: "<<producer<<", OUT: "<<out<<", POS: "<<mapToScene(pos);
119 int trackTop = ((int) mapToScene(pos).y()/50) * 50 + 1;
120 QString clipName = elem.attribute("name");
121 if (clipName.isEmpty()) clipName = KUrl(elem.attribute("resource")).fileName();
122 m_dropItem = new ClipItem(elem.attribute("type").toInt(), clipName, elem.attribute("id").toInt(), QRectF(mapToScene(pos).x(), trackTop, out, 49));
123 scene()->addItem(m_dropItem);
127 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
128 event->setDropAction(Qt::IgnoreAction);
130 int trackTop = ((int) mapToScene(event->pos()).y()/50) * 50 + 1;
131 m_dropItem->moveTo(mapToScene(event->pos()).x(), trackTop - m_dropItem->rect().y());
134 event->setDropAction(Qt::MoveAction);
135 if (event->mimeData()->hasText()) {
136 event->acceptProposedAction();
141 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
142 if (m_dropItem) delete m_dropItem;
146 void CustomTrackView::dropEvent ( QDropEvent * event ) {
151 QStringList CustomTrackView::mimeTypes () const
153 QStringList qstrList;
154 // list of accepted mime types for drop
155 qstrList.append("text/plain");
159 Qt::DropActions CustomTrackView::supportedDropActions () const
161 // returns what actions are supported when dropping
162 return Qt::MoveAction;
165 void CustomTrackView::addTrack ()
170 void CustomTrackView::removeTrack ()
175 void CustomTrackView::setCursorPos(int pos)
180 int CustomTrackView::cursorPos()
185 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
187 QGraphicsView::mouseReleaseEvent(event);
188 setDragMode(QGraphicsView::NoDrag);
191 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )
193 //kDebug()<<"///// DRAWING BG: "<<rect.x()<<", width: "<<rect.width();
194 painter->drawRect(rect);
195 for (uint i = 0; i < m_tracksCount;i++)
197 painter->drawLine(rect.x(), 50 * i, rect.x() + rect.width(), 50 * i);
201 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )
203 //kDebug()<<"///// DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
204 painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
207 #include "customtrackview.moc"