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