]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
clips now respect maximum length
[kdenlive] / src / customtrackview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include <QMouseEvent>
21 #include <QStylePainter>
22 #include <QGraphicsItem>
23 #include <QDomDocument>
24
25 #include <KDebug>
26 #include <KLocale>
27 #include <KUrl>
28
29 #include "customtrackview.h"
30 #include "clipitem.h"
31 #include "definitions.h"
32 #include "moveclipcommand.h"
33 #include "resizeclipcommand.h"
34 #include "addtimelineclipcommand.h"
35
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)
38 {
39   setMouseTracking(true);
40   setAcceptDrops(true);
41 }
42
43 void CustomTrackView::initView()
44 {
45   m_cursorLine = scene()->addLine(0, 0, 0, height());
46 }
47
48 // virtual
49 void CustomTrackView::resizeEvent ( QResizeEvent * event )
50 {
51   if (m_cursorLine) m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), height());
52 }
53
54 // virtual 
55 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
56 {
57   int pos = event->x();
58   if (event->modifiers() == Qt::ControlModifier)
59     setDragMode(QGraphicsView::ScrollHandDrag);
60   else if (event->modifiers() == Qt::ShiftModifier) 
61     setDragMode(QGraphicsView::RubberBandDrag);
62   else {
63     QList<QGraphicsItem *> itemList = items ( event->pos());
64     int i = 0;
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);
69         break;
70       }
71     }
72     if (item) {
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);
80
81     }
82     else {
83       setCursor(Qt::ArrowCursor);
84     }
85   }
86   QGraphicsView::mouseMoveEvent(event);
87 }
88
89 // virtual 
90 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
91 {
92   int pos = event->x();
93   if (event->modifiers() == Qt::ControlModifier) 
94     setDragMode(QGraphicsView::ScrollHandDrag);
95   else if (event->modifiers() == Qt::ShiftModifier) 
96     setDragMode(QGraphicsView::RubberBandDrag);
97   else {
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());
105
106      kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
107       /*while (item->parentItem()) 
108         item = item->parentItem();
109
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);
118     */}
119     else {
120       kDebug()<<"//////// NO ITEM FOUND ON CLICK";
121       m_dragItem = NULL;
122       setCursor(Qt::ArrowCursor);
123       emit cursorMoved((int) mapToScene(event->x(), 0).x());
124     }
125   }
126   //kDebug()<<pos;
127   QGraphicsView::mousePressEvent(event);
128 }
129
130 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
131 {
132   if (event->mimeData()->hasText()) {
133     QString clip = event->mimeData()->text();
134     addItem(clip, event->pos());
135     event->acceptProposedAction();
136   }
137 }
138
139
140 void CustomTrackView::addItem(QString producer, QPoint pos)
141 {
142   QDomDocument doc;
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(), out, QRectF(mapToScene(pos).x(), trackTop, out, 49));
153   scene()->addItem(m_dropItem);
154 }
155
156
157 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
158   event->setDropAction(Qt::IgnoreAction);
159   if (m_dropItem) {
160     int trackTop = ((int) mapToScene(event->pos()).y()/50) * 50 + 1;
161     m_dropItem->moveTo(mapToScene(event->pos()).x(), trackTop - m_dropItem->rect().y());
162   }
163         //if (item) {
164                 event->setDropAction(Qt::MoveAction);
165                 if (event->mimeData()->hasText()) {
166                         event->acceptProposedAction();
167                 }
168         //}
169 }
170
171 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
172   if (m_dropItem) {
173     delete m_dropItem;
174     m_dropItem = NULL;
175   }
176 }
177
178 void CustomTrackView::dropEvent ( QDropEvent * event ) {
179   if (m_dropItem) {
180     AddTimelineClipCommand *command = new AddTimelineClipCommand(this, m_dropItem->clipType(), m_dropItem->clipName(), m_dropItem->clipProducer(), m_dropItem->maxDuration(), m_dropItem->rect(), false);
181     m_commandStack->push(command);
182   }
183   m_dropItem = NULL;
184 }
185
186
187 QStringList CustomTrackView::mimeTypes () const
188 {
189     QStringList qstrList;
190     // list of accepted mime types for drop
191     qstrList.append("text/plain");
192     return qstrList;
193 }
194
195 Qt::DropActions CustomTrackView::supportedDropActions () const
196 {
197     // returns what actions are supported when dropping
198     return Qt::MoveAction;
199 }
200
201 void CustomTrackView::addTrack ()
202 {
203   m_tracksCount++;
204 }
205
206 void CustomTrackView::removeTrack ()
207 {
208   m_tracksCount--;
209 }
210
211 void CustomTrackView::setCursorPos(int pos)
212 {
213   m_cursorPos = pos;
214   m_cursorLine->setPos(pos, 0);
215 }
216
217 int CustomTrackView::cursorPos()
218 {
219   return m_cursorPos;
220 }
221
222 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
223 {
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) {
229     // move clip
230     MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), false);
231     m_commandStack->push(command);
232   }
233   else if (m_operationMode == 1) {
234     // resize start
235     ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), true, false);
236     m_commandStack->push(command);
237   }
238   else if (m_operationMode == 2) {
239     // resize end
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);
242   }
243 }
244
245 void CustomTrackView::deleteClip ( const QRectF &rect )
246 {
247   ClipItem *item = (ClipItem *) scene()->itemAt(rect.x() + 1, rect.y() + 1);
248   if (!item) {
249     kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<rect.x();
250     return;
251   }
252   delete item;
253 }
254
255 void CustomTrackView::addClip ( int clipType, QString clipName, int clipProducer, int maxDuration, const QRectF &rect )
256 {
257   ClipItem *item = new ClipItem(clipType, clipName, clipProducer, maxDuration, rect);
258   scene()->addItem(item);
259 }
260
261 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
262 {
263   ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + 1, startPos.y() + 1);
264   if (!item) {
265     kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<startPos;
266     return;
267   }
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());
272   }
273 }
274
275 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
276 {
277   int offset;
278   if (resizeClipStart) offset = 1;
279   else offset = -1;
280   ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + offset, startPos.y() + 1);
281   if (!item) {
282     kDebug()<<"----------------  ERROR, CANNOT find clip to resize at: "<<startPos;
283     return;
284   }
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());
291     }
292   }
293   else {
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());
299     }
300   }
301 }
302
303
304 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )  
305 {
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++)
309     {
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));
312     }
313 }
314 /*
315 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )  
316 {
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());
320 }
321 */
322 #include "customtrackview.moc"