subtitlemodel.hpp 7.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/***************************************************************************
 *   Copyright (C) 2020 by Sashmita Raghav                                 *
 *   This file is part of Kdenlive. See www.kdenlive.org.                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3 or any later version accepted by the       *
 *   membership of KDE e.V. (or its successor approved  by the membership  *
 *   of KDE e.V.), which shall act as a proxy defined in Section 14 of     *
 *   version 3 of the license.                                             *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

Sashmita Raghav's avatar
Sashmita Raghav committed
22
23
24
#ifndef SUBTITLEMODEL_HPP
#define SUBTITLEMODEL_HPP

25
#include "bin/bin.h"
Sashmita Raghav's avatar
Sashmita Raghav committed
26
27
28
29
30
31
32
33
34
35
#include "definitions.h"
#include "gentime.h"
#include "undohelper.hpp"

#include <QAbstractListModel>
#include <QReadWriteLock>

#include <array>
#include <map>
#include <memory>
36
#include <unordered_set>
Sashmita Raghav's avatar
Sashmita Raghav committed
37
#include <mlt++/MltProperties.h>
38
#include <mlt++/Mlt.h>
Sashmita Raghav's avatar
Sashmita Raghav committed
39
40

class DocUndoStack;
41
class SnapInterface;
Sashmita Raghav's avatar
Sashmita Raghav committed
42
class AssetParameterModel;
43
class TimelineItemModel;
Sashmita Raghav's avatar
Sashmita Raghav committed
44
45
46
47
48
49
50
51
52
53

/* @brief This class is the model for a list of subtitles.
*/

class SubtitleModel:public QAbstractListModel
{
    Q_OBJECT

public:
    /* @brief Construct a subtitle list bound to the timeline */
54
    explicit SubtitleModel(Mlt::Tractor *tractor = nullptr, std::shared_ptr<TimelineItemModel> timeline = nullptr, QObject *parent = nullptr);
Sashmita Raghav's avatar
Sashmita Raghav committed
55

56
    enum { SubtitleRole = Qt::UserRole + 1, StartPosRole, EndPosRole, StartFrameRole, EndFrameRole, IdRole, SelectedRole };
Sashmita Raghav's avatar
Sashmita Raghav committed
57
    /** @brief Function that parses through a subtitle file */ 
58
59
    bool addSubtitle(int id, GenTime start,GenTime end, const QString str, bool temporary = false, bool updateFilter = true);
    bool addSubtitle(GenTime start, GenTime end, const QString str, Fun &undo, Fun &redo, bool updateFilter = true);
Sashmita Raghav's avatar
Sashmita Raghav committed
60
61
62
    /** @brief Converts string of time to GenTime */ 
    GenTime stringtoTime(QString &str);
    /** @brief Return model data item according to the role passed */ 
Sashmita Raghav's avatar
Sashmita Raghav committed
63
64
    QVariant data(const QModelIndex &index, int role) const override;
    QHash<int, QByteArray> roleNames() const override;// overide the same function of QAbstractListModel
65
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Sashmita Raghav's avatar
Sashmita Raghav committed
66

67
68
    /** @brief Returns all subtitles in the model */
    QList<SubtitledTime> getAllSubtitles() const;
69
70
71
    
    /** @brief Get subtitle at position */
    SubtitledTime getSubtitle(GenTime startFrame) const;
72
73
    /** @brief Returns all subtitle ids in a range */
    std::unordered_set<int> getItemsInRange(int startFrame, int endFrame) const;
74
75
76

    /** @brief Registers a snap model to the subtitle model */
    void registerSnap(const std::weak_ptr<SnapInterface> &snapModel);
77
78
79
80
81
82
    
    /** @brief Edit subtitle end timing
        @param startPos is start timing position of subtitles
        @param oldPos is the old position of the end time
        @param pos defines the new position of the end time
    */
83
    void editEndPos(GenTime startPos, GenTime newEndPos, bool refreshModel = true);
84
    bool requestResize(int id, int size, bool right);
85
    bool requestResize(int id, int size, bool right, Fun &undo, Fun &redo, bool logUndo);
86

87
    /** @brief Edit subtitle text
88
89
90
        @param startPos is start timing position of subtitles
        @param newSubtitleText is (new) subtitle text
    */
91
    void editSubtitle(GenTime startPos, QString newSubtitleText);
92

93
    /** @brief Remove subtitle at start position (pos) */
94
    bool removeSubtitle(int id, bool temporary = false, bool updateFilter = true);
95

96
97
    /** @brief Remove all subtitles from subtitle model */
    void removeAllSubtitles();
98
99
100
    
