CuteLogger
Fast and simple logging solution for Qt based applications
markersmodel.h
1 /*
2  * Copyright (c) 2021-2023 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 MARKERSMODEL_H
19 #define MARKERSMODEL_H
20 
21 #include <MltProducer.h>
22 
23 #include <QAbstractItemModel>
24 #include <QColor>
25 #include <QString>
26 
27 namespace Markers {
28 
29 class Marker
30 {
31 public:
32  QString text;
33  int start {-1};
34  int end {-1};
35  QColor color;
36 };
37 
38 }
39 
40 class MarkersModel : public QAbstractItemModel
41 {
42  Q_OBJECT
43  Q_PROPERTY(QStringList recentColors READ recentColors NOTIFY recentColorsChanged)
44 
45 public:
46 
47  enum Roles {
48  TextRole = Qt::UserRole + 1,
49  StartRole,
50  EndRole,
51  ColorRole,
52  };
53 
54  explicit MarkersModel(QObject *parent = 0);
55  virtual ~MarkersModel();
56 
57  void load(Mlt::Producer *producer);
58  Markers::Marker getMarker(int markerIndex);
59  int uniqueKey() const;
60  int markerIndexForPosition(int position);
61  int markerIndexForRange(int start, int end);
62  int rangeMarkerIndexForPosition(int position);
63  Q_INVOKABLE int nextMarkerPosition(int position);
64  Q_INVOKABLE int prevMarkerPosition(int position);
65  QModelIndex modelIndexForRow(int row);
66  QMap<int, QString> ranges();
67  QStringList recentColors();
68  QList<Markers::Marker> getMarkers() const;
69  QList<QColor> allColors() const;
70 
71  // These should only be called by the marker commands
72  void doRemove(int markerIndex);
73  void doInsert(int markerIndex, const Markers::Marker &marker);
74  void doAppend(const Markers::Marker &marker);
75  void doUpdate(int markerIndex, const Markers::Marker &marker);
76  void doClear();
77  void doReplace(QList<Markers::Marker> &markers);
78  void doShift(int shiftPosition, int shiftAmount);
79 
80 signals:
81  void rangesChanged();
82  void modified();
83  void recentColorsChanged();
84 
85 public slots:
86  void remove(int markerIndex);
87  void append(const Markers::Marker &marker);
88  void update(int markerIndex, const Markers::Marker &marker);
89  void move(int markerIndex, int start, int end);
90  void setColor(int markerIndex, const QColor &color);
91  void clear();
92 
93 protected:
94  // Implement QAbstractItemModel
95  int rowCount(const QModelIndex &parent) const;
96  int columnCount(const QModelIndex &parent) const;
97  QVariant data(const QModelIndex &index, int role) const;
98  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
99  QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
100  QModelIndex parent(const QModelIndex &index) const;
101  QHash<int, QByteArray> roleNames() const;
102 
103 private:
104  int markerCount() const;
105  int keyIndex(int key) const;
106  Mlt::Properties *getMarkerProperties(int markerIndex);
107  void updateRecentColors(const QColor &color);
108 
109  Mlt::Producer *m_producer;
110  QList<int> m_keys;
111  QMap<QRgb, QString> m_recentColors;
112 };
113 
114 #endif // MARKERSMODEL_H