]> sigrok.org Git - pulseview.git/blame - pv/views/tabular_decoder/view.cpp
TabularDecView: Increase robustness
[pulseview.git] / pv / views / tabular_decoder / view.cpp
CommitLineData
24d69d27
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 <climits>
21
f54e68b0 22#include <QApplication>
24d69d27
SA
23#include <QDebug>
24#include <QFileDialog>
f54e68b0
SA
25#include <QFontMetrics>
26#include <QHeaderView>
24d69d27
SA
27#include <QLabel>
28#include <QMenu>
29#include <QMessageBox>
30#include <QToolBar>
31#include <QVBoxLayout>
32
33#include <libsigrokdecode/libsigrokdecode.h>
34
35#include "view.hpp"
36
37#include "pv/globalsettings.hpp"
38#include "pv/util.hpp"
39#include "pv/data/decode/decoder.hpp"
40
41using pv::data::DecodeSignal;
42using pv::data::SignalBase;
43using pv::data::decode::Decoder;
44using pv::util::Timestamp;
45
f54e68b0 46using std::make_shared;
24d69d27
SA
47using std::shared_ptr;
48
49namespace pv {
50namespace views {
51namespace tabular_decoder {
52
f54e68b0
SA
53QSize QCustomTableView::minimumSizeHint() const
54{
55 QSize size(QTableView::sizeHint());
56
57 int width = 0;
58 for (int i = 0; i < horizontalHeader()->count(); i++)
59 if (!horizontalHeader()->isSectionHidden(i))
60 width += horizontalHeader()->sectionSizeHint(i);
61
62 size.setWidth(width + (horizontalHeader()->count() * 1));
63
64 return size;
65}
66
67QSize QCustomTableView::sizeHint() const
68{
69 return minimumSizeHint();
70}
71
24d69d27
SA
72
73View::View(Session &session, bool is_main_view, QMainWindow *parent) :
74 ViewBase(session, is_main_view, parent),
75
76 // Note: Place defaults in View::reset_view_state(), not here
77 parent_(parent),
78 decoder_selector_(new QComboBox()),
79 save_button_(new QToolButton()),
80 save_action_(new QAction(this)),
f54e68b0 81 table_view_(new QCustomTableView()),
24d69d27 82 model_(new AnnotationCollectionModel()),
f54e68b0
SA
83 signal_(nullptr),
84 updating_data_(false)
24d69d27
SA
85{
86 QVBoxLayout *root_layout = new QVBoxLayout(this);
87 root_layout->setContentsMargins(0, 0, 0, 0);
88 root_layout->addWidget(table_view_);
89
90 // Create toolbar
91 QToolBar* toolbar = new QToolBar();
92 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
93 parent->addToolBar(toolbar);
94
95 // Populate toolbar
96 toolbar->addWidget(new QLabel(tr("Decoder:")));
97 toolbar->addWidget(decoder_selector_);
98 toolbar->addSeparator();
99 toolbar->addWidget(save_button_);
100
101 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
102 this, SLOT(on_selected_decoder_changed(int)));
103
104 // Configure widgets
105 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
106
107 // Configure actions
108 save_action_->setText(tr("&Save..."));
109 save_action_->setIcon(QIcon::fromTheme("document-save-as",
110 QIcon(":/icons/document-save-as.png")));
111 save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
112 connect(save_action_, SIGNAL(triggered(bool)),
113 this, SLOT(on_actionSave_triggered()));
114
115 QMenu *save_menu = new QMenu();
116 connect(save_menu, SIGNAL(triggered(QAction*)),
117 this, SLOT(on_actionSave_triggered(QAction*)));
118
119 save_button_->setMenu(save_menu);
120 save_button_->setDefaultAction(save_action_);
121 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
122
123 // Set up the table view
124 table_view_->setModel(model_);
125 table_view_->setSortingEnabled(true);
126 table_view_->sortByColumn(0, Qt::AscendingOrder);
127
f54e68b0
SA
128 const int font_height = QFontMetrics(QApplication::font()).height();
129 table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4);
130
131 table_view_->horizontalHeader()->setSectionResizeMode(model_->columnCount() - 1, QHeaderView::Stretch);
132
133 table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
134 parent->setSizePolicy(table_view_->sizePolicy());
135
24d69d27
SA
136 reset_view_state();
137}
138
139ViewType View::get_type() const
140{
141 return ViewTypeTabularDecoder;
142}
143
144void View::reset_view_state()
145{
146 ViewBase::reset_view_state();
147
148 decoder_selector_->clear();
149}
150
151void View::clear_decode_signals()
152{
153 ViewBase::clear_decode_signals();
154
155 reset_data();
156 reset_view_state();
157}
158
159void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
160{
161 ViewBase::add_decode_signal(signal);
162
163 connect(signal.get(), SIGNAL(name_changed(const QString&)),
164 this, SLOT(on_signal_name_changed(const QString&)));
165 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
166 this, SLOT(on_decoder_stacked(void*)));
167 connect(signal.get(), SIGNAL(decoder_removed(void*)),
168 this, SLOT(on_decoder_removed(void*)));
169
f54e68b0 170 // Add the top-level decoder provided by this signal
24d69d27 171 auto stack = signal->decoder_stack();
f54e68b0
SA
172 if (!stack.empty()) {
173 shared_ptr<Decoder>& dec = stack.at(0);
174 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
175 }
24d69d27
SA
176}
177
178void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
179{
180 // Remove all decoders provided by this signal
181 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
182 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
183
184 if (index != -1)
185 decoder_selector_->removeItem(index);
186 }
187
188 ViewBase::remove_decode_signal(signal);
189
190 if (signal.get() == signal_) {
191 reset_data();
192 update_data();
193 reset_view_state();
194 }
195}
196
197void View::save_settings(QSettings &settings) const
198{
199 ViewBase::save_settings(settings);
200}
201
202void View::restore_settings(QSettings &settings)
203{
204 // Note: It is assumed that this function is only called once,
205 // immediately after restoring a previous session.
206 ViewBase::restore_settings(settings);
207}
208
209void View::reset_data()
210{
211 signal_ = nullptr;
212 decoder_ = nullptr;
213}
214
215void View::update_data()
216{
217 if (!signal_)
218 return;
219
f54e68b0
SA
220 if (updating_data_) {
221 if (!delayed_view_updater_.isActive())
222 delayed_view_updater_.start();
223 return;
224 }
225
226 updating_data_ = true;
227
228 table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
229 model_->set_signal_and_segment(signal_, current_segment_);
230
231 updating_data_ = false;
24d69d27
SA
232}
233
234void View::save_data() const
235{
236 assert(decoder_);
237 assert(signal_);
238
239 if (!signal_)
240 return;
241
242/* GlobalSettings settings;
243 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
244
245 const QString file_name = QFileDialog::getSaveFileName(
246 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
247
248 if (file_name.isEmpty())
249 return;
250
251 QFile file(file_name);
252 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
253 pair<size_t, size_t> selection = hex_view_->get_selection();
254
255 vector<uint8_t> data;
256 data.resize(selection.second - selection.first + 1);
257
258 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
259 bin_class_id_, selection.first, selection.second, &data);
260
261 int64_t bytes_written = file.write((const char*)data.data(), data.size());
262
263 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
264 QMessageBox msg(parent_);
265 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
266 msg.setStandardButtons(QMessageBox::Ok);
267 msg.setIcon(QMessageBox::Warning);
268 msg.exec();
269 return;
270 }
271 } */
272}
273
274void View::on_selected_decoder_changed(int index)
275{
02c87df7 276 if (signal_) {
24d69d27 277 disconnect(signal_, SIGNAL(new_annotations()));
02c87df7
SA
278 disconnect(signal_, SIGNAL(decode_reset()));
279 }
24d69d27
SA
280
281 reset_data();
282
283 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
284
285 // Find the signal that contains the selected decoder
286 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
287 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
288 if (decoder_ == dec.get())
289 signal_ = ds.get();
290
02c87df7 291 if (signal_) {
24d69d27 292 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
02c87df7
SA
293 connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset()));
294 }
24d69d27
SA
295
296 update_data();
297}
298
299void View::on_signal_name_changed(const QString &name)
300{
301 (void)name;
302
303 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
304 assert(sb);
305
306 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
307 assert(signal);
308
f54e68b0 309 // Update the top-level decoder provided by this signal
24d69d27 310 auto stack = signal->decoder_stack();
f54e68b0
SA
311 if (!stack.empty()) {
312 shared_ptr<Decoder>& dec = stack.at(0);
313 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
24d69d27 314
f54e68b0
SA
315 if (index != -1)
316 decoder_selector_->setItemText(index, signal->name());
317 }
24d69d27
SA
318}
319
320void View::on_new_annotations()
321{
322 if (!delayed_view_updater_.isActive())
323 delayed_view_updater_.start();
324}
325
02c87df7
SA
326void View::on_decoder_reset()
327{
328 // Invalidate the model's data connection immediately - otherwise we
329 // will use a stale pointer in model_->index() when called from the table view
330 model_->set_signal_and_segment(signal_, current_segment_);
331}
332
24d69d27
SA
333void View::on_decoder_stacked(void* decoder)
334{
24d69d27
SA
335 Decoder* d = static_cast<Decoder*>(decoder);
336
337 // Find the signal that contains the selected decoder
338 DecodeSignal* signal = nullptr;
339
340 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
341 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
342 if (d == dec.get())
343 signal = ds.get();
344
345 assert(signal);
346
f54e68b0
SA
347 if (signal == signal_)
348 update_data();
24d69d27
SA
349}
350
351void View::on_decoder_removed(void* decoder)
352{
353 Decoder* d = static_cast<Decoder*>(decoder);
354
355 // Remove the decoder from the list
356 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
357
358 if (index != -1)
359 decoder_selector_->removeItem(index);
360}
361
362void View::on_actionSave_triggered(QAction* action)
363{
364 (void)action;
365
366 save_data();
367}
368
369void View::perform_delayed_view_update()
370{
371 update_data();
372}
373
374
375} // namespace tabular_decoder
376} // namespace views
377} // namespace pv