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