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