File indexing completed on 2024-04-28 04:52:27

0001 /*
0002     SPDX-FileCopyrightText: 2017 Nicolas Carion
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #pragma once
0007 
0008 #include "timelinemodel.hpp"
0009 #include "undohelper.hpp"
0010 #include <QReadWriteLock>
0011 #include <memory>
0012 
0013 /** @brief This is the base class for objects that can move, for example clips and compositions
0014  */
0015 template <typename Service> class MoveableItem
0016 {
0017     MoveableItem() = delete;
0018 
0019 protected:
0020     virtual ~MoveableItem() = default;
0021 
0022 public:
0023     MoveableItem(std::weak_ptr<TimelineModel> parent, int id = -1);
0024 
0025     /** @brief returns (unique) id of current item
0026      */
0027     int getId() const;
0028 
0029     /** @brief returns parent timeline UUID
0030      */
0031     QUuid getUuid() const;
0032 
0033     /** @brief returns the length of the item on the timeline
0034      */
0035     virtual int getPlaytime() const = 0;
0036 
0037     /** @brief returns the id of the track in which this items is inserted (-1 if none)
0038      */
0039     int getCurrentTrackId() const;
0040 
0041     /** @brief returns the current position of the item (-1 if not inserted)
0042      */
0043     int getPosition() const;
0044 
0045     /** @brief returns the in and out times of the item
0046      */
0047     std::pair<int, int> getInOut() const;
0048     virtual int getIn() const;
0049     virtual int getOut() const;
0050 
0051     /** @brief Set grab status */
0052     virtual void setGrab(bool grab) = 0;
0053 
0054     friend class TrackModel;
0055     friend class TimelineModel;
0056     /** @brief Implicit conversion operator to access the underlying producer
0057      */
0058     operator Service &() { return *service(); }
0059 
0060     /** @brief Returns true if the underlying producer is valid
0061      */
0062     bool isValid();
0063 
0064     /** @brief returns a property of the current item
0065      */
0066     virtual const QString getProperty(const QString &name) const = 0;
0067 
0068     /** @brief Set if the item is in grab state */
0069     bool isGrabbed() const;
0070 
0071     /** @brief True if item is selected in timeline */
0072     bool selected{false};
0073     /** @brief Set selected status */
0074     virtual void setSelected(bool sel) = 0;
0075 
0076 protected:
0077     /** @brief Returns a pointer to the service. It may be used but do NOT store it*/
0078     virtual Service *service() const = 0;
0079 
0080     /** @brief Performs a resize of the given item.
0081        Returns true if the operation succeeded, and otherwise nothing is modified
0082        This method is protected because it shouldn't be called directly. Call the function in the timeline instead.
0083        If a snap point is within reach, the operation will be coerced to use it.
0084        @param size is the new size of the item
0085        @param right is true if we change the right side of the item, false otherwise
0086        @param undo Lambda function containing the current undo stack. Will be updated with current operation
0087        @param redo Lambda function containing the current redo queue. Will be updated with current operation
0088     */
0089     virtual bool requestResize(int size, bool right, Fun &undo, Fun &redo, bool logUndo = true, bool hasMix = false) = 0;
0090 
0091     /** @brief Updates the stored position of the item
0092       This function is meant to be called by the trackmodel, not directly by the user.
0093       If you wish to actually move the item, use the requestMove slot.
0094     */
0095     virtual void setPosition(int position);
0096     /** @brief Updates the stored track id of the item
0097        This function is meant to be called by the timeline, not directly by the user.
0098        If you wish to actually change the track the item, use the slot in the timeline
0099        slot.
0100     */
0101     virtual void setCurrentTrackId(int tid, bool finalMove = true);
0102 
0103     /** @brief Set in and out of service */
0104     virtual void setInOut(int in, int out);
0105 
0106 protected:
0107     std::weak_ptr<TimelineModel> m_parent;
0108     /** @brief this is the creation id of the item, used for book-keeping */
0109     int m_id;
0110     int m_position;
0111     int m_currentTrackId;
0112     bool m_grabbed;
0113     /** @brief This is a lock that ensures safety in case of concurrent access */
0114     mutable QReadWriteLock m_lock;
0115 };
0116 
0117 #include "moveableItem.ipp"