]> git.sesse.net Git - kdenlive/blob - src/customtrackview.cpp
Start porting timeline to QGraphicsView
[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 <KUrl>
27
28 #include "customtrackview.h"
29 #include "clipitem.h"
30 #include "definitions.h"
31
32 CustomTrackView::CustomTrackView(QGraphicsScene * scene, QWidget *parent)
33     : QGraphicsView(scene, parent), m_tracksCount(0), m_cursorPos(0), m_dropItem(NULL)
34 {
35   setMouseTracking(true);
36   setAcceptDrops(true);
37 }
38
39 // virtual 
40 void CustomTrackView::mouseMoveEvent ( QMouseEvent * event )
41 {
42   int pos = event->x();
43   if (event->modifiers() == Qt::ControlModifier)
44     setDragMode(QGraphicsView::ScrollHandDrag);
45   else if (event->modifiers() == Qt::ShiftModifier) 
46     setDragMode(QGraphicsView::RubberBandDrag);
47   else {
48     QGraphicsItem * item = itemAt(event->pos());
49     if (item) {
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);
59
60     }
61     else {
62       setCursor(Qt::ArrowCursor);
63     }
64   }
65   QGraphicsView::mouseMoveEvent(event);
66 }
67
68 // virtual 
69 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
70 {
71   int pos = event->x();
72   if (event->modifiers() == Qt::ControlModifier) 
73     setDragMode(QGraphicsView::ScrollHandDrag);
74   else if (event->modifiers() == Qt::ShiftModifier) 
75     setDragMode(QGraphicsView::RubberBandDrag);
76   else {
77     QGraphicsItem * item = itemAt(event->pos());
78     if (item) {
79       /*while (item->parentItem()) 
80         item = item->parentItem();
81
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);
90     */}
91     else {
92       setCursor(Qt::ArrowCursor);
93       emit cursorMoved((int) mapToScene(event->x(), 0).x());
94     }
95   }
96   //kDebug()<<pos;
97   QGraphicsView::mousePressEvent(event);
98 }
99
100 void CustomTrackView::dragEnterEvent ( QDragEnterEvent * event )
101 {
102   if (event->mimeData()->hasText()) {
103     QString clip = event->mimeData()->text();
104     addItem(clip, event->pos());
105     event->acceptProposedAction();
106   }
107 }
108
109
110 void CustomTrackView::addItem(QString producer, QPoint pos)
111 {
112   QDomDocument doc;
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);
124 }
125
126
127 void CustomTrackView::dragMoveEvent(QDragMoveEvent * event) {
128   event->setDropAction(Qt::IgnoreAction);
129   if (m_dropItem) {
130     int trackTop = ((int) mapToScene(event->pos()).y()/50) * 50 + 1;
131     m_dropItem->moveTo(mapToScene(event->pos()).x(), trackTop - m_dropItem->rect().y());
132   }
133         //if (item) {
134                 event->setDropAction(Qt::MoveAction);
135                 if (event->mimeData()->hasText()) {
136                         event->acceptProposedAction();
137                 }
138         //}
139 }
140
141 void CustomTrackView::dragLeaveEvent ( QDragLeaveEvent * event ) {
142   if (m_dropItem) delete m_dropItem;
143   m_dropItem = NULL;
144 }
145
146 void CustomTrackView::dropEvent ( QDropEvent * event ) {
147   m_dropItem = NULL;
148 }
149
150
151 QStringList CustomTrackView::mimeTypes () const
152 {
153     QStringList qstrList;
154     // list of accepted mime types for drop
155     qstrList.append("text/plain");
156     return qstrList;
157 }
158
159 Qt::DropActions CustomTrackView::supportedDropActions () const
160 {
161     // returns what actions are supported when dropping
162     return Qt::MoveAction;
163 }
164
165 void CustomTrackView::addTrack ()
166 {
167   m_tracksCount++;
168 }
169
170 void CustomTrackView::removeTrack ()
171 {
172   m_tracksCount--;
173 }
174
175 void CustomTrackView::setCursorPos(int pos)
176 {
177   m_cursorPos = pos;
178 }
179
180 int CustomTrackView::cursorPos()
181 {
182   return m_cursorPos;
183 }
184
185 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
186 {
187   QGraphicsView::mouseReleaseEvent(event);
188   setDragMode(QGraphicsView::NoDrag);
189 }
190
191 void CustomTrackView::drawBackground ( QPainter * painter, const QRectF & rect )  
192 {
193   //kDebug()<<"/////  DRAWING BG: "<<rect.x()<<", width: "<<rect.width();
194   painter->drawRect(rect);
195     for (uint i = 0; i < m_tracksCount;i++)
196     {
197       painter->drawLine(rect.x(), 50 * i, rect.x() + rect.width(), 50 * i);
198     }
199 }
200
201 void CustomTrackView::drawForeground ( QPainter * painter, const QRectF & rect )  
202 {
203   //kDebug()<<"/////  DRAWING FB: "<<rect.x()<<", width: "<<rect.width();
204   painter->drawLine(m_cursorPos, rect.y(), m_cursorPos, rect.y() + rect.height());
205 }
206
207 #include "customtrackview.moc"