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