]> sigrok.org Git - pulseview.git/blame - pv/views/decoder_output/view.cpp
Add save feature to DecoderOutputView
[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>
13b726cd 24#include <QFileDialog>
bdbc561f 25#include <QLabel>
2bdc5796 26#include <QMenu>
13b726cd 27#include <QMessageBox>
a24412db 28#include <QToolBar>
2bdc5796
SA
29#include <QVBoxLayout>
30
4b97fe09
SA
31#include <libsigrokdecode/libsigrokdecode.h>
32
2bdc5796 33#include "view.hpp"
560f8377 34#include "QHexView.hpp"
2bdc5796 35
13b726cd 36#include "pv/globalsettings.hpp"
2bdc5796
SA
37#include "pv/session.hpp"
38#include "pv/util.hpp"
e77de61f 39#include "pv/data/decode/decoder.hpp"
2bdc5796 40
4b97fe09 41using pv::data::DecodeSignal;
bdbc561f 42using pv::data::SignalBase;
e77de61f 43using pv::data::decode::Decoder;
2bdc5796
SA
44using pv::util::TimeUnit;
45using pv::util::Timestamp;
46
e77de61f 47using std::dynamic_pointer_cast;
4b97fe09 48using std::numeric_limits;
2bdc5796
SA
49using std::shared_ptr;
50
51namespace pv {
52namespace views {
53namespace decoder_output {
54
03408f5f
SA
55const char* SaveTypeNames[SaveTypeCount] = {
56 "Binary",
57 "Hex Dump"
58};
59
60
a24412db 61View::View(Session &session, bool is_main_view, QMainWindow *parent) :
bdbc561f 62 ViewBase(session, is_main_view, parent),
2bdc5796
SA
63
64 // Note: Place defaults in View::reset_view_state(), not here
6961eab0 65 parent_(parent),
e77de61f 66 decoder_selector_(new QComboBox()),
560f8377 67 format_selector_(new QComboBox()),
e77de61f 68 class_selector_(new QComboBox()),
560f8377 69 stacked_widget_(new QStackedWidget()),
4b97fe09 70 hex_view_(new QHexView()),
03408f5f
SA
71 save_button_(new QToolButton()),
72 save_action_(new QAction(this)),
628b45cc 73 signal_(nullptr)
2bdc5796
SA
74{
75 QVBoxLayout *root_layout = new QVBoxLayout(this);
76 root_layout->setContentsMargins(0, 0, 0, 0);
77
bdbc561f
SA
78 // Create toolbar
79 QToolBar* toolbar = new QToolBar();
80 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
81 parent->addToolBar(toolbar);
a24412db 82
bdbc561f
SA
83 // Populate toolbar
84 toolbar->addWidget(new QLabel(tr("Decoder:")));
e77de61f
SA
85 toolbar->addWidget(decoder_selector_);
86 toolbar->addWidget(class_selector_);
861ab3a4
SA
87 toolbar->addSeparator();
88 toolbar->addWidget(new QLabel(tr("Show data as")));
89 toolbar->addWidget(format_selector_);
03408f5f
SA
90 toolbar->addSeparator();
91 toolbar->addWidget(save_button_);
861ab3a4
SA
92
93 // Add format types
94 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
a24412db 95
560f8377
SA
96 // Add widget stack
97 root_layout->addWidget(stacked_widget_);
98 stacked_widget_->addWidget(hex_view_);
4b97fe09
SA
99 stacked_widget_->setCurrentIndex(0);
100
e77de61f
SA
101 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
102 this, SLOT(on_selected_decoder_changed(int)));
b2b18d3a
SA
103 connect(class_selector_, SIGNAL(currentIndexChanged(int)),
104 this, SLOT(on_selected_class_changed(int)));
4b97fe09 105
a13d836f
SA
106 // Configure widgets
107 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
108 class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
109
03408f5f
SA
110 // Configure actions
111 save_action_->setText(tr("&Save..."));
112 save_action_->setIcon(QIcon::fromTheme("document-save-as",
113 QIcon(":/icons/document-save-as.png")));
114 save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
115 connect(save_action_, SIGNAL(triggered(bool)),
116 this, SLOT(on_actionSave_triggered()));
117
118 QMenu *save_menu = new QMenu();
119 connect(save_menu, SIGNAL(triggered(QAction*)),
120 this, SLOT(on_actionSave_triggered(QAction*)));
121
122 for (int i = 0; i < SaveTypeCount; i++) {
123 QAction *const action = save_menu->addAction(tr(SaveTypeNames[i]));
124 action->setData(qVariantFromValue(i));
125 }
126
127 save_button_->setMenu(save_menu);
128 save_button_->setDefaultAction(save_action_);
129 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
130
6961eab0
SA
131 parent->setSizePolicy(hex_view_->sizePolicy()); // TODO Must be updated when selected widget changes
132
2bdc5796
SA
133 reset_view_state();
134}
135
136View::~View()
137{
138}
139
db298158
SA
140ViewType View::get_type() const
141{
142 return ViewTypeDecoderOutput;
143}
144
2bdc5796
SA
145void View::reset_view_state()
146{
147 ViewBase::reset_view_state();
2bdc5796 148
03408f5f
SA
149 decoder_selector_->clear();
150 class_selector_->clear();
151 format_selector_->setCurrentIndex(0);
152 save_button_->setEnabled(false);
153
154 hex_view_->clear();
2bdc5796
SA
155}
156
157void View::clear_decode_signals()
158{
e77de61f
SA
159 ViewBase::clear_decode_signals();
160
03408f5f
SA
161 reset_data();
162 reset_view_state();
2bdc5796
SA
163}
164
165void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
166{
e77de61f
SA
167 ViewBase::add_decode_signal(signal);
168
2bdc5796 169 connect(signal.get(), SIGNAL(name_changed(const QString&)),
bdbc561f 170 this, SLOT(on_signal_name_changed(const QString&)));
e77de61f
SA
171 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
172 this, SLOT(on_decoder_stacked(void*)));
173 connect(signal.get(), SIGNAL(decoder_removed(void*)),
174 this, SLOT(on_decoder_removed(void*)));
175
176 // Add all decoders provided by this signal
177 auto stack = signal->decoder_stack();
178 if (stack.size() > 1) {
b2b18d3a
SA
179 for (const shared_ptr<Decoder>& dec : stack)
180 // Only add the decoder if it has binary output
181 if (dec->get_binary_class_count() > 0) {
182 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
183 decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
184 }
e77de61f
SA
185 } else
186 if (!stack.empty()) {
187 shared_ptr<Decoder>& dec = stack.at(0);
b2b18d3a
SA
188 if (dec->get_binary_class_count() > 0)
189 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
e77de61f 190 }
2bdc5796
SA
191}
192
193void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
194{
e77de61f
SA
195 // Remove all decoders provided by this signal
196 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
197 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
bdbc561f 198
e77de61f
SA
199 if (index != -1)
200 decoder_selector_->removeItem(index);
201 }
202
203 ViewBase::remove_decode_signal(signal);
4b97fe09
SA
204
205 if (signal.get() == signal_) {
03408f5f 206 reset_data();
4b97fe09 207 update_data();
03408f5f 208 reset_view_state();
4b97fe09 209 }
2bdc5796
SA
210}
211
212void View::save_settings(QSettings &settings) const
213{
214 (void)settings;
215}
216
217void View::restore_settings(QSettings &settings)
218{
219 // Note: It is assumed that this function is only called once,
220 // immediately after restoring a previous session.
221 (void)settings;
222}
223
03408f5f
SA
224void View::reset_data()
225{
226 signal_ = nullptr;
227 decoder_ = nullptr;
228 bin_class_id_ = 0;
229 binary_data_exists_ = false;
230
231 hex_view_->clear();
232}
233
4b97fe09
SA
234void View::update_data()
235{
03408f5f 236 if (!signal_)
4b97fe09 237 return;
4b97fe09 238
03408f5f 239 if (!binary_data_exists_)
4b97fe09 240 return;
4b97fe09 241
628b45cc
SA
242 const DecodeBinaryClass* bin_class =
243 signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
b2b18d3a 244
628b45cc 245 hex_view_->setData(bin_class);
13b726cd
SA
246
247 if (!save_button_->isEnabled())
248 save_button_->setEnabled(true);
249}
250
251void View::save_data() const
252{
253 assert(decoder_);
254 assert(signal_);
255
256 if (!signal_)
257 return;
258
259 GlobalSettings settings;
260 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
261
262 const QString file_name = QFileDialog::getSaveFileName(
263 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
264
265 if (file_name.isEmpty())
266 return;
267
268 QFile file(file_name);
269 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
270 pair<size_t, size_t> selection = hex_view_->get_selection();
271
272 vector<uint8_t> data;
273 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
274 bin_class_id_, selection.first, selection.second, &data);
275
276 int64_t bytes_written = file.write((const char*)data.data(), data.size());
277
278 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
279 QMessageBox msg(parent_);
280 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
281 msg.setStandardButtons(QMessageBox::Ok);
282 msg.setIcon(QMessageBox::Warning);
283 msg.exec();
284 return;
285 }
286 }
287}
288
289void View::save_data_as_hex_dump() const
290{
291
4b97fe09
SA
292}
293
e77de61f 294void View::on_selected_decoder_changed(int index)
4b97fe09
SA
295{
296 if (signal_)
b2b18d3a 297 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
4b97fe09 298
03408f5f
SA
299 reset_data();
300
e77de61f
SA
301 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
302
303 // Find the signal that contains the selected decoder
b2b18d3a
SA
304 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
305 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
306 if (decoder_ == dec.get())
307 signal_ = ds.get();
e77de61f 308
b2b18d3a
SA
309 class_selector_->clear();
310
311 if (signal_) {
312 // Populate binary class selector
ac9494ef
SA
313 uint32_t bin_classes = decoder_->get_binary_class_count();
314 for (uint32_t i = 0; i < bin_classes; i++) {
b2b18d3a 315 const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
cbf428c2 316 class_selector_->addItem(class_info->description, QVariant::fromValue(i));
b2b18d3a
SA
317 }
318
319 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
320 this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
e77de61f 321 }
4b97fe09 322
b2b18d3a
SA
323 update_data();
324}
325
326void View::on_selected_class_changed(int index)
327{
ac9494ef 328 bin_class_id_ = class_selector_->itemData(index).value<uint32_t>();
e77de61f 329
03408f5f
SA
330 binary_data_exists_ =
331 signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_);
332
e77de61f 333 update_data();
4b97fe09
SA
334}
335
bdbc561f 336void View::on_signal_name_changed(const QString &name)
2bdc5796 337{
e77de61f
SA
338 (void)name;
339
340 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
bdbc561f
SA
341 assert(sb);
342
e77de61f
SA
343 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
344 assert(signal);
345
346 // Update all decoder entries provided by this signal
347 auto stack = signal->decoder_stack();
348 if (stack.size() > 1) {
349 for (const shared_ptr<Decoder>& dec : stack) {
350 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
351 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
352
353 if (index != -1)
354 decoder_selector_->setItemText(index, title);
355 }
356 } else
357 if (!stack.empty()) {
358 shared_ptr<Decoder>& dec = stack.at(0);
359 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
360
361 if (index != -1)
362 decoder_selector_->setItemText(index, signal->name());
363 }
2bdc5796
SA
364}
365
b2b18d3a 366void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
4b97fe09 367{
b2b18d3a 368 if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
516d2128
SA
369 if (!delayed_view_updater_.isActive())
370 delayed_view_updater_.start();
4b97fe09
SA
371}
372
e77de61f
SA
373void View::on_decoder_stacked(void* decoder)
374{
375 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
376
377 Decoder* d = static_cast<Decoder*>(decoder);
378
b2b18d3a
SA
379 // Only add the decoder if it has binary output
380 if (d->get_binary_class_count() == 0)
381 return;
382
e77de61f
SA
383 // Find the signal that contains the selected decoder
384 DecodeSignal* signal = nullptr;
385
386 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
387 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
388 if (d == dec.get())
389 signal = ds.get();
390
391 assert(signal);
392
393 // Add the decoder to the list
394 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
395 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
396}
397
398void View::on_decoder_removed(void* decoder)
399{
400 Decoder* d = static_cast<Decoder*>(decoder);
401
402 // Remove the decoder from the list
403 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
404
405 if (index != -1)
406 decoder_selector_->removeItem(index);
407}
408
03408f5f
SA
409void View::on_actionSave_triggered(QAction* action)
410{
13b726cd
SA
411 int save_type = SaveTypeBinary;
412 if (action)
413 save_type = action->data().toInt();
414
415 if (save_type == SaveTypeBinary)
416 save_data();
417 if (save_type == SaveTypeHexDump)
418 save_data_as_hex_dump();
03408f5f
SA
419}
420
516d2128
SA
421void View::perform_delayed_view_update()
422{
03408f5f 423 if (!binary_data_exists_)
13b726cd 424 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_))
03408f5f
SA
425 binary_data_exists_ = true;
426
516d2128
SA
427 update_data();
428}
429
e77de61f 430
2bdc5796
SA
431} // namespace decoder_output
432} // namespace views
433} // namespace pv