]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
Session/MainBar: Fix device change handling
[pulseview.git] / pv / toolbars / mainbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7 3 *
079d39ea 4 * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk>
d4984fe7
JH
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
09f5d123 21#include <extdef.h>
dde1a563 22
079d39ea
JH
23#include <algorithm>
24#include <cassert>
215f9499 25
dde1a563 26#include <QAction>
48888313 27#include <QDebug>
0f8f8c18 28#include <QFileDialog>
40065ab6 29#include <QHelpEvent>
87f0df9b 30#include <QMenu>
0f8f8c18
SA
31#include <QMessageBox>
32#include <QSettings>
40065ab6 33#include <QToolTip>
dde1a563 34
7c657094 35#include "mainbar.hpp"
d4984fe7 36
0f8f8c18
SA
37#include <boost/algorithm/string/join.hpp>
38
2acdb232 39#include <pv/devicemanager.hpp>
da30ecb7 40#include <pv/devices/hardwaredevice.hpp>
0f8f8c18
SA
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>
4e7f5ba8 46#include <pv/mainwindow.hpp>
2acdb232
JH
47#include <pv/popups/deviceoptions.hpp>
48#include <pv/popups/channels.hpp>
49#include <pv/util.hpp>
0f8f8c18 50#include <pv/view/view.hpp>
998b89fd 51#include <pv/widgets/exportmenu.hpp>
ed43ef2e 52#include <pv/widgets/importmenu.hpp>
0f8f8c18
SA
53#ifdef ENABLE_DECODE
54#include <pv/widgets/decodermenu.hpp>
55#endif
cdb50f67 56
fe3a1c21 57#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 58
079d39ea 59using std::back_inserter;
0f8f8c18 60using std::cerr;
079d39ea 61using std::copy;
0f8f8c18 62using std::endl;
079d39ea 63using std::list;
19adbc2c 64using std::map;
b50ef520
JH
65using std::max;
66using std::min;
0f8f8c18 67using std::pair;
f9abf97e 68using std::shared_ptr;
819f4c25 69using std::string;
079d39ea 70using std::vector;
dd63af74 71
e8d00928
ML
72using sigrok::Capability;
73using sigrok::ConfigKey;
e8d00928 74using sigrok::Error;
ed43ef2e 75using sigrok::InputFormat;
0f8f8c18
SA
76using sigrok::OutputFormat;
77
78using boost::algorithm::join;
e8d00928 79
51e77110 80namespace pv {
f4c92e1c 81namespace toolbars {
51e77110 82
7c657094
JH
83const uint64_t MainBar::MinSampleCount = 100ULL;
84const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
85const uint64_t MainBar::DefaultSampleCount = 1000000;
09f5d123 86
0f8f8c18
SA
87const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
88const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
89
101e7a9b 90MainBar::MainBar(Session &session, MainWindow &main_window) :
4e7f5ba8 91 QToolBar("Sampling Bar", &main_window),
0f8f8c18
SA
92 action_open_(new QAction(this)),
93 action_save_as_(new QAction(this)),
94 action_save_selection_as_(new QAction(this)),
95 action_connect_(new QAction(this)),
96 action_view_zoom_in_(new QAction(this)),
97 action_view_zoom_out_(new QAction(this)),
98 action_view_zoom_fit_(new QAction(this)),
99 action_view_zoom_one_to_one_(new QAction(this)),
100 action_view_show_cursors_(new QAction(this)),
8dbbc7f0 101 session_(session),
0f8f8c18
SA
102 device_selector_(&main_window, session.device_manager(),
103 action_connect_),
8dbbc7f0 104 configure_button_(this),
4c60462b 105 configure_button_action_(nullptr),
8dbbc7f0 106 channels_button_(this),
9dd88889 107 channels_button_action_(nullptr),
8dbbc7f0
JH
108 sample_count_(" samples", this),
109 sample_rate_("Hz", this),
110 updating_sample_rate_(false),
111 updating_sample_count_(false),
112 sample_count_supported_(false),
113 icon_red_(":/icons/status-red.svg"),
114 icon_green_(":/icons/status-green.svg"),
115 icon_grey_(":/icons/status-grey.svg"),
87f0df9b 116 run_stop_button_(this),
9dd88889 117 run_stop_button_action_(nullptr),
87f0df9b 118 menu_button_(this)
0f8f8c18
SA
119#ifdef ENABLE_DECODE
120 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
121#endif
6fb67b27 122{
7c657094 123 setObjectName(QString::fromUtf8("MainBar"));
0c7cdea2 124
f2dbf150
JH
125 setMovable(false);
126 setFloatable(false);
758f6023 127 setContextMenuPolicy(Qt::PreventContextMenu);
f2dbf150 128
0f8f8c18
SA
129 // Actions
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 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
135
136 action_save_as_->setText(tr("&Save As..."));
137 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
138 QIcon(":/icons/document-save-as.png")));
139 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
140 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
141
142 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
143 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
144 QIcon(":/icons/document-save-as.png")));
145 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
146 action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
147
148 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
149 session.device_manager().context());
150 menu_file_export->setTitle(tr("&Export"));
151 connect(menu_file_export,
152 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
153 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
154
155 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
156 session.device_manager().context());
157 menu_file_import->setTitle(tr("&Import"));
158 connect(menu_file_import,
159 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
160 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
161
162 action_connect_->setText(tr("&Connect to Device..."));
163 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
164
165 action_view_zoom_in_->setText(tr("Zoom &In"));
166 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
167 QIcon(":/icons/zoom-in.png")));
168 // simply using Qt::Key_Plus shows no + in the menu
169 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
170 action_view_zoom_in_->setObjectName(
171 QString::fromUtf8("actionViewZoomIn"));
172
173 action_view_zoom_out_->setText(tr("Zoom &Out"));
174 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
175 QIcon(":/icons/zoom-out.png")));
176 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
177 action_view_zoom_out_->setObjectName(
178 QString::fromUtf8("actionViewZoomOut"));
179
180 action_view_zoom_fit_->setCheckable(true);
181 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
182 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
183 QIcon(":/icons/zoom-fit.png")));
184 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
185 action_view_zoom_fit_->setObjectName(
186 QString::fromUtf8("actionViewZoomFit"));
187
188 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
189 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
190 QIcon(":/icons/zoom-original.png")));
191 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
192 action_view_zoom_one_to_one_->setObjectName(
193 QString::fromUtf8("actionViewZoomOneToOne"));
194
195 action_view_show_cursors_->setCheckable(true);
196 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
197 QIcon(":/icons/show-cursors.svg")));
198 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
199 action_view_show_cursors_->setObjectName(
200 QString::fromUtf8("actionViewShowCursors"));
201 action_view_show_cursors_->setText(tr("Show &Cursors"));
202
ed43ef2e
JH
203 // Open button
204 QToolButton *const open_button = new QToolButton(this);
205
206 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
0f8f8c18 207 session.device_manager().context(), action_open_);
ed43ef2e
JH
208 connect(import_menu,
209 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
0f8f8c18 210 &main_window,
ed43ef2e
JH
211 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
212
213 open_button->setMenu(import_menu);
0f8f8c18 214 open_button->setDefaultAction(action_open_);
ed43ef2e
JH
215 open_button->setPopupMode(QToolButton::MenuButtonPopup);
216
998b89fd
JH
217 // Save button
218 QToolButton *const save_button = new QToolButton(this);
219
686d9151 220 vector<QAction *> open_actions;
0f8f8c18
SA
221 open_actions.push_back(action_save_as_);
222 open_actions.push_back(action_save_selection_as_);
686d9151 223
998b89fd
JH
224 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
225 session.device_manager().context(),
686d9151 226 open_actions);
998b89fd
JH
227 connect(export_menu,
228 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
0f8f8c18 229 &main_window,
998b89fd
JH
230 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
231
232 save_button->setMenu(export_menu);
0f8f8c18 233 save_button->setDefaultAction(action_save_as_);
998b89fd
JH
234 save_button->setPopupMode(QToolButton::MenuButtonPopup);
235
079d39ea
JH
236 // Device selector menu
237 connect(&device_selector_, SIGNAL(device_selected()),
238 this, SLOT(on_device_selected()));
91f8fe8c
SA
239 connect(&session, SIGNAL(device_changed()),
240 this, SLOT(on_device_changed()));
079d39ea 241
168888e2
JH
242 // Setup the decoder button
243#ifdef ENABLE_DECODE
0f8f8c18
SA
244 menu_decoders_add_->setTitle(tr("&Add"));
245 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
246 this, SLOT(add_decoder(srd_decoder*)));
247
168888e2
JH
248 QToolButton *add_decoder_button = new QToolButton(this);
249 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
250 QIcon(":/icons/add-decoder.svg")));
251 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
0f8f8c18 252 add_decoder_button->setMenu(menu_decoders_add_);
168888e2
JH
253#endif
254
87f0df9b 255 // Setup the toolbar
ed43ef2e 256 addWidget(open_button);
998b89fd 257 addWidget(save_button);
87f0df9b 258 addSeparator();
0f8f8c18
SA
259 addAction(action_view_zoom_in_);
260 addAction(action_view_zoom_out_);
261 addAction(action_view_zoom_fit_);
262 addAction(action_view_zoom_one_to_one_);
87f0df9b 263 addSeparator();
0f8f8c18 264 addAction(action_view_show_cursors_);
bdf57963 265 addSeparator();
87f0df9b 266
8dbbc7f0 267 connect(&run_stop_button_, SIGNAL(clicked()),
9f3d12f3 268 this, SLOT(on_run_stop()));
8dbbc7f0 269 connect(&sample_count_, SIGNAL(value_changed()),
124d97de 270 this, SLOT(on_sample_count_changed()));
8dbbc7f0 271 connect(&sample_rate_, SIGNAL(value_changed()),
1198b887 272 this, SLOT(on_sample_rate_changed()));
dde1a563 273
8dbbc7f0 274 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
215f9499 275
2b81ae46 276 set_capture_state(pv::Session::Stopped);
274d4f13 277
8dbbc7f0 278 configure_button_.setIcon(QIcon::fromTheme("configure",
688ef645 279 QIcon(":/icons/configure.png")));
b7b659aa 280
8dbbc7f0 281 channels_button_.setIcon(QIcon::fromTheme("channels",
6ac6242b 282 QIcon(":/icons/channels.svg")));
cdb50f67 283
8dbbc7f0 284 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
f5798068 285
8dbbc7f0
JH
286 addWidget(&device_selector_);
287 configure_button_action_ = addWidget(&configure_button_);
9dd88889 288 channels_button_action_ = addWidget(&channels_button_);
8dbbc7f0
JH
289 addWidget(&sample_count_);
290 addWidget(&sample_rate_);
9dd88889 291 run_stop_button_action_ = addWidget(&run_stop_button_);
168888e2
JH
292#ifdef ENABLE_DECODE
293 addSeparator();
294 addWidget(add_decoder_button);
295#endif
40065ab6 296
87f0df9b
JH
297 QWidget *const spacer = new QWidget();
298 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
299 addWidget(spacer);
300
301 addWidget(&menu_button_);
302
8dbbc7f0
JH
303 sample_count_.installEventFilter(this);
304 sample_rate_.installEventFilter(this);
0f8f8c18 305
0f8f8c18
SA
306 // Setup session_ events
307 connect(&session_, SIGNAL(capture_state_changed(int)), this,
308 SLOT(capture_state_changed(int)));
309 connect(&session_, SIGNAL(device_selected()), this,
310 SLOT(device_selected()));
311
101e7a9b 312 update_device_list();
0f8f8c18
SA
313}
314
315Session &MainBar::session(void) const
316{
317 return session_;
6fb67b27
JH
318}
319
079d39ea 320void MainBar::update_device_list()
18203d86 321{
079d39ea 322 DeviceManager &mgr = session_.device_manager();
da30ecb7
JH
323 shared_ptr<devices::Device> selected_device = session_.device();
324 list< shared_ptr<devices::Device> > devs;
e8d00928 325
079d39ea 326 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
53a7cce4 327
079d39ea
JH
328 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
329 devs.push_back(selected_device);
e95e8563 330
079d39ea 331 device_selector_.set_device_list(devs, selected_device);
e95e8563 332 update_device_config_widgets();
18203d86
JH
333}
334
6fb67b27 335
7c657094 336void MainBar::set_capture_state(pv::Session::capture_state state)
6ac96c2e 337{
8dbbc7f0
JH
338 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
339 run_stop_button_.setIcon(*icons[state]);
2b81ae46 340 run_stop_button_.setText((state == pv::Session::Stopped) ?
2b49eeb0 341 tr("Run") : tr("Stop"));
8dbbc7f0 342 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
73e170f9
SA
343
344 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
345
346 device_selector_.setEnabled(ui_enabled);
347 configure_button_.setEnabled(ui_enabled);
348 channels_button_.setEnabled(ui_enabled);
349 sample_count_.setEnabled(ui_enabled);
350 sample_rate_.setEnabled(ui_enabled);
6ac96c2e
JH
351}
352
7e0c99bf
SA
353void MainBar::reset_device_selector()
354{
355 device_selector_.reset();
356}
357
101e7a9b
SA
358void MainBar::select_device(shared_ptr<devices::Device> device)
359{
360 try {
361 if (device)
362 session_.set_device(device);
363 else
364 session_.set_default_device();
365 } catch (const QString &e) {
366 QMessageBox msg(this);
367 msg.setText(e);
368 msg.setInformativeText(tr("Failed to Select Device"));
369 msg.setStandardButtons(QMessageBox::Ok);
370 msg.setIcon(QMessageBox::Warning);
371 msg.exec();
372 }
373}
374
375void MainBar::load_init_file(const std::string &file_name,
376 const std::string &format)
377{
378 shared_ptr<InputFormat> input_format;
379
380 DeviceManager& device_manager = session_.device_manager();
381
382 if (!format.empty()) {
383 const map<string, shared_ptr<InputFormat> > formats =
384 device_manager.context()->input_formats();
385 const auto iter = find_if(formats.begin(), formats.end(),
386 [&](const pair<string, shared_ptr<InputFormat> > f) {
387 return f.first == format; });
388 if (iter == formats.end()) {
389 cerr << "Unexpected input format: " << format << endl;
390 return;
391 }
392
393 input_format = (*iter).second;
394 }
395
396 load_file(QString::fromStdString(file_name), input_format);
397}
398
0f8f8c18
SA
399QAction* MainBar::action_open() const
400{
401 return action_open_;
402}
403
404QAction* MainBar::action_save_as() const
405{
406 return action_save_as_;
407}
408
409QAction* MainBar::action_save_selection_as() const
410{
411 return action_save_selection_as_;
412}
413
414QAction* MainBar::action_connect() const
415{
416 return action_connect_;
417}
418
419QAction* MainBar::action_view_zoom_in() const
420{
421 return action_view_zoom_in_;
422}
423
424QAction* MainBar::action_view_zoom_out() const
425{
426 return action_view_zoom_out_;
427}
428
429QAction* MainBar::action_view_zoom_fit() const
430{
431 return action_view_zoom_fit_;
432}
433
434QAction* MainBar::action_view_zoom_one_to_one() const
435{
436 return action_view_zoom_one_to_one_;
437}
438
439QAction* MainBar::action_view_show_cursors() const
440{
441 return action_view_show_cursors_;
442}
443
444void MainBar::run_stop()
445{
446 switch (session_.get_capture_state()) {
447 case Session::Stopped:
448 session_.start_capture([&](QString message) {
449 session_error("Capture failed", message); });
450 break;
451 case Session::AwaitingTrigger:
452 case Session::Running:
453 session_.stop_capture();
454 break;
455 }
456}
457
0f8f8c18
SA
458void MainBar::load_file(QString file_name,
459 std::shared_ptr<sigrok::InputFormat> format,
460 const std::map<std::string, Glib::VariantBase> &options)
461{
462 DeviceManager& device_manager = session_.device_manager();
463
464 const QString errorMessage(
465 QString("Failed to load file %1").arg(file_name));
466
467 try {
468 if (format)
469 session_.set_device(shared_ptr<devices::Device>(
470 new devices::InputFile(
471 device_manager.context(),
472 file_name.toStdString(),
473 format, options)));
474 else
475 session_.set_device(shared_ptr<devices::Device>(
476 new devices::SessionFile(
477 device_manager.context(),
478 file_name.toStdString())));
479 } catch (Error e) {
480 show_session_error(tr("Failed to load ") + file_name, e.what());
481 session_.set_default_device();
482 update_device_list();
483 return;
484 }
485
101e7a9b
SA
486 session_.set_name(QFileInfo(file_name).fileName());
487
0f8f8c18
SA
488 update_device_list();
489
490 session_.start_capture([&, errorMessage](QString infoMessage) {
491 session_error(errorMessage, infoMessage); });
492}
493
7c657094 494void MainBar::update_sample_rate_selector()
dde1a563 495{
e8d00928
ML
496 Glib::VariantContainerBase gvar_dict;
497 GVariant *gvar_list;
4c60462b 498 const uint64_t *elements = nullptr;
488f5d3f 499 gsize num_elements;
a6ed12bf 500 map< const ConfigKey*, std::set<Capability> > keys;
dde1a563 501
9dd88889
JH
502 if (updating_sample_rate_) {
503 sample_rate_.show_none();
3d4f16af 504 return;
9dd88889 505 }
3d4f16af 506
da30ecb7
JH
507 const shared_ptr<devices::Device> device =
508 device_selector_.selected_device();
e8d00928 509 if (!device)
ef4d0201
JH
510 return;
511
8dbbc7f0
JH
512 assert(!updating_sample_rate_);
513 updating_sample_rate_ = true;
1198b887 514
da30ecb7 515 const shared_ptr<sigrok::Device> sr_dev = device->device();
a6ed12bf 516
7bb0fbf4
ML
517 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
518 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
519 } else {
8dbbc7f0
JH
520 sample_rate_.show_none();
521 updating_sample_rate_ = false;
dde1a563 522 return;
1198b887 523 }
dde1a563 524
e8d00928 525 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
2ad82c2e 526 "samplerate-steps", G_VARIANT_TYPE("at")))) {
488f5d3f
BV
527 elements = (const uint64_t *)g_variant_get_fixed_array(
528 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
529
530 const uint64_t min = elements[0];
531 const uint64_t max = elements[1];
532 const uint64_t step = elements[2];
533
488f5d3f 534 g_variant_unref(gvar_list);
aafe53af
JH
535
536 assert(min > 0);
537 assert(max > 0);
538 assert(max > min);
539 assert(step > 0);
540
541 if (step == 1)
8dbbc7f0 542 sample_rate_.show_125_list(min, max);
2ad82c2e 543 else {
aafe53af
JH
544 // When the step is not 1, we cam't make a 1-2-5-10
545 // list of sample rates, because we may not be able to
546 // make round numbers. Therefore in this case, show a
547 // spin box.
8dbbc7f0 548 sample_rate_.show_min_max_step(min, max, step);
aafe53af 549 }
2ad82c2e
UH
550 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
551 "samplerates", G_VARIANT_TYPE("at")))) {
488f5d3f
BV
552 elements = (const uint64_t *)g_variant_get_fixed_array(
553 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 554 sample_rate_.show_list(elements, num_elements);
488f5d3f 555 g_variant_unref(gvar_list);
dde1a563 556 }
8dbbc7f0 557 updating_sample_rate_ = false;
48888313
JH
558
559 update_sample_rate_selector_value();
560}
561
7c657094 562void MainBar::update_sample_rate_selector_value()
48888313 563{
8dbbc7f0 564 if (updating_sample_rate_)
3d4f16af
JH
565 return;
566
da30ecb7
JH
567 const shared_ptr<devices::Device> device =
568 device_selector_.selected_device();
e8d00928 569 if (!device)
19adbc2c
JH
570 return;
571
e8d00928 572 try {
da30ecb7 573 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
e8d00928 574 uint64_t samplerate =
15289d5c 575 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
576 assert(!updating_sample_rate_);
577 updating_sample_rate_ = true;
578 sample_rate_.set_value(samplerate);
579 updating_sample_rate_ = false;
e8d00928 580 } catch (Error error) {
8dd44190 581 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
582 return;
583 }
48888313
JH
584}
585
7c657094 586void MainBar::update_sample_count_selector()
85756012 587{
8dbbc7f0 588 if (updating_sample_count_)
3d4f16af
JH
589 return;
590
da30ecb7
JH
591 const shared_ptr<devices::Device> device =
592 device_selector_.selected_device();
e8d00928 593 if (!device)
19adbc2c
JH
594 return;
595
da30ecb7
JH
596 const shared_ptr<sigrok::Device> sr_dev = device->device();
597
8dbbc7f0
JH
598 assert(!updating_sample_count_);
599 updating_sample_count_ = true;
85756012 600
2ad82c2e 601 if (!sample_count_supported_) {
3e7636f9
JH
602 sample_count_.show_none();
603 updating_sample_count_ = false;
604 return;
605 }
b50ef520 606
3e7636f9
JH
607 uint64_t sample_count = sample_count_.value();
608 uint64_t min_sample_count = 0;
609 uint64_t max_sample_count = MaxSampleCount;
3cbffdcf 610
3e7636f9
JH
611 if (sample_count == 0)
612 sample_count = DefaultSampleCount;
b50ef520 613
7bb0fbf4
ML
614 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
615 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
616 if (gvar.gobj())
617 g_variant_get(gvar.gobj(), "(tt)",
618 &min_sample_count, &max_sample_count);
ed1d9d81 619 }
1d04852f 620
3e7636f9
JH
621 min_sample_count = min(max(min_sample_count, MinSampleCount),
622 max_sample_count);
b50ef520 623
3e7636f9
JH
624 sample_count_.show_125_list(
625 min_sample_count, max_sample_count);
b50ef520 626
7bb0fbf4 627 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
da30ecb7 628 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
3e7636f9
JH
629 sample_count = g_variant_get_uint64(gvar.gobj());
630 if (sample_count == 0)
631 sample_count = DefaultSampleCount;
632 sample_count = min(max(sample_count, MinSampleCount),
633 max_sample_count);
7bb0fbf4 634 }
3e7636f9
JH
635
636 sample_count_.set_value(sample_count);
df3c1aa3 637
8dbbc7f0 638 updating_sample_count_ = false;
85756012
ML
639}
640
7c657094 641void MainBar::update_device_config_widgets()
e95e8563 642{
e95e8563
JH
643 using namespace pv::popups;
644
da30ecb7
JH
645 const shared_ptr<devices::Device> device =
646 device_selector_.selected_device();
9dd88889
JH
647
648 // Hide the widgets if no device is selected
649 channels_button_action_->setVisible(!!device);
650 run_stop_button_action_->setVisible(!!device);
651 if (!device) {
652 configure_button_action_->setVisible(false);
653 sample_count_.show_none();
654 sample_rate_.show_none();
e95e8563 655 return;
9dd88889 656 }
e95e8563 657
da30ecb7
JH
658 const shared_ptr<sigrok::Device> sr_dev = device->device();
659 if (!sr_dev)
660 return;
661
e95e8563 662 // Update the configure popup
da30ecb7 663 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
8dbbc7f0 664 configure_button_action_->setVisible(
e95e8563 665 !opts->binding().properties().empty());
8dbbc7f0 666 configure_button_.set_popup(opts);
e95e8563 667
6ac6242b 668 // Update the channels popup
8dbbc7f0
JH
669 Channels *const channels = new Channels(session_, this);
670 channels_button_.set_popup(channels);
e95e8563
JH
671
672 // Update supported options.
8dbbc7f0 673 sample_count_supported_ = false;
e95e8563 674
7bb0fbf4
ML
675 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
676 sample_count_supported_ = true;
677
678 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
679 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
680 Glib::Variant<guint64>::create(1));
681 on_config_changed();
682 }
e95e8563
JH
683
684 // Add notification of reconfigure events
685 disconnect(this, SLOT(on_config_changed()));
e8d00928 686 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
687 this, SLOT(on_config_changed()));
688
689 // Update sweep timing widgets.
690 update_sample_count_selector();
691 update_sample_rate_selector();
692}
693
0f8f8c18
SA
694void MainBar::commit_sample_rate()
695{
696 uint64_t sample_rate = 0;
697
698 const shared_ptr<devices::Device> device =
699 device_selector_.selected_device();
700 if (!device)
701 return;
702
703 const shared_ptr<sigrok::Device> sr_dev = device->device();
704
705 sample_rate = sample_rate_.value();
706 if (sample_rate == 0)
707 return;
708
709 try {
710 sr_dev->config_set(ConfigKey::SAMPLERATE,
711 Glib::Variant<guint64>::create(sample_rate));
712 update_sample_rate_selector();
713 } catch (Error error) {
714 qDebug() << "Failed to configure samplerate.";
715 return;
716 }
717
718 // Devices with built-in memory might impose limits on certain
719 // configurations, so let's check what sample count the driver
720 // lets us use now.
721 update_sample_count_selector();
722}
723
7c657094 724void MainBar::commit_sample_count()
124d97de
ML
725{
726 uint64_t sample_count = 0;
727
da30ecb7
JH
728 const shared_ptr<devices::Device> device =
729 device_selector_.selected_device();
e8d00928 730 if (!device)
19adbc2c
JH
731 return;
732
da30ecb7
JH
733 const shared_ptr<sigrok::Device> sr_dev = device->device();
734
8dbbc7f0 735 sample_count = sample_count_.value();
2ad82c2e 736 if (sample_count_supported_) {
e8d00928 737 try {
da30ecb7 738 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 739 Glib::Variant<guint64>::create(sample_count));
449aec3c 740 update_sample_count_selector();
e8d00928
ML
741 } catch (Error error) {
742 qDebug() << "Failed to configure sample count.";
743 return;
744 }
124d97de 745 }
e13e7879
SA
746
747 // Devices with built-in memory might impose limits on certain
748 // configurations, so let's check what sample rate the driver
749 // lets us use now.
750 update_sample_rate_selector();
124d97de
ML
751}
752
0f8f8c18 753void MainBar::session_error(const QString text, const QString info_text)
48888313 754{
0f8f8c18
SA
755 QMetaObject::invokeMethod(this, "show_session_error",
756 Qt::QueuedConnection, Q_ARG(QString, text),
757 Q_ARG(QString, info_text));
758}
48888313 759
0f8f8c18
SA
760void MainBar::show_session_error(const QString text, const QString info_text)
761{
762 QMessageBox msg(this);
763 msg.setText(text);
764 msg.setInformativeText(info_text);
765 msg.setStandardButtons(QMessageBox::Ok);
766 msg.setIcon(QMessageBox::Warning);
767 msg.exec();
768}
19adbc2c 769
0f8f8c18
SA
770void MainBar::capture_state_changed(int state)
771{
772 set_capture_state((pv::Session::capture_state)state);
773}
da30ecb7 774
0f8f8c18
SA
775void MainBar::add_decoder(srd_decoder *decoder)
776{
777#ifdef ENABLE_DECODE
778 assert(decoder);
779 session_.add_decoder(decoder);
780#else
781 (void)decoder;
782#endif
783}
784
785void MainBar::export_file(shared_ptr<OutputFormat> format,
786 bool selection_only)
787{
788 using pv::dialogs::StoreProgress;
789
790 // Stop any currently running capture session
791 session_.stop_capture();
792
793 QSettings settings;
794 const QString dir = settings.value(SettingSaveDirectory).toString();
795
796 std::pair<uint64_t, uint64_t> sample_range;
797
798 // Selection only? Verify that the cursors are active and fetch their values
799 if (selection_only) {
800 if (!session_.main_view()->cursors()->enabled()) {
801 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
802 "cursors before you can save the data enclosed by them " \
803 "to a session file (e.g. using ALT-V - Show Cursors)."));
804 return;
805 }
806
807 const double samplerate = session_.get_samplerate();
808
809 const pv::util::Timestamp& start_time = session_.main_view()->cursors()->first()->time();
810 const pv::util::Timestamp& end_time = session_.main_view()->cursors()->second()->time();
811
812 const uint64_t start_sample =
813 std::max((double)0, start_time.convert_to<double>() * samplerate);
814 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
815
816 sample_range = std::make_pair(start_sample, end_sample);
817 } else {
818 sample_range = std::make_pair(0, 0);
819 }
820
821 // Construct the filter
822 const vector<string> exts = format->extensions();
823 QString filter = tr("%1 files ").arg(
824 QString::fromStdString(format->description()));
825
826 if (exts.empty())
827 filter += "(*.*)";
828 else
829 filter += QString("(*.%1);;%2 (*.*)").arg(
830 QString::fromStdString(join(exts, ", *.")),
831 tr("All Files"));
832
833 // Show the file dialog
834 const QString file_name = QFileDialog::getSaveFileName(
835 this, tr("Save File"), dir, filter);
836
837 if (file_name.isEmpty())
f9541bde
JH
838 return;
839
0f8f8c18
SA
840 const QString abs_path = QFileInfo(file_name).absolutePath();
841 settings.setValue(SettingSaveDirectory, abs_path);
842
843 // Show the options dialog
844 map<string, Glib::VariantBase> options;
845 if (!format->options().empty()) {
846 dialogs::InputOutputOptions dlg(
847 tr("Export %1").arg(QString::fromStdString(
848 format->description())),
849 format->options(), this);
850 if (!dlg.exec())
851 return;
852 options = dlg.options();
853 }
854
101e7a9b
SA
855 session_.set_name(QFileInfo(file_name).fileName());
856
0f8f8c18
SA
857 StoreProgress *dlg = new StoreProgress(file_name, format, options,
858 sample_range, session_, this);
859 dlg->run();
860}
861
862void MainBar::import_file(shared_ptr<InputFormat> format)
863{
864 assert(format);
865
866 QSettings settings;
867 const QString dir = settings.value(SettingOpenDirectory).toString();
868
869 // Construct the filter
870 const vector<string> exts = format->extensions();
871 const QString filter = exts.empty() ? "" :
872 tr("%1 files (*.%2)").arg(
873 QString::fromStdString(format->description()),
874 QString::fromStdString(join(exts, ", *.")));
875
876 // Show the file dialog
877 const QString file_name = QFileDialog::getOpenFileName(
878 this, tr("Import File"), dir, tr(
879 "%1 files (*.*);;All Files (*.*)").arg(
880 QString::fromStdString(format->description())));
881
882 if (file_name.isEmpty())
48888313 883 return;
0f8f8c18
SA
884
885 // Show the options dialog
886 map<string, Glib::VariantBase> options;
887 if (!format->options().empty()) {
888 dialogs::InputOutputOptions dlg(
889 tr("Import %1").arg(QString::fromStdString(
890 format->description())),
891 format->options(), this);
892 if (!dlg.exec())
893 return;
894 options = dlg.options();
48888313 895 }
e13e7879 896
0f8f8c18
SA
897 load_file(file_name, format, options);
898
899 const QString abs_path = QFileInfo(file_name).absolutePath();
900 settings.setValue(SettingOpenDirectory, abs_path);
dde1a563
JH
901}
902
7c657094 903void MainBar::on_device_selected()
dde1a563 904{
da30ecb7 905 shared_ptr<devices::Device> device = device_selector_.selected_device();
0f8f8c18
SA
906 if (!device) {
907 reset_device_selector();
19adbc2c 908 return;
0f8f8c18 909 }
19adbc2c 910
0f8f8c18 911 select_device(device);
91f8fe8c 912}
51d4a9ab 913
91f8fe8c
SA
914void MainBar::on_device_changed()
915{
916 update_device_list();
e95e8563 917 update_device_config_widgets();
dde1a563 918}
51e77110 919
7c657094 920void MainBar::on_sample_count_changed()
124d97de 921{
449aec3c
SA
922 if (!updating_sample_count_)
923 commit_sample_count();
124d97de
ML
924}
925
7c657094 926void MainBar::on_sample_rate_changed()
48888313 927{
0e0b6b3e
SA
928 if (!updating_sample_rate_)
929 commit_sample_rate();
48888313
JH
930}
931
7c657094 932void MainBar::on_run_stop()
9f3d12f3 933{
b50ef520 934 commit_sample_count();
9f3d12f3 935 commit_sample_rate();
0f8f8c18 936 run_stop();
9f3d12f3
JH
937}
938
7c657094 939void MainBar::on_config_changed()
82afd5e3
JH
940{
941 commit_sample_count();
82afd5e3 942 commit_sample_rate();
82afd5e3
JH
943}
944
0f8f8c18
SA
945void MainBar::on_actionOpen_triggered()
946{
947 QSettings settings;
948 const QString dir = settings.value(SettingOpenDirectory).toString();
949
950 // Show the dialog
951 const QString file_name = QFileDialog::getOpenFileName(
952 this, tr("Open File"), dir, tr(
953 "Sigrok Sessions (*.sr);;"
954 "All Files (*.*)"));
955
956 if (!file_name.isEmpty()) {
957 load_file(file_name);
958
959 const QString abs_path = QFileInfo(file_name).absolutePath();
960 settings.setValue(SettingOpenDirectory, abs_path);
961 }
962}
963
964void MainBar::on_actionSaveAs_triggered()
965{
966 export_file(session_.device_manager().context()->output_formats()["srzip"]);
967}
968
969void MainBar::on_actionSaveSelectionAs_triggered()
970{
971 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
972}
973
974void MainBar::on_actionConnect_triggered()
975{
976 // Stop any currently running capture session
977 session_.stop_capture();
978
979 dialogs::Connect dlg(this, session_.device_manager());
980
981 // If the user selected a device, select it in the device list. Select the
982 // current device otherwise.
983 if (dlg.exec())
984 select_device(dlg.get_selected_device());
985
986 update_device_list();
987}
988
989void MainBar::on_actionViewZoomIn_triggered()
990{
991 session_.main_view()->zoom(1);
992}
993
994void MainBar::on_actionViewZoomOut_triggered()
995{
996 session_.main_view()->zoom(-1);
997}
998
999void MainBar::on_actionViewZoomFit_triggered()
1000{
1001 session_.main_view()->zoom_fit(action_view_zoom_fit_->isChecked());
1002}
1003
1004void MainBar::on_actionViewZoomOneToOne_triggered()
1005{
1006 session_.main_view()->zoom_one_to_one();
1007}
1008
1009void MainBar::on_actionViewShowCursors_triggered()
1010{
1011 const bool show = !session_.main_view()->cursors_shown();
1012 if (show)
1013 session_.main_view()->centre_cursors();
1014
1015 session_.main_view()->show_cursors(show);
1016}
1017
7c657094 1018bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 1019{
2ad82c2e
UH
1020 if (sample_count_supported_ && (watched == &sample_count_ ||
1021 watched == &sample_rate_) &&
1022 (event->type() == QEvent::ToolTip)) {
60d9b99a 1023 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
40065ab6 1024 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45 1025
3ccf0f7f
JS
1026 QString str = tr("Total sampling time: %1").arg(
1027 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
40065ab6
JS
1028 QToolTip::showText(help_event->globalPos(), str);
1029
1030 return true;
1031 }
1032
1033 return false;
1034}
1035
f4c92e1c 1036} // namespace toolbars
51e77110 1037} // namespace pv