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