]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2019 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 <climits> | |
21 | ||
22 | #include <QByteArray> | |
23 | #include <QDebug> | |
24 | #include <QLabel> | |
25 | #include <QMenu> | |
26 | #include <QToolBar> | |
27 | #include <QVBoxLayout> | |
28 | ||
29 | #include <libsigrokdecode/libsigrokdecode.h> | |
30 | ||
31 | #include "view.hpp" | |
32 | #include "QHexView.hpp" | |
33 | ||
34 | #include "pv/session.hpp" | |
35 | #include "pv/util.hpp" | |
36 | #include "pv/data/decode/decoder.hpp" | |
37 | ||
38 | using pv::data::DecodeSignal; | |
39 | using pv::data::SignalBase; | |
40 | using pv::data::decode::Decoder; | |
41 | using pv::util::TimeUnit; | |
42 | using pv::util::Timestamp; | |
43 | ||
44 | using std::dynamic_pointer_cast; | |
45 | using std::numeric_limits; | |
46 | using std::shared_ptr; | |
47 | ||
48 | namespace pv { | |
49 | namespace views { | |
50 | namespace decoder_output { | |
51 | ||
52 | View::View(Session &session, bool is_main_view, QMainWindow *parent) : | |
53 | ViewBase(session, is_main_view, parent), | |
54 | ||
55 | // Note: Place defaults in View::reset_view_state(), not here | |
56 | decoder_selector_(new QComboBox()), | |
57 | format_selector_(new QComboBox()), | |
58 | class_selector_(new QComboBox()), | |
59 | stacked_widget_(new QStackedWidget()), | |
60 | hex_view_(new QHexView()), | |
61 | signal_(nullptr) | |
62 | { | |
63 | QVBoxLayout *root_layout = new QVBoxLayout(this); | |
64 | root_layout->setContentsMargins(0, 0, 0, 0); | |
65 | ||
66 | // Create toolbar | |
67 | QToolBar* toolbar = new QToolBar(); | |
68 | toolbar->setContextMenuPolicy(Qt::PreventContextMenu); | |
69 | parent->addToolBar(toolbar); | |
70 | ||
71 | // Populate toolbar | |
72 | toolbar->addWidget(new QLabel(tr("Decoder:"))); | |
73 | toolbar->addWidget(decoder_selector_); | |
74 | toolbar->addWidget(class_selector_); | |
75 | toolbar->addSeparator(); | |
76 | toolbar->addWidget(new QLabel(tr("Show data as"))); | |
77 | toolbar->addWidget(format_selector_); | |
78 | ||
79 | // Add format types | |
80 | format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump"))); | |
81 | ||
82 | // Add widget stack | |
83 | root_layout->addWidget(stacked_widget_); | |
84 | stacked_widget_->addWidget(hex_view_); | |
85 | stacked_widget_->setCurrentIndex(0); | |
86 | ||
87 | connect(decoder_selector_, SIGNAL(currentIndexChanged(int)), | |
88 | this, SLOT(on_selected_decoder_changed(int))); | |
89 | connect(class_selector_, SIGNAL(currentIndexChanged(int)), | |
90 | this, SLOT(on_selected_class_changed(int))); | |
91 | ||
92 | // Configure widgets | |
93 | decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); | |
94 | class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); | |
95 | ||
96 | reset_view_state(); | |
97 | } | |
98 | ||
99 | View::~View() | |
100 | { | |
101 | } | |
102 | ||
103 | ViewType View::get_type() const | |
104 | { | |
105 | return ViewTypeDecoderOutput; | |
106 | } | |
107 | ||
108 | void View::reset_view_state() | |
109 | { | |
110 | ViewBase::reset_view_state(); | |
111 | } | |
112 | ||
113 | void View::clear_signals() | |
114 | { | |
115 | ViewBase::clear_signalbases(); | |
116 | signal_ = nullptr; | |
117 | } | |
118 | ||
119 | void View::clear_decode_signals() | |
120 | { | |
121 | ViewBase::clear_decode_signals(); | |
122 | ||
123 | decoder_selector_->clear(); | |
124 | class_selector_->clear(); | |
125 | format_selector_->setCurrentIndex(0); | |
126 | signal_ = nullptr; | |
127 | } | |
128 | ||
129 | void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
130 | { | |
131 | ViewBase::add_decode_signal(signal); | |
132 | ||
133 | connect(signal.get(), SIGNAL(name_changed(const QString&)), | |
134 | this, SLOT(on_signal_name_changed(const QString&))); | |
135 | connect(signal.get(), SIGNAL(decoder_stacked(void*)), | |
136 | this, SLOT(on_decoder_stacked(void*))); | |
137 | connect(signal.get(), SIGNAL(decoder_removed(void*)), | |
138 | this, SLOT(on_decoder_removed(void*))); | |
139 | ||
140 | // Add all decoders provided by this signal | |
141 | auto stack = signal->decoder_stack(); | |
142 | if (stack.size() > 1) { | |
143 | for (const shared_ptr<Decoder>& dec : stack) | |
144 | // Only add the decoder if it has binary output | |
145 | if (dec->get_binary_class_count() > 0) { | |
146 | QString title = QString("%1 (%2)").arg(signal->name(), dec->name()); | |
147 | decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get())); | |
148 | } | |
149 | } else | |
150 | if (!stack.empty()) { | |
151 | shared_ptr<Decoder>& dec = stack.at(0); | |
152 | if (dec->get_binary_class_count() > 0) | |
153 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get())); | |
154 | } | |
155 | } | |
156 | ||
157 | void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
158 | { | |
159 | // Remove all decoders provided by this signal | |
160 | for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) { | |
161 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
162 | ||
163 | if (index != -1) | |
164 | decoder_selector_->removeItem(index); | |
165 | } | |
166 | ||
167 | ViewBase::remove_decode_signal(signal); | |
168 | ||
169 | if (signal.get() == signal_) { | |
170 | signal_ = nullptr; | |
171 | decoder_ = nullptr; | |
172 | bin_class_id_ = 0; | |
173 | update_data(); | |
174 | } | |
175 | } | |
176 | ||
177 | void View::save_settings(QSettings &settings) const | |
178 | { | |
179 | (void)settings; | |
180 | } | |
181 | ||
182 | void View::restore_settings(QSettings &settings) | |
183 | { | |
184 | // Note: It is assumed that this function is only called once, | |
185 | // immediately after restoring a previous session. | |
186 | (void)settings; | |
187 | } | |
188 | ||
189 | void View::update_data() | |
190 | { | |
191 | if (!signal_) { | |
192 | hex_view_->clear(); | |
193 | return; | |
194 | } | |
195 | ||
196 | if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) { | |
197 | hex_view_->clear(); | |
198 | return; | |
199 | } | |
200 | ||
201 | const DecodeBinaryClass* bin_class = | |
202 | signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_); | |
203 | ||
204 | hex_view_->setData(bin_class); | |
205 | } | |
206 | ||
207 | void View::on_selected_decoder_changed(int index) | |
208 | { | |
209 | if (signal_) | |
210 | disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int))); | |
211 | ||
212 | decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>(); | |
213 | ||
214 | // Find the signal that contains the selected decoder | |
215 | signal_ = nullptr; | |
216 | ||
217 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
218 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
219 | if (decoder_ == dec.get()) | |
220 | signal_ = ds.get(); | |
221 | ||
222 | class_selector_->clear(); | |
223 | ||
224 | if (signal_) { | |
225 | // Populate binary class selector | |
226 | uint32_t bin_classes = decoder_->get_binary_class_count(); | |
227 | for (uint32_t i = 0; i < bin_classes; i++) { | |
228 | const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i); | |
229 | class_selector_->addItem(class_info->name, QVariant::fromValue(i)); | |
230 | } | |
231 | ||
232 | connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)), | |
233 | this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int))); | |
234 | } | |
235 | ||
236 | update_data(); | |
237 | } | |
238 | ||
239 | void View::on_selected_class_changed(int index) | |
240 | { | |
241 | bin_class_id_ = class_selector_->itemData(index).value<uint32_t>(); | |
242 | ||
243 | update_data(); | |
244 | } | |
245 | ||
246 | void View::on_signal_name_changed(const QString &name) | |
247 | { | |
248 | (void)name; | |
249 | ||
250 | SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender()); | |
251 | assert(sb); | |
252 | ||
253 | DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb); | |
254 | assert(signal); | |
255 | ||
256 | // Update all decoder entries provided by this signal | |
257 | auto stack = signal->decoder_stack(); | |
258 | if (stack.size() > 1) { | |
259 | for (const shared_ptr<Decoder>& dec : stack) { | |
260 | QString title = QString("%1 (%2)").arg(signal->name(), dec->name()); | |
261 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
262 | ||
263 | if (index != -1) | |
264 | decoder_selector_->setItemText(index, title); | |
265 | } | |
266 | } else | |
267 | if (!stack.empty()) { | |
268 | shared_ptr<Decoder>& dec = stack.at(0); | |
269 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
270 | ||
271 | if (index != -1) | |
272 | decoder_selector_->setItemText(index, signal->name()); | |
273 | } | |
274 | } | |
275 | ||
276 | void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id) | |
277 | { | |
278 | if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_)) | |
279 | update_data(); | |
280 | } | |
281 | ||
282 | void View::on_decoder_stacked(void* decoder) | |
283 | { | |
284 | // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change | |
285 | ||
286 | Decoder* d = static_cast<Decoder*>(decoder); | |
287 | ||
288 | // Only add the decoder if it has binary output | |
289 | if (d->get_binary_class_count() == 0) | |
290 | return; | |
291 | ||
292 | // Find the signal that contains the selected decoder | |
293 | DecodeSignal* signal = nullptr; | |
294 | ||
295 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
296 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
297 | if (d == dec.get()) | |
298 | signal = ds.get(); | |
299 | ||
300 | assert(signal); | |
301 | ||
302 | // Add the decoder to the list | |
303 | QString title = QString("%1 (%2)").arg(signal->name(), d->name()); | |
304 | decoder_selector_->addItem(title, QVariant::fromValue((void*)d)); | |
305 | } | |
306 | ||
307 | void View::on_decoder_removed(void* decoder) | |
308 | { | |
309 | Decoder* d = static_cast<Decoder*>(decoder); | |
310 | ||
311 | // Remove the decoder from the list | |
312 | int index = decoder_selector_->findData(QVariant::fromValue((void*)d)); | |
313 | ||
314 | if (index != -1) | |
315 | decoder_selector_->removeItem(index); | |
316 | } | |
317 | ||
318 | ||
319 | } // namespace decoder_output | |
320 | } // namespace views | |
321 | } // namespace pv |