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) {
29 // m_moveClipsCommand = 0;
30 //m_deleteClipsCommand = 0;
31 m_addingClips = false;
36 TrackPanelClipMoveFunction::~TrackPanelClipMoveFunction() {
39 bool TrackPanelClipMoveFunction::mouseApplies(DocumentTrack *,
40 QMouseEvent * event) const {
41 return mouseApplies(event->pos());
44 bool TrackPanelClipMoveFunction::mouseApplies(const QPoint & pos) const {
45 TrackViewClip *clipUnderMouse = 0;
47 DocumentTrack * panel = m_view->panelAt(pos.y());
50 kDebug() << " TESTINGĀ CURSOR AT: " << pos.x() << ", MAPPEDĀ VAL: " << m_view->mapLocalToValue(pos.x());
51 GenTime mouseTime((int)(m_view->mapLocalToValue(pos.x())), m_document->fps());
52 clipUnderMouse = panel->getClipAt(mouseTime);
54 return clipUnderMouse;
57 QCursor TrackPanelClipMoveFunction::getMouseCursor(DocumentTrack *, QMouseEvent * event) {
58 return QCursor(Qt::SizeAllCursor);
61 bool TrackPanelClipMoveFunction::mousePressed(DocumentTrack * panel,
62 QMouseEvent * event) {
65 if (panel->hasDocumentTrackIndex()) {
67 m_document->track(panel->documentTrackIndex());
69 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
70 m_document->framesPerSecond());
72 m_clipUnderMouse = track->getClipAt(mouseTime);
74 if (m_clipUnderMouse) {
75 emit checkTransition(m_clipUnderMouse);
76 if (event->state() & Qt::ControlButton) {
77 m_app->addCommand(Command::KSelectClipCommand::toggleSelectClipAt(m_document, *track, mouseTime), true);
80 if (!track->clipSelected(m_clipUnderMouse)) {
81 KMacroCommand *macroCommand = new KMacroCommand(i18n("Select Clip"));
82 macroCommand->addCommand(Command::KSelectClipCommand::selectNone(m_document));
83 macroCommand->addCommand(new Command::KSelectClipCommand(m_document, m_clipUnderMouse, true));
84 m_app->addCommand(macroCommand, true);
95 bool TrackPanelClipMoveFunction::mouseDoubleClicked(DocumentTrack * panel, QMouseEvent *event) {
97 if (panel->hasDocumentTrackIndex()) {
99 m_document->track(panel->documentTrackIndex());
101 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
102 m_document->framesPerSecond());
103 m_clipUnderMouse = track->getClipAt(mouseTime);
104 if (m_clipUnderMouse) {
105 track->openClip(m_clipUnderMouse);
109 return false; ///FIXME is that right ?
112 bool TrackPanelClipMoveFunction::mouseReleased(DocumentTrack *, QMouseEvent *) {
113 //m_timeline->stopScrollTimer();
117 bool TrackPanelClipMoveFunction::mouseMoved(DocumentTrack * panel,
118 QMouseEvent * event) {
120 /*if (panel->hasDocumentTrackIndex()) {
121 DocTrackBase *track =
122 m_document->track(panel->documentTrackIndex());
124 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
125 m_document->framesPerSecond());
131 if (m_clipUnderMouse) {
132 if (!m_document->projectClip().clipSelected(m_clipUnderMouse)) {
133 if ((event->state() & Qt::ControlButton)
134 || (event->state() & Qt::ShiftButton)) {
136 addCommand(Command::KSelectClipCommand::
137 selectClipAt(m_document, *track,
140 KMacroCommand *macroCommand = new KMacroCommand(i18n("Select Clip"));
141 macroCommand->addCommand(Command::KSelectClipCommand::selectNone(m_document));
142 macroCommand->addCommand(new Command::KSelectClipCommand(m_document, m_clipUnderMouse, true));
143 m_app->addCommand(macroCommand, true);
147 initiateDrag(m_clipUnderMouse, mouseTime);
158 bool TrackPanelClipMoveFunction::dragEntered(DocumentTrack * panel,
159 QDragEnterEvent * event) {
160 /* if (m_startedClipMove) {
161 m_document->activateSceneListGeneration(false);
163 } else if (ClipDrag::canDecode(event)) {
164 m_document->activateSceneListGeneration(false);
165 m_selection = ClipDrag::decode(m_document, event);
168 if (!m_selection.isEmpty()) {
169 if (m_selection.masterClip() == 0)
170 m_selection.setMasterClip(m_selection.first());
171 m_masterClip = m_selection.masterClip();
172 m_clipOffset = GenTime();
173 if (m_selection.isEmpty()) {
174 event->accept(false);
181 "ERROR! ERROR! ERROR! ClipDrag:decode decoded a null clip!!!"
184 } else if (EffectDrag::canDecode(event)) {
187 event->accept(false);
189 //m_startedClipMove = false;
195 bool TrackPanelClipMoveFunction::dragMoved(DocumentTrack *, QDragMoveEvent * event) {
196 /* QPoint pos = event->pos();
197 if (ClipDrag::canDecode(event)) {
198 GenTime mouseTime = m_timeline->timeUnderMouse((double) pos.x()) - m_clipOffset;
199 mouseTime = m_snapToGrid.getSnappedTime(mouseTime);
200 mouseTime = mouseTime + m_clipOffset;
201 int trackUnder = trackUnderPoint(pos);
203 if (m_selection.isEmpty() || m_dragging) {
204 moveSelectedClips(trackUnder, mouseTime - m_clipOffset);
206 if (m_document->projectClip().canAddClipsToTracks(m_selection,
207 trackUnder, mouseTime)) {
208 m_selection_to_add = m_selection;
209 addClipsToTracks(m_selection, trackUnder, mouseTime, true);
215 } else if (EffectDrag::canDecode(event)) {
216 if (mouseApplies(pos)) {
224 m_timeline->checkScrolling(pos);
229 int TrackPanelClipMoveFunction::trackUnderPoint(const QPoint & pos) {
231 DocumentTrack * panel = m_view->panelAt(y);
234 return panel->documentTrackIndex();
241 bool TrackPanelClipMoveFunction::dragLeft(DocumentTrack *, QDragLeaveEvent *) {
244 if (!m_selection.isEmpty()) {
245 m_selection.setAutoDelete(true);
247 m_selection.setAutoDelete(false);
251 m_addingClips = false;
253 QPtrListIterator < DocTrackBase >
254 trackItt(m_document->trackList());
256 while (trackItt.current()) {
257 (*trackItt)->deleteClips(true);
261 m_document->activateSceneListGeneration(true);
264 if (m_moveClipsCommand) {
265 m_moveClipsCommand->setEndLocation(m_masterClip);
266 m_app->addCommand(m_moveClipsCommand, false);
267 // In a drag Leave Event, any clips in the selection are removed from the timeline.
268 //delete m_moveClipsCommand;
269 m_moveClipsCommand = 0;
270 m_document->activateSceneListGeneration(true);
273 if (m_deleteClipsCommand) {
274 m_app->addCommand(m_deleteClipsCommand, false);
275 m_deleteClipsCommand = 0;
277 QPtrListIterator < DocTrackBase >
278 trackItt(m_document->trackList());
280 while (trackItt.current()) {
281 trackItt.current()->deleteClips(true);
286 m_timeline->drawTrackViewBackBuffer();
287 m_timeline->stopScrollTimer();
293 bool TrackPanelClipMoveFunction::dragDropped(DocumentTrack * panel,
294 QDropEvent * event) {
297 m_startedClipMove = false;
298 if (ClipDrag::canDecode(event)) {
299 if (!m_selection.isEmpty()) {
300 m_selection.setAutoDelete(true);
302 m_selection.setAutoDelete(false);
307 m_app->addCommand(createAddClipsCommand(), true);
308 m_addingClips = false;
309 m_app->clipReferenceChanged();
311 //if (m_firststep) m_document->activateSceneListGeneration(true);
315 if (m_deleteClipsCommand) {
316 delete m_deleteClipsCommand;
317 m_deleteClipsCommand = 0;
320 if (m_moveClipsCommand) {
321 m_moveClipsCommand->setEndLocation(m_masterClip);
322 if (!m_moveClipsCommand->doesMove())
324 //m_document->activateSceneListGeneration(true);
325 moveSelectedClips(m_moveClipsCommand->startTrack(), m_moveClipsCommand->startTime());
326 m_app->addCommand(m_moveClipsCommand, true);
327 m_moveClipsCommand = 0;
328 m_document->slotUpdateMonitorPlaytime();
329 // KdenliveApp is now managing this command, we do not need to delete it.
332 m_document->activateSceneListGeneration(true, false);
333 delete m_moveClipsCommand;
334 m_moveClipsCommand = 0;
338 } else if (EffectDrag::canDecode(event)) {
339 DocClipRef *clipUnderMouse = 0;
340 DocumentTrack * panel =
341 m_view->panelAt(event->pos().y());
343 DocTrackBase *track =
344 m_document->track(panel->documentTrackIndex());
346 GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->pos().
347 x())), m_document->framesPerSecond());
348 clipUnderMouse = track->getClipAt(mouseTime);
352 if (clipUnderMouse) {
353 Effect *effect = EffectDrag::decode(m_document, event);
356 addCommand(Command::KAddEffectCommand::
357 appendEffect(m_document, clipUnderMouse, effect),
359 if (effect->name() == i18n("Freeze")) m_app->getDocument()->emitCurrentClipPosition();
362 "EffectDrag::decode did not return an effect, ignoring drag drop..."
368 m_timeline->stopScrollTimer();
369 m_timeline->drawTrackViewBackBuffer();
374 bool TrackPanelClipMoveFunction::moveSelectedClips(int newTrack,
377 if (!m_masterClip) return false;
379 m_document->trackIndex(m_document->findTrack(m_masterClip));
382 if (trackOffset == -1) {
384 "Trying to move selected clips, master clip is not set." <<
388 startOffset = m_masterClip->trackStart();
391 trackOffset = newTrack - trackOffset;
392 startOffset = start - startOffset;
393 if (startOffset == GenTime()) return false;
394 m_document->moveSelectedClips(startOffset, trackOffset);
400 void TrackPanelClipMoveFunction::addClipsToTracks(DocClipRefList & clips,
401 int track, GenTime value, bool selected)
409 addCommand(Command::KSelectClipCommand::selectNone(m_document),
413 DocClipRef *masterClip = clips.masterClip();
415 masterClip = clips.first();
417 GenTime startOffset = value - masterClip->trackStart();
419 int trackOffset = masterClip->trackNum();
421 if (trackOffset == -1)
423 trackOffset = track - trackOffset;
425 QPtrListIterator < DocClipRef > itt(clips);
428 while (itt.current() != 0) {
429 moveToTrack = itt.current()->trackNum();
431 if (moveToTrack == -1) {
433 itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
434 startOffset += itt.current()->cropDuration();
436 moveToTrack += trackOffset;
437 itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
442 if ((moveToTrack >= 0) && (moveToTrack < (int)m_document->numTracks())) {
443 //if (itt.current()->referencedClip()->numReferences() == 0)
444 m_document->track(moveToTrack)->addClip(itt.current(), selected);
450 m_addingClips = true;
454 void TrackPanelClipMoveFunction::setupSnapToGrid() {
456 m_snapToGrid.clearSnapList();
457 if (m_timeline->snapToSeekTime())
458 m_snapToGrid.addToSnapList(m_timeline->seekPosition());
459 m_snapToGrid.setSnapToFrame(m_timeline->snapToFrame());
461 m_snapToGrid.addToSnapList(m_document->getSnapTimes(m_timeline->
462 snapToBorders(), m_timeline->snapToMarkers(), true, false));
464 QValueVector < GenTime > cursor =
465 m_document->getSnapTimes(m_timeline->snapToBorders(),
466 m_timeline->snapToMarkers(), false, true, false);
467 m_snapToGrid.setCursorTimes(cursor);
469 m_snapToGrid.setSnapTolerance(GenTime((int)(m_timeline->
470 mapLocalToValue(Gui::KTimeLine::snapTolerance) -
471 m_timeline->mapLocalToValue(0)),
472 m_document->framesPerSecond()));
476 /*void TrackPanelClipMoveFunction::initiateDrag(DocClipRef * clipUnderMouse,
479 m_masterClip = clipUnderMouse;
480 m_clipOffset = mouseTime - clipUnderMouse->trackStart();
483 new Command::KMoveClipsCommand(m_document, m_masterClip);
485 m_moveClipsCommand->setClipList(m_document->listSelected());
487 m_deleteClipsCommand =
488 Command::KAddRefClipCommand::deleteSelectedClips(m_document);
491 m_startedClipMove = true;
493 DocClipRefList selection = m_document->listSelected();
495 selection.setMasterClip(m_masterClip);
496 ClipDrag *clip = new ClipDrag(selection, m_timeline, "Timeline Drag");
501 KMacroCommand *TrackPanelClipMoveFunction::createAddClipsCommand()
503 KMacroCommand *macroCommand = new KMacroCommand(i18n("Add Clips"));
505 for (int count = 0; count < (int)m_document->numTracks(); ++count) {
506 DocTrackBase *track = m_document->track(count);
508 QPtrListIterator < DocClipRef > itt = track->firstClip(true);
510 while (itt.current()) {
511 Command::KAddRefClipCommand * command =
512 new Command::KAddRefClipCommand(*m_document, itt.current(), true);
513 macroCommand->addCommand(command);
516 m_document->generateProducersList();
522 #include "trackpanelclipmovefunction.moc"