]> git.sesse.net Git - kdenlive/blob - src/trackpanelclipmovefunction.cpp
bd7167f0cf59e831dbb472bb6ed5c1aee22ac1b2
[kdenlive] / src / trackpanelclipmovefunction.cpp
1 /***************************************************************************
2                        TrackPanelClipMoveFunction.cpp  -  description
3                           -------------------
4  begin                : Sun May 18 2003
5  copyright            : (C) 2003 by Jason Wood
6  email                : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <QMouseEvent>
19 #include <KDebug>
20
21 #include "trackpanelclipmovefunction.h"
22 #include "definitions.h"
23 #include "documenttrack.h"
24 #include "trackview.h"
25
26
27 TrackPanelClipMoveFunction::TrackPanelClipMoveFunction(TrackView *view):
28 m_view(view), m_dragging(false), m_startedClipMove(false), m_masterClip(0), m_clipOffset(0)
29 {
30     //  m_moveClipsCommand = 0;
31     //m_deleteClipsCommand = 0;
32     m_addingClips = false;
33     m_firststep = true;
34 }
35
36
37 TrackPanelClipMoveFunction::~TrackPanelClipMoveFunction()
38 {
39 }
40
41 bool TrackPanelClipMoveFunction::mouseApplies(DocumentTrack *,
42     QMouseEvent * event) const
43 {
44     return mouseApplies(event->pos());
45 }
46
47 bool TrackPanelClipMoveFunction::mouseApplies(const QPoint & pos) const
48 {
49     TrackViewClip *clipUnderMouse = 0;
50
51     DocumentTrack * panel = m_view->panelAt(pos.y());
52
53     if (panel) {
54         kDebug()<<" TESTINGĀ CURSOR AT: "<<pos.x()<<", MAPPEDĀ VAL: "<<m_view->mapLocalToValue(pos.x());
55         GenTime mouseTime((int)(m_view->mapLocalToValue(pos.x())), m_document->fps());
56         clipUnderMouse = panel->getClipAt(mouseTime);
57     }
58     return clipUnderMouse;
59 }
60
61 QCursor TrackPanelClipMoveFunction::getMouseCursor(DocumentTrack *, QMouseEvent * event)
62 {
63     return QCursor(Qt::SizeAllCursor);
64 }
65
66 bool TrackPanelClipMoveFunction::mousePressed(DocumentTrack * panel,
67     QMouseEvent * event)
68 {
69     bool result = false;
70 /* 
71    if (panel->hasDocumentTrackIndex()) {
72         DocTrackBase *track =
73             m_document->track(panel->documentTrackIndex());
74         if (track) {
75                 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
76                 m_document->framesPerSecond());
77             m_clipUnderMouse = 0;
78             m_clipUnderMouse = track->getClipAt(mouseTime);
79
80             if (m_clipUnderMouse) {
81                 emit checkTransition(m_clipUnderMouse);
82                 if (event->state() & Qt::ControlButton) {
83                     m_app->addCommand(Command::KSelectClipCommand::toggleSelectClipAt(m_document, *track, mouseTime), true);
84                 }
85                 else {
86                     if (!track->clipSelected(m_clipUnderMouse)) {
87                         KMacroCommand *macroCommand = new KMacroCommand(i18n("Select Clip"));
88                         macroCommand->addCommand(Command::KSelectClipCommand::selectNone(m_document));
89                         macroCommand->addCommand(new Command::KSelectClipCommand(m_document, m_clipUnderMouse, true));
90                         m_app->addCommand(macroCommand, true);
91                     }
92                 }
93                 result = true;
94             }
95         }
96     }
97 */
98     return result;
99 }
100
101 bool TrackPanelClipMoveFunction::mouseDoubleClicked(DocumentTrack * panel, QMouseEvent *event)
102 {
103 /*
104         if (panel->hasDocumentTrackIndex()) {
105                 DocTrackBase *track =
106                     m_document->track(panel->documentTrackIndex());
107                 if (track) {
108                         GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
109                 m_document->framesPerSecond());
110                         m_clipUnderMouse = track->getClipAt(mouseTime);
111                         if (m_clipUnderMouse) {
112                                 track->openClip(m_clipUnderMouse);
113                         }
114                 }
115         }*/
116         return false; ///FIXME is that right ?
117 }
118
119 bool TrackPanelClipMoveFunction::mouseReleased(DocumentTrack *, QMouseEvent *)
120 {
121     //m_timeline->stopScrollTimer();
122     return true;
123 }
124
125 bool TrackPanelClipMoveFunction::mouseMoved(DocumentTrack * panel,
126     QMouseEvent * event)
127 {
128     bool result = false;
129     /*if (panel->hasDocumentTrackIndex()) {
130         DocTrackBase *track =
131             m_document->track(panel->documentTrackIndex());
132         if (track) {
133                 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
134                 m_document->framesPerSecond());
135
136             if (m_dragging) {
137                 m_dragging = false;
138                 result = true;
139             } else {
140                 if (m_clipUnderMouse) {
141                     if (!m_document->projectClip().clipSelected(m_clipUnderMouse)) {
142                         if ((event->state() & Qt::ControlButton)
143                             || (event->state() & Qt::ShiftButton)) {
144                             m_app->
145                                 addCommand(Command::KSelectClipCommand::
146                                 selectClipAt(m_document, *track,
147                                     mouseTime), true);
148                         } else {
149                             KMacroCommand *macroCommand = new KMacroCommand(i18n("Select Clip"));
150                             macroCommand->addCommand(Command::KSelectClipCommand::selectNone(m_document));
151                             macroCommand->addCommand(new Command::KSelectClipCommand(m_document, m_clipUnderMouse, true));
152                             m_app->addCommand(macroCommand, true);
153                         }
154                     }
155                     m_dragging = true;
156                     initiateDrag(m_clipUnderMouse, mouseTime);
157                     result = true;
158                 }
159             }
160         }
161     }
162 */
163     return result;
164 }
165
166 // virtual
167 bool TrackPanelClipMoveFunction::dragEntered(DocumentTrack * panel,
168     QDragEnterEvent * event)
169 {
170 /*    if (m_startedClipMove) {
171         m_document->activateSceneListGeneration(false);
172         event->accept(true);
173     } else if (ClipDrag::canDecode(event)) {
174         m_document->activateSceneListGeneration(false);
175         m_selection = ClipDrag::decode(m_document, event);
176
177
178         if (!m_selection.isEmpty()) {
179             if (m_selection.masterClip() == 0)
180                 m_selection.setMasterClip(m_selection.first());
181             m_masterClip = m_selection.masterClip();
182             m_clipOffset = GenTime();
183             if (m_selection.isEmpty()) {
184                 event->accept(false);
185             } else {
186                 setupSnapToGrid();
187                 event->accept(true);
188             }
189         } else {
190             kdError() <<
191                 "ERROR! ERROR! ERROR! ClipDrag:decode decoded a null clip!!!"
192                 << endl;
193         }
194     } else if (EffectDrag::canDecode(event)) {
195         event->accept(true);
196     } else {
197         event->accept(false);
198     }
199     //m_startedClipMove = false;
200 */
201     return true;
202 }
203
204 // virtual
205 bool TrackPanelClipMoveFunction::dragMoved(DocumentTrack *, QDragMoveEvent * event)
206 {
207 /*    QPoint pos = event->pos();
208     if (ClipDrag::canDecode(event)) {
209         GenTime mouseTime = m_timeline->timeUnderMouse((double) pos.x()) - m_clipOffset;
210         mouseTime = m_snapToGrid.getSnappedTime(mouseTime);
211         mouseTime = mouseTime + m_clipOffset;
212         int trackUnder = trackUnderPoint(pos);
213
214         if (m_selection.isEmpty() || m_dragging) {
215                 moveSelectedClips(trackUnder, mouseTime - m_clipOffset);
216         } else {
217             if (m_document->projectClip().canAddClipsToTracks(m_selection,
218                     trackUnder, mouseTime)) {
219                 m_selection_to_add = m_selection;
220                 addClipsToTracks(m_selection, trackUnder, mouseTime, true);
221                 setupSnapToGrid();
222                 m_selection.clear();
223                 m_dragging = true;
224             }
225         }
226     } else if (EffectDrag::canDecode(event)) {
227         if (mouseApplies(pos)) {
228             event->accept();
229         } else {
230             event->ignore();
231         }
232     } else {
233         event->ignore();
234     }
235     m_timeline->checkScrolling(pos);
236 */
237     return true;
238 }
239
240 int TrackPanelClipMoveFunction::trackUnderPoint(const QPoint & pos)
241 {
242     uint y = pos.y();
243     DocumentTrack * panel = m_view->panelAt(y);
244 /*
245     if (panel) {
246         return panel->documentTrackIndex();
247     }*/
248
249     return -1;
250 }
251
252 // virtual
253 bool TrackPanelClipMoveFunction::dragLeft(DocumentTrack *, QDragLeaveEvent *)
254 {
255     m_dragging = false;
256 /*
257     if (!m_selection.isEmpty()) {
258         m_selection.setAutoDelete(true);
259         m_selection.clear();
260         m_selection.setAutoDelete(false);
261     }
262     
263     if (m_addingClips) {
264         m_addingClips = false;
265
266         QPtrListIterator < DocTrackBase >
267             trackItt(m_document->trackList());
268
269         while (trackItt.current()) {
270             (*trackItt)->deleteClips(true);
271             ++trackItt;
272         }
273
274         m_document->activateSceneListGeneration(true);
275     }
276
277     if (m_moveClipsCommand) {
278         m_moveClipsCommand->setEndLocation(m_masterClip);
279         m_app->addCommand(m_moveClipsCommand, false);
280         // In a drag Leave Event, any clips in the selection are removed from the timeline.
281         //delete m_moveClipsCommand;
282         m_moveClipsCommand = 0;
283         m_document->activateSceneListGeneration(true);
284     }
285
286     if (m_deleteClipsCommand) {
287         m_app->addCommand(m_deleteClipsCommand, false);
288         m_deleteClipsCommand = 0;
289
290         QPtrListIterator < DocTrackBase >
291             trackItt(m_document->trackList());
292
293         while (trackItt.current()) {
294             trackItt.current()->deleteClips(true);
295             ++trackItt;
296         }
297     }
298
299     m_timeline->drawTrackViewBackBuffer();
300     m_timeline->stopScrollTimer();
301 */
302     return true;
303 }
304
305 // virtual
306 bool TrackPanelClipMoveFunction::dragDropped(DocumentTrack * panel,
307     QDropEvent * event)
308 {
309 /*
310     m_dragging = false;
311     m_startedClipMove = false;
312     if (ClipDrag::canDecode(event)) {
313         if (!m_selection.isEmpty()) {
314             m_selection.setAutoDelete(true);
315             m_selection.clear();
316             m_selection.setAutoDelete(false);
317         }
318
319         if (m_addingClips) {
320         
321             m_app->addCommand(createAddClipsCommand(), true);
322             m_addingClips = false;
323             m_app->clipReferenceChanged();
324
325             //if (m_firststep) m_document->activateSceneListGeneration(true);
326             m_firststep = false;
327         }
328
329         if (m_deleteClipsCommand) {
330             delete m_deleteClipsCommand;
331             m_deleteClipsCommand = 0;
332         }
333
334         if (m_moveClipsCommand) {
335             m_moveClipsCommand->setEndLocation(m_masterClip);
336             if (!m_moveClipsCommand->doesMove())
337             {
338                 //m_document->activateSceneListGeneration(true);
339                 moveSelectedClips(m_moveClipsCommand->startTrack(), m_moveClipsCommand->startTime());
340                 m_app->addCommand(m_moveClipsCommand, true);
341                 m_moveClipsCommand = 0; 
342                 m_document->slotUpdateMonitorPlaytime();
343                 // KdenliveApp is now managing this command, we do not need to delete it.
344             }
345             else {
346                 m_document->activateSceneListGeneration(true, false);
347                 delete m_moveClipsCommand;
348                 m_moveClipsCommand = 0;
349             }
350         }
351         event->accept();
352     } else if (EffectDrag::canDecode(event)) {
353         DocClipRef *clipUnderMouse = 0;
354         DocumentTrack * panel =
355             m_view->panelAt(event->pos().y());
356         if (panel) {
357             DocTrackBase *track =
358                 m_document->track(panel->documentTrackIndex());
359             if (track) {
360                          GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->pos().
361                         x())), m_document->framesPerSecond());
362                 clipUnderMouse = track->getClipAt(mouseTime);
363             }
364         }
365
366         if (clipUnderMouse) {
367             Effect *effect = EffectDrag::decode(m_document, event);
368             if (effect) {
369                 m_app->
370                     addCommand(Command::KAddEffectCommand::
371                     appendEffect(m_document, clipUnderMouse, effect),
372                     true);
373                     if (effect->name() == i18n("Freeze")) m_app->getDocument()->emitCurrentClipPosition();
374             } else {
375                 kdWarning() <<
376                     "EffectDrag::decode did not return an effect, ignoring drag drop..."
377                     << endl;
378             }
379             delete effect;
380         }
381     }
382     m_timeline->stopScrollTimer();
383     m_timeline->drawTrackViewBackBuffer();
384 */
385     return true;
386 }
387
388 bool TrackPanelClipMoveFunction::moveSelectedClips(int newTrack,
389     GenTime start)
390 {
391 /*
392     if (!m_masterClip) return false;
393     int trackOffset =
394         m_document->trackIndex(m_document->findTrack(m_masterClip));
395     GenTime startOffset;
396
397     if (trackOffset == -1) {
398         kdError() <<
399             "Trying to move selected clips, master clip is not set." <<
400             endl;
401         return false;
402     } else {
403         startOffset = m_masterClip->trackStart();
404     }
405
406     trackOffset = newTrack - trackOffset;
407     startOffset = start - startOffset;
408     if (startOffset == GenTime()) return false;
409     m_document->moveSelectedClips(startOffset, trackOffset);
410 */
411     return true;
412 }
413
414 /*
415 void TrackPanelClipMoveFunction::addClipsToTracks(DocClipRefList & clips,
416     int track, GenTime value, bool selected)
417 {
418
419     if (clips.isEmpty())
420         return;
421
422     if (selected) {
423         m_app->
424             addCommand(Command::KSelectClipCommand::selectNone(m_document),
425             true);
426     }
427
428     DocClipRef *masterClip = clips.masterClip();
429     if (!masterClip)
430         masterClip = clips.first();
431
432     GenTime startOffset = value - masterClip->trackStart();
433
434     int trackOffset = masterClip->trackNum();
435
436     if (trackOffset == -1)
437         trackOffset = 0;
438     trackOffset = track - trackOffset;
439
440     QPtrListIterator < DocClipRef > itt(clips);
441     int moveToTrack;
442
443     while (itt.current() != 0) {
444         moveToTrack = itt.current()->trackNum();
445
446         if (moveToTrack == -1) {
447             moveToTrack = track;
448             itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
449             startOffset += itt.current()->cropDuration();
450         } else {
451             moveToTrack += trackOffset;
452             itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
453         }
454
455
456
457         if ((moveToTrack >= 0) && (moveToTrack < (int)m_document->numTracks())) {
458             //if (itt.current()->referencedClip()->numReferences() == 0)
459             m_document->track(moveToTrack)->addClip(itt.current(), selected);
460         }
461
462         ++itt;
463     }
464
465     m_addingClips = true;
466 }
467 */
468
469 void TrackPanelClipMoveFunction::setupSnapToGrid()
470 {
471 /*
472     m_snapToGrid.clearSnapList();
473     if (m_timeline->snapToSeekTime())
474         m_snapToGrid.addToSnapList(m_timeline->seekPosition());
475     m_snapToGrid.setSnapToFrame(m_timeline->snapToFrame());
476
477     m_snapToGrid.addToSnapList(m_document->getSnapTimes(m_timeline->
478             snapToBorders(), m_timeline->snapToMarkers(), true, false));
479
480     QValueVector < GenTime > cursor =
481         m_document->getSnapTimes(m_timeline->snapToBorders(),
482         m_timeline->snapToMarkers(), false, true, false);
483     m_snapToGrid.setCursorTimes(cursor);
484
485          m_snapToGrid.setSnapTolerance(GenTime((int)(m_timeline->
486             mapLocalToValue(Gui::KTimeLine::snapTolerance) -
487                          m_timeline->mapLocalToValue(0)),
488             m_document->framesPerSecond()));
489 */
490 }
491
492 /*void TrackPanelClipMoveFunction::initiateDrag(DocClipRef * clipUnderMouse,
493     GenTime mouseTime)
494 {
495     m_masterClip = clipUnderMouse;
496     m_clipOffset = mouseTime - clipUnderMouse->trackStart();
497
498     m_moveClipsCommand =
499         new Command::KMoveClipsCommand(m_document, m_masterClip);
500
501     m_moveClipsCommand->setClipList(m_document->listSelected());
502
503     m_deleteClipsCommand =
504         Command::KAddRefClipCommand::deleteSelectedClips(m_document);
505     setupSnapToGrid();
506
507     m_startedClipMove = true;
508
509     DocClipRefList selection = m_document->listSelected();
510
511     selection.setMasterClip(m_masterClip);
512     ClipDrag *clip = new ClipDrag(selection, m_timeline, "Timeline Drag");
513
514     clip->dragCopy();
515 }
516
517 KMacroCommand *TrackPanelClipMoveFunction::createAddClipsCommand()
518 {
519     KMacroCommand *macroCommand = new KMacroCommand(i18n("Add Clips"));
520
521          for (int count = 0; count < (int)m_document->numTracks(); ++count) {
522         DocTrackBase *track = m_document->track(count);
523
524         QPtrListIterator < DocClipRef > itt = track->firstClip(true);
525
526         while (itt.current()) {
527             Command::KAddRefClipCommand * command =
528                 new Command::KAddRefClipCommand(*m_document, itt.current(), true);
529             macroCommand->addCommand(command);
530             ++itt;
531         }
532         m_document->generateProducersList();
533     }
534     return macroCommand;
535 }
536 */
537
538 #include "trackpanelclipmovefunction.moc"