]> sigrok.org Git - pulseview.git/blame - pv/views/tabular_decoder/view.cpp
Add row colors and some fixes
[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))
88a25978 60 width += horizontalHeader()->sectionSize(i);
f54e68b0
SA
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
88a25978
SA
131 table_view_->horizontalHeader()->setStretchLastSection(true);
132 table_view_->horizontalHeader()->setCascadingSectionResizes(true);
133 table_view_->horizontalHeader()->setSectionsMovable(true);
f54e68b0
SA
134
135 table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
136 parent->setSizePolicy(table_view_->sizePolicy());
137
24d69d27
SA
138 reset_view_state();
139}
140
141ViewType View::get_type() const
142{
143 return ViewTypeTabularDecoder;
144}
145
146void View::reset_view_state()
147{
148 ViewBase::reset_view_state();
149
150 decoder_selector_->clear();
151}
152
153void View::clear_decode_signals()
154{
155 ViewBase::clear_decode_signals();
156
157 reset_data();
158 reset_view_state();
159}
160
161void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
162{
163 ViewBase::add_decode_signal(signal);
164
165 connect(signal.get(), SIGNAL(name_changed(const QString&)),
166 this, SLOT(on_signal_name_changed(const QString&)));
88a25978
SA
167
168 // Note: At time of initial creation, decode signals have no decoders so we
169 // need to watch for decoder stacking events
170
24d69d27
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
88a25978 176 // Add the top-level decoder provided by an already-existing signal
24d69d27 177 auto stack = signal->decoder_stack();
f54e68b0
SA
178 if (!stack.empty()) {
179 shared_ptr<Decoder>& dec = stack.at(0);
180 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
181 }
24d69d27
SA
182}
183
184void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
185{
186 // Remove all decoders provided by this signal
187 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
188 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
189
190 if (index != -1)
191 decoder_selector_->removeItem(index);
192 }
193
194 ViewBase::remove_decode_signal(signal);
195
196 if (signal.get() == signal_) {
197 reset_data();
198 update_data();
199 reset_view_state();
200 }
201}
202
203void View::save_settings(QSettings &settings) const
204{
205 ViewBase::save_settings(settings);
206}
207
208void View::restore_settings(QSettings &settings)
209{
210 // Note: It is assumed that this function is only called once,
211 // immediately after restoring a previous session.
212 ViewBase::restore_settings(settings);
213}
214
215void View::reset_data()
216{
217 signal_ = nullptr;
218 decoder_ = nullptr;
219}
220
221void View::update_data()
222{
223 if (!signal_)
224 return;
225
f54e68b0
SA
226 if (updating_data_) {
227 if (!delayed_view_updater_.isActive())
228 delayed_view_updater_.start();
229 return;
230 }
231
232 updating_data_ = true;
233
234 table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
235 model_->set_signal_and_segment(signal_, current_segment_);
236
237 updating_data_ = false;
24d69d27
SA
238}
239
240void View::save_data() const
241{
242 assert(decoder_);
243 assert(signal_);
244
245 if (!signal_)
246 return;
247
248/* GlobalSettings settings;
249 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
250
251 const QString file_name = QFileDialog::getSaveFileName(
252 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
253
254 if (file_name.isEmpty())
255 return;
256
257 QFile file(file_name);
258 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
259 pair<size_t, size_t> selection = hex_view_->get_selection();
260
261 vector<uint8_t> data;
262 data.resize(selection.second - selection.first + 1);
263
264 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
265 bin_class_id_, selection.first, selection.second, &data);
266
267 int64_t bytes_written = file.write((const char*)data.data(), data.size());
268
269 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
270 QMessageBox msg(parent_);
271 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
272 msg.setStandardButtons(QMessageBox::Ok);
273 msg.setIcon(QMessageBox::Warning);
274 msg.exec();
275 return;
276 }
277 } */
278}
279
280void View::on_selected_decoder_changed(int index)
281{
02c87df7 282 if (signal_) {
88a25978 283 disconnect(signal_, SIGNAL(signal_color_changed()));
24d69d27 284 disconnect(signal_, SIGNAL(new_annotations()));
02c87df7
SA
285 disconnect(signal_, SIGNAL(decode_reset()));
286 }
24d69d27
SA
287
288 reset_data();
289
290 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
291
292 // Find the signal that contains the selected decoder
293 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
294 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
295 if (decoder_ == dec.get())
296 signal_ = ds.get();
297
02c87df7 298 if (signal_) {
88a25978 299 connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor)));
24d69d27 300 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
02c87df7
SA
301 connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset()));
302 }
24d69d27
SA
303
304 update_data();
305}
306
307void View::on_signal_name_changed(const QString &name)
308{
309 (void)name;
310
311 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
312 assert(sb);
313
314 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
315 assert(signal);
316
f54e68b0 317 // Update the top-level decoder provided by this signal
24d69d27 318 auto stack = signal->decoder_stack();
f54e68b0
SA
319 if (!stack.empty()) {
320 shared_ptr<Decoder>& dec = stack.at(0);
321 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
24d69d27 322
f54e68b0
SA
323 if (index != -1)
324 decoder_selector_->setItemText(index, signal->name());
325 }
24d69d27
SA
326}
327
88a25978
SA
328void View::on_signal_color_changed(const QColor &color)
329{
330 (void)color;
331
332 table_view_->update();
333}
334
24d69d27
SA
335void View::on_new_annotations()
336{
337 if (!delayed_view_updater_.isActive())
338 delayed_view_updater_.start();
339}
340
02c87df7
SA
341void View::on_decoder_reset()
342{
343 // Invalidate the model's data connection immediately - otherwise we
344 // will use a stale pointer in model_->index() when called from the table view
345 model_->set_signal_and_segment(signal_, current_segment_);
346}
347
24d69d27
SA
348void View::on_decoder_stacked(void* decoder)
349{
24d69d27
SA
350 Decoder* d = static_cast<Decoder*>(decoder);
351
352 // Find the signal that contains the selected decoder
353 DecodeSignal* signal = nullptr;
354
355 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
356 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
357 if (d == dec.get())
358 signal = ds.get();
359
360 assert(signal);
361
88a25978
SA
362 const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0);
363 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
364
365 if (index == -1) {
366 // Add the decoder to the list
367 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
368 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
369 }
24d69d27
SA
370}
371
372void View::on_decoder_removed(void* decoder)
373{
374 Decoder* d = static_cast<Decoder*>(decoder);
375
376 // Remove the decoder from the list
377 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
378
379 if (index != -1)
380 decoder_selector_->removeItem(index);
381}
382
383void View::on_actionSave_triggered(QAction* action)
384{
385 (void)action;
386
387 save_data();
388}
389
390void View::perform_delayed_view_update()
391{
392 update_data();
393}
394
395
396} // namespace tabular_decoder
397} // namespace views
398} // namespace pv