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/util.hpp"
39 #include "pv/data/decode/decoder.hpp"
41 using pv::data::DecodeSignal;
42 using pv::data::SignalBase;
43 using pv::data::decode::Decoder;
44 using pv::util::Timestamp;
46 using std::make_shared;
47 using std::shared_ptr;
51 namespace tabular_decoder {
53 QSize QCustomTableView::minimumSizeHint() const
55 QSize size(QTableView::sizeHint());
58 for (int i = 0; i < horizontalHeader()->count(); i++)
59 if (!horizontalHeader()->isSectionHidden(i))
60 width += horizontalHeader()->sectionSize(i);
62 size.setWidth(width + (horizontalHeader()->count() * 1));
67 QSize QCustomTableView::sizeHint() const
69 return minimumSizeHint();
73 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
74 ViewBase(session, is_main_view, parent),
76 // Note: Place defaults in View::reset_view_state(), not here
78 decoder_selector_(new QComboBox()),
79 save_button_(new QToolButton()),
80 save_action_(new QAction(this)),
81 table_view_(new QCustomTableView()),
82 model_(new AnnotationCollectionModel()),
86 QVBoxLayout *root_layout = new QVBoxLayout(this);
87 root_layout->setContentsMargins(0, 0, 0, 0);
88 root_layout->addWidget(table_view_);
91 QToolBar* toolbar = new QToolBar();
92 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
93 parent->addToolBar(toolbar);
96 toolbar->addWidget(new QLabel(tr("Decoder:")));
97 toolbar->addWidget(decoder_selector_);
98 toolbar->addSeparator();
99 toolbar->addWidget(save_button_);
101 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
102 this, SLOT(on_selected_decoder_changed(int)));
105 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
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()));
115 QMenu *save_menu = new QMenu();
116 connect(save_menu, SIGNAL(triggered(QAction*)),
117 this, SLOT(on_actionSave_triggered(QAction*)));
119 save_button_->setMenu(save_menu);
120 save_button_->setDefaultAction(save_action_);
121 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
123 // Set up the table view
124 table_view_->setModel(model_);
125 table_view_->setSortingEnabled(true);
126 table_view_->sortByColumn(0, Qt::AscendingOrder);
128 const int font_height = QFontMetrics(QApplication::font()).height();
129 table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4);
131 table_view_->horizontalHeader()->setStretchLastSection(true);
132 table_view_->horizontalHeader()->setCascadingSectionResizes(true);
133 table_view_->horizontalHeader()->setSectionsMovable(true);
135 table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
136 parent->setSizePolicy(table_view_->sizePolicy());
141 ViewType View::get_type() const
143 return ViewTypeTabularDecoder;
146 void View::reset_view_state()
148 ViewBase::reset_view_state();
150 decoder_selector_->clear();
153 void View::clear_decode_signals()
155 ViewBase::clear_decode_signals();
161 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
163 ViewBase::add_decode_signal(signal);
165 connect(signal.get(), SIGNAL(name_changed(const QString&)),
166 this, SLOT(on_signal_name_changed(const QString&)));
168 // Note: At time of initial creation, decode signals have no decoders so we
169 // need to watch for decoder stacking events
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*)));
176 // Add the top-level decoder provided by an already-existing signal
177 auto stack = signal->decoder_stack();
178 if (!stack.empty()) {
179 shared_ptr<Decoder>& dec = stack.at(0);
180 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
184 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
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()));
191 decoder_selector_->removeItem(index);
194 ViewBase::remove_decode_signal(signal);
196 if (signal.get() == signal_) {
203 void View::save_settings(QSettings &settings) const
205 ViewBase::save_settings(settings);
208 void View::restore_settings(QSettings &settings)
210 // Note: It is assumed that this function is only called once,
211 // immediately after restoring a previous session.
212 ViewBase::restore_settings(settings);
215 void View::reset_data()
221 void View::update_data()
226 if (updating_data_) {
227 if (!delayed_view_updater_.isActive())
228 delayed_view_updater_.start();
232 updating_data_ = true;
234 table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
235 model_->set_signal_and_segment(signal_, current_segment_);
237 updating_data_ = false;
240 void View::save_data() const
248 /* GlobalSettings settings;
249 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
251 const QString file_name = QFileDialog::getSaveFileName(
252 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
254 if (file_name.isEmpty())
257 QFile file(file_name);
258 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
259 pair<size_t, size_t> selection = hex_view_->get_selection();
261 vector<uint8_t> data;
262 data.resize(selection.second - selection.first + 1);
264 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
265 bin_class_id_, selection.first, selection.second, &data);
267 int64_t bytes_written = file.write((const char*)data.data(), data.size());
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);
280 void View::on_selected_decoder_changed(int index)
283 disconnect(signal_, SIGNAL(signal_color_changed()));
284 disconnect(signal_, SIGNAL(new_annotations()));
285 disconnect(signal_, SIGNAL(decode_reset()));
290 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
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())
299 connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor)));
300 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
301 connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset()));
307 void View::on_signal_name_changed(const QString &name)
311 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
314 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
317 // Update the top-level decoder provided by this signal
318 auto stack = signal->decoder_stack();
319 if (!stack.empty()) {
320 shared_ptr<Decoder>& dec = stack.at(0);
321 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
324 decoder_selector_->setItemText(index, signal->name());
328 void View::on_signal_color_changed(const QColor &color)
332 table_view_->update();
335 void View::on_new_annotations()
337 if (!delayed_view_updater_.isActive())
338 delayed_view_updater_.start();
341 void View::on_decoder_reset()
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_);
348 void View::on_decoder_stacked(void* decoder)
350 Decoder* d = static_cast<Decoder*>(decoder);
352 // Find the signal that contains the selected decoder
353 DecodeSignal* signal = nullptr;
355 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
356 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
362 const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0);
363 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
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));
372 void View::on_decoder_removed(void* decoder)
374 Decoder* d = static_cast<Decoder*>(decoder);
376 // Remove the decoder from the list
377 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
380 decoder_selector_->removeItem(index);
383 void View::on_actionSave_triggered(QAction* action)
390 void View::perform_delayed_view_update()
396 } // namespace tabular_decoder