]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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 <extdef.h> | |
21 | ||
22 | #include <algorithm> | |
23 | #include <cassert> | |
24 | ||
25 | #include <QAction> | |
26 | #include <QDebug> | |
27 | #include <QFileDialog> | |
28 | #include <QHelpEvent> | |
29 | #include <QMenu> | |
30 | #include <QMessageBox> | |
31 | #include <QSettings> | |
32 | #include <QToolTip> | |
33 | ||
34 | #include "mainbar.hpp" | |
35 | ||
36 | #include <boost/algorithm/string/join.hpp> | |
37 | ||
38 | #include <pv/data/mathsignal.hpp> | |
39 | #include <pv/devicemanager.hpp> | |
40 | #include <pv/devices/hardwaredevice.hpp> | |
41 | #include <pv/devices/inputfile.hpp> | |
42 | #include <pv/devices/sessionfile.hpp> | |
43 | #include <pv/dialogs/connect.hpp> | |
44 | #include <pv/dialogs/inputoutputoptions.hpp> | |
45 | #include <pv/dialogs/storeprogress.hpp> | |
46 | #include <pv/mainwindow.hpp> | |
47 | #include <pv/popups/channels.hpp> | |
48 | #include <pv/popups/deviceoptions.hpp> | |
49 | #include <pv/util.hpp> | |
50 | #include <pv/views/trace/view.hpp> | |
51 | #include <pv/widgets/exportmenu.hpp> | |
52 | #include <pv/widgets/importmenu.hpp> | |
53 | #ifdef ENABLE_DECODE | |
54 | #include <pv/data/decodesignal.hpp> | |
55 | #endif | |
56 | ||
57 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
58 | ||
59 | using std::back_inserter; | |
60 | using std::copy; | |
61 | using std::list; | |
62 | using std::make_pair; | |
63 | using std::make_shared; | |
64 | using std::map; | |
65 | using std::max; | |
66 | using std::min; | |
67 | using std::pair; | |
68 | using std::set; | |
69 | using std::shared_ptr; | |
70 | using std::string; | |
71 | using std::vector; | |
72 | ||
73 | using sigrok::Capability; | |
74 | using sigrok::ConfigKey; | |
75 | using sigrok::Error; | |
76 | using sigrok::InputFormat; | |
77 | using sigrok::OutputFormat; | |
78 | ||
79 | using boost::algorithm::join; | |
80 | ||
81 | namespace pv { | |
82 | namespace toolbars { | |
83 | ||
84 | const uint64_t MainBar::MinSampleCount = 100ULL; | |
85 | const uint64_t MainBar::MaxSampleCount = 1000000000000ULL; | |
86 | const uint64_t MainBar::DefaultSampleCount = 1000000; | |
87 | ||
88 | const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory"; | |
89 | const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
90 | ||
91 | MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view) : | |
92 | StandardBar(session, parent, view, false), | |
93 | action_new_view_(new QAction(this)), | |
94 | action_open_(new QAction(this)), | |
95 | action_save_(new QAction(this)), | |
96 | action_save_as_(new QAction(this)), | |
97 | action_save_selection_as_(new QAction(this)), | |
98 | action_restore_setup_(new QAction(this)), | |
99 | action_save_setup_(new QAction(this)), | |
100 | action_connect_(new QAction(this)), | |
101 | new_view_button_(new QToolButton()), | |
102 | open_button_(new QToolButton()), | |
103 | save_button_(new QToolButton()), | |
104 | device_selector_(parent, session.device_manager(), action_connect_), | |
105 | configure_button_(this), | |
106 | configure_button_action_(nullptr), | |
107 | channels_button_(this), | |
108 | channels_button_action_(nullptr), | |
109 | sample_count_(" samples", this), | |
110 | sample_rate_("Hz", this), | |
111 | updating_sample_rate_(false), | |
112 | updating_sample_count_(false), | |
113 | sample_count_supported_(false), | |
114 | #ifdef ENABLE_DECODE | |
115 | add_decoder_button_(new QToolButton()), | |
116 | #endif | |
117 | add_math_signal_button_(new QToolButton()) | |
118 | { | |
119 | setObjectName(QString::fromUtf8("MainBar")); | |
120 | ||
121 | setContextMenuPolicy(Qt::PreventContextMenu); | |
122 | ||
123 | // Actions | |
124 | action_new_view_->setText(tr("New &View")); | |
125 | action_new_view_->setIcon(QIcon::fromTheme("window-new", | |
126 | QIcon(":/icons/window-new.png"))); | |
127 | connect(action_new_view_, SIGNAL(triggered(bool)), | |
128 | this, SLOT(on_actionNewView_triggered())); | |
129 | ||
130 | action_open_->setText(tr("&Open...")); | |
131 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
132 | QIcon(":/icons/document-open.png"))); | |
133 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); | |
134 | connect(action_open_, SIGNAL(triggered(bool)), | |
135 | this, SLOT(on_actionOpen_triggered())); | |
136 | ||
137 | action_restore_setup_->setText(tr("Restore Session Setu&p...")); | |
138 | connect(action_restore_setup_, SIGNAL(triggered(bool)), | |
139 | this, SLOT(on_actionRestoreSetup_triggered())); | |
140 | ||
141 | action_save_->setText(tr("&Save...")); | |
142 | action_save_->setIcon(QIcon::fromTheme("document-save-as", | |
143 | QIcon(":/icons/document-save-as.png"))); | |
144 | action_save_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
145 | connect(action_save_, SIGNAL(triggered(bool)), | |
146 | this, SLOT(on_actionSave_triggered())); | |
147 | ||
148 | action_save_as_->setText(tr("Save &As...")); | |
149 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
150 | QIcon(":/icons/document-save-as.png"))); | |
151 | connect(action_save_as_, SIGNAL(triggered(bool)), | |
152 | this, SLOT(on_actionSaveAs_triggered())); | |
153 | ||
154 | action_save_selection_as_->setText(tr("Save Selected &Range As...")); | |
155 | action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as", | |
156 | QIcon(":/icons/document-save-as.png"))); | |
157 | action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); | |
158 | connect(action_save_selection_as_, SIGNAL(triggered(bool)), | |
159 | this, SLOT(on_actionSaveSelectionAs_triggered())); | |
160 | ||
161 | action_save_setup_->setText(tr("Save Session Setu&p...")); | |
162 | connect(action_save_setup_, SIGNAL(triggered(bool)), | |
163 | this, SLOT(on_actionSaveSetup_triggered())); | |
164 | ||
165 | widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, | |
166 | session.device_manager().context()); | |
167 | menu_file_export->setTitle(tr("&Export")); | |
168 | connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)), | |
169 | this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>))); | |
170 | ||
171 | widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this, | |
172 | session.device_manager().context()); | |
173 | menu_file_import->setTitle(tr("&Import")); | |
174 | connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)), | |
175 | this, SLOT(import_file(shared_ptr<sigrok::InputFormat>))); | |
176 | ||
177 | action_connect_->setText(tr("&Connect to Device...")); | |
178 | connect(action_connect_, SIGNAL(triggered(bool)), | |
179 | this, SLOT(on_actionConnect_triggered())); | |
180 | ||
181 | // New view button | |
182 | QMenu *menu_new_view = new QMenu(); | |
183 | connect(menu_new_view, SIGNAL(triggered(QAction*)), | |
184 | this, SLOT(on_actionNewView_triggered(QAction*))); | |
185 | ||
186 | for (int i = 0; i < views::ViewTypeCount; i++) { | |
187 | QAction *const action = menu_new_view->addAction(tr(views::ViewTypeNames[i])); | |
188 | action->setData(QVariant::fromValue(i)); | |
189 | } | |
190 | ||
191 | new_view_button_->setMenu(menu_new_view); | |
192 | new_view_button_->setDefaultAction(action_new_view_); | |
193 | new_view_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
194 | ||
195 | // Open button | |
196 | vector<QAction*> open_actions; | |
197 | open_actions.push_back(action_open_); | |
198 | QAction* separator_o = new QAction(this); | |
199 | separator_o->setSeparator(true); | |
200 | open_actions.push_back(separator_o); | |
201 | open_actions.push_back(action_restore_setup_); | |
202 | ||
203 | widgets::ImportMenu *import_menu = new widgets::ImportMenu(this, | |
204 | session.device_manager().context(), open_actions); | |
205 | connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)), | |
206 | this, SLOT(import_file(shared_ptr<sigrok::InputFormat>))); | |
207 | ||
208 | open_button_->setMenu(import_menu); | |
209 | open_button_->setDefaultAction(action_open_); | |
210 | open_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
211 | ||
212 | // Save button | |
213 | vector<QAction*> save_actions; | |
214 | save_actions.push_back(action_save_); | |
215 | save_actions.push_back(action_save_as_); | |
216 | save_actions.push_back(action_save_selection_as_); | |
217 | QAction* separator_s = new QAction(this); | |
218 | separator_s->setSeparator(true); | |
219 | save_actions.push_back(separator_s); | |
220 | save_actions.push_back(action_save_setup_); | |
221 | ||
222 | widgets::ExportMenu *export_menu = new widgets::ExportMenu(this, | |
223 | session.device_manager().context(), save_actions); | |
224 | connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)), | |
225 | this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>))); | |
226 | ||
227 | save_button_->setMenu(export_menu); | |
228 | save_button_->setDefaultAction(action_save_); | |
229 | save_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
230 | ||
231 | // Device selector menu | |
232 | connect(&device_selector_, SIGNAL(device_selected()), | |
233 | this, SLOT(on_device_selected())); | |
234 | ||
235 | // Setup the decoder button | |
236 | #ifdef ENABLE_DECODE | |
237 | add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg")); | |
238 | add_decoder_button_->setPopupMode(QToolButton::InstantPopup); | |
239 | add_decoder_button_->setToolTip(tr("Add protocol decoder")); | |
240 | add_decoder_button_->setShortcut(QKeySequence(Qt::Key_D)); | |
241 | ||
242 | connect(add_decoder_button_, SIGNAL(clicked()), | |
243 | this, SLOT(on_add_decoder_clicked())); | |
244 | #endif | |
245 | ||
246 | // Setup the math signal button | |
247 | add_math_signal_button_->setIcon(QIcon(":/icons/add-math-signal.svg")); | |
248 | add_math_signal_button_->setPopupMode(QToolButton::InstantPopup); | |
249 | add_math_signal_button_->setToolTip(tr("Add math signal")); | |
250 | add_math_signal_button_->setShortcut(QKeySequence(Qt::Key_M)); | |
251 | ||
252 | connect(add_math_signal_button_, SIGNAL(clicked()), | |
253 | this, SLOT(on_add_math_signal_clicked())); | |
254 | ||
255 | ||
256 | connect(&sample_count_, SIGNAL(value_changed()), | |
257 | this, SLOT(on_sample_count_changed())); | |
258 | connect(&sample_rate_, SIGNAL(value_changed()), | |
259 | this, SLOT(on_sample_rate_changed())); | |
260 | ||
261 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); | |
262 | ||
263 | set_capture_state(pv::Session::Stopped); | |
264 | ||
265 | configure_button_.setToolTip(tr("Configure Device")); | |
266 | configure_button_.setIcon(QIcon::fromTheme("preferences-system", | |
267 | QIcon(":/icons/preferences-system.png"))); | |
268 | ||
269 | channels_button_.setToolTip(tr("Configure Channels")); | |
270 | channels_button_.setIcon(QIcon(":/icons/channels.svg")); | |
271 | ||
272 | add_toolbar_widgets(); | |
273 | ||
274 | sample_count_.installEventFilter(this); | |
275 | sample_rate_.installEventFilter(this); | |
276 | ||
277 | // Setup session_ events | |
278 | connect(&session_, SIGNAL(capture_state_changed(int)), | |
279 | this, SLOT(on_capture_state_changed(int))); | |
280 | connect(&session, SIGNAL(device_changed()), | |
281 | this, SLOT(on_device_changed())); | |
282 | ||
283 | update_device_list(); | |
284 | } | |
285 | ||
286 | void MainBar::update_device_list() | |
287 | { | |
288 | DeviceManager &mgr = session_.device_manager(); | |
289 | shared_ptr<devices::Device> selected_device = session_.device(); | |
290 | list< shared_ptr<devices::Device> > devs; | |
291 | ||
292 | copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs)); | |
293 | ||
294 | if (std::find(devs.begin(), devs.end(), selected_device) == devs.end()) | |
295 | devs.push_back(selected_device); | |
296 | ||
297 | device_selector_.set_device_list(devs, selected_device); | |
298 | update_device_config_widgets(); | |
299 | } | |
300 | ||
301 | void MainBar::set_capture_state(pv::Session::capture_state state) | |
302 | { | |
303 | bool ui_enabled = (state == pv::Session::Stopped) ? true : false; | |
304 | ||
305 | device_selector_.setEnabled(ui_enabled); | |
306 | configure_button_.setEnabled(ui_enabled); | |
307 | channels_button_.setEnabled(ui_enabled); | |
308 | sample_count_.setEnabled(ui_enabled); | |
309 | sample_rate_.setEnabled(ui_enabled); | |
310 | } | |
311 | ||
312 | void MainBar::reset_device_selector() | |
313 | { | |
314 | device_selector_.reset(); | |
315 | } | |
316 | ||
317 | QAction* MainBar::action_open() const | |
318 | { | |
319 | return action_open_; | |
320 | } | |
321 | ||
322 | QAction* MainBar::action_save() const | |
323 | { | |
324 | return action_save_; | |
325 | } | |
326 | ||
327 | QAction* MainBar::action_save_as() const | |
328 | { | |
329 | return action_save_as_; | |
330 | } | |
331 | ||
332 | QAction* MainBar::action_save_selection_as() const | |
333 | { | |
334 | return action_save_selection_as_; | |
335 | } | |
336 | ||
337 | QAction* MainBar::action_connect() const | |
338 | { | |
339 | return action_connect_; | |
340 | } | |
341 | ||
342 | void MainBar::update_sample_rate_selector() | |
343 | { | |
344 | Glib::VariantContainerBase gvar_dict; | |
345 | GVariant *gvar_list; | |
346 | const uint64_t *elements = nullptr; | |
347 | gsize num_elements; | |
348 | map< const ConfigKey*, set<Capability> > keys; | |
349 | ||
350 | if (updating_sample_rate_) { | |
351 | sample_rate_.show_none(); | |
352 | return; | |
353 | } | |
354 | ||
355 | const shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
356 | if (!device) | |
357 | return; | |
358 | ||
359 | assert(!updating_sample_rate_); | |
360 | updating_sample_rate_ = true; | |
361 | ||
362 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
363 | ||
364 | sample_rate_.allow_user_entered_values(false); | |
365 | if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) { | |
366 | try { | |
367 | auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK); | |
368 | if (gvar.gobj()) { | |
369 | bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>( | |
370 | gvar).get(); | |
371 | sample_rate_.allow_user_entered_values(value); | |
372 | } | |
373 | } catch (Error& error) { | |
374 | // Do nothing | |
375 | } | |
376 | } | |
377 | ||
378 | ||
379 | if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) { | |
380 | try { | |
381 | gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE); | |
382 | } catch (Error& error) { | |
383 | qDebug() << tr("Failed to get sample rate list:") << error.what(); | |
384 | } | |
385 | } else { | |
386 | sample_rate_.show_none(); | |
387 | updating_sample_rate_ = false; | |
388 | return; | |
389 | } | |
390 | ||
391 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
392 | "samplerate-steps", G_VARIANT_TYPE("at")))) { | |
393 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
394 | gvar_list, &num_elements, sizeof(uint64_t)); | |
395 | ||
396 | const uint64_t min = elements[0]; | |
397 | const uint64_t max = elements[1]; | |
398 | const uint64_t step = elements[2]; | |
399 | ||
400 | g_variant_unref(gvar_list); | |
401 | ||
402 | assert(min > 0); | |
403 | assert(max > 0); | |
404 | assert(max > min); | |
405 | assert(step > 0); | |
406 | ||
407 | if (step == 1) | |
408 | sample_rate_.show_125_list(min, max); | |
409 | else { | |
410 | // When the step is not 1, we cam't make a 1-2-5-10 | |
411 | // list of sample rates, because we may not be able to | |
412 | // make round numbers. Therefore in this case, show a | |
413 | // spin box. | |
414 | sample_rate_.show_min_max_step(min, max, step); | |
415 | } | |
416 | } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
417 | "samplerates", G_VARIANT_TYPE("at")))) { | |
418 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
419 | gvar_list, &num_elements, sizeof(uint64_t)); | |
420 | sample_rate_.show_list(elements, num_elements); | |
421 | g_variant_unref(gvar_list); | |
422 | } | |
423 | updating_sample_rate_ = false; | |
424 | ||
425 | update_sample_rate_selector_value(); | |
426 | } | |
427 | ||
428 | void MainBar::update_sample_rate_selector_value() | |
429 | { | |
430 | if (updating_sample_rate_) | |
431 | return; | |
432 | ||
433 | const shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
434 | if (!device) | |
435 | return; | |
436 | ||
437 | try { | |
438 | auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE); | |
439 | uint64_t samplerate = | |
440 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); | |
441 | assert(!updating_sample_rate_); | |
442 | updating_sample_rate_ = true; | |
443 | sample_rate_.set_value(samplerate); | |
444 | updating_sample_rate_ = false; | |
445 | } catch (Error& error) { | |
446 | qDebug() << tr("Failed to get sample rate:") << error.what(); | |
447 | } | |
448 | } | |
449 | ||
450 | void MainBar::update_sample_count_selector() | |
451 | { | |
452 | if (updating_sample_count_) | |
453 | return; | |
454 | ||
455 | const shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
456 | if (!device) | |
457 | return; | |
458 | ||
459 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
460 | ||
461 | assert(!updating_sample_count_); | |
462 | updating_sample_count_ = true; | |
463 | ||
464 | if (!sample_count_supported_) { | |
465 | sample_count_.show_none(); | |
466 | updating_sample_count_ = false; | |
467 | return; | |
468 | } | |
469 | ||
470 | uint64_t sample_count = sample_count_.value(); | |
471 | uint64_t min_sample_count = 0; | |
472 | uint64_t max_sample_count = MaxSampleCount; | |
473 | bool default_count_set = false; | |
474 | ||
475 | if (sample_count == 0) { | |
476 | sample_count = DefaultSampleCount; | |
477 | default_count_set = true; | |
478 | } | |
479 | ||
480 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) { | |
481 | try { | |
482 | auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES); | |
483 | if (gvar.gobj()) | |
484 | g_variant_get(gvar.gobj(), "(tt)", | |
485 | &min_sample_count, &max_sample_count); | |
486 | } catch (Error& error) { | |
487 | qDebug() << tr("Failed to get sample limit list:") << error.what(); | |
488 | } | |
489 | } | |
490 | ||
491 | min_sample_count = min(max(min_sample_count, MinSampleCount), | |
492 | max_sample_count); | |
493 | ||
494 | sample_count_.show_125_list(min_sample_count, max_sample_count); | |
495 | ||
496 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) { | |
497 | auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES); | |
498 | sample_count = g_variant_get_uint64(gvar.gobj()); | |
499 | if (sample_count == 0) { | |
500 | sample_count = DefaultSampleCount; | |
501 | default_count_set = true; | |
502 | } | |
503 | sample_count = min(max(sample_count, MinSampleCount), | |
504 | max_sample_count); | |
505 | } | |
506 | ||
507 | sample_count_.set_value(sample_count); | |
508 | ||
509 | updating_sample_count_ = false; | |
510 | ||
511 | // If we show the default rate then make sure the device uses the same | |
512 | if (default_count_set) | |
513 | commit_sample_count(); | |
514 | } | |
515 | ||
516 | void MainBar::update_device_config_widgets() | |
517 | { | |
518 | using namespace pv::popups; | |
519 | ||
520 | const shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
521 | ||
522 | // Hide the widgets if no device is selected | |
523 | channels_button_action_->setVisible(!!device); | |
524 | if (!device) { | |
525 | configure_button_action_->setVisible(false); | |
526 | sample_count_.show_none(); | |
527 | sample_rate_.show_none(); | |
528 | return; | |
529 | } | |
530 | ||
531 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
532 | if (!sr_dev) | |
533 | return; | |
534 | ||
535 | // Update the configure popup | |
536 | DeviceOptions *const opts = new DeviceOptions(sr_dev, this); | |
537 | configure_button_action_->setVisible(!opts->binding().properties().empty()); | |
538 | configure_button_.set_popup(opts); | |
539 | ||
540 | // Update the channels popup | |
541 | Channels *const channels = new Channels(session_, this); | |
542 | channels_button_.set_popup(channels); | |
543 | ||
544 | // Update supported options. | |
545 | sample_count_supported_ = false; | |
546 | ||
547 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET)) | |
548 | sample_count_supported_ = true; | |
549 | ||
550 | // Add notification of reconfigure events | |
551 | disconnect(this, SLOT(on_config_changed())); | |
552 | connect(&opts->binding(), SIGNAL(config_changed()), | |
553 | this, SLOT(on_config_changed())); | |
554 | ||
555 | // Update sweep timing widgets. | |
556 | update_sample_count_selector(); | |
557 | update_sample_rate_selector(); | |
558 | } | |
559 | ||
560 | void MainBar::commit_sample_rate() | |
561 | { | |
562 | uint64_t sample_rate = 0; | |
563 | ||
564 | const shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
565 | if (!device) | |
566 | return; | |
567 | ||
568 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
569 | ||
570 | sample_rate = sample_rate_.value(); | |
571 | ||
572 | try { | |
573 | sr_dev->config_set(ConfigKey::SAMPLERATE, | |
574 | Glib::Variant<guint64>::create(sample_rate)); | |
575 | update_sample_rate_selector(); | |
576 | } catch (Error& error) { | |
577 | qDebug() << tr("Failed to configure samplerate:") << error.what(); | |
578 | return; | |
579 | } | |
580 | ||
581 | // Devices with built-in memory might impose limits on certain | |
582 | // configurations, so let's check what sample count the driver | |
583 | // lets us use now. | |
584 | update_sample_count_selector(); | |
585 | } | |
586 | ||
587 | void MainBar::commit_sample_count() | |
588 | { | |
589 | uint64_t sample_count = 0; | |
590 | ||
591 | const shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
592 | if (!device) | |
593 | return; | |
594 | ||
595 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
596 | ||
597 | sample_count = sample_count_.value(); | |
598 | if (sample_count_supported_) { | |
599 | try { | |
600 | sr_dev->config_set(ConfigKey::LIMIT_SAMPLES, | |
601 | Glib::Variant<guint64>::create(sample_count)); | |
602 | update_sample_count_selector(); | |
603 | } catch (Error& error) { | |
604 | qDebug() << tr("Failed to configure sample count:") << error.what(); | |
605 | return; | |
606 | } | |
607 | } | |
608 | ||
609 | // Devices with built-in memory might impose limits on certain | |
610 | // configurations, so let's check what sample rate the driver | |
611 | // lets us use now. | |
612 | update_sample_rate_selector(); | |
613 | } | |
614 | ||
615 | void MainBar::show_session_error(const QString text, const QString info_text) | |
616 | { | |
617 | QMessageBox msg(this); | |
618 | msg.setText(text + "\n\n" + info_text); | |
619 | msg.setStandardButtons(QMessageBox::Ok); | |
620 | msg.setIcon(QMessageBox::Warning); | |
621 | msg.exec(); | |
622 | } | |
623 | ||
624 | void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only, QString file_name) | |
625 | { | |
626 | using pv::dialogs::StoreProgress; | |
627 | ||
628 | // Stop any currently running capture session | |
629 | session_.stop_capture(); | |
630 | ||
631 | QSettings settings; | |
632 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
633 | ||
634 | pair<uint64_t, uint64_t> sample_range; | |
635 | ||
636 | // Selection only? Verify that the cursors are active and fetch their values | |
637 | if (selection_only) { | |
638 | views::trace::View *trace_view = | |
639 | qobject_cast<views::trace::View*>(session_.main_view().get()); | |
640 | ||
641 | if (!trace_view->cursors()->enabled()) { | |
642 | show_session_error(tr("Missing Cursors"), tr("You need to set the " \ | |
643 | "cursors before you can save the data enclosed by them " \ | |
644 | "to a session file (e.g. using the Show Cursors button).")); | |
645 | return; | |
646 | } | |
647 | ||
648 | const double samplerate = session_.get_samplerate(); | |
649 | ||
650 | const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time(); | |
651 | const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time(); | |
652 | ||
653 | const uint64_t start_sample = (uint64_t)max( | |
654 | 0.0, start_time.convert_to<double>() * samplerate); | |
655 | const uint64_t end_sample = (uint64_t)max( | |
656 | 0.0, end_time.convert_to<double>() * samplerate); | |
657 | ||
658 | if ((start_sample == 0) && (end_sample == 0)) { | |
659 | // Both cursors are negative and were clamped to 0 | |
660 | show_session_error(tr("Invalid Range"), tr("The cursors don't " \ | |
661 | "define a valid range of samples.")); | |
662 | return; | |
663 | } | |
664 | ||
665 | sample_range = make_pair(start_sample, end_sample); | |
666 | } else { | |
667 | sample_range = make_pair(0, 0); | |
668 | } | |
669 | ||
670 | // Construct the filter | |
671 | const vector<string> exts = format->extensions(); | |
672 | QString filter = tr("%1 files ").arg( | |
673 | QString::fromStdString(format->description())); | |
674 | ||
675 | if (exts.empty()) | |
676 | filter += "(*)"; | |
677 | else | |
678 | filter += QString("(*.%1);;%2 (*)").arg( | |
679 | QString::fromStdString(join(exts, ", *.")), | |
680 | tr("All Files")); | |
681 | ||
682 | // Show the file dialog | |
683 | if (file_name.isEmpty()) | |
684 | file_name = QFileDialog::getSaveFileName(this, tr("Save File"), dir, filter); | |
685 | ||
686 | if (file_name.isEmpty()) | |
687 | return; | |
688 | ||
689 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
690 | settings.setValue(SettingSaveDirectory, abs_path); | |
691 | ||
692 | // Show the options dialog | |
693 | map<string, Glib::VariantBase> options; | |
694 | if (!format->options().empty()) { | |
695 | dialogs::InputOutputOptions dlg( | |
696 | tr("Export %1").arg(QString::fromStdString( | |
697 | format->description())), | |
698 | format->options(), this); | |
699 | if (!dlg.exec()) | |
700 | return; | |
701 | options = dlg.options(); | |
702 | } | |
703 | ||
704 | if (!selection_only) { | |
705 | if (format == session_.device_manager().context()->output_formats()["srzip"]) { | |
706 | session_.set_save_path(QFileInfo(file_name).absolutePath()); | |
707 | session_.set_name(QFileInfo(file_name).fileName()); | |
708 | } else | |
709 | session_.set_save_path(""); | |
710 | } | |
711 | ||
712 | StoreProgress *dlg = new StoreProgress(file_name, format, options, | |
713 | sample_range, session_, this); | |
714 | dlg->run(); | |
715 | } | |
716 | ||
717 | void MainBar::import_file(shared_ptr<InputFormat> format) | |
718 | { | |
719 | assert(format); | |
720 | ||
721 | QSettings settings; | |
722 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
723 | ||
724 | // Construct the filter | |
725 | const vector<string> exts = format->extensions(); | |
726 | const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg( | |
727 | tr("%1 files").arg(QString::fromStdString(format->description())), | |
728 | QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *.")))); | |
729 | const QString filter_all = QString::fromStdString("%1 (%2)").arg( | |
730 | tr("All Files"), QString::fromStdString("*")); | |
731 | const QString filter = QString::fromStdString("%1%2%3").arg( | |
732 | exts.empty() ? "" : filter_exts, | |
733 | exts.empty() ? "" : ";;", | |
734 | filter_all); | |
735 | ||
736 | // Show the file dialog | |
737 | const QString file_name = QFileDialog::getOpenFileName( | |
738 | this, tr("Import File"), dir, filter); | |
739 | ||
740 | if (file_name.isEmpty()) | |
741 | return; | |
742 | ||
743 | // Show the options dialog | |
744 | map<string, Glib::VariantBase> options; | |
745 | if (!format->options().empty()) { | |
746 | dialogs::InputOutputOptions dlg( | |
747 | tr("Import %1").arg(QString::fromStdString( | |
748 | format->description())), | |
749 | format->options(), this); | |
750 | if (!dlg.exec()) | |
751 | return; | |
752 | options = dlg.options(); | |
753 | } | |
754 | ||
755 | session_.load_file(file_name, "", format, options); | |
756 | ||
757 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
758 | settings.setValue(SettingOpenDirectory, abs_path); | |
759 | } | |
760 | ||
761 | void MainBar::on_device_selected() | |
762 | { | |
763 | shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
764 | ||
765 | if (device) | |
766 | session_.select_device(device); | |
767 | else | |
768 | reset_device_selector(); | |
769 | } | |
770 | ||
771 | void MainBar::on_device_changed() | |
772 | { | |
773 | update_device_list(); | |
774 | update_device_config_widgets(); | |
775 | } | |
776 | ||
777 | void MainBar::on_capture_state_changed(int state) | |
778 | { | |
779 | set_capture_state((pv::Session::capture_state)state); | |
780 | } | |
781 | ||
782 | void MainBar::on_sample_count_changed() | |
783 | { | |
784 | if (!updating_sample_count_) | |
785 | commit_sample_count(); | |
786 | } | |
787 | ||
788 | void MainBar::on_sample_rate_changed() | |
789 | { | |
790 | if (!updating_sample_rate_) | |
791 | commit_sample_rate(); | |
792 | } | |
793 | ||
794 | void MainBar::on_config_changed() | |
795 | { | |
796 | // We want to also call update_sample_rate_selector() here in case | |
797 | // the user changed the SR_CONF_EXTERNAL_CLOCK option. However, | |
798 | // commit_sample_rate() does this already, so we don't call it here | |
799 | ||
800 | commit_sample_count(); | |
801 | commit_sample_rate(); | |
802 | } | |
803 | ||
804 | void MainBar::on_actionNewView_triggered(QAction* action) | |
805 | { | |
806 | if (action) | |
807 | new_view(&session_, action->data().toInt()); | |
808 | else | |
809 | // When the icon of the button is clicked, we create a trace view | |
810 | new_view(&session_, views::ViewTypeTrace); | |
811 | } | |
812 | ||
813 | void MainBar::on_actionOpen_triggered() | |
814 | { | |
815 | QSettings settings; | |
816 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
817 | ||
818 | // Show the dialog | |
819 | const QString file_name = QFileDialog::getOpenFileName( | |
820 | this, tr("Open File"), dir, tr( | |
821 | "sigrok Sessions (*.sr);;" | |
822 | "All Files (*)")); | |
823 | ||
824 | if (!file_name.isEmpty()) { | |
825 | session_.load_file(file_name); | |
826 | ||
827 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
828 | settings.setValue(SettingOpenDirectory, abs_path); | |
829 | } | |
830 | } | |
831 | ||
832 | void MainBar::on_actionSave_triggered() | |
833 | { | |
834 | // A path is only set if we loaded/saved an srzip file before | |
835 | if (session_.save_path().isEmpty()) { | |
836 | on_actionSaveAs_triggered(); | |
837 | return; | |
838 | } | |
839 | ||
840 | QFileInfo fi = QFileInfo(QDir(session_.save_path()), session_.name()); | |
841 | export_file(session_.device_manager().context()->output_formats()["srzip"], false, | |
842 | fi.absoluteFilePath()); | |
843 | } | |
844 | ||
845 | void MainBar::on_actionSaveAs_triggered() | |
846 | { | |
847 | export_file(session_.device_manager().context()->output_formats()["srzip"]); | |
848 | } | |
849 | ||
850 | void MainBar::on_actionSaveSelectionAs_triggered() | |
851 | { | |
852 | export_file(session_.device_manager().context()->output_formats()["srzip"], true); | |
853 | } | |
854 | ||
855 | void MainBar::on_actionSaveSetup_triggered() | |
856 | { | |
857 | QSettings settings; | |
858 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
859 | ||
860 | const QString file_name = QFileDialog::getSaveFileName( | |
861 | this, tr("Save File"), dir, tr( | |
862 | "PulseView Session Setups (*.pvs);;" | |
863 | "All Files (*)")); | |
864 | ||
865 | if (file_name.isEmpty()) | |
866 | return; | |
867 | ||
868 | QSettings settings_storage(file_name, QSettings::IniFormat); | |
869 | session_.save_setup(settings_storage); | |
870 | } | |
871 | ||
872 | void MainBar::on_actionRestoreSetup_triggered() | |
873 | { | |
874 | QSettings settings; | |
875 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
876 | ||
877 | const QString file_name = QFileDialog::getOpenFileName( | |
878 | this, tr("Open File"), dir, tr( | |
879 | "PulseView Session Setups (*.pvs);;" | |
880 | "All Files (*)")); | |
881 | ||
882 | if (file_name.isEmpty()) | |
883 | return; | |
884 | ||
885 | QSettings settings_storage(file_name, QSettings::IniFormat); | |
886 | session_.restore_setup(settings_storage); | |
887 | } | |
888 | ||
889 | void MainBar::on_actionConnect_triggered() | |
890 | { | |
891 | // Stop any currently running capture session | |
892 | session_.stop_capture(); | |
893 | ||
894 | dialogs::Connect dlg(this, session_.device_manager()); | |
895 | ||
896 | // If the user selected a device, select it in the device list. Select the | |
897 | // current device otherwise. | |
898 | if (dlg.exec()) | |
899 | session_.select_device(dlg.get_selected_device()); | |
900 | ||
901 | update_device_list(); | |
902 | } | |
903 | ||
904 | void MainBar::on_add_decoder_clicked() | |
905 | { | |
906 | show_decoder_selector(&session_); | |
907 | } | |
908 | ||
909 | void MainBar::on_add_math_signal_clicked() | |
910 | { | |
911 | shared_ptr<data::SignalBase> signal = make_shared<data::MathSignal>(session_); | |
912 | session_.add_generated_signal(signal); | |
913 | } | |
914 | ||
915 | void MainBar::add_toolbar_widgets() | |
916 | { | |
917 | addWidget(new_view_button_); | |
918 | addSeparator(); | |
919 | addWidget(open_button_); | |
920 | addWidget(save_button_); | |
921 | addSeparator(); | |
922 | ||
923 | StandardBar::add_toolbar_widgets(); | |
924 | ||
925 | addWidget(&device_selector_); | |
926 | configure_button_action_ = addWidget(&configure_button_); | |
927 | channels_button_action_ = addWidget(&channels_button_); | |
928 | addWidget(&sample_count_); | |
929 | addWidget(&sample_rate_); | |
930 | #ifdef ENABLE_DECODE | |
931 | addSeparator(); | |
932 | addWidget(add_decoder_button_); | |
933 | #endif | |
934 | addWidget(add_math_signal_button_); | |
935 | } | |
936 | ||
937 | bool MainBar::eventFilter(QObject *watched, QEvent *event) | |
938 | { | |
939 | if (sample_count_supported_ && (watched == &sample_count_ || | |
940 | watched == &sample_rate_) && | |
941 | (event->type() == QEvent::ToolTip)) { | |
942 | ||
943 | auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value(); | |
944 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); | |
945 | ||
946 | QString str = tr("Total sampling time: %1").arg( | |
947 | pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false)); | |
948 | QToolTip::showText(help_event->globalPos(), str); | |
949 | ||
950 | return true; | |
951 | } | |
952 | ||
953 | return false; | |
954 | } | |
955 | ||
956 | } // namespace toolbars | |
957 | } // namespace pv |