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