CuteLogger
Fast and simple logging solution for Qt based applications
subtitlesmodel.h
1 /*
2  * Copyright (c) 2024 Meltytech, LLC
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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SUBTITLESMODEL_H
19 #define SUBTITLESMODEL_H
20 
21 #include <MltProducer.h>
22 
23 #include "models/subtitles.h"
24 
25 #include <QAbstractItemModel>
26 #include <QTimer>
27 #include <QString>
28 
29 #include <vector>
30 
31 class SubtitlesModel : public QAbstractItemModel
32 {
33  Q_OBJECT
34  Q_PROPERTY(int trackCount READ trackCount NOTIFY tracksChanged)
35 
36 public:
37 
38  enum Roles {
39  TextRole = Qt::UserRole + 1,
40  StartRole,
41  EndRole,
42  DurationRole,
43  SimpleText,
44  StartFrameRole,
45  EndFrameRole,
46  SiblingCountRole,
47  };
48 
49  struct SubtitleTrack {
50  QString name;
51  QString lang;
52  };
53 
54  explicit SubtitlesModel(QObject *parent = 0);
55  virtual ~SubtitlesModel();
56 
57  void load(Mlt::Producer *producer);
58  bool isValid() const;
59  int64_t maxTime() const;
60 
61  // Track Functions
62  int trackCount() const;
63  Q_INVOKABLE QModelIndex trackModelIndex(int trackIndex) const;
64  QList<SubtitlesModel::SubtitleTrack> getTracks() const;
65  int getTrackIndex(const QString &name);
66  SubtitlesModel::SubtitleTrack getTrack(const QString &name);
67  SubtitlesModel::SubtitleTrack getTrack(int index);
68  void addTrack(SubtitlesModel::SubtitleTrack &track);
69  void removeTrack(QString &name);
70  void editTrack(int trackIndex, SubtitlesModel::SubtitleTrack &track);
71 
72  // Item Functions
73  Q_INVOKABLE int itemCount(int trackIndex) const;
74  int64_t endTime(int trackIndex) const;
75  QModelIndex itemModelIndex(int trackIndex, int itemIndex) const;
76  int itemIndexAtTime(int trackIndex, int64_t msTime) const;
77  int itemIndexBeforeTime(int trackIndex, int64_t msTime) const;
78  int itemIndexAfterTime(int trackIndex, int64_t msTime) const;
79  const Subtitles::SubtitleItem &getItem(int trackIndex, int itemIndex) const;
80  void importSubtitles(int trackIndex, int64_t msTime, QList<Subtitles::SubtitleItem> &items);
81  void importSubtitlesToNewTrack(SubtitlesModel::SubtitleTrack &track,
82  QList<Subtitles::SubtitleItem> &items);
83  void exportSubtitles(const QString &filePath, int trackIndex) const;
84  void overwriteItem(int trackIndex, const Subtitles::SubtitleItem &item);
85  void appendItem(int trackIndex, const Subtitles::SubtitleItem &item);
86  void removeItems(int trackIndex, int firstItemIndex, int lastItemIndex);
87  void setItemStart(int trackIndex, int itemIndex, int64_t msTime);
88  void setItemEnd(int trackIndex, int itemIndex, int64_t msTime);
89  void setText(int trackIndex, int itemIndex, const QString &text);
90  Q_INVOKABLE void moveItems(int trackIndex, int firstItemIndex, int lastItemIndex, int64_t msTime);
91  Q_INVOKABLE bool validateMove(const QModelIndexList &items, int64_t msTime);
92 
93  // Only to be called by subtitle commands
94  void doInsertTrack(const SubtitlesModel::SubtitleTrack &track, int trackIndex);
95  void doRemoveTrack(int trackIndex);
96  void doEditTrack(const SubtitlesModel::SubtitleTrack &track, int trackIndex);
97  void doRemoveSubtitleItems(int trackIndex, const QList<Subtitles::SubtitleItem> &subtitles);
98  void doInsertSubtitleItems(int trackIndex, const QList<Subtitles::SubtitleItem> &subtitles);
99  void doSetText(int trackIndex, int itemIndex, const QString &text);
100  void doSetTime(int trackIndex, int itemIndex, int64_t startTime, int64_t endTime);
101 
102 signals:
103  void tracksChanged(int count);
104  void modified();
105 
106 protected:
107  // Implement QAbstractItemModel
108  int rowCount(const QModelIndex &parent) const;
109  int columnCount(const QModelIndex &parent) const;
110  QVariant data(const QModelIndex &index, int role) const;
111  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
112  QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
113  QModelIndex parent(const QModelIndex &index) const;
114  QHash<int, QByteArray> roleNames() const;
115 
116 private:
117  void requestFeedCommit(int trackIndex);
118  void commitToFeed(int trackIndex);
119  Mlt::Producer *m_producer;
120  QList<SubtitlesModel::SubtitleTrack> m_tracks;
121  QList<QList<Subtitles::SubtitleItem>> m_items;
122  QTimer *m_commitTimer;
123  int m_commitTrack;
124 };
125 
126 #endif // SUBTITLESMODEL_H