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