]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
improvements to move and resize clips
[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
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)
37 {
38   setMouseTracking(true);
39   setAcceptDrops(true);
40 }
41
42 void CustomTrackView::initView()
43 {
44   m_cursorLine = scene()->addLine(0, 0, 0, height());
45 }
46
47 // virtual
48 void CustomTrackView::resizeEvent ( QResizeEvent * event )
49 {
50   if (m_cursorLine) m_cursorLine->setLine(m_cursorLine->line().x1(), 0, m_cursorLine->line().x1(), height());
51 }
52
53 // virtual 
54 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
55 {
56   int pos = event->x();
57   if (event->modifiers() == Qt::ControlModifier)
58     setDragMode(QGraphicsView::ScrollHandDrag);
59   else if (event->modifiers() == Qt::ShiftModifier) 
60     setDragMode(QGraphicsView::RubberBandDrag);
61   else {
62     QList<QGraphicsItem *> itemList = items ( event->pos());
63     int i = 0;
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);
68         break;
69       }
70     }
71     if (item) {
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);
79
80     }
81     else {
82       setCursor(Qt::ArrowCursor);
83     }
84   }
85   QGraphicsView::mouseMoveEvent(event);
86 }
87
88 // virtual 
89 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
90 {
91   int pos = event->x();
92   if (event->modifiers() == Qt::ControlModifier) 
93     setDragMode(QGraphicsView::ScrollHandDrag);
94   else if (event->modifiers() == Qt::ShiftModifier) 
95     setDragMode(QGraphicsView::RubberBandDrag);
96   else {
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());
104
105      kDebug()<<"//////// ITEM CLICKED: "<<m_startPos;
106       /*while (item->parentItem()) 
107         item = item->parentItem();
108
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);
117     */}
118     else {
119       kDebug()<<"//////// NO ITEM FOUND ON CLICK";
120       m_dragItem = NULL;
121       setCursor(Qt::ArrowCursor);
122       emit cursorMoved((int) mapToScene(event->x(), 0).x());
123     }
124   }
125   //kDebug()<<pos;
126   QGraphicsView::mousePressEvent(event);
127 }
128
129 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
130 {
131   if (event->mimeData()->hasText()) {
132     QString clip = event->mimeData()->text();
133     addItem(clip, event->pos());
134     event->acceptProposedAction();
135   }
136 }
137
138
139 void CustomTrackView::addItem(QString producer, QPoint pos)
140 {
141   QDomDocument doc;
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);
153 }
154
155
156 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
157   event->setDropAction(Qt::IgnoreAction);
158   if (m_dropItem) {
159     int trackTop = ((int) mapToScene(event->pos()).y()/50) * 50 + 1;
160     m_dropItem->moveTo(mapToScene(event->pos()).x(), trackTop - m_dropItem->rect().y());
161   }
162         //if (item) {
163                 event->setDropAction(Qt::MoveAction);
164                 if (event->mimeData()->hasText()) {
165                         event->acceptProposedAction();
166                 }
167         //}
168 }
169
170 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
171   if (m_dropItem) delete m_dropItem;
172   m_dropItem = NULL;
173 }
174
175 void CustomTrackView::dropEvent ( QDropEvent * event ) {
176   m_dropItem = NULL;
177 }
178
179
180 QStringList CustomTrackView::mimeTypes () const
181 {
182     QStringList qstrList;
183     // list of accepted mime types for drop
184     qstrList.append("text/plain");
185     return qstrList;
186 }
187
188 Qt::DropActions CustomTrackView::supportedDropActions () const
189 {
190     // returns what actions are supported when dropping
191     return Qt::MoveAction;
192 }
193
194 void CustomTrackView::addTrack ()
195 {
196   m_tracksCount++;
197 }
198
199 void CustomTrackView::removeTrack ()
200 {
201   m_tracksCount--;
202 }
203
204 void CustomTrackView::setCursorPos(int pos)
205 {
206   m_cursorPos = pos;
207   m_cursorLine->setPos(pos, 0);
208 }
209
210 int CustomTrackView::cursorPos()
211 {
212   return m_cursorPos;
213 }
214
215 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
216 {
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) {
222     // move clip
223     MoveClipCommand *command = new MoveClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), false);
224     m_commandStack->push(command);
225   }
226   else if (m_operationMode == 1) {
227     // resize start
228     ResizeClipCommand *command = new ResizeClipCommand(this, m_startPos, QPointF(m_dragItem->rect().x(), m_dragItem->rect().y()), true, false);
229     m_commandStack->push(command);
230   }
231   else if (m_operationMode == 2) {
232     // resize end
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);
235   }
236 }
237
238 void CustomTrackView::moveClip ( const QPointF &startPos, const QPointF &endPos )
239 {
240   ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + 1, startPos.y() + 1);
241   if (!item) {
242     kDebug()<<"----------------  ERROR, CANNOT find clip to move at: "<<startPos;
243     return;
244   }
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());
249   }
250 }
251
252 void CustomTrackView::resizeClip ( const QPointF &startPos, const QPointF &endPos, bool resizeClipStart )
253 {
254   int offset;
255   if (resizeClipStart) offset = 1;
256   else offset = -1;
257   ClipItem *item = (ClipItem *) scene()->itemAt(startPos.x() + offset, startPos.y() + 1);
258   if (!item) {
259     kDebug()<<"----------------  ERROR, CANNOT find clip to resize at: "<<startPos;
260     return;
261   }
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());
268     }
269   }
270   else {
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());
276     }
277   }
278 }
279
280
281 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )  
282 {
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++)
286     {
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));
289     }
290 }
291 /*
292 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )  
293 {
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());
297 }
298 */
299 #include "customtrackview.moc"