1 /***************************************************************************
2 TrackPanelClipMoveFunction.cpp - description
4 begin : Sun May 18 2003
5 copyright : (C) 2003 by Jason Wood
6 email : jasonwood@blueyonder.co.uk
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include <QMouseEvent>
21 #include "trackpanelclipmovefunction.h"
22 #include "definitions.h"
23 #include "documenttrack.h"
24 #include "trackview.h"
27 TrackPanelClipMoveFunction::TrackPanelClipMoveFunction(TrackView *view):
28 m_view(view), m_dragging(false), m_startedClipMove(false), m_masterClip(0), m_clipOffset(0)
30 // m_moveClipsCommand = 0;
31 //m_deleteClipsCommand = 0;
32 m_addingClips = false;
37 TrackPanelClipMoveFunction::~TrackPanelClipMoveFunction()
41 bool TrackPanelClipMoveFunction::mouseApplies(DocumentTrack *,
42 QMouseEvent * event) const
44 return mouseApplies(event->pos());
47 bool TrackPanelClipMoveFunction::mouseApplies(const QPoint & pos) const
49 TrackViewClip *clipUnderMouse = 0;
51 DocumentTrack * panel = m_view->panelAt(pos.y());
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);
58 return clipUnderMouse;
61 QCursor TrackPanelClipMoveFunction::getMouseCursor(DocumentTrack *, QMouseEvent * event)
63 return QCursor(Qt::SizeAllCursor);
66 bool TrackPanelClipMoveFunction::mousePressed(DocumentTrack * panel,
71 if (panel->hasDocumentTrackIndex()) {
73 m_document->track(panel->documentTrackIndex());
75 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
76 m_document->framesPerSecond());
78 m_clipUnderMouse = track->getClipAt(mouseTime);
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);
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);
101 bool TrackPanelClipMoveFunction::mouseDoubleClicked(DocumentTrack * panel, QMouseEvent *event)
104 if (panel->hasDocumentTrackIndex()) {
105 DocTrackBase *track =
106 m_document->track(panel->documentTrackIndex());
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);
116 return false; ///FIXME is that right ?
119 bool TrackPanelClipMoveFunction::mouseReleased(DocumentTrack *, QMouseEvent *)
121 //m_timeline->stopScrollTimer();
125 bool TrackPanelClipMoveFunction::mouseMoved(DocumentTrack * panel,
129 /*if (panel->hasDocumentTrackIndex()) {
130 DocTrackBase *track =
131 m_document->track(panel->documentTrackIndex());
133 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
134 m_document->framesPerSecond());
140 if (m_clipUnderMouse) {
141 if (!m_document->projectClip().clipSelected(m_clipUnderMouse)) {
142 if ((event->state() & Qt::ControlButton)
143 || (event->state() & Qt::ShiftButton)) {
145 addCommand(Command::KSelectClipCommand::
146 selectClipAt(m_document, *track,
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);
156 initiateDrag(m_clipUnderMouse, mouseTime);
167 bool TrackPanelClipMoveFunction::dragEntered(DocumentTrack * panel,
168 QDragEnterEvent * event)
170 /* if (m_startedClipMove) {
171 m_document->activateSceneListGeneration(false);
173 } else if (ClipDrag::canDecode(event)) {
174 m_document->activateSceneListGeneration(false);
175 m_selection = ClipDrag::decode(m_document, event);
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);
191 "ERROR! ERROR! ERROR! ClipDrag:decode decoded a null clip!!!"
194 } else if (EffectDrag::canDecode(event)) {
197 event->accept(false);
199 //m_startedClipMove = false;
205 bool TrackPanelClipMoveFunction::dragMoved(DocumentTrack *, QDragMoveEvent * event)
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);
214 if (m_selection.isEmpty() || m_dragging) {
215 moveSelectedClips(trackUnder, mouseTime - m_clipOffset);
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);
226 } else if (EffectDrag::canDecode(event)) {
227 if (mouseApplies(pos)) {
235 m_timeline->checkScrolling(pos);
240 int TrackPanelClipMoveFunction::trackUnderPoint(const QPoint & pos)
243 DocumentTrack * panel = m_view->panelAt(y);
246 return panel->documentTrackIndex();
253 bool TrackPanelClipMoveFunction::dragLeft(DocumentTrack *, QDragLeaveEvent *)
257 if (!m_selection.isEmpty()) {
258 m_selection.setAutoDelete(true);
260 m_selection.setAutoDelete(false);
264 m_addingClips = false;
266 QPtrListIterator < DocTrackBase >
267 trackItt(m_document->trackList());
269 while (trackItt.current()) {
270 (*trackItt)->deleteClips(true);
274 m_document->activateSceneListGeneration(true);
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);
286 if (m_deleteClipsCommand) {
287 m_app->addCommand(m_deleteClipsCommand, false);
288 m_deleteClipsCommand = 0;
290 QPtrListIterator < DocTrackBase >
291 trackItt(m_document->trackList());
293 while (trackItt.current()) {
294 trackItt.current()->deleteClips(true);
299 m_timeline->drawTrackViewBackBuffer();
300 m_timeline->stopScrollTimer();
306 bool TrackPanelClipMoveFunction::dragDropped(DocumentTrack * panel,
311 m_startedClipMove = false;
312 if (ClipDrag::canDecode(event)) {
313 if (!m_selection.isEmpty()) {
314 m_selection.setAutoDelete(true);
316 m_selection.setAutoDelete(false);
321 m_app->addCommand(createAddClipsCommand(), true);
322 m_addingClips = false;
323 m_app->clipReferenceChanged();
325 //if (m_firststep) m_document->activateSceneListGeneration(true);
329 if (m_deleteClipsCommand) {
330 delete m_deleteClipsCommand;
331 m_deleteClipsCommand = 0;
334 if (m_moveClipsCommand) {
335 m_moveClipsCommand->setEndLocation(m_masterClip);
336 if (!m_moveClipsCommand->doesMove())
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.
346 m_document->activateSceneListGeneration(true, false);
347 delete m_moveClipsCommand;
348 m_moveClipsCommand = 0;
352 } else if (EffectDrag::canDecode(event)) {
353 DocClipRef *clipUnderMouse = 0;
354 DocumentTrack * panel =
355 m_view->panelAt(event->pos().y());
357 DocTrackBase *track =
358 m_document->track(panel->documentTrackIndex());
360 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->pos().
361 x())), m_document->framesPerSecond());
362 clipUnderMouse = track->getClipAt(mouseTime);
366 if (clipUnderMouse) {
367 Effect *effect = EffectDrag::decode(m_document, event);
370 addCommand(Command::KAddEffectCommand::
371 appendEffect(m_document, clipUnderMouse, effect),
373 if (effect->name() == i18n("Freeze")) m_app->getDocument()->emitCurrentClipPosition();
376 "EffectDrag::decode did not return an effect, ignoring drag drop..."
382 m_timeline->stopScrollTimer();
383 m_timeline->drawTrackViewBackBuffer();
388 bool TrackPanelClipMoveFunction::moveSelectedClips(int newTrack,
392 if (!m_masterClip) return false;
394 m_document->trackIndex(m_document->findTrack(m_masterClip));
397 if (trackOffset == -1) {
399 "Trying to move selected clips, master clip is not set." <<
403 startOffset = m_masterClip->trackStart();
406 trackOffset = newTrack - trackOffset;
407 startOffset = start - startOffset;
408 if (startOffset == GenTime()) return false;
409 m_document->moveSelectedClips(startOffset, trackOffset);
415 void TrackPanelClipMoveFunction::addClipsToTracks(DocClipRefList & clips,
416 int track, GenTime value, bool selected)
424 addCommand(Command::KSelectClipCommand::selectNone(m_document),
428 DocClipRef *masterClip = clips.masterClip();
430 masterClip = clips.first();
432 GenTime startOffset = value - masterClip->trackStart();
434 int trackOffset = masterClip->trackNum();
436 if (trackOffset == -1)
438 trackOffset = track - trackOffset;
440 QPtrListIterator < DocClipRef > itt(clips);
443 while (itt.current() != 0) {
444 moveToTrack = itt.current()->trackNum();
446 if (moveToTrack == -1) {
448 itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
449 startOffset += itt.current()->cropDuration();
451 moveToTrack += trackOffset;
452 itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
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);
465 m_addingClips = true;
469 void TrackPanelClipMoveFunction::setupSnapToGrid()
472 m_snapToGrid.clearSnapList();
473 if (m_timeline->snapToSeekTime())
474 m_snapToGrid.addToSnapList(m_timeline->seekPosition());
475 m_snapToGrid.setSnapToFrame(m_timeline->snapToFrame());
477 m_snapToGrid.addToSnapList(m_document->getSnapTimes(m_timeline->
478 snapToBorders(), m_timeline->snapToMarkers(), true, false));
480 QValueVector < GenTime > cursor =
481 m_document->getSnapTimes(m_timeline->snapToBorders(),
482 m_timeline->snapToMarkers(), false, true, false);
483 m_snapToGrid.setCursorTimes(cursor);
485 m_snapToGrid.setSnapTolerance(GenTime((int)(m_timeline->
486 mapLocalToValue(Gui::KTimeLine::snapTolerance) -
487 m_timeline->mapLocalToValue(0)),
488 m_document->framesPerSecond()));
492 /*void TrackPanelClipMoveFunction::initiateDrag(DocClipRef * clipUnderMouse,
495 m_masterClip = clipUnderMouse;
496 m_clipOffset = mouseTime - clipUnderMouse->trackStart();
499 new Command::KMoveClipsCommand(m_document, m_masterClip);
501 m_moveClipsCommand->setClipList(m_document->listSelected());
503 m_deleteClipsCommand =
504 Command::KAddRefClipCommand::deleteSelectedClips(m_document);
507 m_startedClipMove = true;
509 DocClipRefList selection = m_document->listSelected();
511 selection.setMasterClip(m_masterClip);
512 ClipDrag *clip = new ClipDrag(selection, m_timeline, "Timeline Drag");
517 KMacroCommand *TrackPanelClipMoveFunction::createAddClipsCommand()
519 KMacroCommand *macroCommand = new KMacroCommand(i18n("Add Clips"));
521 for (int count = 0; count < (int)m_document->numTracks(); ++count) {
522 DocTrackBase *track = m_document->track(count);
524 QPtrListIterator < DocClipRef > itt = track->firstClip(true);
526 while (itt.current()) {
527 Command::KAddRefClipCommand * command =
528 new Command::KAddRefClipCommand(*m_document, itt.current(), true);
529 macroCommand->addCommand(command);
532 m_document->generateProducersList();
538 #include "trackpanelclipmovefunction.moc"