    /** @brief Update some properties in the view */
    void updateSub(int id, QVector <int> roles);
101

102
    /** @brief Move an existing subtitle
103
        @param subId is the subtitle's ID
104
105
        @param newPos is new start position of subtitle
    */
106
    bool moveSubtitle(int subId, GenTime newPos, bool updateModel, bool updateView);
107
    void requestSubtitleMove(int clipId, GenTime position);
108
109
    
    /** @brief Function that imports a subtitle file */
110
    void importSubtitle(const QString filePath, int offset = 0, bool externalImport = false);
111

112
113
    /** @brief Exports the subtitle model to json */
    QString toJson();
114
115
    /** @brief Returns the path to sub file */
    const QString getUrl();
116
117
118
119
120
121
122
123
124
125
    /** @brief Get a subtitle Id from its start position*/
    int getIdForStartPos(GenTime startTime) const;
    /** @brief Get the duration of a subtitle by id*/
    int getSubtitlePlaytime(int id) const;
    /** @brief Mark the subtitle item as selected or not*/
    void setSelected(int id, bool select);
    bool isSelected(int id) const;
    /** @brief Cut a subtitle */
    void cutSubtitle(int position);
    bool cutSubtitle(int position, Fun &undo, Fun &redo);
126
127
128
129
130
    QString getText(int id) const;
    int getRowForId(int id) const;
    GenTime getStartPosForId(int id) const;
    int getPreviousSub(int id) const;
    int getNextSub(int id) const;
131
132
    /** @brief Copy subtitle file to a new path */
    void copySubtitle(const QString &path, bool checkOverwrite);
133
    int trackDuration() const;
134
135
136
137
    void switchDisabled();
    bool isDisabled() const;
    void switchLocked();
    bool isLocked() const;
138
139
    /** @brief Load some subtitle filter properties from file */
    void loadProperties(QMap<QString, QString> subProperties);
140
141
    /** @brief Add all subtitle items to snaps */
    void allSnaps(std::vector<int> &snaps);
142

143
public slots:
Sashmita Raghav's avatar
Sashmita Raghav committed
144
    /** @brief Function that parses through a subtitle file */
145
    void parseSubtitle(const QString subPath = QString());
146
147
    
    /** @brief Import model to a temporary subtitle file to which the Subtitle effect is applied*/
148
    void jsontoSubtitle(const QString &data);
149
150
    /** @brief Update a subtitle text*/
    bool setText(int id, const QString text);
151

Sashmita Raghav's avatar
Sashmita Raghav committed
152
private:
153
    std::shared_ptr<TimelineItemModel> m_timeline;
Sashmita Raghav's avatar
Sashmita Raghav committed
154
    std::weak_ptr<DocUndoStack> m_undoStack;
155
    std::map<GenTime, std::pair<QString, GenTime>> m_subtitleList;
156

157
158
    QString scriptInfoSection, styleSection,eventSection;
    QString styleName;
159
    QString m_subFilePath;
160

Sashmita Raghav's avatar
Sashmita Raghav committed
161
162
163
    //To get subtitle file from effects parameter:
    //std::unique_ptr<Mlt::Properties> m_asset;
    //std::shared_ptr<AssetParameterModel> m_model;
164
165
166
    
    std::vector<std::weak_ptr<SnapInterface>> m_regSnaps;
    mutable QReadWriteLock m_lock;
167
168
    std::unique_ptr<Mlt::Filter> m_subtitleFilter;
    Mlt::Tractor *m_tractor;
169
    QVector <int> m_selected;
Sashmita Raghav's avatar
Sashmita Raghav committed
170

171
172
173
signals:
    void modelChanged();
    
174
protected:
175
    /** @brief Add time as snap in the registered snap model */
176
    void addSnapPoint(GenTime startpos);
177
178
    /** @brief Remove time as snap in the registered snap model */
    void removeSnapPoint(GenTime startpos);
Sashmita Raghav's avatar
Sashmita Raghav committed
179
    /** @brief Connect changes in model with signal */
180
    void setup();
181

Sashmita Raghav's avatar
Sashmita Raghav committed
182
183
};
Q_DECLARE_METATYPE(SubtitleModel *)
Sashmita Raghav's avatar
Sashmita Raghav committed
184
#endif // SUBTITLEMODEL_HPP