]>
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <extdef.h> | |
22 | ||
23 | #include <algorithm> | |
24 | #include <cassert> | |
25 | ||
26 | #include <QAction> | |
27 | #include <QDebug> | |
28 | #include <QHelpEvent> | |
29 | #include <QMenu> | |
30 | #include <QToolTip> | |
31 | ||
32 | #include "mainbar.hpp" | |
33 | ||
34 | #include <pv/devicemanager.hpp> | |
35 | #include <pv/devices/hardwaredevice.hpp> | |
36 | #include <pv/mainwindow.hpp> | |
37 | #include <pv/popups/deviceoptions.hpp> | |
38 | #include <pv/popups/channels.hpp> | |
39 | #include <pv/util.hpp> | |
40 | #include <pv/widgets/exportmenu.hpp> | |
41 | #include <pv/widgets/importmenu.hpp> | |
42 | ||
43 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
44 | ||
45 | using std::back_inserter; | |
46 | using std::copy; | |
47 | using std::list; | |
48 | using std::map; | |
49 | using std::max; | |
50 | using std::min; | |
51 | using std::shared_ptr; | |
52 | using std::string; | |
53 | using std::vector; | |
54 | ||
55 | using sigrok::Capability; | |
56 | using sigrok::ConfigKey; | |
57 | using sigrok::Error; | |
58 | using sigrok::InputFormat; | |
59 | ||
60 | namespace pv { | |
61 | namespace toolbars { | |
62 | ||
63 | const uint64_t MainBar::MinSampleCount = 100ULL; | |
64 | const uint64_t MainBar::MaxSampleCount = 1000000000000ULL; | |
65 | const uint64_t MainBar::DefaultSampleCount = 1000000; | |
66 | ||
67 | MainBar::MainBar(Session &session, MainWindow &main_window) : | |
68 | QToolBar("Sampling Bar", &main_window), | |
69 | session_(session), | |
70 | main_window_(main_window), | |
71 | device_selector_(this, session.device_manager(), | |
72 | main_window.action_connect()), | |
73 | configure_button_(this), | |
74 | configure_button_action_(nullptr), | |
75 | channels_button_(this), | |
76 | channels_button_action_(nullptr), | |
77 | sample_count_(" samples", this), | |
78 | sample_rate_("Hz", this), | |
79 | updating_sample_rate_(false), | |
80 | updating_sample_count_(false), | |
81 | sample_count_supported_(false), | |
82 | icon_red_(":/icons/status-red.svg"), | |
83 | icon_green_(":/icons/status-green.svg"), | |
84 | icon_grey_(":/icons/status-grey.svg"), | |
85 | run_stop_button_(this), | |
86 | run_stop_button_action_(nullptr), | |
87 | menu_button_(this) | |
88 | { | |
89 | setObjectName(QString::fromUtf8("MainBar")); | |
90 | ||
91 | setMovable(false); | |
92 | setFloatable(false); | |
93 | setContextMenuPolicy(Qt::PreventContextMenu); | |
94 | ||
95 | // Open button | |
96 | QToolButton *const open_button = new QToolButton(this); | |
97 | ||
98 | widgets::ImportMenu *import_menu = new widgets::ImportMenu(this, | |
99 | session.device_manager().context(), | |
100 | main_window.action_open()); | |
101 | connect(import_menu, | |
102 | SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)), | |
103 | &main_window_, | |
104 | SLOT(import_file(std::shared_ptr<sigrok::InputFormat>))); | |
105 | ||
106 | open_button->setMenu(import_menu); | |
107 | open_button->setDefaultAction(main_window.action_open()); | |
108 | open_button->setPopupMode(QToolButton::MenuButtonPopup); | |
109 | ||
110 | // Save button | |
111 | QToolButton *const save_button = new QToolButton(this); | |
112 | ||
113 | vector<QAction *> open_actions; | |
114 | open_actions.push_back(main_window.action_save_as()); | |
115 | open_actions.push_back(main_window.action_save_selection_as()); | |
116 | ||
117 | widgets::ExportMenu *export_menu = new widgets::ExportMenu(this, | |
118 | session.device_manager().context(), | |
119 | open_actions); | |
120 | connect(export_menu, | |
121 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
122 | &main_window_, | |
123 | SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
124 | ||
125 | save_button->setMenu(export_menu); | |
126 | save_button->setDefaultAction(main_window.action_save_as()); | |
127 | save_button->setPopupMode(QToolButton::MenuButtonPopup); | |
128 | ||
129 | // Device selector menu | |
130 | connect(&device_selector_, SIGNAL(device_selected()), | |
131 | this, SLOT(on_device_selected())); | |
132 | ||
133 | // Setup the decoder button | |
134 | #ifdef ENABLE_DECODE | |
135 | QToolButton *add_decoder_button = new QToolButton(this); | |
136 | add_decoder_button->setIcon(QIcon::fromTheme("add-decoder", | |
137 | QIcon(":/icons/add-decoder.svg"))); | |
138 | add_decoder_button->setPopupMode(QToolButton::InstantPopup); | |
139 | add_decoder_button->setMenu(main_window_.menu_decoder_add()); | |
140 | #endif | |
141 | ||
142 | // Setup the burger menu | |
143 | QMenu *const menu = new QMenu(this); | |
144 | ||
145 | QMenu *const menu_view = new QMenu; | |
146 | menu_view->setTitle(tr("&View")); | |
147 | menu_view->addAction(main_window.action_view_sticky_scrolling()); | |
148 | ||
149 | QMenu *const menu_help = new QMenu; | |
150 | menu_help->setTitle(tr("&Help")); | |
151 | menu_help->addAction(main_window.action_about()); | |
152 | ||
153 | menu->addAction(menu_view->menuAction()); | |
154 | menu->addSeparator(); | |
155 | menu->addAction(menu_help->menuAction()); | |
156 | menu->addSeparator(); | |
157 | menu->addAction(main_window.action_quit()); | |
158 | ||
159 | menu_button_.setMenu(menu); | |
160 | menu_button_.setPopupMode(QToolButton::InstantPopup); | |
161 | menu_button_.setIcon(QIcon::fromTheme("menu", | |
162 | QIcon(":/icons/menu.svg"))); | |
163 | ||
164 | // Setup the toolbar | |
165 | addWidget(open_button); | |
166 | addWidget(save_button); | |
167 | addSeparator(); | |
168 | addAction(main_window.action_view_zoom_in()); | |
169 | addAction(main_window.action_view_zoom_out()); | |
170 | addAction(main_window.action_view_zoom_fit()); | |
171 | addAction(main_window.action_view_zoom_one_to_one()); | |
172 | addSeparator(); | |
173 | addAction(main_window.action_view_show_cursors()); | |
174 | addSeparator(); | |
175 | ||
176 | connect(&run_stop_button_, SIGNAL(clicked()), | |
177 | this, SLOT(on_run_stop())); | |
178 | connect(&sample_count_, SIGNAL(value_changed()), | |
179 | this, SLOT(on_sample_count_changed())); | |
180 | connect(&sample_rate_, SIGNAL(value_changed()), | |
181 | this, SLOT(on_sample_rate_changed())); | |
182 | ||
183 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); | |
184 | ||
185 | set_capture_state(pv::Session::Stopped); | |
186 | ||
187 | configure_button_.setIcon(QIcon::fromTheme("configure", | |
188 | QIcon(":/icons/configure.png"))); | |
189 | ||
190 | channels_button_.setIcon(QIcon::fromTheme("channels", | |
191 | QIcon(":/icons/channels.svg"))); | |
192 | ||
193 | run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
194 | ||
195 | addWidget(&device_selector_); | |
196 | configure_button_action_ = addWidget(&configure_button_); | |
197 | channels_button_action_ = addWidget(&channels_button_); | |
198 | addWidget(&sample_count_); | |
199 | addWidget(&sample_rate_); | |
200 | run_stop_button_action_ = addWidget(&run_stop_button_); | |
201 | #ifdef ENABLE_DECODE | |
202 | addSeparator(); | |
203 | addWidget(add_decoder_button); | |
204 | #endif | |
205 | ||
206 | QWidget *const spacer = new QWidget(); | |
207 | spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
208 | addWidget(spacer); | |
209 | ||
210 | addWidget(&menu_button_); | |
211 | ||
212 | sample_count_.installEventFilter(this); | |
213 | sample_rate_.installEventFilter(this); | |
214 | } | |
215 | ||
216 | void MainBar::update_device_list() | |
217 | { | |
218 | DeviceManager &mgr = session_.device_manager(); | |
219 | shared_ptr<devices::Device> selected_device = session_.device(); | |
220 | list< shared_ptr<devices::Device> > devs; | |
221 | ||
222 | copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs)); | |
223 | ||
224 | if (std::find(devs.begin(), devs.end(), selected_device) == devs.end()) | |
225 | devs.push_back(selected_device); | |
226 | ||
227 | device_selector_.set_device_list(devs, selected_device); | |
228 | update_device_config_widgets(); | |
229 | } | |
230 | ||
231 | ||
232 | void MainBar::set_capture_state(pv::Session::capture_state state) | |
233 | { | |
234 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; | |
235 | run_stop_button_.setIcon(*icons[state]); | |
236 | run_stop_button_.setText((state == pv::Session::Stopped) ? | |
237 | tr("Run") : tr("Stop")); | |
238 | run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); | |
239 | ||
240 | bool ui_enabled = (state == pv::Session::Stopped) ? true : false; | |
241 | ||
242 | device_selector_.setEnabled(ui_enabled); | |
243 | configure_button_.setEnabled(ui_enabled); | |
244 | channels_button_.setEnabled(ui_enabled); | |
245 | sample_count_.setEnabled(ui_enabled); | |
246 | sample_rate_.setEnabled(ui_enabled); | |
247 | } | |
248 | ||
249 | void MainBar::update_sample_rate_selector() | |
250 | { | |
251 | Glib::VariantContainerBase gvar_dict; | |
252 | GVariant *gvar_list; | |
253 | const uint64_t *elements = nullptr; | |
254 | gsize num_elements; | |
255 | map< const ConfigKey*, std::set<Capability> > keys; | |
256 | ||
257 | if (updating_sample_rate_) { | |
258 | sample_rate_.show_none(); | |
259 | return; | |
260 | } | |
261 | ||
262 | const shared_ptr<devices::Device> device = | |
263 | device_selector_.selected_device(); | |
264 | if (!device) | |
265 | return; | |
266 | ||
267 | assert(!updating_sample_rate_); | |
268 | updating_sample_rate_ = true; | |
269 | ||
270 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
271 | ||
272 | if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) { | |
273 | gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE); | |
274 | } else { | |
275 | sample_rate_.show_none(); | |
276 | updating_sample_rate_ = false; | |
277 | return; | |
278 | } | |
279 | ||
280 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
281 | "samplerate-steps", G_VARIANT_TYPE("at")))) { | |
282 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
283 | gvar_list, &num_elements, sizeof(uint64_t)); | |
284 | ||
285 | const uint64_t min = elements[0]; | |
286 | const uint64_t max = elements[1]; | |
287 | const uint64_t step = elements[2]; | |
288 | ||
289 | g_variant_unref(gvar_list); | |
290 | ||
291 | assert(min > 0); | |
292 | assert(max > 0); | |
293 | assert(max > min); | |
294 | assert(step > 0); | |
295 | ||
296 | if (step == 1) | |
297 | sample_rate_.show_125_list(min, max); | |
298 | else { | |
299 | // When the step is not 1, we cam't make a 1-2-5-10 | |
300 | // list of sample rates, because we may not be able to | |
301 | // make round numbers. Therefore in this case, show a | |
302 | // spin box. | |
303 | sample_rate_.show_min_max_step(min, max, step); | |
304 | } | |
305 | } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
306 | "samplerates", G_VARIANT_TYPE("at")))) { | |
307 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
308 | gvar_list, &num_elements, sizeof(uint64_t)); | |
309 | sample_rate_.show_list(elements, num_elements); | |
310 | g_variant_unref(gvar_list); | |
311 | } | |
312 | updating_sample_rate_ = false; | |
313 | ||
314 | update_sample_rate_selector_value(); | |
315 | } | |
316 | ||
317 | void MainBar::update_sample_rate_selector_value() | |
318 | { | |
319 | if (updating_sample_rate_) | |
320 | return; | |
321 | ||
322 | const shared_ptr<devices::Device> device = | |
323 | device_selector_.selected_device(); | |
324 | if (!device) | |
325 | return; | |
326 | ||
327 | try { | |
328 | auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE); | |
329 | uint64_t samplerate = | |
330 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); | |
331 | assert(!updating_sample_rate_); | |
332 | updating_sample_rate_ = true; | |
333 | sample_rate_.set_value(samplerate); | |
334 | updating_sample_rate_ = false; | |
335 | } catch (Error error) { | |
336 | qDebug() << "WARNING: Failed to get value of sample rate"; | |
337 | return; | |
338 | } | |
339 | } | |
340 | ||
341 | void MainBar::update_sample_count_selector() | |
342 | { | |
343 | if (updating_sample_count_) | |
344 | return; | |
345 | ||
346 | const shared_ptr<devices::Device> device = | |
347 | device_selector_.selected_device(); | |
348 | if (!device) | |
349 | return; | |
350 | ||
351 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
352 | ||
353 | assert(!updating_sample_count_); | |
354 | updating_sample_count_ = true; | |
355 | ||
356 | if (!sample_count_supported_) { | |
357 | sample_count_.show_none(); | |
358 | updating_sample_count_ = false; | |
359 | return; | |
360 | } | |
361 | ||
362 | uint64_t sample_count = sample_count_.value(); | |
363 | uint64_t min_sample_count = 0; | |
364 | uint64_t max_sample_count = MaxSampleCount; | |
365 | ||
366 | if (sample_count == 0) | |
367 | sample_count = DefaultSampleCount; | |
368 | ||
369 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) { | |
370 | auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES); | |
371 | if (gvar.gobj()) | |
372 | g_variant_get(gvar.gobj(), "(tt)", | |
373 | &min_sample_count, &max_sample_count); | |
374 | } | |
375 | ||
376 | min_sample_count = min(max(min_sample_count, MinSampleCount), | |
377 | max_sample_count); | |
378 | ||
379 | sample_count_.show_125_list( | |
380 | min_sample_count, max_sample_count); | |
381 | ||
382 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) { | |
383 | auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES); | |
384 | sample_count = g_variant_get_uint64(gvar.gobj()); | |
385 | if (sample_count == 0) | |
386 | sample_count = DefaultSampleCount; | |
387 | sample_count = min(max(sample_count, MinSampleCount), | |
388 | max_sample_count); | |
389 | } | |
390 | ||
391 | sample_count_.set_value(sample_count); | |
392 | ||
393 | updating_sample_count_ = false; | |
394 | } | |
395 | ||
396 | void MainBar::update_device_config_widgets() | |
397 | { | |
398 | using namespace pv::popups; | |
399 | ||
400 | const shared_ptr<devices::Device> device = | |
401 | device_selector_.selected_device(); | |
402 | ||
403 | // Hide the widgets if no device is selected | |
404 | channels_button_action_->setVisible(!!device); | |
405 | run_stop_button_action_->setVisible(!!device); | |
406 | if (!device) { | |
407 | configure_button_action_->setVisible(false); | |
408 | sample_count_.show_none(); | |
409 | sample_rate_.show_none(); | |
410 | return; | |
411 | } | |
412 | ||
413 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
414 | if (!sr_dev) | |
415 | return; | |
416 | ||
417 | // Update the configure popup | |
418 | DeviceOptions *const opts = new DeviceOptions(sr_dev, this); | |
419 | configure_button_action_->setVisible( | |
420 | !opts->binding().properties().empty()); | |
421 | configure_button_.set_popup(opts); | |
422 | ||
423 | // Update the channels popup | |
424 | Channels *const channels = new Channels(session_, this); | |
425 | channels_button_.set_popup(channels); | |
426 | ||
427 | // Update supported options. | |
428 | sample_count_supported_ = false; | |
429 | ||
430 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET)) | |
431 | sample_count_supported_ = true; | |
432 | ||
433 | if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) { | |
434 | sr_dev->config_set(ConfigKey::LIMIT_FRAMES, | |
435 | Glib::Variant<guint64>::create(1)); | |
436 | on_config_changed(); | |
437 | } | |
438 | ||
439 | // Add notification of reconfigure events | |
440 | disconnect(this, SLOT(on_config_changed())); | |
441 | connect(&opts->binding(), SIGNAL(config_changed()), | |
442 | this, SLOT(on_config_changed())); | |
443 | ||
444 | // Update sweep timing widgets. | |
445 | update_sample_count_selector(); | |
446 | update_sample_rate_selector(); | |
447 | } | |
448 | ||
449 | void MainBar::commit_sample_count() | |
450 | { | |
451 | uint64_t sample_count = 0; | |
452 | ||
453 | const shared_ptr<devices::Device> device = | |
454 | device_selector_.selected_device(); | |
455 | if (!device) | |
456 | return; | |
457 | ||
458 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
459 | ||
460 | sample_count = sample_count_.value(); | |
461 | if (sample_count_supported_) { | |
462 | try { | |
463 | sr_dev->config_set(ConfigKey::LIMIT_SAMPLES, | |
464 | Glib::Variant<guint64>::create(sample_count)); | |
465 | update_sample_count_selector(); | |
466 | } catch (Error error) { | |
467 | qDebug() << "Failed to configure sample count."; | |
468 | return; | |
469 | } | |
470 | } | |
471 | ||
472 | // Devices with built-in memory might impose limits on certain | |
473 | // configurations, so let's check what sample rate the driver | |
474 | // lets us use now. | |
475 | update_sample_rate_selector(); | |
476 | } | |
477 | ||
478 | void MainBar::commit_sample_rate() | |
479 | { | |
480 | uint64_t sample_rate = 0; | |
481 | ||
482 | const shared_ptr<devices::Device> device = | |
483 | device_selector_.selected_device(); | |
484 | if (!device) | |
485 | return; | |
486 | ||
487 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
488 | ||
489 | sample_rate = sample_rate_.value(); | |
490 | if (sample_rate == 0) | |
491 | return; | |
492 | ||
493 | try { | |
494 | sr_dev->config_set(ConfigKey::SAMPLERATE, | |
495 | Glib::Variant<guint64>::create(sample_rate)); | |
496 | update_sample_rate_selector(); | |
497 | } catch (Error error) { | |
498 | qDebug() << "Failed to configure samplerate."; | |
499 | return; | |
500 | } | |
501 | ||
502 | // Devices with built-in memory might impose limits on certain | |
503 | // configurations, so let's check what sample count the driver | |
504 | // lets us use now. | |
505 | update_sample_count_selector(); | |
506 | } | |
507 | ||
508 | void MainBar::on_device_selected() | |
509 | { | |
510 | shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
511 | if (!device) | |
512 | return; | |
513 | ||
514 | main_window_.select_device(device); | |
515 | ||
516 | update_device_config_widgets(); | |
517 | } | |
518 | ||
519 | void MainBar::on_sample_count_changed() | |
520 | { | |
521 | if (!updating_sample_count_) | |
522 | commit_sample_count(); | |
523 | } | |
524 | ||
525 | void MainBar::on_sample_rate_changed() | |
526 | { | |
527 | if (!updating_sample_rate_) | |
528 | commit_sample_rate(); | |
529 | } | |
530 | ||
531 | void MainBar::on_run_stop() | |
532 | { | |
533 | commit_sample_count(); | |
534 | commit_sample_rate(); | |
535 | main_window_.run_stop(); | |
536 | } | |
537 | ||
538 | void MainBar::on_config_changed() | |
539 | { | |
540 | commit_sample_count(); | |
541 | commit_sample_rate(); | |
542 | } | |
543 | ||
544 | bool MainBar::eventFilter(QObject *watched, QEvent *event) | |
545 | { | |
546 | if (sample_count_supported_ && (watched == &sample_count_ || | |
547 | watched == &sample_rate_) && | |
548 | (event->type() == QEvent::ToolTip)) { | |
549 | auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value(); | |
550 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); | |
551 | ||
552 | QString str = tr("Total sampling time: %1").arg( | |
553 | pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false)); | |
554 | QToolTip::showText(help_event->globalPos(), str); | |
555 | ||
556 | return true; | |
557 | } | |
558 | ||
559 | return false; | |
560 | } | |
561 | ||
562 | } // namespace toolbars | |
563 | } // namespace pv |