]> git.sesse.net Git - kdenlive/blob - src/trackpanelclipmovefunction.cpp
Reindent all source files
[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     // m_moveClipsCommand = 0;
30     //m_deleteClipsCommand = 0;
31     m_addingClips = false;
32     m_firststep = true;
33 }
34
35
36 TrackPanelClipMoveFunction::~TrackPanelClipMoveFunction() {
37 }
38
39 bool TrackPanelClipMoveFunction::mouseApplies(DocumentTrack *,
40         QMouseEvent * event) const {
41     return mouseApplies(event->pos());
42 }
43
44 bool TrackPanelClipMoveFunction::mouseApplies(const QPoint & pos) const {
45     TrackViewClip *clipUnderMouse = 0;
46
47     DocumentTrack * panel = m_view->panelAt(pos.y());
48
49     if (panel) {
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);
53     }
54     return clipUnderMouse;
55 }
56
57 QCursor TrackPanelClipMoveFunction::getMouseCursor(DocumentTrack *, QMouseEvent * event) {
58     return QCursor(Qt::SizeAllCursor);
59 }
60
61 bool TrackPanelClipMoveFunction::mousePressed(DocumentTrack * panel,
62         QMouseEvent * event) {
63     bool result = false;
64     /*
65        if (panel->hasDocumentTrackIndex()) {
66      DocTrackBase *track =
67          m_document->track(panel->documentTrackIndex());
68      if (track) {
69       GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
70       m_document->framesPerSecond());
71          m_clipUnderMouse = 0;
72          m_clipUnderMouse = track->getClipAt(mouseTime);
73
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);
78       }
79       else {
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);
85           }
86       }
87       result = true;
88          }
89      }
90         }
91     */
92     return result;
93 }
94
95 bool TrackPanelClipMoveFunction::mouseDoubleClicked(DocumentTrack * panel, QMouseEvent *event) {
96     /*
97      if (panel->hasDocumentTrackIndex()) {
98       DocTrackBase *track =
99           m_document->track(panel->documentTrackIndex());
100       if (track) {
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);
106        }
107       }
108      }*/
109     return false; ///FIXME is that right ?
110 }
111
112 bool TrackPanelClipMoveFunction::mouseReleased(DocumentTrack *, QMouseEvent *) {
113     //m_timeline->stopScrollTimer();
114     return true;
115 }
116
117 bool TrackPanelClipMoveFunction::mouseMoved(DocumentTrack * panel,
118         QMouseEvent * event) {
119     bool result = false;
120     /*if (panel->hasDocumentTrackIndex()) {
121     DocTrackBase *track =
122      m_document->track(panel->documentTrackIndex());
123     if (track) {
124     GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->x())),
125     m_document->framesPerSecond());
126
127      if (m_dragging) {
128     m_dragging = false;
129     result = true;
130      } else {
131     if (m_clipUnderMouse) {
132       if (!m_document->projectClip().clipSelected(m_clipUnderMouse)) {
133     if ((event->state() & Qt::ControlButton)
134        || (event->state() & Qt::ShiftButton)) {
135        m_app->
136     addCommand(Command::KSelectClipCommand::
137     selectClipAt(m_document, *track,
138         mouseTime), true);
139     } else {
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);
144     }
145       }
146       m_dragging = true;
147       initiateDrag(m_clipUnderMouse, mouseTime);
148       result = true;
149     }
150      }
151     }
152     }
153     */
154     return result;
155 }
156
157 // virtual
158 bool TrackPanelClipMoveFunction::dragEntered(DocumentTrack * panel,
159         QDragEnterEvent * event) {
160     /*    if (m_startedClipMove) {
161      m_document->activateSceneListGeneration(false);
162      event->accept(true);
163         } else if (ClipDrag::canDecode(event)) {
164      m_document->activateSceneListGeneration(false);
165      m_selection = ClipDrag::decode(m_document, event);
166
167
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);
175          } else {
176       setupSnapToGrid();
177       event->accept(true);
178          }
179      } else {
180          kdError() <<
181       "ERROR! ERROR! ERROR! ClipDrag:decode decoded a null clip!!!"
182       << endl;
183      }
184         } else if (EffectDrag::canDecode(event)) {
185      event->accept(true);
186         } else {
187      event->accept(false);
188         }
189         //m_startedClipMove = false;
190     */
191     return true;
192 }
193
194 // virtual
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);
202
203      if (m_selection.isEmpty() || m_dragging) {
204              moveSelectedClips(trackUnder, mouseTime - m_clipOffset);
205      } else {
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);
210       setupSnapToGrid();
211       m_selection.clear();
212       m_dragging = true;
213          }
214      }
215         } else if (EffectDrag::canDecode(event)) {
216      if (mouseApplies(pos)) {
217          event->accept();
218      } else {
219          event->ignore();
220      }
221         } else {
222      event->ignore();
223         }
224         m_timeline->checkScrolling(pos);
225     */
226     return true;
227 }
228
229 int TrackPanelClipMoveFunction::trackUnderPoint(const QPoint & pos) {
230     uint y = pos.y();
231     DocumentTrack * panel = m_view->panelAt(y);
232     /*
233         if (panel) {
234      return panel->documentTrackIndex();
235         }*/
236
237     return -1;
238 }
239
240 // virtual
241 bool TrackPanelClipMoveFunction::dragLeft(DocumentTrack *, QDragLeaveEvent *) {
242     m_dragging = false;
243     /*
244         if (!m_selection.isEmpty()) {
245      m_selection.setAutoDelete(true);
246      m_selection.clear();
247      m_selection.setAutoDelete(false);
248         }
249
250         if (m_addingClips) {
251      m_addingClips = false;
252
253      QPtrListIterator < DocTrackBase >
254          trackItt(m_document->trackList());
255
256      while (trackItt.current()) {
257          (*trackItt)->deleteClips(true);
258          ++trackItt;
259      }
260
261      m_document->activateSceneListGeneration(true);
262         }
263
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);
271         }
272
273         if (m_deleteClipsCommand) {
274      m_app->addCommand(m_deleteClipsCommand, false);
275      m_deleteClipsCommand = 0;
276
277      QPtrListIterator < DocTrackBase >
278          trackItt(m_document->trackList());
279
280      while (trackItt.current()) {
281          trackItt.current()->deleteClips(true);
282          ++trackItt;
283      }
284         }
285
286         m_timeline->drawTrackViewBackBuffer();
287         m_timeline->stopScrollTimer();
288     */
289     return true;
290 }
291
292 // virtual
293 bool TrackPanelClipMoveFunction::dragDropped(DocumentTrack * panel,
294         QDropEvent * event) {
295     /*
296         m_dragging = false;
297         m_startedClipMove = false;
298         if (ClipDrag::canDecode(event)) {
299      if (!m_selection.isEmpty()) {
300          m_selection.setAutoDelete(true);
301          m_selection.clear();
302          m_selection.setAutoDelete(false);
303      }
304
305      if (m_addingClips) {
306
307          m_app->addCommand(createAddClipsCommand(), true);
308          m_addingClips = false;
309                 m_app->clipReferenceChanged();
310
311          //if (m_firststep) m_document->activateSceneListGeneration(true);
312          m_firststep = false;
313      }
314
315      if (m_deleteClipsCommand) {
316          delete m_deleteClipsCommand;
317          m_deleteClipsCommand = 0;
318      }
319
320      if (m_moveClipsCommand) {
321          m_moveClipsCommand->setEndLocation(m_masterClip);
322          if (!m_moveClipsCommand->doesMove())
323          {
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.
330          }
331          else {
332       m_document->activateSceneListGeneration(true, false);
333       delete m_moveClipsCommand;
334       m_moveClipsCommand = 0;
335          }
336      }
337      event->accept();
338         } else if (EffectDrag::canDecode(event)) {
339      DocClipRef *clipUnderMouse = 0;
340      DocumentTrack * panel =
341          m_view->panelAt(event->pos().y());
342      if (panel) {
343          DocTrackBase *track =
344       m_document->track(panel->documentTrackIndex());
345          if (track) {
346         GenTime mouseTime((int)(m_timeline->mapLocalToValue(event->pos().
347        x())), m_document->framesPerSecond());
348       clipUnderMouse = track->getClipAt(mouseTime);
349          }
350      }
351
352      if (clipUnderMouse) {
353          Effect *effect = EffectDrag::decode(m_document, event);
354          if (effect) {
355       m_app->
356           addCommand(Command::KAddEffectCommand::
357           appendEffect(m_document, clipUnderMouse, effect),
358           true);
359           if (effect->name() == i18n("Freeze")) m_app->getDocument()->emitCurrentClipPosition();
360          } else {
361       kdWarning() <<
362           "EffectDrag::decode did not return an effect, ignoring drag drop..."
363           << endl;
364          }
365          delete effect;
366      }
367         }
368         m_timeline->stopScrollTimer();
369         m_timeline->drawTrackViewBackBuffer();
370     */
371     return true;
372 }
373
374 bool TrackPanelClipMoveFunction::moveSelectedClips(int newTrack,
375         GenTime start) {
376     /*
377         if (!m_masterClip) return false;
378         int trackOffset =
379      m_document->trackIndex(m_document->findTrack(m_masterClip));
380         GenTime startOffset;
381
382         if (trackOffset == -1) {
383      kdError() <<
384          "Trying to move selected clips, master clip is not set." <<
385          endl;
386      return false;
387         } else {
388      startOffset = m_masterClip->trackStart();
389         }
390
391         trackOffset = newTrack - trackOffset;
392         startOffset = start - startOffset;
393         if (startOffset == GenTime()) return false;
394         m_document->moveSelectedClips(startOffset, trackOffset);
395     */
396     return true;
397 }
398
399 /*
400 void TrackPanelClipMoveFunction::addClipsToTracks(DocClipRefList & clips,
401     int track, GenTime value, bool selected)
402 {
403
404     if (clips.isEmpty())
405  return;
406
407     if (selected) {
408  m_app->
409      addCommand(Command::KSelectClipCommand::selectNone(m_document),
410      true);
411     }
412
413     DocClipRef *masterClip = clips.masterClip();
414     if (!masterClip)
415  masterClip = clips.first();
416
417     GenTime startOffset = value - masterClip->trackStart();
418
419     int trackOffset = masterClip->trackNum();
420
421     if (trackOffset == -1)
422  trackOffset = 0;
423     trackOffset = track - trackOffset;
424
425     QPtrListIterator < DocClipRef > itt(clips);
426     int moveToTrack;
427
428     while (itt.current() != 0) {
429  moveToTrack = itt.current()->trackNum();
430
431  if (moveToTrack == -1) {
432      moveToTrack = track;
433      itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
434      startOffset += itt.current()->cropDuration();
435  } else {
436      moveToTrack += trackOffset;
437      itt.current()->moveTrackStart(itt.current()->trackStart() + startOffset);
438  }
439
440
441
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);
445  }
446
447  ++itt;
448     }
449
450     m_addingClips = true;
451 }
452 */
453
454 void TrackPanelClipMoveFunction::setupSnapToGrid() {
455     /*
456         m_snapToGrid.clearSnapList();
457         if (m_timeline->snapToSeekTime())
458      m_snapToGrid.addToSnapList(m_timeline->seekPosition());
459         m_snapToGrid.setSnapToFrame(m_timeline->snapToFrame());
460
461         m_snapToGrid.addToSnapList(m_document->getSnapTimes(m_timeline->
462          snapToBorders(), m_timeline->snapToMarkers(), true, false));
463
464         QValueVector < GenTime > cursor =
465      m_document->getSnapTimes(m_timeline->snapToBorders(),
466      m_timeline->snapToMarkers(), false, true, false);
467         m_snapToGrid.setCursorTimes(cursor);
468
469       m_snapToGrid.setSnapTolerance(GenTime((int)(m_timeline->
470          mapLocalToValue(Gui::KTimeLine::snapTolerance) -
471         m_timeline->mapLocalToValue(0)),
472          m_document->framesPerSecond()));
473     */
474 }
475
476 /*void TrackPanelClipMoveFunction::initiateDrag(DocClipRef * clipUnderMouse,
477     GenTime mouseTime)
478 {
479     m_masterClip = clipUnderMouse;
480     m_clipOffset = mouseTime - clipUnderMouse->trackStart();
481
482     m_moveClipsCommand =
483  new Command::KMoveClipsCommand(m_document, m_masterClip);
484
485     m_moveClipsCommand->setClipList(m_document->listSelected());
486
487     m_deleteClipsCommand =
488  Command::KAddRefClipCommand::deleteSelectedClips(m_document);
489     setupSnapToGrid();
490
491     m_startedClipMove = true;
492
493     DocClipRefList selection = m_document->listSelected();
494
495     selection.setMasterClip(m_masterClip);
496     ClipDrag *clip = new ClipDrag(selection, m_timeline, "Timeline Drag");
497
498     clip->dragCopy();
499 }
500
501 KMacroCommand *TrackPanelClipMoveFunction::createAddClipsCommand()
502 {
503     KMacroCommand *macroCommand = new KMacroCommand(i18n("Add Clips"));
504
505   for (int count = 0; count < (int)m_document->numTracks(); ++count) {
506  DocTrackBase *track = m_document->track(count);
507
508  QPtrListIterator < DocClipRef > itt = track->firstClip(true);
509
510  while (itt.current()) {
511      Command::KAddRefClipCommand * command =
512   new Command::KAddRefClipCommand(*m_document, itt.current(), true);
513      macroCommand->addCommand(command);
514      ++itt;
515  }
516  m_document->generateProducersList();
517     }
518     return macroCommand;
519 }
520 */
521
522 #include "trackpanelclipmovefunction.moc"