]> git.sesse.net Git - kdenlive/blob - src/customtrackview.h
Cleaner detection of overlapping transitions
[kdenlive] / src / customtrackview.h
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
21 #ifndef CUSTOMTRACKVIEW_H
22 #define CUSTOMTRACKVIEW_H
23
24 #include <KPixmapCache>
25 #include <KColorScheme>
26
27 #include <QGraphicsView>
28 #include <QGraphicsItemAnimation>
29 #include <QTimeLine>
30 #include <QMenu>
31 #include <QUndoStack>
32 #include <QMutex>
33 #include <QWaitCondition>
34
35 #include "kdenlivedoc.h"
36 #include "docclipbase.h"
37 #include "guide.h"
38 #include "effectslist.h"
39 #include "customtrackscene.h"
40
41 class ClipItem;
42 class AbstractClipItem;
43 class AbstractGroupItem;
44 class Transition;
45
46
47 class CustomTrackView : public QGraphicsView
48 {
49     Q_OBJECT
50
51 public:
52     CustomTrackView(KdenliveDoc *doc, CustomTrackScene* projectscene, QWidget *parent = 0);
53     virtual ~ CustomTrackView();
54     virtual void mousePressEvent(QMouseEvent * event);
55     virtual void mouseReleaseEvent(QMouseEvent * event);
56     virtual void mouseMoveEvent(QMouseEvent * event);
57     virtual void mouseDoubleClickEvent(QMouseEvent *event);
58     void addTrack(TrackInfo type, int ix = -1);
59     void removeTrack(int ix);
60     /** @brief Makes the document use new track infos (name, type, ...). */
61     void configTracks(QList <TrackInfo> trackInfos);
62     int cursorPos();
63     void checkAutoScroll();
64     void moveClip(const ItemInfo start, const ItemInfo end, bool refresh);
65     void moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> startTransition, const GenTime offset, const int trackOffset, bool reverseMove = false);
66     /** move transition, startPos = (old start, old end), endPos = (new start, new end) */
67     void moveTransition(const ItemInfo start, const ItemInfo end, bool m_refresh);
68     void resizeClip(const ItemInfo start, const ItemInfo end, bool dontWorry = false);
69     void addClip(QDomElement xml, const QString &clipId, ItemInfo info, EffectsList list = EffectsList(), bool overwrite = false, bool push = false, bool refresh = true);
70     void deleteClip(ItemInfo info, bool refresh = true);
71     void slotDeleteClipMarker(const QString &comment, const QString &id, const GenTime &position);
72     void slotDeleteAllClipMarkers(const QString &id);
73     void addMarker(const QString &id, const GenTime &pos, const QString comment);
74     void setScale(double scaleFactor, double verticalScale);
75     void deleteClip(const QString &clipId);
76     void slotAddEffect(QDomElement effect, GenTime pos, int track);
77     void slotAddGroupEffect(QDomElement effect, AbstractGroupItem *group);
78     void addEffect(int track, GenTime pos, QDomElement effect);
79     void deleteEffect(int track, GenTime pos, QDomElement effect);
80     void updateEffect(int track, GenTime pos, QDomElement insertedEffect, int ix, bool triggeredByUser = true);
81     void moveEffect(int track, GenTime pos, int oldPos, int newPos);
82     void addTransition(ItemInfo transitionInfo, int endTrack, QDomElement params, bool refresh);
83     void deleteTransition(ItemInfo transitionInfo, int endTrack, QDomElement params, bool refresh);
84     void updateTransition(int track, GenTime pos,  QDomElement oldTransition, QDomElement transition, bool updateTransitionWidget);
85     void moveTransition(GenTime oldpos, GenTime newpos);
86     void activateMonitor();
87     int duration() const;
88     void deleteSelectedClips();
89     /** @brief Cuts all clips that are selected at the timeline cursor position. */
90     void cutSelectedClips();
91     void setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition, QActionGroup *clipTypeGroup, QMenu *markermenu);
92     void checkTrackHeight();
93     //QList <TrackInfo> tracksList() const;
94     void setTool(PROJECTTOOL tool);
95     ClipItem *cutClip(ItemInfo info, GenTime cutTime, bool cut, bool execute = true);
96     void slotSeekToPreviousSnap();
97     void slotSeekToNextSnap();
98     double getSnapPointForPos(double pos);
99     void editKeyFrame(const GenTime pos, const int track, const int index, const QString keyframes);
100     bool findString(const QString &text);
101     void selectFound(QString track, QString pos);
102     bool findNextString(const QString &text);
103     void initSearchStrings();
104     void clearSearchStrings();
105     QList<ItemInfo> findId(const QString &clipId);
106     void clipStart();
107     void clipEnd();
108     void changeClipSpeed();
109     void doChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, const double speed, const double oldspeed, int strobe, const QString &id);
110     /** @brief Sets the document as modified. */
111     void setDocumentModified();
112     void setInPoint();
113     void setOutPoint();
114
115     /** @brief Prepares inserting space.
116     *
117     * Shows a dialog to configure length and track. */
118     void slotInsertSpace();
119     /** @brief Prepares removing space. */
120     void slotRemoveSpace();
121     void insertSpace(QList<ItemInfo> clipsToMove, QList<ItemInfo> transToMove, int track, const GenTime duration, const GenTime offset);
122     ClipItem *getActiveClipUnderCursor(bool allowOutsideCursor = false) const;
123     void deleteTimelineTrack(int ix, TrackInfo trackinfo);
124     void saveThumbnails();
125     void autoTransition();
126     QStringList getLadspaParams(QDomElement effect) const;
127     void initCursorPos(int pos);
128
129     /** @brief Locks or unlocks a track.
130     * @param ix number of track
131     * @param lock whether to lock or unlock
132     * @param requestUpdate (default = true) Whether to request an update of the icon in the track header
133     *
134     * Makes sure no clip on track to lock is selected. */
135     void lockTrack(int ix, bool lock, bool requestUpdate = true);
136     void groupClips(bool group = true);
137     void doGroupClips(QList <ItemInfo> clipInfos, QList <ItemInfo> transitionInfos, bool group);
138     void loadGroups(const QDomNodeList groups);
139
140     /** @brief Creates SplitAudioCommands for selected clips. */
141     void splitAudio();
142
143     /** @brief Seperates the audio of a clip to a audio track.
144     * @param pos Position of the clip to split
145     * @param track Track of the clip
146     * @param split Split or unsplit */
147     void doSplitAudio(const GenTime &pos, int track, bool split);
148     void setVideoOnly();
149     void setAudioOnly();
150     void setAudioAndVideo();
151     void doChangeClipType(const GenTime &pos, int track, bool videoOnly, bool audioOnly);
152     int hasGuide(int pos, int offset);
153     void reloadTransitionLumas();
154     void updateProjectFps();
155     double fps() const;
156     int selectedTrack() const;
157     QStringList selectedClips() const;
158     QList<ClipItem *> selectedClipItems() const;
159     /** @brief Checks wheter an item can be inserted (make sure it does not overlap another item) */
160     bool canBePastedTo(ItemInfo info, int type) const;
161
162     /** @brief Selects a clip.
163     * @param add Whether to select or deselect
164     * @param group (optional) Whether to add the clip to a group
165     * @param track (optional) The track of the clip (has to be combined with @param pos)
166     * @param pos (optional) The position of the clip (has to be combined with @param track) */
167     void selectClip(bool add, bool group = false, int track = -1, int pos = -1);
168     void selectTransition(bool add, bool group = false);
169     QStringList extractTransitionsLumas();
170     void setEditMode(EDITMODE mode);
171
172     /** @brief Inserts @param clip.
173     * @param clip The clip to insert
174     * @param in The inpoint of the clip (crop from start)
175     * @param out The outpoint of the clip (crop from end)
176     *
177     * Inserts at the position of timeline cursor and selected track. */
178     void insertClipCut(DocClipBase *clip, int in, int out);
179     void clearSelection();
180     void editItemDuration();
181     void buildGuidesMenu(QMenu *goMenu) const;
182     KPixmapCache* m_pixmapCache;
183     /** update the timeline objects when palette changes */
184     void updatePalette();
185     /** @brief Returns true if a track has audio data on it.
186     * @param track The track number
187     *
188     * Check whether given track has a clip with audio in it. */
189     bool hasAudio(int track) const;
190
191 public slots:
192     void setCursorPos(int pos, bool seek = true);
193     void moveCursorPos(int delta);
194     void updateCursorPos();
195     void slotDeleteEffect(ClipItem *clip, int track, QDomElement effect, bool affectGroup = true);
196     void slotChangeEffectState(ClipItem *clip, int track, int effectPos, bool disable);
197     void slotChangeEffectPosition(ClipItem *clip, int track, int currentPos, int newPos);
198     void slotUpdateClipEffect(ClipItem *clip, int track, QDomElement oldeffect, QDomElement effect, int ix);
199     void slotUpdateClipRegion(ClipItem *clip, int ix, QString region);
200     void slotRefreshEffects(ClipItem *clip);
201     void setDuration(int duration);
202     void slotAddTransition(ClipItem* clip, ItemInfo transitionInfo, int endTrack, QDomElement transition = QDomElement());
203     void slotAddTransitionToSelectedClips(QDomElement transition);
204     void slotTransitionUpdated(Transition *, QDomElement);
205     void slotSwitchTrackAudio(int ix);
206     void slotSwitchTrackVideo(int ix);
207     void slotSwitchTrackLock(int ix);
208     void slotUpdateClip(const QString &clipId, bool reload = true);
209
210     /** @brief Creates a AddClipCommand to add, edit or delete a marker.
211      * @param id Id of the marker's clip
212      * @param t Position of the marker
213      * @param c Comment of the marker */
214     void slotAddClipMarker(const QString &id, GenTime t, QString c);
215     bool addGuide(const GenTime pos, const QString &comment);
216
217     /** @brief Shows a dialog for adding a guide.
218      * @param dialog (default = true) false = do not show the dialog but use current position as position and comment */
219     void slotAddGuide(bool dialog = true);
220     void slotEditGuide(CommentedTime guide);
221     void slotEditGuide(int guidePos = -1);
222     void slotDeleteGuide(int guidePos = -1);
223     void slotDeleteAllGuides();
224     void editGuide(const GenTime oldPos, const GenTime pos, const QString &comment);
225     void copyClip();
226     void pasteClip();
227     void pasteClipEffects();
228     void slotUpdateAllThumbs();
229     void slotCheckPositionScrolling();
230     void slotInsertTrack(int ix);
231
232     /** @brief Shows a dialog for selecting a track to delete.
233     * @param ix Number of the track, which should be pre-selected in the dialog */
234     void slotDeleteTrack(int ix);
235     /** @brief Shows the configure tracks dialog. */
236     void slotConfigTracks(int ix);
237     void clipNameChanged(const QString id, const QString name);
238     void slotTrackUp();
239     void slotTrackDown();
240     void slotSelectTrack(int ix);
241     void insertZoneOverwrite(QStringList data, int in);
242
243     /** @brief Rebuilds a group to fit again after children changed.
244     * @param childTrack the track of one of the groups children
245     * @param childPos The position of the same child */
246     void rebuildGroup(int childTrack, GenTime childPos);
247     /** @brief Rebuilds a group to fit again after children changed.
248     * @param group The group to rebuild */
249     void rebuildGroup(AbstractGroupItem *group);
250
251     /** @brief Cuts a group into two parts.
252     * @param clips1 Clips before the cut
253     * @param transitions1 Transitions before the cut
254     * @param clipsCut Clips that need to be cut
255     * @param transitionsCut Transitions that need to be cut
256     * @param clips2 Clips behind the cut
257     * @param transitions2 Transitions behind the cut
258     * @param cutPos Absolute position of the cut
259     * @param cut true = cut, false = "uncut" */
260     void slotRazorGroup(QList <ItemInfo> clips1, QList <ItemInfo> transitions1, QList <ItemInfo> clipsCut, QList <ItemInfo> transitionsCut, QList <ItemInfo> clips2, QList <ItemInfo> transitions2, GenTime cutPos, bool cut);
261
262     /** @brief Add en effect to a track.
263     * @param effect The new effect xml
264     * @param ix The track index */
265     void slotAddTrackEffect(const QDomElement effect, int ix);
266
267     /** @brief Update the list of snap points (sticky timeline hotspots).
268     * @param selected The currently selected clip if any
269     * @param offsetList The list of points that should also snap (for example when movin a clip, start and end points should snap
270     * @param skipSelectedItems if true, the selected item start and end points will not be added to snap list */
271     void updateSnapPoints(AbstractClipItem *selected, QList <GenTime> offsetList = QList <GenTime> (), bool skipSelectedItems = false);
272     /** @brief Save a snapshot image of current timeline view */
273     void saveTimelinePreview(const QString path);
274
275 protected:
276     virtual void drawBackground(QPainter * painter, const QRectF & rect);
277     //virtual void drawForeground ( QPainter * painter, const QRectF & rect );
278     virtual void dragEnterEvent(QDragEnterEvent * event);
279     virtual void dragMoveEvent(QDragMoveEvent * event);
280     virtual void dragLeaveEvent(QDragLeaveEvent * event);
281     /** @brief Something has been dropped onto the timeline */
282     virtual void dropEvent(QDropEvent * event);
283     virtual void wheelEvent(QWheelEvent * e);
284     virtual void keyPressEvent(QKeyEvent * event);
285     virtual QStringList mimeTypes() const;
286     virtual Qt::DropActions supportedDropActions() const;
287
288 private:
289     int m_tracksHeight;
290     int m_projectDuration;
291     int m_cursorPos;
292     KdenliveDoc *m_document;
293     CustomTrackScene *m_scene;
294     QGraphicsLineItem *m_cursorLine;
295     ItemInfo m_dragItemInfo;
296     ItemInfo m_selectionGroupInfo;
297     OPERATIONTYPE m_operationMode;
298     OPERATIONTYPE m_moveOpMode;
299     AbstractClipItem *m_dragItem;
300     Guide *m_dragGuide;
301     QUndoStack *m_commandStack;
302     QGraphicsItem *m_visualTip;
303     QGraphicsItemAnimation *m_animation;
304     QTimeLine *m_animationTimer;
305     QColor m_tipColor;
306     QPen m_tipPen;
307     QPoint m_clickPoint;
308     QPoint m_clickEvent;
309     QList <CommentedTime> m_searchPoints;
310     QList <Guide *> m_guides;
311
312     ClipItem *getClipItemAt(int pos, int track);
313     ClipItem *getClipItemAt(GenTime pos, int track);
314     ClipItem *getClipItemAtEnd(GenTime pos, int track);
315     ClipItem *getClipItemAtStart(GenTime pos, int track);
316     Transition *getTransitionItemAt(int pos, int track);
317     Transition *getTransitionItemAt(GenTime pos, int track);
318     Transition *getTransitionItemAtEnd(GenTime pos, int track);
319     Transition *getTransitionItemAtStart(GenTime pos, int track);
320     void checkScrolling();
321     /** Should we auto scroll while playing (keep in sync with KdenliveSettings::autoscroll() */
322     bool m_autoScroll;
323     void displayContextMenu(QPoint pos, AbstractClipItem *clip, AbstractGroupItem *group);
324     QMenu *m_timelineContextMenu;
325     QMenu *m_timelineContextClipMenu;
326     QMenu *m_timelineContextTransitionMenu;
327     QMenu *m_markerMenu;
328     QAction *m_autoTransition;
329     QAction *m_pasteEffectsAction;
330     QAction *m_ungroupAction;
331     QAction *m_editGuide;
332     QAction *m_deleteGuide;
333     QActionGroup *m_clipTypeGroup;
334     QTimer m_scrollTimer;
335     QTimer m_thumbsTimer;
336     int m_scrollOffset;
337     bool m_clipDrag;
338
339     int m_findIndex;
340     PROJECTTOOL m_tool;
341     QCursor m_razorCursor;
342     QCursor m_spacerCursor;
343     /** list containing items currently copied in the timeline */
344     QList<AbstractClipItem *> m_copiedItems;
345     /** Used to get the point in timeline where a context menu was opened */
346     QPoint m_menuPosition;
347     bool m_blockRefresh;
348     AbstractGroupItem *m_selectionGroup;
349     QList <ClipItem *> m_waitingThumbs;
350     int m_selectedTrack;
351     int m_spacerOffset;
352
353     QMutex m_mutex;
354     QWaitCondition m_producerNotReady;
355     KStatefulBrush m_activeTrackBrush;
356
357     /** stores the state of the control modifier during mouse press.
358      * Will then be used to identify whether we resize a group or only one item of it. */
359     bool m_controlModifier;
360
361     /** Get the index of the video track that is just below current track */
362     int getPreviousVideoTrack(int track);
363     void updatePositionEffects(ClipItem * item, ItemInfo info, bool standalone = true);
364     bool insertDropClips(const QMimeData *data, const QPoint pos);
365     bool canBePastedTo(QList <ItemInfo> infoList, int type) const;
366     bool canBePasted(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const;
367     bool canBeMoved(QList<AbstractClipItem *> items, GenTime offset, int trackOffset) const;
368     ClipItem *getClipUnderCursor() const;
369     AbstractClipItem *getMainActiveClip() const;
370     void resetSelectionGroup(bool selectItems = true);
371     void groupSelectedItems(bool force = false, bool createNewGroup = false);
372     /** Get available space for clip move (min and max free positions) */
373     void getClipAvailableSpace(AbstractClipItem *item, GenTime &minimum, GenTime &maximum);
374     /** Get available space for transition move (min and max free positions) */
375     void getTransitionAvailableSpace(AbstractClipItem *item, GenTime &minimum, GenTime &maximum);
376     void updateClipTypeActions(ClipItem *clip);
377     /** Whether an item can be moved to a new position without colliding with similar items */
378     bool itemCollision(AbstractClipItem *item, ItemInfo newPos);
379     /** Selects all items in the scene rect, and sets ok to false if a group going over several tracks is found in it */
380     QList<QGraphicsItem *> checkForGroups(const QRectF &rect, bool *ok);
381     /** Adjust clips under another one when working in overwrite mode */
382     void adjustTimelineClips(EDITMODE mode, ClipItem *item, ItemInfo posinfo, QUndoCommand *command);
383     void adjustTimelineTransitions(EDITMODE mode, Transition *item, QUndoCommand *command);
384     /** Adjust keyframes when pasted to another clip */
385     void adjustKeyfames(GenTime oldstart, GenTime newstart, GenTime duration, QDomElement xml);
386
387     /** @brief Removes the tip and stops the animation timer. */
388     void removeTipAnimation();
389     /** @brief Creates a new tip animation.
390     * @param clip clip to display the tip on
391     * @param mode operation mode for which the tip should be displayed
392     * @param size size of the tip */
393     void setTipAnimation(AbstractClipItem *clip, OPERATIONTYPE mode, const double size);
394
395     /** @brief Takes care of updating effects and attached transitions during a resize from start.
396     * @param item Item to resize
397     * @param oldInfo The item's info before resizement (set to item->info() is @param check true)
398     * @param pos New startPos
399     * @param check (optional, default = false) Whether to check for collisions
400     * @param command (optional) Will be used as parent command (for undo history) */
401     void prepareResizeClipStart(AbstractClipItem *item, ItemInfo oldInfo, int pos, bool check = false, QUndoCommand *command = NULL);
402
403     /** @brief Takes care of updating effects and attached transitions during a resize from end.
404     * @param item Item to resize
405     * @param oldInfo The item's info before resizement (set to item->info() is @param check true)
406     * @param pos New endPos
407     * @param check (optional, default = false) Whether to check for collisions
408     * @param command (optional) Will be used as parent command (for undo history) */
409     void prepareResizeClipEnd(AbstractClipItem *item, ItemInfo oldInfo, int pos, bool check = false, QUndoCommand *command = NULL);
410
411     /** @brief Collects information about the group's children to pass it on to RazorGroupCommand.
412     * @param group The group to cut
413     * @param cutPos The absolute position of the cut */
414     void razorGroup(AbstractGroupItem *group, GenTime cutPos);
415
416     /** @brief Gets the effect parameters that will be passed to Mlt. */
417     EffectsParameterList getEffectArgs(const QDomElement effect);
418
419     /** @brief Updates @param item's pan and zoom effect after resize or cut.
420      * @param item clip whose pan and zoom effect should be updated
421      * @param cutPos (optional) if clip was cut, cut position relative to the original's clip position */
422     void updatePanZoom(ClipItem *item, GenTime cutPos = GenTime());
423
424     /** @brief Update Tracknames to fit again after track was added/deleted.
425      * @param track Number of track which was added/deleted
426      * @param added true = track added, false = track deleted
427      * 
428      * The default track name consists of type + number. If we add/delete a track the number has to be adjusted
429      * if the name is still the default one. */
430     void updateTrackNames(int track, bool added);
431
432     /** @brief Updates the duration stored in a track's TrackInfo.
433      * @param track Number of track as used in ItemInfo (not the numbering used in KdenliveDoc) (negative for all tracks)
434      * @param command If effects need to be updated the commands to do this will be attached to this undo command
435      * 
436      * In addition to update the duration in TrackInfo it updates effects with keyframes on the track. */
437     void updateTrackDuration(int track, QUndoCommand *command);
438
439     /** @brief Adjusts effects after a clip resize.
440      * @param item The item that was resized
441      * @param oldInfo pre resize info
442      * @param fromStart false = resize from end
443      * @param command Used as a parent for EditEffectCommand */
444     void adjustEffects(ClipItem *item, ItemInfo oldInfo, QUndoCommand *command);
445
446 private slots:
447     void slotRefreshGuides();
448     void slotEnableRefresh();
449     void slotCheckMouseScrolling();
450     void slotEditTimeLineGuide();
451     void slotDeleteTimeLineGuide();
452     void slotFetchNextThumbs();
453     void checkTrackSequence(int track);
454     void slotGoToMarker(QAction *action);
455     void slotResetMenuPosition();
456     void slotDoResetMenuPosition();
457     /** @brief Re-create the clip thumbnails.
458      *  @param id The clip's Id string.
459      *  @param resetThumbs Should we recreate the timeline thumbnails. */
460     void slotRefreshThumbs(const QString &id, bool resetThumbs);
461
462 signals:
463     void cursorMoved(int, int);
464     void zoomIn();
465     void zoomOut();
466     void mousePosition(int);
467     /** @brief A clip was selected in timeline, update the effect stack
468      *  @param clip The clip
469      *  @param ix The index of currently selected effect
470      *  @param raise If true, the effect stack widget will be raised (come to front). */
471     void clipItemSelected(ClipItem *clip, int ix = -1, bool raise = true);
472     void transitionItemSelected(Transition*, int track = 0, QPoint p = QPoint(), bool update = false);
473     void activateDocumentMonitor();
474     void trackHeightChanged();
475     void tracksChanged();
476     void displayMessage(const QString, MessageType);
477     void showClipFrame(DocClipBase *, QPoint, const int);
478     void doTrackLock(int, bool);
479     void updateClipMarkers(DocClipBase *);
480     void updateTrackHeaders();
481     void playMonitor();
482     /** @brief Monitor document changes (for example the presence of audio data in timeline for export widget.*/
483     void documentModified();
484     void forceClipProcessing(const QString &);
485     void showTrackEffects(int, TrackInfo);
486     /** @brief Update the track effect button that shows if a track has effects or not.*/
487     void updateTrackEffectState(int);
488 };
489
490 #endif
491