]>
Commit | Line | Data |
---|---|---|
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
d4984fe7 JH |
18 | */ |
19 | ||
09f5d123 | 20 | #include <extdef.h> |
dde1a563 | 21 | |
079d39ea JH |
22 | #include <algorithm> |
23 | #include <cassert> | |
215f9499 | 24 | |
dde1a563 | 25 | #include <QAction> |
48888313 | 26 | #include <QDebug> |
0f8f8c18 | 27 | #include <QFileDialog> |
40065ab6 | 28 | #include <QHelpEvent> |
87f0df9b | 29 | #include <QMenu> |
0f8f8c18 SA |
30 | #include <QMessageBox> |
31 | #include <QSettings> | |
40065ab6 | 32 | #include <QToolTip> |
dde1a563 | 33 | |
7c657094 | 34 | #include "mainbar.hpp" |
d4984fe7 | 35 | |
0f8f8c18 SA |
36 | #include <boost/algorithm/string/join.hpp> |
37 | ||
e771b42d | 38 | #include <pv/data/decodesignal.hpp> |
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 | 47 | #include <pv/popups/channels.hpp> |
aca9aa83 | 48 | #include <pv/popups/deviceoptions.hpp> |
2acdb232 | 49 | #include <pv/util.hpp> |
1573bf16 | 50 | #include <pv/views/trace/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 JH |
59 | using std::back_inserter; |
60 | using std::copy; | |
61 | using std::list; | |
6f925ba9 | 62 | using std::make_pair; |
19adbc2c | 63 | using std::map; |
b50ef520 JH |
64 | using std::max; |
65 | using std::min; | |
6f925ba9 UH |
66 | using std::pair; |
67 | using std::set; | |
f9abf97e | 68 | using std::shared_ptr; |
819f4c25 | 69 | using std::string; |
079d39ea | 70 | using std::vector; |
dd63af74 | 71 | |
e8d00928 ML |
72 | using sigrok::Capability; |
73 | using sigrok::ConfigKey; | |
e8d00928 | 74 | using sigrok::Error; |
ed43ef2e | 75 | using sigrok::InputFormat; |
0f8f8c18 SA |
76 | using sigrok::OutputFormat; |
77 | ||
78 | using boost::algorithm::join; | |
e8d00928 | 79 | |
51e77110 | 80 | namespace pv { |
f4c92e1c | 81 | namespace toolbars { |
51e77110 | 82 | |
7c657094 JH |
83 | const uint64_t MainBar::MinSampleCount = 100ULL; |
84 | const uint64_t MainBar::MaxSampleCount = 1000000000000ULL; | |
85 | const uint64_t MainBar::DefaultSampleCount = 1000000; | |
09f5d123 | 86 | |
0f8f8c18 SA |
87 | const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory"; |
88 | const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
89 | ||
e0ba4f6f | 90 | MainBar::MainBar(Session &session, QWidget *parent, |
f23c4692 | 91 | pv::views::trace::View *view) : |
e0ba4f6f | 92 | StandardBar(session, parent, view, false), |
c9da5118 | 93 | action_new_view_(new QAction(this)), |
0f8f8c18 SA |
94 | action_open_(new QAction(this)), |
95 | action_save_as_(new QAction(this)), | |
96 | action_save_selection_as_(new QAction(this)), | |
97 | action_connect_(new QAction(this)), | |
e0ba4f6f SA |
98 | open_button_(new QToolButton()), |
99 | save_button_(new QToolButton()), | |
100 | device_selector_(parent, session.device_manager(), | |
0f8f8c18 | 101 | action_connect_), |
8dbbc7f0 | 102 | configure_button_(this), |
4c60462b | 103 | configure_button_action_(nullptr), |
8dbbc7f0 | 104 | channels_button_(this), |
9dd88889 | 105 | channels_button_action_(nullptr), |
8dbbc7f0 JH |
106 | sample_count_(" samples", this), |
107 | sample_rate_("Hz", this), | |
108 | updating_sample_rate_(false), | |
109 | updating_sample_count_(false), | |
3231fbf9 | 110 | sample_count_supported_(false) |
0f8f8c18 | 111 | #ifdef ENABLE_DECODE |
e0ba4f6f SA |
112 | , add_decoder_button_(new QToolButton()), |
113 | menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) | |
0f8f8c18 | 114 | #endif |
6fb67b27 | 115 | { |
7c657094 | 116 | setObjectName(QString::fromUtf8("MainBar")); |
0c7cdea2 | 117 | |
d375140b SA |
118 | setContextMenuPolicy(Qt::PreventContextMenu); |
119 | ||
0f8f8c18 | 120 | // Actions |
c9da5118 SA |
121 | action_new_view_->setText(tr("New &View")); |
122 | action_new_view_->setIcon(QIcon::fromTheme("window-new", | |
123 | QIcon(":/icons/window-new.png"))); | |
124 | connect(action_new_view_, SIGNAL(triggered(bool)), | |
125 | this, SLOT(on_actionNewView_triggered())); | |
126 | ||
0f8f8c18 SA |
127 | action_open_->setText(tr("&Open...")); |
128 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
129 | QIcon(":/icons/document-open.png"))); | |
130 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); | |
d552c5c7 SA |
131 | connect(action_open_, SIGNAL(triggered(bool)), |
132 | this, SLOT(on_actionOpen_triggered())); | |
0f8f8c18 SA |
133 | |
134 | action_save_as_->setText(tr("&Save As...")); | |
135 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
136 | QIcon(":/icons/document-save-as.png"))); | |
137 | action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
d552c5c7 SA |
138 | connect(action_save_as_, SIGNAL(triggered(bool)), |
139 | this, SLOT(on_actionSaveAs_triggered())); | |
0f8f8c18 SA |
140 | |
141 | action_save_selection_as_->setText(tr("Save Selected &Range As...")); | |
142 | action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as", | |
143 | QIcon(":/icons/document-save-as.png"))); | |
144 | action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); | |
d552c5c7 SA |
145 | connect(action_save_selection_as_, SIGNAL(triggered(bool)), |
146 | this, SLOT(on_actionSaveSelectionAs_triggered())); | |
0f8f8c18 SA |
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, | |
6f925ba9 UH |
152 | SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)), |
153 | this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>))); | |
0f8f8c18 SA |
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, | |
6f925ba9 UH |
159 | SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)), |
160 | this, SLOT(import_file(shared_ptr<sigrok::InputFormat>))); | |
0f8f8c18 SA |
161 | |
162 | action_connect_->setText(tr("&Connect to Device...")); | |
d552c5c7 SA |
163 | connect(action_connect_, SIGNAL(triggered(bool)), |
164 | this, SLOT(on_actionConnect_triggered())); | |
0f8f8c18 | 165 | |
ed43ef2e | 166 | // Open button |
ed43ef2e | 167 | widgets::ImportMenu *import_menu = new widgets::ImportMenu(this, |
0f8f8c18 | 168 | session.device_manager().context(), action_open_); |
ed43ef2e | 169 | connect(import_menu, |
6f925ba9 | 170 | SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)), |
d552c5c7 | 171 | this, |
6f925ba9 | 172 | SLOT(import_file(shared_ptr<sigrok::InputFormat>))); |
ed43ef2e | 173 | |
e0ba4f6f SA |
174 | open_button_->setMenu(import_menu); |
175 | open_button_->setDefaultAction(action_open_); | |
176 | open_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
ed43ef2e | 177 | |
998b89fd | 178 | // Save button |
686d9151 | 179 | vector<QAction *> open_actions; |
0f8f8c18 SA |
180 | open_actions.push_back(action_save_as_); |
181 | open_actions.push_back(action_save_selection_as_); | |
686d9151 | 182 | |
998b89fd JH |
183 | widgets::ExportMenu *export_menu = new widgets::ExportMenu(this, |
184 | session.device_manager().context(), | |
686d9151 | 185 | open_actions); |
998b89fd | 186 | connect(export_menu, |
6f925ba9 | 187 | SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)), |
d552c5c7 | 188 | this, |
6f925ba9 | 189 | SLOT(export_file(shared_ptr<sigrok::OutputFormat>))); |
998b89fd | 190 | |
e0ba4f6f SA |
191 | save_button_->setMenu(export_menu); |
192 | save_button_->setDefaultAction(action_save_as_); | |
193 | save_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
998b89fd | 194 | |
079d39ea JH |
195 | // Device selector menu |
196 | connect(&device_selector_, SIGNAL(device_selected()), | |
197 | this, SLOT(on_device_selected())); | |
198 | ||
168888e2 JH |
199 | // Setup the decoder button |
200 | #ifdef ENABLE_DECODE | |
0f8f8c18 SA |
201 | menu_decoders_add_->setTitle(tr("&Add")); |
202 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
203 | this, SLOT(add_decoder(srd_decoder*))); | |
204 | ||
4d8130bb | 205 | add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg")); |
e0ba4f6f SA |
206 | add_decoder_button_->setPopupMode(QToolButton::InstantPopup); |
207 | add_decoder_button_->setMenu(menu_decoders_add_); | |
ae95fe58 | 208 | add_decoder_button_->setToolTip(tr("Add low-level, non-stacked protocol decoder")); |
168888e2 JH |
209 | #endif |
210 | ||
8dbbc7f0 | 211 | connect(&sample_count_, SIGNAL(value_changed()), |
124d97de | 212 | this, SLOT(on_sample_count_changed())); |
8dbbc7f0 | 213 | connect(&sample_rate_, SIGNAL(value_changed()), |
1198b887 | 214 | this, SLOT(on_sample_rate_changed())); |
dde1a563 | 215 | |
8dbbc7f0 | 216 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); |
215f9499 | 217 | |
2b81ae46 | 218 | set_capture_state(pv::Session::Stopped); |
274d4f13 | 219 | |
5f66b56e | 220 | configure_button_.setToolTip(tr("Configure Device")); |
3432032f UH |
221 | configure_button_.setIcon(QIcon::fromTheme("preferences-system", |
222 | QIcon(":/icons/preferences-system.png"))); | |
b7b659aa | 223 | |
5f66b56e | 224 | channels_button_.setToolTip(tr("Configure Channels")); |
4d8130bb | 225 | channels_button_.setIcon(QIcon(":/icons/channels.svg")); |
cdb50f67 | 226 | |
e0ba4f6f | 227 | add_toolbar_widgets(); |
40065ab6 | 228 | |
8dbbc7f0 JH |
229 | sample_count_.installEventFilter(this); |
230 | sample_rate_.installEventFilter(this); | |
0f8f8c18 | 231 | |
0f8f8c18 | 232 | // Setup session_ events |
d552c5c7 | 233 | connect(&session_, SIGNAL(capture_state_changed(int)), |
f4ce3e77 | 234 | this, SLOT(on_capture_state_changed(int))); |
d552c5c7 SA |
235 | connect(&session, SIGNAL(device_changed()), |
236 | this, SLOT(on_device_changed())); | |
0f8f8c18 | 237 | |
101e7a9b | 238 | update_device_list(); |
0f8f8c18 SA |
239 | } |
240 | ||
079d39ea | 241 | void MainBar::update_device_list() |
18203d86 | 242 | { |
079d39ea | 243 | DeviceManager &mgr = session_.device_manager(); |
da30ecb7 JH |
244 | shared_ptr<devices::Device> selected_device = session_.device(); |
245 | list< shared_ptr<devices::Device> > devs; | |
e8d00928 | 246 | |
079d39ea | 247 | copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs)); |
53a7cce4 | 248 | |
079d39ea JH |
249 | if (std::find(devs.begin(), devs.end(), selected_device) == devs.end()) |
250 | devs.push_back(selected_device); | |
e95e8563 | 251 | |
079d39ea | 252 | device_selector_.set_device_list(devs, selected_device); |
e95e8563 | 253 | update_device_config_widgets(); |
18203d86 JH |
254 | } |
255 | ||
7c657094 | 256 | void MainBar::set_capture_state(pv::Session::capture_state state) |
6ac96c2e | 257 | { |
73e170f9 SA |
258 | bool ui_enabled = (state == pv::Session::Stopped) ? true : false; |
259 | ||
260 | device_selector_.setEnabled(ui_enabled); | |
261 | configure_button_.setEnabled(ui_enabled); | |
262 | channels_button_.setEnabled(ui_enabled); | |
263 | sample_count_.setEnabled(ui_enabled); | |
264 | sample_rate_.setEnabled(ui_enabled); | |
6ac96c2e JH |
265 | } |
266 | ||
7e0c99bf SA |
267 | void MainBar::reset_device_selector() |
268 | { | |
269 | device_selector_.reset(); | |
270 | } | |
271 | ||
0f8f8c18 SA |
272 | QAction* MainBar::action_open() const |
273 | { | |
274 | return action_open_; | |
275 | } | |
276 | ||
277 | QAction* MainBar::action_save_as() const | |
278 | { | |
279 | return action_save_as_; | |
280 | } | |
281 | ||
282 | QAction* MainBar::action_save_selection_as() const | |
283 | { | |
284 | return action_save_selection_as_; | |
285 | } | |
286 | ||
287 | QAction* MainBar::action_connect() const | |
288 | { | |
289 | return action_connect_; | |
290 | } | |
291 | ||
7c657094 | 292 | void MainBar::update_sample_rate_selector() |
dde1a563 | 293 | { |
e8d00928 ML |
294 | Glib::VariantContainerBase gvar_dict; |
295 | GVariant *gvar_list; | |
4c60462b | 296 | const uint64_t *elements = nullptr; |
488f5d3f | 297 | gsize num_elements; |
6f925ba9 | 298 | map< const ConfigKey*, set<Capability> > keys; |
dde1a563 | 299 | |
9dd88889 JH |
300 | if (updating_sample_rate_) { |
301 | sample_rate_.show_none(); | |
3d4f16af | 302 | return; |
9dd88889 | 303 | } |
3d4f16af | 304 | |
da30ecb7 JH |
305 | const shared_ptr<devices::Device> device = |
306 | device_selector_.selected_device(); | |
e8d00928 | 307 | if (!device) |
ef4d0201 JH |
308 | return; |
309 | ||
8dbbc7f0 JH |
310 | assert(!updating_sample_rate_); |
311 | updating_sample_rate_ = true; | |
1198b887 | 312 | |
da30ecb7 | 313 | const shared_ptr<sigrok::Device> sr_dev = device->device(); |
a6ed12bf | 314 | |
7bb0fbf4 | 315 | if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) { |
519a6ed8 WS |
316 | try { |
317 | gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE); | |
318 | } catch (Error error) { | |
319 | qDebug() << tr("Failed to get sample rate list:") << error.what(); | |
320 | } | |
7bb0fbf4 | 321 | } else { |
8dbbc7f0 JH |
322 | sample_rate_.show_none(); |
323 | updating_sample_rate_ = false; | |
dde1a563 | 324 | return; |
1198b887 | 325 | } |
dde1a563 | 326 | |
e8d00928 | 327 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), |
2ad82c2e | 328 | "samplerate-steps", G_VARIANT_TYPE("at")))) { |
488f5d3f BV |
329 | elements = (const uint64_t *)g_variant_get_fixed_array( |
330 | gvar_list, &num_elements, sizeof(uint64_t)); | |
aafe53af JH |
331 | |
332 | const uint64_t min = elements[0]; | |
333 | const uint64_t max = elements[1]; | |
334 | const uint64_t step = elements[2]; | |
335 | ||
488f5d3f | 336 | g_variant_unref(gvar_list); |
aafe53af JH |
337 | |
338 | assert(min > 0); | |
339 | assert(max > 0); | |
340 | assert(max > min); | |
341 | assert(step > 0); | |
342 | ||
343 | if (step == 1) | |
8dbbc7f0 | 344 | sample_rate_.show_125_list(min, max); |
2ad82c2e | 345 | else { |
aafe53af JH |
346 | // When the step is not 1, we cam't make a 1-2-5-10 |
347 | // list of sample rates, because we may not be able to | |
348 | // make round numbers. Therefore in this case, show a | |
349 | // spin box. | |
8dbbc7f0 | 350 | sample_rate_.show_min_max_step(min, max, step); |
aafe53af | 351 | } |
2ad82c2e UH |
352 | } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), |
353 | "samplerates", G_VARIANT_TYPE("at")))) { | |
488f5d3f BV |
354 | elements = (const uint64_t *)g_variant_get_fixed_array( |
355 | gvar_list, &num_elements, sizeof(uint64_t)); | |
8dbbc7f0 | 356 | sample_rate_.show_list(elements, num_elements); |
488f5d3f | 357 | g_variant_unref(gvar_list); |
dde1a563 | 358 | } |
8dbbc7f0 | 359 | updating_sample_rate_ = false; |
48888313 JH |
360 | |
361 | update_sample_rate_selector_value(); | |
362 | } | |
363 | ||
7c657094 | 364 | void MainBar::update_sample_rate_selector_value() |
48888313 | 365 | { |
8dbbc7f0 | 366 | if (updating_sample_rate_) |
3d4f16af JH |
367 | return; |
368 | ||
da30ecb7 JH |
369 | const shared_ptr<devices::Device> device = |
370 | device_selector_.selected_device(); | |
e8d00928 | 371 | if (!device) |
19adbc2c JH |
372 | return; |
373 | ||
e8d00928 | 374 | try { |
da30ecb7 | 375 | auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE); |
e8d00928 | 376 | uint64_t samplerate = |
15289d5c | 377 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); |
8dbbc7f0 JH |
378 | assert(!updating_sample_rate_); |
379 | updating_sample_rate_ = true; | |
380 | sample_rate_.set_value(samplerate); | |
381 | updating_sample_rate_ = false; | |
e8d00928 | 382 | } catch (Error error) { |
8dd44190 | 383 | qDebug() << "WARNING: Failed to get value of sample rate"; |
eb4008a6 JH |
384 | return; |
385 | } | |
48888313 JH |
386 | } |
387 | ||
7c657094 | 388 | void MainBar::update_sample_count_selector() |
85756012 | 389 | { |
8dbbc7f0 | 390 | if (updating_sample_count_) |
3d4f16af JH |
391 | return; |
392 | ||
da30ecb7 JH |
393 | const shared_ptr<devices::Device> device = |
394 | device_selector_.selected_device(); | |
e8d00928 | 395 | if (!device) |
19adbc2c JH |
396 | return; |
397 | ||
da30ecb7 JH |
398 | const shared_ptr<sigrok::Device> sr_dev = device->device(); |
399 | ||
8dbbc7f0 JH |
400 | assert(!updating_sample_count_); |
401 | updating_sample_count_ = true; | |
85756012 | 402 | |
2ad82c2e | 403 | if (!sample_count_supported_) { |
3e7636f9 JH |
404 | sample_count_.show_none(); |
405 | updating_sample_count_ = false; | |
406 | return; | |
407 | } | |
b50ef520 | 408 | |
3e7636f9 JH |
409 | uint64_t sample_count = sample_count_.value(); |
410 | uint64_t min_sample_count = 0; | |
411 | uint64_t max_sample_count = MaxSampleCount; | |
348f9cec | 412 | bool default_count_set = false; |
3cbffdcf | 413 | |
348f9cec | 414 | if (sample_count == 0) { |
3e7636f9 | 415 | sample_count = DefaultSampleCount; |
348f9cec SA |
416 | default_count_set = true; |
417 | } | |
b50ef520 | 418 | |
7bb0fbf4 | 419 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) { |
519a6ed8 WS |
420 | try { |
421 | auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES); | |
422 | if (gvar.gobj()) | |
423 | g_variant_get(gvar.gobj(), "(tt)", | |
424 | &min_sample_count, &max_sample_count); | |
425 | } catch (Error error) { | |
426 | qDebug() << tr("Failed to get sample limit list:") << error.what(); | |
427 | } | |
ed1d9d81 | 428 | } |
1d04852f | 429 | |
3e7636f9 JH |
430 | min_sample_count = min(max(min_sample_count, MinSampleCount), |
431 | max_sample_count); | |
b50ef520 | 432 | |
c063290a | 433 | sample_count_.show_125_list(min_sample_count, max_sample_count); |
b50ef520 | 434 | |
7bb0fbf4 | 435 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) { |
da30ecb7 | 436 | auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES); |
3e7636f9 | 437 | sample_count = g_variant_get_uint64(gvar.gobj()); |
348f9cec | 438 | if (sample_count == 0) { |
3e7636f9 | 439 | sample_count = DefaultSampleCount; |
348f9cec SA |
440 | default_count_set = true; |
441 | } | |
3e7636f9 JH |
442 | sample_count = min(max(sample_count, MinSampleCount), |
443 | max_sample_count); | |
7bb0fbf4 | 444 | } |
3e7636f9 JH |
445 | |
446 | sample_count_.set_value(sample_count); | |
df3c1aa3 | 447 | |
8dbbc7f0 | 448 | updating_sample_count_ = false; |
348f9cec SA |
449 | |
450 | // If we show the default rate then make sure the device uses the same | |
451 | if (default_count_set) | |
452 | commit_sample_count(); | |
85756012 ML |
453 | } |
454 | ||
7c657094 | 455 | void MainBar::update_device_config_widgets() |
e95e8563 | 456 | { |
e95e8563 JH |
457 | using namespace pv::popups; |
458 | ||
da30ecb7 JH |
459 | const shared_ptr<devices::Device> device = |
460 | device_selector_.selected_device(); | |
9dd88889 JH |
461 | |
462 | // Hide the widgets if no device is selected | |
463 | channels_button_action_->setVisible(!!device); | |
9dd88889 JH |
464 | if (!device) { |
465 | configure_button_action_->setVisible(false); | |
466 | sample_count_.show_none(); | |
467 | sample_rate_.show_none(); | |
e95e8563 | 468 | return; |
9dd88889 | 469 | } |
e95e8563 | 470 | |
da30ecb7 JH |
471 | const shared_ptr<sigrok::Device> sr_dev = device->device(); |
472 | if (!sr_dev) | |
473 | return; | |
474 | ||
e95e8563 | 475 | // Update the configure popup |
da30ecb7 | 476 | DeviceOptions *const opts = new DeviceOptions(sr_dev, this); |
8dbbc7f0 | 477 | configure_button_action_->setVisible( |
e95e8563 | 478 | !opts->binding().properties().empty()); |
8dbbc7f0 | 479 | configure_button_.set_popup(opts); |
e95e8563 | 480 | |
6ac6242b | 481 | // Update the channels popup |
8dbbc7f0 JH |
482 | Channels *const channels = new Channels(session_, this); |
483 | channels_button_.set_popup(channels); | |
e95e8563 JH |
484 | |
485 | // Update supported options. | |
8dbbc7f0 | 486 | sample_count_supported_ = false; |
e95e8563 | 487 | |
7bb0fbf4 ML |
488 | if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET)) |
489 | sample_count_supported_ = true; | |
490 | ||
e95e8563 JH |
491 | // Add notification of reconfigure events |
492 | disconnect(this, SLOT(on_config_changed())); | |
e8d00928 | 493 | connect(&opts->binding(), SIGNAL(config_changed()), |
e95e8563 JH |
494 | this, SLOT(on_config_changed())); |
495 | ||
496 | // Update sweep timing widgets. | |
497 | update_sample_count_selector(); | |
498 | update_sample_rate_selector(); | |
499 | } | |
500 | ||
0f8f8c18 SA |
501 | void MainBar::commit_sample_rate() |
502 | { | |
503 | uint64_t sample_rate = 0; | |
504 | ||
505 | const shared_ptr<devices::Device> device = | |
506 | device_selector_.selected_device(); | |
507 | if (!device) | |
508 | return; | |
509 | ||
510 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
511 | ||
512 | sample_rate = sample_rate_.value(); | |
513 | if (sample_rate == 0) | |
514 | return; | |
515 | ||
516 | try { | |
517 | sr_dev->config_set(ConfigKey::SAMPLERATE, | |
518 | Glib::Variant<guint64>::create(sample_rate)); | |
519 | update_sample_rate_selector(); | |
520 | } catch (Error error) { | |
521 | qDebug() << "Failed to configure samplerate."; | |
522 | return; | |
523 | } | |
524 | ||
525 | // Devices with built-in memory might impose limits on certain | |
526 | // configurations, so let's check what sample count the driver | |
527 | // lets us use now. | |
528 | update_sample_count_selector(); | |
529 | } | |
530 | ||
7c657094 | 531 | void MainBar::commit_sample_count() |
124d97de ML |
532 | { |
533 | uint64_t sample_count = 0; | |
534 | ||
da30ecb7 JH |
535 | const shared_ptr<devices::Device> device = |
536 | device_selector_.selected_device(); | |
e8d00928 | 537 | if (!device) |
19adbc2c JH |
538 | return; |
539 | ||
da30ecb7 JH |
540 | const shared_ptr<sigrok::Device> sr_dev = device->device(); |
541 | ||
8dbbc7f0 | 542 | sample_count = sample_count_.value(); |
2ad82c2e | 543 | if (sample_count_supported_) { |
e8d00928 | 544 | try { |
da30ecb7 | 545 | sr_dev->config_set(ConfigKey::LIMIT_SAMPLES, |
15289d5c | 546 | Glib::Variant<guint64>::create(sample_count)); |
449aec3c | 547 | update_sample_count_selector(); |
e8d00928 ML |
548 | } catch (Error error) { |
549 | qDebug() << "Failed to configure sample count."; | |
550 | return; | |
551 | } | |
124d97de | 552 | } |
e13e7879 SA |
553 | |
554 | // Devices with built-in memory might impose limits on certain | |
555 | // configurations, so let's check what sample rate the driver | |
556 | // lets us use now. | |
557 | update_sample_rate_selector(); | |
124d97de ML |
558 | } |
559 | ||
0f8f8c18 | 560 | void MainBar::session_error(const QString text, const QString info_text) |
48888313 | 561 | { |
0f8f8c18 SA |
562 | QMetaObject::invokeMethod(this, "show_session_error", |
563 | Qt::QueuedConnection, Q_ARG(QString, text), | |
564 | Q_ARG(QString, info_text)); | |
565 | } | |
48888313 | 566 | |
0f8f8c18 SA |
567 | void MainBar::show_session_error(const QString text, const QString info_text) |
568 | { | |
569 | QMessageBox msg(this); | |
570 | msg.setText(text); | |
571 | msg.setInformativeText(info_text); | |
572 | msg.setStandardButtons(QMessageBox::Ok); | |
573 | msg.setIcon(QMessageBox::Warning); | |
574 | msg.exec(); | |
575 | } | |
19adbc2c | 576 | |
0f8f8c18 SA |
577 | void MainBar::add_decoder(srd_decoder *decoder) |
578 | { | |
579 | #ifdef ENABLE_DECODE | |
580 | assert(decoder); | |
e771b42d SA |
581 | shared_ptr<data::DecodeSignal> signal = session_.add_decode_signal(); |
582 | if (signal) | |
583 | signal->stack_decoder(decoder); | |
0f8f8c18 SA |
584 | #else |
585 | (void)decoder; | |
586 | #endif | |
587 | } | |
588 | ||
c063290a | 589 | void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only) |
0f8f8c18 SA |
590 | { |
591 | using pv::dialogs::StoreProgress; | |
592 | ||
593 | // Stop any currently running capture session | |
594 | session_.stop_capture(); | |
595 | ||
596 | QSettings settings; | |
597 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
598 | ||
6f925ba9 | 599 | pair<uint64_t, uint64_t> sample_range; |
0f8f8c18 SA |
600 | |
601 | // Selection only? Verify that the cursors are active and fetch their values | |
602 | if (selection_only) { | |
f23c4692 SA |
603 | views::trace::View *trace_view = |
604 | qobject_cast<views::trace::View*>(session_.main_view().get()); | |
f4e57597 SA |
605 | |
606 | if (!trace_view->cursors()->enabled()) { | |
0f8f8c18 SA |
607 | show_session_error(tr("Missing Cursors"), tr("You need to set the " \ |
608 | "cursors before you can save the data enclosed by them " \ | |
e0ddc2a4 | 609 | "to a session file (e.g. using the Show Cursors button).")); |
0f8f8c18 SA |
610 | return; |
611 | } | |
612 | ||
613 | const double samplerate = session_.get_samplerate(); | |
614 | ||
f4e57597 SA |
615 | const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time(); |
616 | const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time(); | |
0f8f8c18 | 617 | |
6f925ba9 | 618 | const uint64_t start_sample = (uint64_t)max( |
49ec9e07 | 619 | (double)0, start_time.convert_to<double>() * samplerate); |
6f925ba9 | 620 | const uint64_t end_sample = (uint64_t)max( |
49ec9e07 | 621 | (double)0, end_time.convert_to<double>() * samplerate); |
0f8f8c18 | 622 | |
7b7ab1f5 SA |
623 | if ((start_sample == 0) && (end_sample == 0)) { |
624 | // Both cursors are negative and were clamped to 0 | |
625 | show_session_error(tr("Invalid Range"), tr("The cursors don't " \ | |
626 | "define a valid range of samples.")); | |
627 | return; | |
628 | } | |
629 | ||
6f925ba9 | 630 | sample_range = make_pair(start_sample, end_sample); |
0f8f8c18 | 631 | } else { |
6f925ba9 | 632 | sample_range = make_pair(0, 0); |
0f8f8c18 SA |
633 | } |
634 | ||
635 | // Construct the filter | |
636 | const vector<string> exts = format->extensions(); | |
637 | QString filter = tr("%1 files ").arg( | |
638 | QString::fromStdString(format->description())); | |
639 | ||
640 | if (exts.empty()) | |
75ab6b87 | 641 | filter += "(*)"; |
0f8f8c18 | 642 | else |
75ab6b87 | 643 | filter += QString("(*.%1);;%2 (*)").arg( |
0f8f8c18 SA |
644 | QString::fromStdString(join(exts, ", *.")), |
645 | tr("All Files")); | |
646 | ||
647 | // Show the file dialog | |
648 | const QString file_name = QFileDialog::getSaveFileName( | |
649 | this, tr("Save File"), dir, filter); | |
650 | ||
651 | if (file_name.isEmpty()) | |
f9541bde JH |
652 | return; |
653 | ||
0f8f8c18 SA |
654 | const QString abs_path = QFileInfo(file_name).absolutePath(); |
655 | settings.setValue(SettingSaveDirectory, abs_path); | |
656 | ||
657 | // Show the options dialog | |
658 | map<string, Glib::VariantBase> options; | |
659 | if (!format->options().empty()) { | |
660 | dialogs::InputOutputOptions dlg( | |
661 | tr("Export %1").arg(QString::fromStdString( | |
662 | format->description())), | |
663 | format->options(), this); | |
664 | if (!dlg.exec()) | |
665 | return; | |
666 | options = dlg.options(); | |
667 | } | |
668 | ||
c32bf63a SA |
669 | if (!selection_only) |
670 | session_.set_name(QFileInfo(file_name).fileName()); | |
101e7a9b | 671 | |
0f8f8c18 SA |
672 | StoreProgress *dlg = new StoreProgress(file_name, format, options, |
673 | sample_range, session_, this); | |
674 | dlg->run(); | |
675 | } | |
676 | ||
677 | void MainBar::import_file(shared_ptr<InputFormat> format) | |
678 | { | |
679 | assert(format); | |
680 | ||
681 | QSettings settings; | |
682 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
683 | ||
684 | // Construct the filter | |
685 | const vector<string> exts = format->extensions(); | |
ca1d6d5f GS |
686 | const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg( |
687 | tr("%1 files").arg(QString::fromStdString(format->description())), | |
688 | QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *.")))); | |
689 | const QString filter_all = QString::fromStdString("%1 (%2)").arg( | |
690 | tr("All Files"), QString::fromStdString("*")); | |
691 | const QString filter = QString::fromStdString("%1%2%3").arg( | |
692 | exts.empty() ? "" : filter_exts, | |
693 | exts.empty() ? "" : ";;", | |
694 | filter_all); | |
0f8f8c18 SA |
695 | |
696 | // Show the file dialog | |
697 | const QString file_name = QFileDialog::getOpenFileName( | |
ca1d6d5f | 698 | this, tr("Import File"), dir, filter); |
0f8f8c18 SA |
699 | |
700 | if (file_name.isEmpty()) | |
48888313 | 701 | return; |
0f8f8c18 SA |
702 | |
703 | // Show the options dialog | |
704 | map<string, Glib::VariantBase> options; | |
705 | if (!format->options().empty()) { | |
706 | dialogs::InputOutputOptions dlg( | |
707 | tr("Import %1").arg(QString::fromStdString( | |
708 | format->description())), | |
709 | format->options(), this); | |
710 | if (!dlg.exec()) | |
711 | return; | |
712 | options = dlg.options(); | |
48888313 | 713 | } |
e13e7879 | 714 | |
fd22c71c | 715 | session_.load_file(file_name, format, options); |
0f8f8c18 SA |
716 | |
717 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
718 | settings.setValue(SettingOpenDirectory, abs_path); | |
dde1a563 JH |
719 | } |
720 | ||
7c657094 | 721 | void MainBar::on_device_selected() |
dde1a563 | 722 | { |
da30ecb7 | 723 | shared_ptr<devices::Device> device = device_selector_.selected_device(); |
0f8f8c18 SA |
724 | if (!device) { |
725 | reset_device_selector(); | |
19adbc2c | 726 | return; |
0f8f8c18 | 727 | } |
19adbc2c | 728 | |
fd22c71c | 729 | session_.select_device(device); |
91f8fe8c | 730 | } |
51d4a9ab | 731 | |
91f8fe8c SA |
732 | void MainBar::on_device_changed() |
733 | { | |
734 | update_device_list(); | |
e95e8563 | 735 | update_device_config_widgets(); |
dde1a563 | 736 | } |
51e77110 | 737 | |
f4ce3e77 SA |
738 | void MainBar::on_capture_state_changed(int state) |
739 | { | |
740 | set_capture_state((pv::Session::capture_state)state); | |
741 | } | |
742 | ||
7c657094 | 743 | void MainBar::on_sample_count_changed() |
124d97de | 744 | { |
449aec3c SA |
745 | if (!updating_sample_count_) |
746 | commit_sample_count(); | |
124d97de ML |
747 | } |
748 | ||
7c657094 | 749 | void MainBar::on_sample_rate_changed() |
48888313 | 750 | { |
0e0b6b3e SA |
751 | if (!updating_sample_rate_) |
752 | commit_sample_rate(); | |
48888313 JH |
753 | } |
754 | ||
7c657094 | 755 | void MainBar::on_config_changed() |
82afd5e3 JH |
756 | { |
757 | commit_sample_count(); | |
c063290a | 758 | commit_sample_rate(); |
82afd5e3 JH |
759 | } |
760 | ||
c9da5118 SA |
761 | void MainBar::on_actionNewView_triggered() |
762 | { | |
763 | new_view(&session_); | |
764 | } | |
765 | ||
0f8f8c18 SA |
766 | void MainBar::on_actionOpen_triggered() |
767 | { | |
768 | QSettings settings; | |
769 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
770 | ||
771 | // Show the dialog | |
772 | const QString file_name = QFileDialog::getOpenFileName( | |
773 | this, tr("Open File"), dir, tr( | |
b0e15aa2 | 774 | "sigrok Sessions (*.sr);;" |
75ab6b87 | 775 | "All Files (*)")); |
0f8f8c18 SA |
776 | |
777 | if (!file_name.isEmpty()) { | |
fd22c71c | 778 | session_.load_file(file_name); |
0f8f8c18 SA |
779 | |
780 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
781 | settings.setValue(SettingOpenDirectory, abs_path); | |
782 | } | |
783 | } | |
784 | ||
785 | void MainBar::on_actionSaveAs_triggered() | |
786 | { | |
787 | export_file(session_.device_manager().context()->output_formats()["srzip"]); | |
788 | } | |
789 | ||
790 | void MainBar::on_actionSaveSelectionAs_triggered() | |
791 | { | |
792 | export_file(session_.device_manager().context()->output_formats()["srzip"], true); | |
793 | } | |
794 | ||
795 | void MainBar::on_actionConnect_triggered() | |
796 | { | |
797 | // Stop any currently running capture session | |
798 | session_.stop_capture(); | |
799 | ||
800 | dialogs::Connect dlg(this, session_.device_manager()); | |
801 | ||
802 | // If the user selected a device, select it in the device list. Select the | |
803 | // current device otherwise. | |
804 | if (dlg.exec()) | |
fd22c71c | 805 | session_.select_device(dlg.get_selected_device()); |
0f8f8c18 SA |
806 | |
807 | update_device_list(); | |
808 | } | |
809 | ||
e0ba4f6f | 810 | void MainBar::add_toolbar_widgets() |
0f8f8c18 | 811 | { |
e0ba4f6f SA |
812 | addAction(action_new_view_); |
813 | addSeparator(); | |
814 | addWidget(open_button_); | |
815 | addWidget(save_button_); | |
816 | addSeparator(); | |
0f8f8c18 | 817 | |
e0ba4f6f | 818 | StandardBar::add_toolbar_widgets(); |
0f8f8c18 | 819 | |
e0ba4f6f SA |
820 | addWidget(&device_selector_); |
821 | configure_button_action_ = addWidget(&configure_button_); | |
822 | channels_button_action_ = addWidget(&channels_button_); | |
823 | addWidget(&sample_count_); | |
824 | addWidget(&sample_rate_); | |
825 | #ifdef ENABLE_DECODE | |
826 | addSeparator(); | |
827 | addWidget(add_decoder_button_); | |
828 | #endif | |
d552c5c7 SA |
829 | } |
830 | ||
7c657094 | 831 | bool MainBar::eventFilter(QObject *watched, QEvent *event) |
40065ab6 | 832 | { |
2ad82c2e UH |
833 | if (sample_count_supported_ && (watched == &sample_count_ || |
834 | watched == &sample_rate_) && | |
835 | (event->type() == QEvent::ToolTip)) { | |
60d9b99a | 836 | auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value(); |
40065ab6 | 837 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); |
62974f45 | 838 | |
3ccf0f7f JS |
839 | QString str = tr("Total sampling time: %1").arg( |
840 | pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false)); | |
40065ab6 JS |
841 | QToolTip::showText(help_event->globalPos(), str); |
842 | ||
843 | return true; | |
844 | } | |
845 | ||
846 | return false; | |
847 | } | |
848 | ||
f4c92e1c | 849 | } // namespace toolbars |
51e77110 | 850 | } // namespace pv |