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
31class SubtitlesModel : public QAbstractItemModel
32{
33 Q_OBJECT
34 Q_PROPERTY(int trackCount READ trackCount NOTIFY tracksChanged)
35
36public:
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 exportSubtitles(const QString &filePath, int trackIndex) const;
82 void overwriteItem(int trackIndex, const Subtitles::SubtitleItem &item);
83 void appendItem(int trackIndex, const Subtitles::SubtitleItem &item);
84 void removeItems(int trackIndex, int firstItemIndex, int lastItemIndex);
85 void setItemStart(int trackIndex, int itemIndex, int64_t msTime);
86 void setItemEnd(int trackIndex, int itemIndex, int64_t msTime);
87 void setText(int trackIndex, int itemIndex, const QString &text);
88 Q_INVOKABLE void moveItems(int trackIndex, int firstItemIndex, int lastItemIndex, int64_t msTime);
89 Q_INVOKABLE bool validateMove(const QModelIndexList &items, int64_t msTime);
90
91 // Only to be called by subtitle commands
92 void doInsertTrack(const SubtitlesModel::SubtitleTrack &track, int trackIndex);
93 void doRemoveTrack(int trackIndex);
94 void doEditTrack(const SubtitlesModel::SubtitleTrack &track, int trackIndex);
95 void doRemoveSubtitleItems(int trackIndex, const QList<Subtitles::SubtitleItem> &subtitles);
96 void doInsertSubtitleItems(int trackIndex, const QList<Subtitles::SubtitleItem> &subtitles);
97 void doSetText(int trackIndex, int itemIndex, const QString &text);
98 void doSetTime(int trackIndex, int itemIndex, int64_t startTime, int64_t endTime);
99
100signals:
101 void tracksChanged(int count);
102 void modified();
103
104protected:
105 // Implement QAbstractItemModel
106 int rowCount(const QModelIndex &parent) const;
107 int columnCount(const QModelIndex &parent) const;
108 QVariant data(const QModelIndex &index, int role) const;
109 QVariant headerData(int section, Qt::Orientation orientation, int role) const;
110 QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
111 QModelIndex parent(const QModelIndex &index) const;
112 QHash<int, QByteArray> roleNames() const;
113
114private:
115 void requestFeedCommit(int trackIndex);
116 void commitToFeed(int trackIndex);
117 Mlt::Producer *m_producer;
118 QList<SubtitlesModel::SubtitleTrack> m_tracks;
119 QList<QList<Subtitles::SubtitleItem>> m_items;
120 QTimer *m_commitTimer;
121 int m_commitTrack;
122};
123
124#endif // SUBTITLESMODEL_H