]>
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 | widgets::ExportMenu *export_menu = new widgets::ExportMenu(this, | |
114 | session.device_manager().context(), | |
115 | main_window.action_save_as()); | |
116 | connect(export_menu, | |
117 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
118 | &main_window_, | |
119 | SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
120 | ||
121 | save_button->setMenu(export_menu); | |
122 | save_button->setDefaultAction(main_window.action_save_as()); | |
123 | save_button->setPopupMode(QToolButton::MenuButtonPopup); | |
124 | ||
125 | // Device selector menu | |
126 | connect(&device_selector_, SIGNAL(device_selected()), | |
127 | this, SLOT(on_device_selected())); | |
128 | ||
129 | // Setup the decoder button | |
130 | #ifdef ENABLE_DECODE | |
131 | QToolButton *add_decoder_button = new QToolButton(this); | |
132 | add_decoder_button->setIcon(QIcon::fromTheme("add-decoder", | |
133 | QIcon(":/icons/add-decoder.svg"))); | |
134 | add_decoder_button->setPopupMode(QToolButton::InstantPopup); | |
135 | add_decoder_button->setMenu(main_window_.menu_decoder_add()); | |
136 | #endif | |
137 | ||
138 | // Setup the menu | |
139 | QMenu *const menu = new QMenu(this); | |
140 | ||
141 | QMenu *const menu_help = new QMenu; | |
142 | menu_help->setTitle(tr("&Help")); | |
143 | menu_help->addAction(main_window.action_about()); | |
144 | ||
145 | menu->addAction(menu_help->menuAction()); | |
146 | menu->addSeparator(); | |
147 | menu->addAction(main_window.action_quit()); | |
148 | ||
149 | menu_button_.setMenu(menu); | |
150 | menu_button_.setPopupMode(QToolButton::InstantPopup); | |
151 | menu_button_.setIcon(QIcon::fromTheme("menu", | |
152 | QIcon(":/icons/menu.svg"))); | |
153 | ||
154 | // Setup the toolbar | |
155 | addWidget(open_button); | |
156 | addWidget(save_button); | |
157 | addSeparator(); | |
158 | addAction(main_window.action_view_zoom_in()); | |
159 | addAction(main_window.action_view_zoom_out()); | |
160 | addAction(main_window.action_view_zoom_fit()); | |
161 | addAction(main_window.action_view_zoom_one_to_one()); | |
162 | addSeparator(); | |
163 | addAction(main_window.action_view_show_cursors()); | |
164 | addSeparator(); | |
165 | ||
166 | connect(&run_stop_button_, SIGNAL(clicked()), | |
167 | this, SLOT(on_run_stop())); | |
168 | connect(&sample_count_, SIGNAL(value_changed()), | |
169 | this, SLOT(on_sample_count_changed())); | |
170 | connect(&sample_rate_, SIGNAL(value_changed()), | |
171 | this, SLOT(on_sample_rate_changed())); | |
172 | ||
173 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); | |
174 | ||
175 | set_capture_state(pv::Session::Stopped); | |
176 | ||
177 | configure_button_.setIcon(QIcon::fromTheme("configure", | |
178 | QIcon(":/icons/configure.png"))); | |
179 | ||
180 | channels_button_.setIcon(QIcon::fromTheme("channels", | |
181 | QIcon(":/icons/channels.svg"))); | |
182 | ||
183 | run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
184 | ||
185 | addWidget(&device_selector_); | |
186 | configure_button_action_ = addWidget(&configure_button_); | |
187 | channels_button_action_ = addWidget(&channels_button_); | |
188 | addWidget(&sample_count_); | |
189 | addWidget(&sample_rate_); | |
190 | run_stop_button_action_ = addWidget(&run_stop_button_); | |
191 | #ifdef ENABLE_DECODE | |
192 | addSeparator(); | |
193 | addWidget(add_decoder_button); | |
194 | #endif | |
195 | ||
196 | QWidget *const spacer = new QWidget(); | |
197 | spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
198 | addWidget(spacer); | |
199 | ||
200 | addWidget(&menu_button_); | |
201 | ||
202 | sample_count_.installEventFilter(this); | |
203 | sample_rate_.installEventFilter(this); | |
204 | } | |
205 | ||
206 | void MainBar::update_device_list() | |
207 | { | |
208 | DeviceManager &mgr = session_.device_manager(); | |
209 | shared_ptr<devices::Device> selected_device = session_.device(); | |
210 | list< shared_ptr<devices::Device> > devs; | |
211 | ||
212 | copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs)); | |
213 | ||
214 | if (std::find(devs.begin(), devs.end(), selected_device) == devs.end()) | |
215 | devs.push_back(selected_device); | |
216 | ||
217 | device_selector_.set_device_list(devs, selected_device); | |
218 | update_device_config_widgets(); | |
219 | } | |
220 | ||
221 | ||
222 | void MainBar::set_capture_state(pv::Session::capture_state state) | |
223 | { | |
224 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; | |
225 | run_stop_button_.setIcon(*icons[state]); | |
226 | run_stop_button_.setText((state == pv::Session::Stopped) ? | |
227 | tr("Run") : tr("Stop")); | |
228 | run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); | |
229 | } | |
230 | ||
231 | void MainBar::update_sample_rate_selector() | |
232 | { | |
233 | Glib::VariantContainerBase gvar_dict; | |
234 | GVariant *gvar_list; | |
235 | const uint64_t *elements = nullptr; | |
236 | gsize num_elements; | |
237 | map< const ConfigKey*, std::set<Capability> > keys; | |
238 | ||
239 | if (updating_sample_rate_) { | |
240 | sample_rate_.show_none(); | |
241 | return; | |
242 | } | |
243 | ||
244 | const shared_ptr<devices::Device> device = | |
245 | device_selector_.selected_device(); | |
246 | if (!device) | |
247 | return; | |
248 | ||
249 | assert(!updating_sample_rate_); | |
250 | updating_sample_rate_ = true; | |
251 | ||
252 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
253 | ||
254 | try { | |
255 | keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS); | |
256 | } catch (Error) {} | |
257 | ||
258 | const auto iter = keys.find(ConfigKey::SAMPLERATE); | |
259 | if (iter != keys.end() && | |
260 | (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { | |
261 | try { | |
262 | gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE); | |
263 | } catch(const sigrok::Error &e) { | |
264 | // Failed to enunmerate samplerate | |
265 | (void)e; | |
266 | } | |
267 | } | |
268 | ||
269 | if (!gvar_dict.gobj()) { | |
270 | sample_rate_.show_none(); | |
271 | updating_sample_rate_ = false; | |
272 | return; | |
273 | } | |
274 | ||
275 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
276 | "samplerate-steps", G_VARIANT_TYPE("at")))) | |
277 | { | |
278 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
279 | gvar_list, &num_elements, sizeof(uint64_t)); | |
280 | ||
281 | const uint64_t min = elements[0]; | |
282 | const uint64_t max = elements[1]; | |
283 | const uint64_t step = elements[2]; | |
284 | ||
285 | g_variant_unref(gvar_list); | |
286 | ||
287 | assert(min > 0); | |
288 | assert(max > 0); | |
289 | assert(max > min); | |
290 | assert(step > 0); | |
291 | ||
292 | if (step == 1) | |
293 | sample_rate_.show_125_list(min, max); | |
294 | else | |
295 | { | |
296 | // When the step is not 1, we cam't make a 1-2-5-10 | |
297 | // list of sample rates, because we may not be able to | |
298 | // make round numbers. Therefore in this case, show a | |
299 | // spin box. | |
300 | sample_rate_.show_min_max_step(min, max, step); | |
301 | } | |
302 | } | |
303 | else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
304 | "samplerates", G_VARIANT_TYPE("at")))) | |
305 | { | |
306 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
307 | gvar_list, &num_elements, sizeof(uint64_t)); | |
308 | sample_rate_.show_list(elements, num_elements); | |
309 | g_variant_unref(gvar_list); | |
310 | } | |
311 | updating_sample_rate_ = false; | |
312 | ||
313 | update_sample_rate_selector_value(); | |
314 | } | |
315 | ||
316 | void MainBar::update_sample_rate_selector_value() | |
317 | { | |
318 | if (updating_sample_rate_) | |
319 | return; | |
320 | ||
321 | const shared_ptr<devices::Device> device = | |
322 | device_selector_.selected_device(); | |
323 | if (!device) | |
324 | return; | |
325 | ||
326 | try { | |
327 | auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE); | |
328 | uint64_t samplerate = | |
329 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); | |
330 | assert(!updating_sample_rate_); | |
331 | updating_sample_rate_ = true; | |
332 | sample_rate_.set_value(samplerate); | |
333 | updating_sample_rate_ = false; | |
334 | } catch (Error error) { | |
335 | qDebug() << "WARNING: Failed to get value of sample rate"; | |
336 | return; | |
337 | } | |
338 | } | |
339 | ||
340 | void MainBar::update_sample_count_selector() | |
341 | { | |
342 | if (updating_sample_count_) | |
343 | return; | |
344 | ||
345 | const shared_ptr<devices::Device> device = | |
346 | device_selector_.selected_device(); | |
347 | if (!device) | |
348 | return; | |
349 | ||
350 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
351 | ||
352 | assert(!updating_sample_count_); | |
353 | updating_sample_count_ = true; | |
354 | ||
355 | if (!sample_count_supported_) | |
356 | { | |
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 | const auto keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS); | |
370 | const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES); | |
371 | if (iter != keys.end() && | |
372 | (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { | |
373 | try { | |
374 | auto gvar = | |
375 | sr_dev->config_list(ConfigKey::LIMIT_SAMPLES); | |
376 | if (gvar.gobj()) | |
377 | g_variant_get(gvar.gobj(), "(tt)", | |
378 | &min_sample_count, &max_sample_count); | |
379 | } catch(const sigrok::Error &e) { | |
380 | // Failed to query sample limit | |
381 | (void)e; | |
382 | } | |
383 | } | |
384 | ||
385 | min_sample_count = min(max(min_sample_count, MinSampleCount), | |
386 | max_sample_count); | |
387 | ||
388 | sample_count_.show_125_list( | |
389 | min_sample_count, max_sample_count); | |
390 | ||
391 | try { | |
392 | auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES); | |
393 | sample_count = g_variant_get_uint64(gvar.gobj()); | |
394 | if (sample_count == 0) | |
395 | sample_count = DefaultSampleCount; | |
396 | sample_count = min(max(sample_count, MinSampleCount), | |
397 | max_sample_count); | |
398 | } catch (Error error) {} | |
399 | ||
400 | sample_count_.set_value(sample_count); | |
401 | ||
402 | updating_sample_count_ = false; | |
403 | } | |
404 | ||
405 | void MainBar::update_device_config_widgets() | |
406 | { | |
407 | using namespace pv::popups; | |
408 | ||
409 | const shared_ptr<devices::Device> device = | |
410 | device_selector_.selected_device(); | |
411 | ||
412 | // Hide the widgets if no device is selected | |
413 | channels_button_action_->setVisible(!!device); | |
414 | run_stop_button_action_->setVisible(!!device); | |
415 | if (!device) { | |
416 | configure_button_action_->setVisible(false); | |
417 | sample_count_.show_none(); | |
418 | sample_rate_.show_none(); | |
419 | return; | |
420 | } | |
421 | ||
422 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
423 | if (!sr_dev) | |
424 | return; | |
425 | ||
426 | // Update the configure popup | |
427 | DeviceOptions *const opts = new DeviceOptions(sr_dev, this); | |
428 | configure_button_action_->setVisible( | |
429 | !opts->binding().properties().empty()); | |
430 | configure_button_.set_popup(opts); | |
431 | ||
432 | // Update the channels popup | |
433 | Channels *const channels = new Channels(session_, this); | |
434 | channels_button_.set_popup(channels); | |
435 | ||
436 | // Update supported options. | |
437 | sample_count_supported_ = false; | |
438 | ||
439 | try { | |
440 | for (auto entry : sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS)) | |
441 | { | |
442 | auto key = entry.first; | |
443 | auto capabilities = entry.second; | |
444 | switch (key->id()) { | |
445 | case SR_CONF_LIMIT_SAMPLES: | |
446 | if (capabilities.count(Capability::SET)) | |
447 | sample_count_supported_ = true; | |
448 | break; | |
449 | case SR_CONF_LIMIT_FRAMES: | |
450 | if (capabilities.count(Capability::SET)) | |
451 | { | |
452 | sr_dev->config_set(ConfigKey::LIMIT_FRAMES, | |
453 | Glib::Variant<guint64>::create(1)); | |
454 | on_config_changed(); | |
455 | } | |
456 | break; | |
457 | default: | |
458 | break; | |
459 | } | |
460 | } | |
461 | } catch (Error error) {} | |
462 | ||
463 | // Add notification of reconfigure events | |
464 | disconnect(this, SLOT(on_config_changed())); | |
465 | connect(&opts->binding(), SIGNAL(config_changed()), | |
466 | this, SLOT(on_config_changed())); | |
467 | ||
468 | // Update sweep timing widgets. | |
469 | update_sample_count_selector(); | |
470 | update_sample_rate_selector(); | |
471 | } | |
472 | ||
473 | void MainBar::commit_sample_count() | |
474 | { | |
475 | uint64_t sample_count = 0; | |
476 | ||
477 | if (updating_sample_count_) | |
478 | return; | |
479 | ||
480 | const shared_ptr<devices::Device> device = | |
481 | device_selector_.selected_device(); | |
482 | if (!device) | |
483 | return; | |
484 | ||
485 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
486 | ||
487 | sample_count = sample_count_.value(); | |
488 | ||
489 | // Set the sample count | |
490 | assert(!updating_sample_count_); | |
491 | updating_sample_count_ = true; | |
492 | if (sample_count_supported_) | |
493 | { | |
494 | try { | |
495 | sr_dev->config_set(ConfigKey::LIMIT_SAMPLES, | |
496 | Glib::Variant<guint64>::create(sample_count)); | |
497 | on_config_changed(); | |
498 | } catch (Error error) { | |
499 | qDebug() << "Failed to configure sample count."; | |
500 | return; | |
501 | } | |
502 | } | |
503 | updating_sample_count_ = false; | |
504 | } | |
505 | ||
506 | void MainBar::commit_sample_rate() | |
507 | { | |
508 | uint64_t sample_rate = 0; | |
509 | ||
510 | if (updating_sample_rate_) | |
511 | return; | |
512 | ||
513 | const shared_ptr<devices::Device> device = | |
514 | device_selector_.selected_device(); | |
515 | if (!device) | |
516 | return; | |
517 | ||
518 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
519 | ||
520 | sample_rate = sample_rate_.value(); | |
521 | if (sample_rate == 0) | |
522 | return; | |
523 | ||
524 | // Set the samplerate | |
525 | assert(!updating_sample_rate_); | |
526 | updating_sample_rate_ = true; | |
527 | try { | |
528 | sr_dev->config_set(ConfigKey::SAMPLERATE, | |
529 | Glib::Variant<guint64>::create(sample_rate)); | |
530 | on_config_changed(); | |
531 | } catch (Error error) { | |
532 | qDebug() << "Failed to configure samplerate."; | |
533 | return; | |
534 | } | |
535 | updating_sample_rate_ = false; | |
536 | } | |
537 | ||
538 | void MainBar::on_device_selected() | |
539 | { | |
540 | shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
541 | if (!device) | |
542 | return; | |
543 | ||
544 | main_window_.select_device(device); | |
545 | ||
546 | update_device_config_widgets(); | |
547 | } | |
548 | ||
549 | void MainBar::on_sample_count_changed() | |
550 | { | |
551 | commit_sample_count(); | |
552 | } | |
553 | ||
554 | void MainBar::on_sample_rate_changed() | |
555 | { | |
556 | commit_sample_rate(); | |
557 | } | |
558 | ||
559 | void MainBar::on_run_stop() | |
560 | { | |
561 | commit_sample_count(); | |
562 | commit_sample_rate(); | |
563 | main_window_.run_stop(); | |
564 | } | |
565 | ||
566 | void MainBar::on_config_changed() | |
567 | { | |
568 | commit_sample_count(); | |
569 | update_sample_count_selector(); | |
570 | commit_sample_rate(); | |
571 | update_sample_rate_selector(); | |
572 | } | |
573 | ||
574 | bool MainBar::eventFilter(QObject *watched, QEvent *event) | |
575 | { | |
576 | if ((watched == &sample_count_ || watched == &sample_rate_) && | |
577 | (event->type() == QEvent::ToolTip)) { | |
578 | double sec = (double)sample_count_.value() / sample_rate_.value(); | |
579 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); | |
580 | ||
581 | QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec)); | |
582 | QToolTip::showText(help_event->globalPos(), str); | |
583 | ||
584 | return true; | |
585 | } | |
586 | ||
587 | return false; | |
588 | } | |
589 | ||
590 | } // namespace toolbars | |
591 | } // namespace pv |