]>
Commit | Line | Data |
---|---|---|
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" | |
9a35b05d | 38 | #include "pv/session.hpp" |
24d69d27 SA |
39 | #include "pv/util.hpp" |
40 | #include "pv/data/decode/decoder.hpp" | |
41 | ||
42 | using pv::data::DecodeSignal; | |
43 | using pv::data::SignalBase; | |
44 | using pv::data::decode::Decoder; | |
45 | using pv::util::Timestamp; | |
46 | ||
f54e68b0 | 47 | using std::make_shared; |
24d69d27 SA |
48 | using std::shared_ptr; |
49 | ||
50 | namespace pv { | |
51 | namespace views { | |
52 | namespace tabular_decoder { | |
53 | ||
f54e68b0 SA |
54 | QSize QCustomTableView::minimumSizeHint() const |
55 | { | |
56 | QSize size(QTableView::sizeHint()); | |
57 | ||
58 | int width = 0; | |
59 | for (int i = 0; i < horizontalHeader()->count(); i++) | |
60 | if (!horizontalHeader()->isSectionHidden(i)) | |
88a25978 | 61 | width += horizontalHeader()->sectionSize(i); |
f54e68b0 SA |
62 | |
63 | size.setWidth(width + (horizontalHeader()->count() * 1)); | |
64 | ||
65 | return size; | |
66 | } | |
67 | ||
68 | QSize QCustomTableView::sizeHint() const | |
69 | { | |
70 | return minimumSizeHint(); | |
71 | } | |
72 | ||
24d69d27 SA |
73 | |
74 | View::View(Session &session, bool is_main_view, QMainWindow *parent) : | |
75 | ViewBase(session, is_main_view, parent), | |
76 | ||
77 | // Note: Place defaults in View::reset_view_state(), not here | |
78 | parent_(parent), | |
79 | decoder_selector_(new QComboBox()), | |
80 | save_button_(new QToolButton()), | |
81 | save_action_(new QAction(this)), | |
f54e68b0 | 82 | table_view_(new QCustomTableView()), |
24d69d27 | 83 | model_(new AnnotationCollectionModel()), |
f54e68b0 SA |
84 | signal_(nullptr), |
85 | updating_data_(false) | |
24d69d27 SA |
86 | { |
87 | QVBoxLayout *root_layout = new QVBoxLayout(this); | |
88 | root_layout->setContentsMargins(0, 0, 0, 0); | |
89 | root_layout->addWidget(table_view_); | |
90 | ||
91 | // Create toolbar | |
92 | QToolBar* toolbar = new QToolBar(); | |
93 | toolbar->setContextMenuPolicy(Qt::PreventContextMenu); | |
94 | parent->addToolBar(toolbar); | |
95 | ||
96 | // Populate toolbar | |
97 | toolbar->addWidget(new QLabel(tr("Decoder:"))); | |
98 | toolbar->addWidget(decoder_selector_); | |
99 | toolbar->addSeparator(); | |
100 | toolbar->addWidget(save_button_); | |
101 | ||
102 | connect(decoder_selector_, SIGNAL(currentIndexChanged(int)), | |
103 | this, SLOT(on_selected_decoder_changed(int))); | |
104 | ||
105 | // Configure widgets | |
106 | decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); | |
107 | ||
108 | // Configure actions | |
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())); | |
115 | ||
116 | QMenu *save_menu = new QMenu(); | |
117 | connect(save_menu, SIGNAL(triggered(QAction*)), | |
118 | this, SLOT(on_actionSave_triggered(QAction*))); | |
119 | ||
120 | save_button_->setMenu(save_menu); | |
121 | save_button_->setDefaultAction(save_action_); | |
122 | save_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
123 | ||
124 | // Set up the table view | |
125 | table_view_->setModel(model_); | |
9a35b05d SA |
126 | table_view_->setSelectionBehavior(QAbstractItemView::SelectRows); |
127 | table_view_->setSelectionMode(QAbstractItemView::SingleSelection); | |
24d69d27 SA |
128 | table_view_->setSortingEnabled(true); |
129 | table_view_->sortByColumn(0, Qt::AscendingOrder); | |
130 | ||
f54e68b0 SA |
131 | const int font_height = QFontMetrics(QApplication::font()).height(); |
132 | table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4); | |
133 | ||
88a25978 SA |
134 | table_view_->horizontalHeader()->setStretchLastSection(true); |
135 | table_view_->horizontalHeader()->setCascadingSectionResizes(true); | |
136 | table_view_->horizontalHeader()->setSectionsMovable(true); | |
9a35b05d | 137 | table_view_->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu); |
f54e68b0 SA |
138 | |
139 | table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
140 | parent->setSizePolicy(table_view_->sizePolicy()); | |
141 | ||
9a35b05d SA |
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&))); | |
148 | ||
24d69d27 SA |
149 | reset_view_state(); |
150 | } | |
151 | ||
152 | ViewType View::get_type() const | |
153 | { | |
154 | return ViewTypeTabularDecoder; | |
155 | } | |
156 | ||
157 | void View::reset_view_state() | |
158 | { | |
159 | ViewBase::reset_view_state(); | |
160 | ||
161 | decoder_selector_->clear(); | |
162 | } | |
163 | ||
164 | void View::clear_decode_signals() | |
165 | { | |
166 | ViewBase::clear_decode_signals(); | |
167 | ||
168 | reset_data(); | |
169 | reset_view_state(); | |
170 | } | |
171 | ||
172 | void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
173 | { | |
174 | ViewBase::add_decode_signal(signal); | |
175 | ||
176 | connect(signal.get(), SIGNAL(name_changed(const QString&)), | |
177 | this, SLOT(on_signal_name_changed(const QString&))); | |
88a25978 SA |
178 | |
179 | // Note: At time of initial creation, decode signals have no decoders so we | |
180 | // need to watch for decoder stacking events | |
181 | ||
24d69d27 SA |
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*))); | |
186 | ||
88a25978 | 187 | // Add the top-level decoder provided by an already-existing signal |
24d69d27 | 188 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
189 | if (!stack.empty()) { |
190 | shared_ptr<Decoder>& dec = stack.at(0); | |
191 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get())); | |
192 | } | |
24d69d27 SA |
193 | } |
194 | ||
195 | void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
196 | { | |
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())); | |
200 | ||
201 | if (index != -1) | |
202 | decoder_selector_->removeItem(index); | |
203 | } | |
204 | ||
205 | ViewBase::remove_decode_signal(signal); | |
206 | ||
207 | if (signal.get() == signal_) { | |
208 | reset_data(); | |
209 | update_data(); | |
210 | reset_view_state(); | |
211 | } | |
212 | } | |
213 | ||
214 | void View::save_settings(QSettings &settings) const | |
215 | { | |
216 | ViewBase::save_settings(settings); | |
217 | } | |
218 | ||
219 | void View::restore_settings(QSettings &settings) | |
220 | { | |
221 | // Note: It is assumed that this function is only called once, | |
222 | // immediately after restoring a previous session. | |
223 | ViewBase::restore_settings(settings); | |
224 | } | |
225 | ||
226 | void View::reset_data() | |
227 | { | |
228 | signal_ = nullptr; | |
229 | decoder_ = nullptr; | |
230 | } | |
231 | ||
232 | void View::update_data() | |
233 | { | |
234 | if (!signal_) | |
235 | return; | |
236 | ||
f54e68b0 SA |
237 | if (updating_data_) { |
238 | if (!delayed_view_updater_.isActive()) | |
239 | delayed_view_updater_.start(); | |
240 | return; | |
241 | } | |
242 | ||
243 | updating_data_ = true; | |
244 | ||
245 | table_view_->setRootIndex(model_->index(1, 0, QModelIndex())); | |
246 | model_->set_signal_and_segment(signal_, current_segment_); | |
247 | ||
248 | updating_data_ = false; | |
24d69d27 SA |
249 | } |
250 | ||
251 | void 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 | data.resize(selection.second - selection.first + 1); | |
274 | ||
275 | signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_, | |
276 | bin_class_id_, selection.first, selection.second, &data); | |
277 | ||
278 | int64_t bytes_written = file.write((const char*)data.data(), data.size()); | |
279 | ||
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); | |
285 | msg.exec(); | |
286 | return; | |
287 | } | |
288 | } */ | |
289 | } | |
290 | ||
291 | void View::on_selected_decoder_changed(int index) | |
292 | { | |
02c87df7 | 293 | if (signal_) { |
88a25978 | 294 | disconnect(signal_, SIGNAL(signal_color_changed())); |
24d69d27 | 295 | disconnect(signal_, SIGNAL(new_annotations())); |
02c87df7 SA |
296 | disconnect(signal_, SIGNAL(decode_reset())); |
297 | } | |
24d69d27 SA |
298 | |
299 | reset_data(); | |
300 | ||
301 | decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>(); | |
302 | ||
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()) | |
307 | signal_ = ds.get(); | |
308 | ||
02c87df7 | 309 | if (signal_) { |
88a25978 | 310 | connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor))); |
24d69d27 | 311 | connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations())); |
02c87df7 SA |
312 | connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset())); |
313 | } | |
24d69d27 SA |
314 | |
315 | update_data(); | |
316 | } | |
317 | ||
318 | void View::on_signal_name_changed(const QString &name) | |
319 | { | |
320 | (void)name; | |
321 | ||
322 | SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender()); | |
323 | assert(sb); | |
324 | ||
325 | DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb); | |
326 | assert(signal); | |
327 | ||
f54e68b0 | 328 | // Update the top-level decoder provided by this signal |
24d69d27 | 329 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
330 | if (!stack.empty()) { |
331 | shared_ptr<Decoder>& dec = stack.at(0); | |
332 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
24d69d27 | 333 | |
f54e68b0 SA |
334 | if (index != -1) |
335 | decoder_selector_->setItemText(index, signal->name()); | |
336 | } | |
24d69d27 SA |
337 | } |
338 | ||
88a25978 SA |
339 | void View::on_signal_color_changed(const QColor &color) |
340 | { | |
341 | (void)color; | |
342 | ||
343 | table_view_->update(); | |
344 | } | |
345 | ||
24d69d27 SA |
346 | void View::on_new_annotations() |
347 | { | |
348 | if (!delayed_view_updater_.isActive()) | |
349 | delayed_view_updater_.start(); | |
350 | } | |
351 | ||
02c87df7 SA |
352 | void View::on_decoder_reset() |
353 | { | |
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_); | |
357 | } | |
358 | ||
24d69d27 SA |
359 | void View::on_decoder_stacked(void* decoder) |
360 | { | |
24d69d27 SA |
361 | Decoder* d = static_cast<Decoder*>(decoder); |
362 | ||
363 | // Find the signal that contains the selected decoder | |
364 | DecodeSignal* signal = nullptr; | |
365 | ||
366 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
367 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
368 | if (d == dec.get()) | |
369 | signal = ds.get(); | |
370 | ||
371 | assert(signal); | |
372 | ||
88a25978 SA |
373 | const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0); |
374 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
375 | ||
376 | if (index == -1) { | |
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)); | |
380 | } | |
24d69d27 SA |
381 | } |
382 | ||
383 | void View::on_decoder_removed(void* decoder) | |
384 | { | |
385 | Decoder* d = static_cast<Decoder*>(decoder); | |
386 | ||
387 | // Remove the decoder from the list | |
388 | int index = decoder_selector_->findData(QVariant::fromValue((void*)d)); | |
389 | ||
390 | if (index != -1) | |
391 | decoder_selector_->removeItem(index); | |
392 | } | |
393 | ||
394 | void View::on_actionSave_triggered(QAction* action) | |
395 | { | |
396 | (void)action; | |
397 | ||
398 | save_data(); | |
399 | } | |
400 | ||
9a35b05d SA |
401 | void View::on_table_item_clicked(const QModelIndex& index) |
402 | { | |
403 | (void)index; | |
404 | ||
405 | // Force repaint, otherwise the new selection isn't shown for some reason | |
406 | table_view_->viewport()->update(); | |
407 | } | |
408 | ||
409 | void View::on_table_item_double_clicked(const QModelIndex& index) | |
410 | { | |
411 | const Annotation* ann = static_cast<const Annotation*>(index.internalPointer()); | |
412 | ||
413 | shared_ptr<views::ViewBase> main_view = session_.main_view(); | |
414 | ||
415 | main_view->focus_on_range(ann->start_sample(), ann->end_sample()); | |
416 | } | |
417 | ||
418 | void View::on_table_header_requested(const QPoint& pos) | |
419 | { | |
420 | QMenu* menu = new QMenu(this); | |
421 | ||
422 | for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) { | |
423 | int column = table_view_->horizontalHeader()->logicalIndex(i); | |
424 | ||
425 | const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); | |
426 | QAction* action = new QAction(title, this); | |
427 | ||
428 | action->setCheckable(true); | |
429 | action->setChecked(!table_view_->horizontalHeader()->isSectionHidden(column)); | |
430 | action->setData(column); | |
431 | ||
432 | connect(action, SIGNAL(toggled(bool)), this, SLOT(on_table_header_toggled(bool))); | |
433 | ||
434 | menu->addAction(action); | |
435 | } | |
436 | ||
437 | menu->popup(table_view_->horizontalHeader()->viewport()->mapToGlobal(pos)); | |
438 | } | |
439 | ||
440 | void View::on_table_header_toggled(bool checked) | |
441 | { | |
442 | QAction* action = qobject_cast<QAction*>(QObject::sender()); | |
443 | assert(action); | |
444 | ||
445 | const int column = action->data().toInt(); | |
446 | ||
447 | table_view_->horizontalHeader()->setSectionHidden(column, !checked); | |
448 | } | |
449 | ||
24d69d27 SA |
450 | void View::perform_delayed_view_update() |
451 | { | |
452 | update_data(); | |
453 | } | |
454 | ||
455 | ||
456 | } // namespace tabular_decoder | |
457 | } // namespace views | |
458 | } // namespace pv |