]> sigrok.org Git - pulseview.git/blob - pv/metadata_obj.hpp
Add metadata object handling
[pulseview.git] / pv / metadata_obj.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef PULSEVIEW_PV_METADATA_OBJ_HPP
21 #define PULSEVIEW_PV_METADATA_OBJ_HPP
22
23 #include <deque>
24 #include <vector>
25
26 #include <QObject>
27 #include <QSettings>
28 #include <QString>
29
30 using std::deque;
31 using std::vector;
32
33 namespace pv {
34
35
36 // When adding an entry here, don't forget to update MetadataObjectNames as well
37 enum MetadataObjectType {
38         MetadataObjMainViewRange,
39         MetadataObjSelection,
40         MetadataObjTimeMarker,
41         MetadataObjTypeCount  // Indicates how many metadata object types there are, must always be last
42 };
43
44 // When adding an entry here, don't forget to update MetadataValueNames as well
45 enum MetadataValueType {
46         MetadataObjStartSample,
47         MetadataObjEndSample,
48         MetadataObjText,
49         MetadataObjValueCount  // Indicates how many metadata value types there are, must always be last
50 };
51
52 extern const char* MetadataObjectNames[MetadataObjTypeCount];
53 extern const char* MetadataValueNames[MetadataObjValueCount];
54
55
56 class MetadataObjManager;
57
58
59 class MetadataObjObserverInterface
60 {
61 public:
62         virtual void on_metadata_object_created(uint32_t obj_id, MetadataObjectType obj_type) = 0;
63         virtual void on_metadata_object_deleted(uint32_t obj_id, MetadataObjectType obj_type) = 0;
64         virtual void on_metadata_object_changed(uint32_t obj_id, MetadataObjectType obj_type,
65                 MetadataValueType value_type, const QVariant& value) = 0;
66 };
67
68
69 class MetadataObject
70 {
71 public:
72         MetadataObject(MetadataObjManager* obj_manager, uint32_t obj_id, MetadataObjectType obj_type);
73         virtual ~MetadataObject() = default;
74
75         virtual uint32_t id() const;
76         virtual MetadataObjectType type() const;
77
78         virtual void set_value(MetadataValueType value_type, QVariant& value);
79         virtual QVariant value(MetadataValueType value_type) const;
80 private:
81         MetadataObjManager* obj_manager_;
82         uint32_t id_;
83         MetadataObjectType type_;
84         vector<QVariant> values_;
85 };
86
87
88 class MetadataObjManager : public QObject
89 {
90         Q_OBJECT
91
92 public:
93         MetadataObject* create_object(MetadataObjectType obj_type);
94         void delete_object(uint32_t obj_id);
95         MetadataObject* find_object_by_type(MetadataObjectType obj_type);
96         MetadataObject* object(uint32_t obj_id);
97
98         void add_observer(MetadataObjObserverInterface *cb);
99         void remove_observer(MetadataObjObserverInterface *cb);
100
101         void save_objects(QSettings &settings) const;
102         void restore_objects(QSettings &settings);
103
104         void notify_observers(MetadataObject* obj, MetadataValueType changed_value);
105
106 private:
107         vector<MetadataObjObserverInterface*> callbacks_;
108         deque<MetadataObject> objects_;
109 };
110
111
112 } // namespace pv
113
114 #endif // PULSEVIEW_PV_METADATA_OBJ_HPP