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