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