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