]>
Commit | Line | Data |
---|---|---|
f2739bae SA |
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 | #include "metadata_obj.hpp" | |
21 | ||
22 | ||
23 | namespace pv { | |
24 | ||
25 | const char* MetadataObjectNames[MetadataObjTypeCount] = { | |
26 | "main_view_range", | |
27 | "selection", | |
28 | "time_marker" | |
29 | }; | |
30 | ||
31 | const char* MetadataValueNames[MetadataObjValueCount] = { | |
32 | "start_sample", | |
33 | "end_sample" | |
34 | "text" | |
35 | }; | |
36 | ||
37 | ||
38 | MetadataObject::MetadataObject(MetadataObjManager* obj_manager, uint32_t obj_id, | |
39 | MetadataObjectType obj_type) : | |
40 | obj_manager_(obj_manager), | |
41 | id_(obj_id), | |
42 | type_(obj_type) | |
43 | { | |
44 | // Make sure we accept all value type indices | |
45 | values_.resize(MetadataObjValueCount); | |
46 | } | |
47 | ||
48 | uint32_t MetadataObject::id() const | |
49 | { | |
50 | return id_; | |
51 | } | |
52 | ||
53 | MetadataObjectType MetadataObject::type() const | |
54 | { | |
55 | return type_; | |
56 | } | |
57 | ||
58 | void MetadataObject::set_value(MetadataValueType value_type, QVariant& value) | |
59 | { | |
60 | values_.at((uint8_t)value_type) = value; | |
61 | obj_manager_->notify_observers(this, value_type); | |
62 | } | |
63 | ||
64 | QVariant MetadataObject::value(MetadataValueType value_type) const | |
65 | { | |
66 | return values_.at((uint8_t)value_type); | |
67 | } | |
68 | ||
69 | ||
70 | MetadataObject* MetadataObjManager::create_object(MetadataObjectType obj_type) | |
71 | { | |
72 | // Note: This function is not reentrant as race conditions between | |
73 | // emplace_back() and back() may occur | |
74 | ||
75 | objects_.emplace_back(this, objects_.size(), obj_type); | |
76 | MetadataObject* obj = &(objects_.back()); | |
77 | ||
78 | for (MetadataObjObserverInterface *cb : callbacks_) | |
79 | cb->on_metadata_object_created(obj->id(), obj->type()); | |
80 | ||
81 | return obj; | |
82 | } | |
83 | ||
84 | void MetadataObjManager::delete_object(uint32_t obj_id) | |
85 | { | |
86 | MetadataObjectType type = objects_.at(obj_id).type(); | |
87 | ||
88 | objects_.erase(std::remove_if(objects_.begin(), objects_.end(), | |
89 | [&](MetadataObject obj) { return obj.id() == obj_id; }), | |
90 | objects_.end()); | |
91 | ||
92 | for (MetadataObjObserverInterface *cb : callbacks_) | |
93 | cb->on_metadata_object_deleted(obj_id, type); | |
94 | } | |
95 | ||
96 | MetadataObject* MetadataObjManager::find_object_by_type(MetadataObjectType obj_type) | |
97 | { | |
98 | for (MetadataObject& obj : objects_) | |
99 | if (obj.type() == obj_type) | |
100 | return &obj; | |
101 | ||
102 | return nullptr; | |
103 | } | |
104 | ||
105 | MetadataObject* MetadataObjManager::object(uint32_t obj_id) | |
106 | { | |
107 | return &(objects_.at(obj_id)); | |
108 | } | |
109 | ||
110 | void MetadataObjManager::add_observer(MetadataObjObserverInterface *cb) | |
111 | { | |
112 | callbacks_.emplace_back(cb); | |
113 | } | |
114 | ||
115 | void MetadataObjManager::remove_observer(MetadataObjObserverInterface *cb) | |
116 | { | |
117 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
118 | if (*cb_it == cb) { | |
119 | callbacks_.erase(cb_it); | |
120 | break; | |
121 | } | |
122 | } | |
123 | ||
124 | void MetadataObjManager::save_objects(QSettings &settings) const | |
125 | { | |
126 | (void)settings; | |
127 | } | |
128 | ||
129 | void MetadataObjManager::restore_objects(QSettings &settings) | |
130 | { | |
131 | (void)settings; | |
132 | } | |
133 | ||
134 | void MetadataObjManager::notify_observers(MetadataObject* obj, | |
135 | MetadataValueType changed_value) | |
136 | { | |
137 | for (MetadataObjObserverInterface *cb : callbacks_) | |
138 | cb->on_metadata_object_changed(obj->id(), obj->type(), changed_value, | |
139 | obj->value(changed_value)); | |
140 | } | |
141 | ||
142 | } // namespace pv |