]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <cassert> | |
22 | ||
23 | #ifdef ENABLE_DECODE | |
24 | #include <libsigrokdecode/libsigrokdecode.h> | |
25 | #endif | |
26 | ||
27 | #include <algorithm> | |
28 | #include <iterator> | |
29 | ||
30 | #include <boost/algorithm/string/join.hpp> | |
31 | ||
32 | #include <QAction> | |
33 | #include <QApplication> | |
34 | #include <QButtonGroup> | |
35 | #include <QCloseEvent> | |
36 | #include <QFileDialog> | |
37 | #include <QMessageBox> | |
38 | #include <QMenu> | |
39 | #include <QMenuBar> | |
40 | #include <QSettings> | |
41 | #include <QStatusBar> | |
42 | #include <QVBoxLayout> | |
43 | #include <QWidget> | |
44 | ||
45 | #include "mainwindow.hpp" | |
46 | ||
47 | #include "devicemanager.hpp" | |
48 | #include "devices/hardwaredevice.hpp" | |
49 | #include "devices/inputfile.hpp" | |
50 | #include "devices/sessionfile.hpp" | |
51 | #include "dialogs/about.hpp" | |
52 | #include "dialogs/connect.hpp" | |
53 | #include "dialogs/inputoutputoptions.hpp" | |
54 | #include "dialogs/storeprogress.hpp" | |
55 | #include "toolbars/mainbar.hpp" | |
56 | #include "view/logicsignal.hpp" | |
57 | #include "view/view.hpp" | |
58 | #include "widgets/exportmenu.hpp" | |
59 | #include "widgets/importmenu.hpp" | |
60 | #ifdef ENABLE_DECODE | |
61 | #include "widgets/decodermenu.hpp" | |
62 | #endif | |
63 | #include "widgets/hidingmenubar.hpp" | |
64 | ||
65 | #include <inttypes.h> | |
66 | #include <stdint.h> | |
67 | #include <stdarg.h> | |
68 | #include <glib.h> | |
69 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
70 | ||
71 | using std::cerr; | |
72 | using std::endl; | |
73 | using std::list; | |
74 | using std::map; | |
75 | using std::pair; | |
76 | using std::shared_ptr; | |
77 | using std::string; | |
78 | using std::vector; | |
79 | ||
80 | using boost::algorithm::join; | |
81 | ||
82 | using sigrok::Error; | |
83 | using sigrok::OutputFormat; | |
84 | using sigrok::InputFormat; | |
85 | ||
86 | namespace pv { | |
87 | ||
88 | namespace view { | |
89 | class ViewItem; | |
90 | } | |
91 | ||
92 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; | |
93 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
94 | ||
95 | MainWindow::MainWindow(DeviceManager &device_manager, | |
96 | string open_file_name, string open_file_format, | |
97 | QWidget *parent) : | |
98 | QMainWindow(parent), | |
99 | device_manager_(device_manager), | |
100 | session_(device_manager), | |
101 | action_open_(new QAction(this)), | |
102 | action_save_as_(new QAction(this)), | |
103 | action_connect_(new QAction(this)), | |
104 | action_quit_(new QAction(this)), | |
105 | action_view_zoom_in_(new QAction(this)), | |
106 | action_view_zoom_out_(new QAction(this)), | |
107 | action_view_zoom_fit_(new QAction(this)), | |
108 | action_view_zoom_one_to_one_(new QAction(this)), | |
109 | action_view_show_cursors_(new QAction(this)), | |
110 | action_about_(new QAction(this)) | |
111 | #ifdef ENABLE_DECODE | |
112 | , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) | |
113 | #endif | |
114 | { | |
115 | setup_ui(); | |
116 | restore_ui_settings(); | |
117 | if (open_file_name.empty()) | |
118 | select_init_device(); | |
119 | else | |
120 | load_init_file(open_file_name, open_file_format); | |
121 | } | |
122 | ||
123 | QAction* MainWindow::action_open() const | |
124 | { | |
125 | return action_open_; | |
126 | } | |
127 | ||
128 | QAction* MainWindow::action_save_as() const | |
129 | { | |
130 | return action_save_as_; | |
131 | } | |
132 | ||
133 | QAction* MainWindow::action_connect() const | |
134 | { | |
135 | return action_connect_; | |
136 | } | |
137 | ||
138 | QAction* MainWindow::action_quit() const | |
139 | { | |
140 | return action_quit_; | |
141 | } | |
142 | ||
143 | QAction* MainWindow::action_view_zoom_in() const | |
144 | { | |
145 | return action_view_zoom_in_; | |
146 | } | |
147 | ||
148 | QAction* MainWindow::action_view_zoom_out() const | |
149 | { | |
150 | return action_view_zoom_out_; | |
151 | } | |
152 | ||
153 | QAction* MainWindow::action_view_zoom_fit() const | |
154 | { | |
155 | return action_view_zoom_fit_; | |
156 | } | |
157 | ||
158 | QAction* MainWindow::action_view_zoom_one_to_one() const | |
159 | { | |
160 | return action_view_zoom_one_to_one_; | |
161 | } | |
162 | ||
163 | QAction* MainWindow::action_view_show_cursors() const | |
164 | { | |
165 | return action_view_show_cursors_; | |
166 | } | |
167 | ||
168 | QAction* MainWindow::action_about() const | |
169 | { | |
170 | return action_about_; | |
171 | } | |
172 | ||
173 | #ifdef ENABLE_DECODE | |
174 | QMenu* MainWindow::menu_decoder_add() const | |
175 | { | |
176 | return menu_decoders_add_; | |
177 | } | |
178 | #endif | |
179 | ||
180 | void MainWindow::run_stop() | |
181 | { | |
182 | switch(session_.get_capture_state()) { | |
183 | case Session::Stopped: | |
184 | session_.start_capture([&](QString message) { | |
185 | session_error("Capture failed", message); }); | |
186 | break; | |
187 | ||
188 | case Session::AwaitingTrigger: | |
189 | case Session::Running: | |
190 | session_.stop_capture(); | |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
195 | void MainWindow::select_device(shared_ptr<devices::Device> device) | |
196 | { | |
197 | try { | |
198 | if (device) | |
199 | session_.set_device(device); | |
200 | else | |
201 | session_.set_default_device(); | |
202 | } catch(const QString &e) { | |
203 | QMessageBox msg(this); | |
204 | msg.setText(e); | |
205 | msg.setInformativeText(tr("Failed to Select Device")); | |
206 | msg.setStandardButtons(QMessageBox::Ok); | |
207 | msg.setIcon(QMessageBox::Warning); | |
208 | msg.exec(); | |
209 | } | |
210 | } | |
211 | ||
212 | void MainWindow::export_file(shared_ptr<OutputFormat> format) | |
213 | { | |
214 | using pv::dialogs::StoreProgress; | |
215 | ||
216 | // Stop any currently running capture session | |
217 | session_.stop_capture(); | |
218 | ||
219 | QSettings settings; | |
220 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
221 | ||
222 | // Construct the filter | |
223 | const vector<string> exts = format->extensions(); | |
224 | QString filter = tr("%1 files ").arg( | |
225 | QString::fromStdString(format->description())); | |
226 | ||
227 | if (exts.empty()) | |
228 | filter += "(*.*)"; | |
229 | else | |
230 | filter += QString("(*.%1);;%2 (*.*)").arg( | |
231 | QString::fromStdString(join(exts, ", *."))).arg( | |
232 | tr("All Files")); | |
233 | ||
234 | // Show the file dialog | |
235 | const QString file_name = QFileDialog::getSaveFileName( | |
236 | this, tr("Save File"), dir, filter); | |
237 | ||
238 | if (file_name.isEmpty()) | |
239 | return; | |
240 | ||
241 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
242 | settings.setValue(SettingSaveDirectory, abs_path); | |
243 | ||
244 | // Show the options dialog | |
245 | map<string, Glib::VariantBase> options; | |
246 | if (!format->options().empty()) { | |
247 | dialogs::InputOutputOptions dlg( | |
248 | tr("Export %1").arg(QString::fromStdString( | |
249 | format->description())), | |
250 | format->options(), this); | |
251 | if (!dlg.exec()) | |
252 | return; | |
253 | options = dlg.options(); | |
254 | } | |
255 | ||
256 | StoreProgress *dlg = new StoreProgress(file_name, format, options, | |
257 | session_, this); | |
258 | dlg->run(); | |
259 | } | |
260 | ||
261 | void MainWindow::import_file(shared_ptr<InputFormat> format) | |
262 | { | |
263 | assert(format); | |
264 | ||
265 | QSettings settings; | |
266 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
267 | ||
268 | // Construct the filter | |
269 | const vector<string> exts = format->extensions(); | |
270 | const QString filter = exts.empty() ? "" : | |
271 | tr("%1 files (*.%2)").arg( | |
272 | QString::fromStdString(format->description())).arg( | |
273 | QString::fromStdString(join(exts, ", *."))); | |
274 | ||
275 | // Show the file dialog | |
276 | const QString file_name = QFileDialog::getOpenFileName( | |
277 | this, tr("Import File"), dir, tr( | |
278 | "%1 files (*.*);;All Files (*.*)").arg( | |
279 | QString::fromStdString(format->description()))); | |
280 | ||
281 | if (file_name.isEmpty()) | |
282 | return; | |
283 | ||
284 | // Show the options dialog | |
285 | map<string, Glib::VariantBase> options; | |
286 | if (!format->options().empty()) { | |
287 | dialogs::InputOutputOptions dlg( | |
288 | tr("Import %1").arg(QString::fromStdString( | |
289 | format->description())), | |
290 | format->options(), this); | |
291 | if (!dlg.exec()) | |
292 | return; | |
293 | options = dlg.options(); | |
294 | } | |
295 | ||
296 | load_file(file_name, format, options); | |
297 | ||
298 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
299 | settings.setValue(SettingOpenDirectory, abs_path); | |
300 | } | |
301 | ||
302 | void MainWindow::setup_ui() | |
303 | { | |
304 | setObjectName(QString::fromUtf8("MainWindow")); | |
305 | ||
306 | // Set the window icon | |
307 | QIcon icon; | |
308 | icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); | |
309 | setWindowIcon(icon); | |
310 | ||
311 | // Setup the central widget | |
312 | central_widget_ = new QWidget(this); | |
313 | vertical_layout_ = new QVBoxLayout(central_widget_); | |
314 | vertical_layout_->setSpacing(6); | |
315 | vertical_layout_->setContentsMargins(0, 0, 0, 0); | |
316 | setCentralWidget(central_widget_); | |
317 | ||
318 | view_ = new pv::view::View(session_, this); | |
319 | ||
320 | vertical_layout_->addWidget(view_); | |
321 | ||
322 | // Setup the menu bar | |
323 | pv::widgets::HidingMenuBar *const menu_bar = | |
324 | new pv::widgets::HidingMenuBar(this); | |
325 | ||
326 | // File Menu | |
327 | QMenu *const menu_file = new QMenu; | |
328 | menu_file->setTitle(tr("&File")); | |
329 | ||
330 | action_open_->setText(tr("&Open...")); | |
331 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
332 | QIcon(":/icons/document-open.png"))); | |
333 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); | |
334 | action_open_->setObjectName(QString::fromUtf8("actionOpen")); | |
335 | menu_file->addAction(action_open_); | |
336 | ||
337 | action_save_as_->setText(tr("&Save As...")); | |
338 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
339 | QIcon(":/icons/document-save-as.png"))); | |
340 | action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
341 | action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs")); | |
342 | menu_file->addAction(action_save_as_); | |
343 | ||
344 | menu_file->addSeparator(); | |
345 | ||
346 | widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, | |
347 | device_manager_.context()); | |
348 | menu_file_export->setTitle(tr("&Export")); | |
349 | connect(menu_file_export, | |
350 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
351 | this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
352 | menu_file->addAction(menu_file_export->menuAction()); | |
353 | ||
354 | widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this, | |
355 | device_manager_.context()); | |
356 | menu_file_import->setTitle(tr("&Import")); | |
357 | connect(menu_file_import, | |
358 | SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)), | |
359 | this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>))); | |
360 | menu_file->addAction(menu_file_import->menuAction()); | |
361 | ||
362 | menu_file->addSeparator(); | |
363 | ||
364 | action_connect_->setText(tr("&Connect to Device...")); | |
365 | action_connect_->setObjectName(QString::fromUtf8("actionConnect")); | |
366 | menu_file->addAction(action_connect_); | |
367 | ||
368 | menu_file->addSeparator(); | |
369 | ||
370 | action_quit_->setText(tr("&Quit")); | |
371 | action_quit_->setIcon(QIcon::fromTheme("application-exit", | |
372 | QIcon(":/icons/application-exit.png"))); | |
373 | action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); | |
374 | action_quit_->setObjectName(QString::fromUtf8("actionQuit")); | |
375 | menu_file->addAction(action_quit_); | |
376 | ||
377 | // View Menu | |
378 | QMenu *menu_view = new QMenu; | |
379 | menu_view->setTitle(tr("&View")); | |
380 | ||
381 | action_view_zoom_in_->setText(tr("Zoom &In")); | |
382 | action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", | |
383 | QIcon(":/icons/zoom-in.png"))); | |
384 | // simply using Qt::Key_Plus shows no + in the menu | |
385 | action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); | |
386 | action_view_zoom_in_->setObjectName( | |
387 | QString::fromUtf8("actionViewZoomIn")); | |
388 | menu_view->addAction(action_view_zoom_in_); | |
389 | ||
390 | action_view_zoom_out_->setText(tr("Zoom &Out")); | |
391 | action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", | |
392 | QIcon(":/icons/zoom-out.png"))); | |
393 | action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); | |
394 | action_view_zoom_out_->setObjectName( | |
395 | QString::fromUtf8("actionViewZoomOut")); | |
396 | menu_view->addAction(action_view_zoom_out_); | |
397 | ||
398 | action_view_zoom_fit_->setText(tr("Zoom to &Fit")); | |
399 | action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", | |
400 | QIcon(":/icons/zoom-fit.png"))); | |
401 | action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); | |
402 | action_view_zoom_fit_->setObjectName( | |
403 | QString::fromUtf8("actionViewZoomFit")); | |
404 | menu_view->addAction(action_view_zoom_fit_); | |
405 | ||
406 | action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); | |
407 | action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", | |
408 | QIcon(":/icons/zoom-original.png"))); | |
409 | action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); | |
410 | action_view_zoom_one_to_one_->setObjectName( | |
411 | QString::fromUtf8("actionViewZoomOneToOne")); | |
412 | menu_view->addAction(action_view_zoom_one_to_one_); | |
413 | ||
414 | menu_view->addSeparator(); | |
415 | ||
416 | action_view_show_cursors_->setCheckable(true); | |
417 | action_view_show_cursors_->setChecked(view_->cursors_shown()); | |
418 | action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", | |
419 | QIcon(":/icons/show-cursors.svg"))); | |
420 | action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); | |
421 | action_view_show_cursors_->setObjectName( | |
422 | QString::fromUtf8("actionViewShowCursors")); | |
423 | action_view_show_cursors_->setText(tr("Show &Cursors")); | |
424 | menu_view->addAction(action_view_show_cursors_); | |
425 | ||
426 | // Decoders Menu | |
427 | #ifdef ENABLE_DECODE | |
428 | QMenu *const menu_decoders = new QMenu; | |
429 | menu_decoders->setTitle(tr("&Decoders")); | |
430 | ||
431 | menu_decoders_add_->setTitle(tr("&Add")); | |
432 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
433 | this, SLOT(add_decoder(srd_decoder*))); | |
434 | ||
435 | menu_decoders->addMenu(menu_decoders_add_); | |
436 | #endif | |
437 | ||
438 | // Help Menu | |
439 | QMenu *const menu_help = new QMenu; | |
440 | menu_help->setTitle(tr("&Help")); | |
441 | ||
442 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); | |
443 | action_about_->setText(tr("&About...")); | |
444 | menu_help->addAction(action_about_); | |
445 | ||
446 | menu_bar->addAction(menu_file->menuAction()); | |
447 | menu_bar->addAction(menu_view->menuAction()); | |
448 | #ifdef ENABLE_DECODE | |
449 | menu_bar->addAction(menu_decoders->menuAction()); | |
450 | #endif | |
451 | menu_bar->addAction(menu_help->menuAction()); | |
452 | ||
453 | setMenuBar(menu_bar); | |
454 | QMetaObject::connectSlotsByName(this); | |
455 | ||
456 | // Also add all actions to the main window for always-enabled hotkeys | |
457 | for (QAction* action : menu_bar->actions()) | |
458 | this->addAction(action); | |
459 | ||
460 | // Setup the toolbar | |
461 | main_bar_ = new toolbars::MainBar(session_, *this); | |
462 | ||
463 | // Populate the device list and select the initially selected device | |
464 | update_device_list(); | |
465 | ||
466 | addToolBar(main_bar_); | |
467 | ||
468 | // Set the title | |
469 | setWindowTitle(tr("PulseView")); | |
470 | ||
471 | // Setup session_ events | |
472 | connect(&session_, SIGNAL(capture_state_changed(int)), this, | |
473 | SLOT(capture_state_changed(int))); | |
474 | connect(&session_, SIGNAL(device_selected()), this, | |
475 | SLOT(device_selected())); | |
476 | } | |
477 | ||
478 | void MainWindow::select_init_device() { | |
479 | QSettings settings; | |
480 | map<string, string> dev_info; | |
481 | list<string> key_list; | |
482 | ||
483 | // Re-select last used device if possible. | |
484 | settings.beginGroup("Device"); | |
485 | key_list.push_back("vendor"); | |
486 | key_list.push_back("model"); | |
487 | key_list.push_back("version"); | |
488 | key_list.push_back("serial_num"); | |
489 | key_list.push_back("connection_id"); | |
490 | ||
491 | for (string key : key_list) { | |
492 | const QString k = QString::fromStdString(key); | |
493 | if (!settings.contains(k)) | |
494 | continue; | |
495 | ||
496 | const string value = settings.value(k).toString().toStdString(); | |
497 | if (!value.empty()) | |
498 | dev_info.insert(std::make_pair(key, value)); | |
499 | } | |
500 | ||
501 | const shared_ptr<devices::HardwareDevice> device = | |
502 | device_manager_.find_device_from_info(dev_info); | |
503 | select_device(device); | |
504 | update_device_list(); | |
505 | ||
506 | settings.endGroup(); | |
507 | } | |
508 | ||
509 | void MainWindow::load_init_file(const std::string &file_name, | |
510 | const std::string &format) { | |
511 | shared_ptr<InputFormat> input_format; | |
512 | ||
513 | if (!format.empty()) { | |
514 | const map<string, shared_ptr<InputFormat> > formats = | |
515 | device_manager_.context()->input_formats(); | |
516 | const auto iter = find_if(formats.begin(), formats.end(), | |
517 | [&](const pair<string, shared_ptr<InputFormat> > f) { | |
518 | return f.first == format; }); | |
519 | if (iter == formats.end()) { | |
520 | cerr << "Unexpected input format: " << format << endl; | |
521 | return; | |
522 | } | |
523 | ||
524 | input_format = (*iter).second; | |
525 | } | |
526 | ||
527 | load_file(QString::fromStdString(file_name), input_format); | |
528 | } | |
529 | ||
530 | ||
531 | void MainWindow::save_ui_settings() | |
532 | { | |
533 | QSettings settings; | |
534 | ||
535 | map<string, string> dev_info; | |
536 | list<string> key_list; | |
537 | ||
538 | settings.beginGroup("MainWindow"); | |
539 | settings.setValue("state", saveState()); | |
540 | settings.setValue("geometry", saveGeometry()); | |
541 | settings.endGroup(); | |
542 | ||
543 | if (session_.device()) { | |
544 | settings.beginGroup("Device"); | |
545 | key_list.push_back("vendor"); | |
546 | key_list.push_back("model"); | |
547 | key_list.push_back("version"); | |
548 | key_list.push_back("serial_num"); | |
549 | key_list.push_back("connection_id"); | |
550 | ||
551 | dev_info = device_manager_.get_device_info( | |
552 | session_.device()); | |
553 | ||
554 | for (string key : key_list) { | |
555 | ||
556 | if (dev_info.count(key)) | |
557 | settings.setValue(QString::fromUtf8(key.c_str()), | |
558 | QString::fromUtf8(dev_info.at(key).c_str())); | |
559 | else | |
560 | settings.remove(QString::fromUtf8(key.c_str())); | |
561 | } | |
562 | ||
563 | settings.endGroup(); | |
564 | } | |
565 | } | |
566 | ||
567 | void MainWindow::restore_ui_settings() | |
568 | { | |
569 | QSettings settings; | |
570 | ||
571 | settings.beginGroup("MainWindow"); | |
572 | ||
573 | if (settings.contains("geometry")) { | |
574 | restoreGeometry(settings.value("geometry").toByteArray()); | |
575 | restoreState(settings.value("state").toByteArray()); | |
576 | } else | |
577 | resize(1000, 720); | |
578 | ||
579 | settings.endGroup(); | |
580 | } | |
581 | ||
582 | void MainWindow::session_error( | |
583 | const QString text, const QString info_text) | |
584 | { | |
585 | QMetaObject::invokeMethod(this, "show_session_error", | |
586 | Qt::QueuedConnection, Q_ARG(QString, text), | |
587 | Q_ARG(QString, info_text)); | |
588 | } | |
589 | ||
590 | void MainWindow::update_device_list() | |
591 | { | |
592 | main_bar_->update_device_list(); | |
593 | } | |
594 | ||
595 | void MainWindow::load_file(QString file_name, | |
596 | std::shared_ptr<sigrok::InputFormat> format, | |
597 | const std::map<std::string, Glib::VariantBase> &options) | |
598 | { | |
599 | const QString errorMessage( | |
600 | QString("Failed to load file %1").arg(file_name)); | |
601 | const QString infoMessage; | |
602 | ||
603 | try { | |
604 | if (format) | |
605 | session_.set_device(shared_ptr<devices::Device>( | |
606 | new devices::InputFile( | |
607 | device_manager_.context(), | |
608 | file_name.toStdString(), | |
609 | format, options))); | |
610 | else | |
611 | session_.set_device(shared_ptr<devices::Device>( | |
612 | new devices::SessionFile( | |
613 | device_manager_.context(), | |
614 | file_name.toStdString()))); | |
615 | } catch(Error e) { | |
616 | show_session_error(tr("Failed to load ") + file_name, e.what()); | |
617 | session_.set_default_device(); | |
618 | update_device_list(); | |
619 | return; | |
620 | } | |
621 | ||
622 | update_device_list(); | |
623 | ||
624 | session_.start_capture([&, errorMessage, infoMessage](QString) { | |
625 | session_error(errorMessage, infoMessage); }); | |
626 | } | |
627 | ||
628 | void MainWindow::closeEvent(QCloseEvent *event) | |
629 | { | |
630 | save_ui_settings(); | |
631 | event->accept(); | |
632 | } | |
633 | ||
634 | void MainWindow::keyReleaseEvent(QKeyEvent *event) | |
635 | { | |
636 | if (event->key() == Qt::Key_Alt) { | |
637 | menuBar()->setHidden(!menuBar()->isHidden()); | |
638 | menuBar()->setFocus(); | |
639 | } | |
640 | QMainWindow::keyReleaseEvent(event); | |
641 | } | |
642 | ||
643 | void MainWindow::show_session_error( | |
644 | const QString text, const QString info_text) | |
645 | { | |
646 | QMessageBox msg(this); | |
647 | msg.setText(text); | |
648 | msg.setInformativeText(info_text); | |
649 | msg.setStandardButtons(QMessageBox::Ok); | |
650 | msg.setIcon(QMessageBox::Warning); | |
651 | msg.exec(); | |
652 | } | |
653 | ||
654 | void MainWindow::on_actionOpen_triggered() | |
655 | { | |
656 | QSettings settings; | |
657 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
658 | ||
659 | // Show the dialog | |
660 | const QString file_name = QFileDialog::getOpenFileName( | |
661 | this, tr("Open File"), dir, tr( | |
662 | "Sigrok Sessions (*.sr);;" | |
663 | "All Files (*.*)")); | |
664 | ||
665 | if (!file_name.isEmpty()) { | |
666 | load_file(file_name); | |
667 | ||
668 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
669 | settings.setValue(SettingOpenDirectory, abs_path); | |
670 | } | |
671 | } | |
672 | ||
673 | void MainWindow::on_actionSaveAs_triggered() | |
674 | { | |
675 | export_file(device_manager_.context()->output_formats()["srzip"]); | |
676 | } | |
677 | ||
678 | void MainWindow::on_actionConnect_triggered() | |
679 | { | |
680 | // Stop any currently running capture session | |
681 | session_.stop_capture(); | |
682 | ||
683 | dialogs::Connect dlg(this, device_manager_); | |
684 | ||
685 | // If the user selected a device, select it in the device list. Select the | |
686 | // current device otherwise. | |
687 | if (dlg.exec()) | |
688 | select_device(dlg.get_selected_device()); | |
689 | ||
690 | update_device_list(); | |
691 | } | |
692 | ||
693 | void MainWindow::on_actionQuit_triggered() | |
694 | { | |
695 | close(); | |
696 | } | |
697 | ||
698 | void MainWindow::on_actionViewZoomIn_triggered() | |
699 | { | |
700 | view_->zoom(1); | |
701 | } | |
702 | ||
703 | void MainWindow::on_actionViewZoomOut_triggered() | |
704 | { | |
705 | view_->zoom(-1); | |
706 | } | |
707 | ||
708 | void MainWindow::on_actionViewZoomFit_triggered() | |
709 | { | |
710 | view_->zoom_fit(); | |
711 | } | |
712 | ||
713 | void MainWindow::on_actionViewZoomOneToOne_triggered() | |
714 | { | |
715 | view_->zoom_one_to_one(); | |
716 | } | |
717 | ||
718 | void MainWindow::on_actionViewShowCursors_triggered() | |
719 | { | |
720 | assert(view_); | |
721 | ||
722 | const bool show = !view_->cursors_shown(); | |
723 | if(show) | |
724 | view_->centre_cursors(); | |
725 | ||
726 | view_->show_cursors(show); | |
727 | } | |
728 | ||
729 | void MainWindow::on_actionAbout_triggered() | |
730 | { | |
731 | dialogs::About dlg(device_manager_.context(), this); | |
732 | dlg.exec(); | |
733 | } | |
734 | ||
735 | void MainWindow::add_decoder(srd_decoder *decoder) | |
736 | { | |
737 | #ifdef ENABLE_DECODE | |
738 | assert(decoder); | |
739 | session_.add_decoder(decoder); | |
740 | #else | |
741 | (void)decoder; | |
742 | #endif | |
743 | } | |
744 | ||
745 | void MainWindow::capture_state_changed(int state) | |
746 | { | |
747 | main_bar_->set_capture_state((pv::Session::capture_state)state); | |
748 | } | |
749 | ||
750 | void MainWindow::device_selected() | |
751 | { | |
752 | // Set the title to include the device/file name | |
753 | const shared_ptr<devices::Device> device = session_.device(); | |
754 | if (!device) | |
755 | return; | |
756 | ||
757 | const string display_name = device->display_name(device_manager_); | |
758 | setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str())); | |
759 | } | |
760 | ||
761 | } // namespace pv |