1 /*
2     SPDX-FileCopyrightText: 2017 Nicolas Carion
3     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
4 */
5 
6 #ifndef MOVEABLEITEM_H
7 #define MOVEABLEITEM_H
8 
9 #include "timelinemodel.hpp"
10 #include "undohelper.hpp"
11 #include <QReadWriteLock>
12 #include <memory>
13 
14 /** @brief This is the base class for objects that can move, for example clips and compositions
15  */
16 template <typename Service> class MoveableItem
17 {
18     MoveableItem() = delete;
19 
20 protected:
21     virtual ~MoveableItem() = default;
22 
23 public:
24     MoveableItem(std::weak_ptr<TimelineModel> parent, int id = -1);
25 
26     /** @brief returns (unique) id of current item
27      */
28     int getId() const;
29 
30     /** @brief returns the length of the item on the timeline
31      */
32     virtual int getPlaytime() const = 0;
33 
34     /** @brief returns the id of the track in which this items is inserted (-1 if none)
35      */
36     int getCurrentTrackId() const;
37 
38     /** @brief returns the current position of the item (-1 if not inserted)
39      */
40     int getPosition() const;
41 
42     /** @brief returns the in and out times of the item
43      */
44     std::pair<int, int> getInOut() const;
45     virtual int getIn() const;
46     virtual int getOut() const;
47 
48     /** @brief Set grab status */
49     virtual void setGrab(bool grab) = 0;
50 
51     friend class TrackModel;
52     friend class TimelineModel;
53     /** @brief Implicit conversion operator to access the underlying producer
54      */
operator Service&()55     operator Service &() { return *service(); }
56 
57     /** @brief Returns true if the underlying producer is valid
58      */
59     bool isValid();
60 
61     /** @brief returns a property of the current item
62      */
63     virtual const QString getProperty(const QString &name) const = 0;
64 
65     /** @brief Set if the item is in grab state */
66     bool isGrabbed() const;
67 
68     /** @brief True if item is selected in timeline */
69     bool selected {false};
70     /** @brief Set selected status */
71     virtual void setSelected(bool sel) = 0;
72 
73 protected:
74     /** @brief Returns a pointer to the service. It may be used but do NOT store it*/
75     virtual Service *service() const = 0;
76 
77     /** @brief Performs a resize of the given item.
78        Returns true if the operation succeeded, and otherwise nothing is modified
79        This method is protected because it shouldn't be called directly. Call the function in the timeline instead.
80        If a snap point is within reach, the operation will be coerced to use it.
81        @param size is the new size of the item
82        @param right is true if we change the right side of the item, false otherwise
83        @param undo Lambda function containing the current undo stack. Will be updated with current operation
84        @param redo Lambda function containing the current redo queue. Will be updated with current operation
85     */
86     virtual bool requestResize(int size, bool right, Fun &undo, Fun &redo, bool logUndo = true, bool hasMix = false) = 0;
87 
88     /** @brief Updates the stored position of the item
89       This function is meant to be called by the trackmodel, not directly by the user.
90       If you wish to actually move the item, use the requestMove slot.
91     */
92     virtual void setPosition(int position);
93     /** @brief Updates the stored track id of the item
94        This function is meant to be called by the timeline, not directly by the user.
95        If you wish to actually change the track the item, use the slot in the timeline
96        slot.
97     */
98     virtual void setCurrentTrackId(int tid, bool finalMove = true);
99 
100     /** @brief Set in and out of service */
101     virtual void setInOut(int in, int out);
102 
103 protected:
104     std::weak_ptr<TimelineModel> m_parent;
105     /** @brief this is the creation id of the item, used for book-keeping */
106     int m_id;
107     int m_position;
108     int m_currentTrackId;
109     bool m_grabbed;
110     /** @brief This is a lock that ensures safety in case of concurrent access */
111     mutable QReadWriteLock m_lock;
112 };
113 
114 #include "moveableItem.ipp"
115 #endif
116