2 * This file is part of the PulseView project.
4 * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
22 #include <QApplication>
24 #include <QFileDialog>
25 #include <QFontMetrics>
26 #include <QHeaderView>
29 #include <QMessageBox>
31 #include <QVBoxLayout>
33 #include <libsigrokdecode/libsigrokdecode.h>
37 #include "pv/globalsettings.hpp"
38 #include "pv/session.hpp"
39 #include "pv/util.hpp"
40 #include "pv/data/decode/decoder.hpp"
42 using pv::data::DecodeSignal;
43 using pv::data::SignalBase;
44 using pv::data::decode::Decoder;
45 using pv::util::Timestamp;
47 using std::make_shared;
48 using std::shared_ptr;
52 namespace tabular_decoder {
54 QSize QCustomTableView::minimumSizeHint() const
56 QSize size(QTableView::sizeHint());
59 for (int i = 0; i < horizontalHeader()->count(); i++)
60 if (!horizontalHeader()->isSectionHidden(i))
61 width += horizontalHeader()->sectionSize(i);
63 size.setWidth(width + (horizontalHeader()->count() * 1));
68 QSize QCustomTableView::sizeHint() const
70 return minimumSizeHint();
74 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
75 ViewBase(session, is_main_view, parent),
77 // Note: Place defaults in View::reset_view_state(), not here
79 decoder_selector_(new QComboBox()),
80 save_button_(new QToolButton()),
81 save_action_(new QAction(this)),
82 table_view_(new QCustomTableView()),
83 model_(new AnnotationCollectionModel()),
87 QVBoxLayout *root_layout = new QVBoxLayout(this);
88 root_layout->setContentsMargins(0, 0, 0, 0);
89 root_layout->addWidget(table_view_);
92 QToolBar* toolbar = new QToolBar();
93 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
94 parent->addToolBar(toolbar);
97 toolbar->addWidget(new QLabel(tr("Decoder:")));
98 toolbar->addWidget(decoder_selector_);
99 toolbar->addSeparator();
100 toolbar->addWidget(save_button_);
102 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
103 this, SLOT(on_selected_decoder_changed(int)));
106 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
109 save_action_->setText(tr("&Save..."));
110 save_action_->setIcon(QIcon::fromTheme("document-save-as",
111 QIcon(":/icons/document-save-as.png")));
112 save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
113 connect(save_action_, SIGNAL(triggered(bool)),
114 this, SLOT(on_actionSave_triggered()));
116 QMenu *save_menu = new QMenu();
117 connect(save_menu, SIGNAL(triggered(QAction*)),
118 this, SLOT(on_actionSave_triggered(QAction*)));
120 save_button_->setMenu(save_menu);
121 save_button_->setDefaultAction(save_action_);
122 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
124 // Set up the table view
125 table_view_->setModel(model_);
126 table_view_->setSelectionBehavior(QAbstractItemView::SelectRows);
127 table_view_->setSelectionMode(QAbstractItemView::SingleSelection);
128 table_view_->setSortingEnabled(true);
129 table_view_->sortByColumn(0, Qt::AscendingOrder);
131 const int font_height = QFontMetrics(QApplication::font()).height();
132 table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4);
134 table_view_->horizontalHeader()->setStretchLastSection(true);
135 table_view_->horizontalHeader()->setCascadingSectionResizes(true);
136 table_view_->horizontalHeader()->setSectionsMovable(true);
137 table_view_->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
139 table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
140 parent->setSizePolicy(table_view_->sizePolicy());
142 connect(table_view_, SIGNAL(clicked(const QModelIndex&)),
143 this, SLOT(on_table_item_clicked(const QModelIndex&)));
144 connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)),
145 this, SLOT(on_table_item_double_clicked(const QModelIndex&)));
146 connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)),
147 this, SLOT(on_table_header_requested(const QPoint&)));
152 ViewType View::get_type() const
154 return ViewTypeTabularDecoder;
157 void View::reset_view_state()
159 ViewBase::reset_view_state();
161 decoder_selector_->clear();
164 void View::clear_decode_signals()
166 ViewBase::clear_decode_signals();
172 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
174 ViewBase::add_decode_signal(signal);
176 connect(signal.get(), SIGNAL(name_changed(const QString&)),
177 this, SLOT(on_signal_name_changed(const QString&)));
179 // Note: At time of initial creation, decode signals have no decoders so we
180 // need to watch for decoder stacking events
182 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
183 this, SLOT(on_decoder_stacked(void*)));
184 connect(signal.get(), SIGNAL(decoder_removed(void*)),
185 this, SLOT(on_decoder_removed(void*)));
187 // Add the top-level decoder provided by an already-existing signal
188 auto stack = signal->decoder_stack();
189 if (!stack.empty()) {
190 shared_ptr<Decoder>& dec = stack.at(0);
191 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
195 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
197 // Remove all decoders provided by this signal
198 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
199 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
202 decoder_selector_->removeItem(index);
205 ViewBase::remove_decode_signal(signal);
207 if (signal.get() == signal_) {
214 void View::save_settings(QSettings &settings) const
216 ViewBase::save_settings(settings);
219 void View::restore_settings(QSettings &settings)
221 // Note: It is assumed that this function is only called once,
222 // immediately after restoring a previous session.
223 ViewBase::restore_settings(settings);
226 void View::reset_data()
232 void View::update_data()
237 if (updating_data_) {
238 if (!delayed_view_updater_.isActive())
239 delayed_view_updater_.start();
243 updating_data_ = true;
245 table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
246 model_->set_signal_and_segment(signal_, current_segment_);
248 updating_data_ = false;
251 void View::save_data() const
259 /* GlobalSettings settings;
260 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
262 const QString file_name = QFileDialog::getSaveFileName(
263 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
265 if (file_name.isEmpty())
268 QFile file(file_name);
269 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
270 pair<size_t, size_t> selection = hex_view_->get_selection();
272 vector<uint8_t> data;
273 data.resize(selection.second - selection.first + 1);
275 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
276 bin_class_id_, selection.first, selection.second, &data);
278 int64_t bytes_written = file.write((const char*)data.data(), data.size());
280 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
281 QMessageBox msg(parent_);
282 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
283 msg.setStandardButtons(QMessageBox::Ok);
284 msg.setIcon(QMessageBox::Warning);
291 void View::on_selected_decoder_changed(int index)
294 disconnect(signal_, SIGNAL(signal_color_changed()));
295 disconnect(signal_, SIGNAL(new_annotations()));
296 disconnect(signal_, SIGNAL(decode_reset()));
301 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
303 // Find the signal that contains the selected decoder
304 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
305 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
306 if (decoder_ == dec.get())
310 connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor)));
311 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
312 connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset()));
318 void View::on_signal_name_changed(const QString &name)
322 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
325 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
328 // Update the top-level decoder provided by this signal
329 auto stack = signal->decoder_stack();
330 if (!stack.empty()) {
331 shared_ptr<Decoder>& dec = stack.at(0);
332 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
335 decoder_selector_->setItemText(index, signal->name());
339 void View::on_signal_color_changed(const QColor &color)
343 table_view_->update();
346 void View::on_new_annotations()
348 if (!delayed_view_updater_.isActive())
349 delayed_view_updater_.start();
352 void View::on_decoder_reset()
354 // Invalidate the model's data connection immediately - otherwise we
355 // will use a stale pointer in model_->index() when called from the table view
356 model_->set_signal_and_segment(signal_, current_segment_);
359 void View::on_decoder_stacked(void* decoder)
361 Decoder* d = static_cast<Decoder*>(decoder);
363 // Find the signal that contains the selected decoder
364 DecodeSignal* signal = nullptr;
366 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
367 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
373 const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0);
374 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
377 // Add the decoder to the list
378 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
379 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
383 void View::on_decoder_removed(void* decoder)
385 Decoder* d = static_cast<Decoder*>(decoder);
387 // Remove the decoder from the list
388 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
391 decoder_selector_->removeItem(index);
394 void View::on_actionSave_triggered(QAction* action)
401 void View::on_table_item_clicked(const QModelIndex& index)
405 // Force repaint, otherwise the new selection isn't shown for some reason
406 table_view_->viewport()->update();
409 void View::on_table_item_double_clicked(const QModelIndex& index)
411 const Annotation* ann = static_cast<const Annotation*>(index.internalPointer());
413 shared_ptr<views::ViewBase> main_view = session_.main_view();
415 main_view->focus_on_range(ann->start_sample(), ann->end_sample());
418 void View::on_table_header_requested(const QPoint& pos)
420 QMenu* menu = new QMenu(this);
422 for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) {
423 int column = table_view_->horizontalHeader()->logicalIndex(i);
425 const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
426 QAction* action = new QAction(title, this);
428 action->setCheckable(true);
429 action->setChecked(!table_view_->horizontalHeader()->isSectionHidden(column));
430 action->setData(column);
432 connect(action, SIGNAL(toggled(bool)), this, SLOT(on_table_header_toggled(bool)));
434 menu->addAction(action);
437 menu->popup(table_view_->horizontalHeader()->viewport()->mapToGlobal(pos));
440 void View::on_table_header_toggled(bool checked)
442 QAction* action = qobject_cast<QAction*>(QObject::sender());
445 const int column = action->data().toInt();
447 table_view_->horizontalHeader()->setSectionHidden(column, !checked);
450 void View::perform_delayed_view_update()
456 } // namespace tabular_decoder