]>
Commit | Line | Data |
---|---|---|
d7bed479 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
d7bed479 JH |
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 | ||
943edd76 MC |
21 | #include <cassert> |
22 | ||
269528f5 | 23 | #ifdef ENABLE_DECODE |
aaabd61b | 24 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 25 | #endif |
30236a54 | 26 | |
85843b14 JH |
27 | #include <algorithm> |
28 | #include <iterator> | |
29 | ||
ea2dbfea JH |
30 | #include <boost/algorithm/string/join.hpp> |
31 | ||
7d5425ef JH |
32 | #include <QAction> |
33 | #include <QApplication> | |
34 | #include <QButtonGroup> | |
93f683ad | 35 | #include <QCloseEvent> |
2953961c | 36 | #include <QFileDialog> |
f2edb557 | 37 | #include <QMessageBox> |
7d5425ef JH |
38 | #include <QMenu> |
39 | #include <QMenuBar> | |
643f65f9 | 40 | #include <QSettings> |
7d5425ef JH |
41 | #include <QStatusBar> |
42 | #include <QVBoxLayout> | |
43 | #include <QWidget> | |
2953961c | 44 | |
2acdb232 | 45 | #include "mainwindow.hpp" |
107ca6d3 | 46 | |
2acdb232 JH |
47 | #include "devicemanager.hpp" |
48 | #include "dialogs/about.hpp" | |
49 | #include "dialogs/connect.hpp" | |
e93f5538 | 50 | #include "dialogs/inputoutputoptions.hpp" |
2acdb232 | 51 | #include "dialogs/storeprogress.hpp" |
7c657094 | 52 | #include "toolbars/mainbar.hpp" |
2acdb232 JH |
53 | #include "view/logicsignal.hpp" |
54 | #include "view/view.hpp" | |
998b89fd | 55 | #include "widgets/exportmenu.hpp" |
269528f5 | 56 | #ifdef ENABLE_DECODE |
2acdb232 | 57 | #include "widgets/decodermenu.hpp" |
269528f5 | 58 | #endif |
1c4c23b8 | 59 | #include "widgets/hidingmenubar.hpp" |
d7bed479 | 60 | |
30236a54 JH |
61 | #include <inttypes.h> |
62 | #include <stdint.h> | |
63 | #include <stdarg.h> | |
64 | #include <glib.h> | |
fe3a1c21 | 65 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e82fd481 | 66 | |
819f4c25 | 67 | using std::list; |
6842b5fc | 68 | using std::map; |
f9abf97e | 69 | using std::shared_ptr; |
6842b5fc | 70 | using std::string; |
ea2dbfea JH |
71 | using std::vector; |
72 | ||
73 | using boost::algorithm::join; | |
30236a54 | 74 | |
e8d00928 ML |
75 | using sigrok::Device; |
76 | using sigrok::Error; | |
77 | using sigrok::HardwareDevice; | |
998b89fd | 78 | using sigrok::OutputFormat; |
e8d00928 | 79 | |
51e77110 JH |
80 | namespace pv { |
81 | ||
b1264f56 | 82 | namespace view { |
26e3af6b | 83 | class ViewItem; |
b1264f56 JH |
84 | } |
85 | ||
643f65f9 JS |
86 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; |
87 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
88 | ||
107ca6d3 JH |
89 | MainWindow::MainWindow(DeviceManager &device_manager, |
90 | const char *open_file_name, | |
1d478458 | 91 | QWidget *parent) : |
107ca6d3 | 92 | QMainWindow(parent), |
8dbbc7f0 | 93 | device_manager_(device_manager), |
69654681 JH |
94 | session_(device_manager), |
95 | action_open_(new QAction(this)), | |
96 | action_save_as_(new QAction(this)), | |
97 | action_connect_(new QAction(this)), | |
98 | action_quit_(new QAction(this)), | |
99 | action_view_zoom_in_(new QAction(this)), | |
100 | action_view_zoom_out_(new QAction(this)), | |
101 | action_view_zoom_fit_(new QAction(this)), | |
102 | action_view_zoom_one_to_one_(new QAction(this)), | |
103 | action_view_show_cursors_(new QAction(this)), | |
e79171dc BG |
104 | action_about_(new QAction(this)) |
105 | #ifdef ENABLE_DECODE | |
106 | , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) | |
19be0af1 | 107 | #endif |
d7bed479 | 108 | { |
7d5425ef | 109 | setup_ui(); |
93f683ad | 110 | restore_ui_settings(); |
1d478458 JH |
111 | if (open_file_name) { |
112 | const QString s(QString::fromUtf8(open_file_name)); | |
113 | QMetaObject::invokeMethod(this, "load_file", | |
114 | Qt::QueuedConnection, | |
115 | Q_ARG(QString, s)); | |
116 | } | |
7d5425ef JH |
117 | } |
118 | ||
69654681 JH |
119 | QAction* MainWindow::action_open() const |
120 | { | |
121 | return action_open_; | |
122 | } | |
123 | ||
124 | QAction* MainWindow::action_save_as() const | |
125 | { | |
126 | return action_save_as_; | |
127 | } | |
128 | ||
129 | QAction* MainWindow::action_connect() const | |
130 | { | |
131 | return action_connect_; | |
132 | } | |
133 | ||
134 | QAction* MainWindow::action_quit() const | |
135 | { | |
136 | return action_quit_; | |
137 | } | |
138 | ||
139 | QAction* MainWindow::action_view_zoom_in() const | |
140 | { | |
141 | return action_view_zoom_in_; | |
142 | } | |
143 | ||
144 | QAction* MainWindow::action_view_zoom_out() const | |
145 | { | |
146 | return action_view_zoom_out_; | |
147 | } | |
148 | ||
149 | QAction* MainWindow::action_view_zoom_fit() const | |
150 | { | |
151 | return action_view_zoom_fit_; | |
152 | } | |
153 | ||
154 | QAction* MainWindow::action_view_zoom_one_to_one() const | |
155 | { | |
156 | return action_view_zoom_one_to_one_; | |
157 | } | |
158 | ||
159 | QAction* MainWindow::action_view_show_cursors() const | |
160 | { | |
161 | return action_view_show_cursors_; | |
162 | } | |
163 | ||
164 | QAction* MainWindow::action_about() const | |
165 | { | |
166 | return action_about_; | |
167 | } | |
168 | ||
e79171dc | 169 | #ifdef ENABLE_DECODE |
0f90452b JH |
170 | QMenu* MainWindow::menu_decoder_add() const |
171 | { | |
172 | return menu_decoders_add_; | |
173 | } | |
19be0af1 | 174 | #endif |
0f90452b | 175 | |
4e7f5ba8 JH |
176 | void MainWindow::run_stop() |
177 | { | |
178 | switch(session_.get_capture_state()) { | |
2b81ae46 | 179 | case Session::Stopped: |
4e7f5ba8 JH |
180 | session_.start_capture([&](QString message) { |
181 | session_error("Capture failed", message); }); | |
182 | break; | |
183 | ||
2b81ae46 JH |
184 | case Session::AwaitingTrigger: |
185 | case Session::Running: | |
4e7f5ba8 JH |
186 | session_.stop_capture(); |
187 | break; | |
188 | } | |
189 | } | |
190 | ||
51cf49fe JH |
191 | void MainWindow::select_device(shared_ptr<Device> device) |
192 | { | |
193 | try { | |
194 | session_.set_device(device); | |
195 | } catch(const QString &e) { | |
196 | QMessageBox msg(this); | |
197 | msg.setText(e); | |
198 | msg.setInformativeText(tr("Failed to Select Device")); | |
199 | msg.setStandardButtons(QMessageBox::Ok); | |
200 | msg.setIcon(QMessageBox::Warning); | |
201 | msg.exec(); | |
202 | } | |
203 | } | |
204 | ||
998b89fd JH |
205 | void MainWindow::export_file(shared_ptr<OutputFormat> format) |
206 | { | |
207 | using pv::dialogs::StoreProgress; | |
208 | ||
209 | // Stop any currently running capture session | |
210 | session_.stop_capture(); | |
211 | ||
212 | QSettings settings; | |
213 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
214 | ||
ea2dbfea JH |
215 | // Construct the filter |
216 | const vector<string> exts = format->extensions(); | |
217 | QString filter = tr("%1 files ").arg( | |
218 | QString::fromStdString(format->description())); | |
219 | ||
220 | if (exts.empty()) | |
221 | filter += "(*.*)"; | |
222 | else | |
223 | filter += QString("(*.%1);;%2 (*.*)").arg( | |
224 | QString::fromStdString(join(exts, ", *."))).arg( | |
225 | tr("All Files")); | |
226 | ||
e93f5538 | 227 | // Show the file dialog |
998b89fd | 228 | const QString file_name = QFileDialog::getSaveFileName( |
ea2dbfea | 229 | this, tr("Save File"), dir, filter); |
998b89fd JH |
230 | |
231 | if (file_name.isEmpty()) | |
232 | return; | |
233 | ||
234 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
235 | settings.setValue(SettingSaveDirectory, abs_path); | |
236 | ||
e93f5538 JH |
237 | // Show the options dialog |
238 | map<string, Glib::VariantBase> options; | |
239 | if (!format->options().empty()) { | |
240 | dialogs::InputOutputOptions dlg( | |
241 | tr("Export %1").arg(QString::fromStdString( | |
242 | format->description())), | |
243 | format->options(), this); | |
244 | if (!dlg.exec()) | |
245 | return; | |
246 | options = dlg.options(); | |
247 | } | |
248 | ||
249 | StoreProgress *dlg = new StoreProgress(file_name, format, options, | |
998b89fd JH |
250 | session_, this); |
251 | dlg->run(); | |
252 | } | |
253 | ||
7d5425ef JH |
254 | void MainWindow::setup_ui() |
255 | { | |
256 | setObjectName(QString::fromUtf8("MainWindow")); | |
257 | ||
7d5425ef JH |
258 | // Set the window icon |
259 | QIcon icon; | |
4523c509 | 260 | icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); |
7d5425ef JH |
261 | setWindowIcon(icon); |
262 | ||
e072efc8 | 263 | // Setup the central widget |
8dbbc7f0 JH |
264 | central_widget_ = new QWidget(this); |
265 | vertical_layout_ = new QVBoxLayout(central_widget_); | |
266 | vertical_layout_->setSpacing(6); | |
267 | vertical_layout_->setContentsMargins(0, 0, 0, 0); | |
268 | setCentralWidget(central_widget_); | |
e072efc8 | 269 | |
8dbbc7f0 | 270 | view_ = new pv::view::View(session_, this); |
b1264f56 | 271 | |
8dbbc7f0 | 272 | vertical_layout_->addWidget(view_); |
e072efc8 | 273 | |
ab1d13ee | 274 | // Setup the menu bar |
1c4c23b8 JH |
275 | pv::widgets::HidingMenuBar *const menu_bar = |
276 | new pv::widgets::HidingMenuBar(this); | |
a429590b | 277 | |
ab1d13ee | 278 | // File Menu |
ced1cc13 | 279 | QMenu *const menu_file = new QMenu; |
0a9fdca5 | 280 | menu_file->setTitle(tr("&File")); |
e072efc8 | 281 | |
69654681 JH |
282 | action_open_->setText(tr("&Open...")); |
283 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
362eea96 | 284 | QIcon(":/icons/document-open.png"))); |
69654681 JH |
285 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); |
286 | action_open_->setObjectName(QString::fromUtf8("actionOpen")); | |
287 | menu_file->addAction(action_open_); | |
009e1503 | 288 | |
69654681 JH |
289 | action_save_as_->setText(tr("&Save As...")); |
290 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
0fbda3c2 | 291 | QIcon(":/icons/document-save-as.png"))); |
69654681 JH |
292 | action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); |
293 | action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs")); | |
294 | menu_file->addAction(action_save_as_); | |
0fbda3c2 | 295 | |
ced1cc13 | 296 | menu_file->addSeparator(); |
2a032dcb | 297 | |
998b89fd JH |
298 | widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, |
299 | device_manager_.context()); | |
300 | menu_file_export->setTitle(tr("&Export")); | |
301 | connect(menu_file_export, | |
302 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
303 | this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
304 | menu_file->addAction(menu_file_export->menuAction()); | |
305 | ||
306 | menu_file->addSeparator(); | |
307 | ||
69654681 JH |
308 | action_connect_->setText(tr("&Connect to Device...")); |
309 | action_connect_->setObjectName(QString::fromUtf8("actionConnect")); | |
310 | menu_file->addAction(action_connect_); | |
9663c82b | 311 | |
ced1cc13 | 312 | menu_file->addSeparator(); |
9663c82b | 313 | |
69654681 JH |
314 | action_quit_->setText(tr("&Quit")); |
315 | action_quit_->setIcon(QIcon::fromTheme("application-exit", | |
2a032dcb | 316 | QIcon(":/icons/application-exit.png"))); |
69654681 JH |
317 | action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
318 | action_quit_->setObjectName(QString::fromUtf8("actionQuit")); | |
319 | menu_file->addAction(action_quit_); | |
2a032dcb | 320 | |
ab1d13ee | 321 | // View Menu |
ced1cc13 | 322 | QMenu *menu_view = new QMenu; |
0a9fdca5 | 323 | menu_view->setTitle(tr("&View")); |
ab1d13ee | 324 | |
69654681 JH |
325 | action_view_zoom_in_->setText(tr("Zoom &In")); |
326 | action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", | |
ab1d13ee | 327 | QIcon(":/icons/zoom-in.png"))); |
d2899ad5 | 328 | // simply using Qt::Key_Plus shows no + in the menu |
69654681 JH |
329 | action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); |
330 | action_view_zoom_in_->setObjectName( | |
ab1d13ee | 331 | QString::fromUtf8("actionViewZoomIn")); |
69654681 | 332 | menu_view->addAction(action_view_zoom_in_); |
ab1d13ee | 333 | |
69654681 JH |
334 | action_view_zoom_out_->setText(tr("Zoom &Out")); |
335 | action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", | |
ab1d13ee | 336 | QIcon(":/icons/zoom-out.png"))); |
69654681 JH |
337 | action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); |
338 | action_view_zoom_out_->setObjectName( | |
ab1d13ee | 339 | QString::fromUtf8("actionViewZoomOut")); |
69654681 | 340 | menu_view->addAction(action_view_zoom_out_); |
ab1d13ee | 341 | |
69654681 JH |
342 | action_view_zoom_fit_->setText(tr("Zoom to &Fit")); |
343 | action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", | |
ca46b534 | 344 | QIcon(":/icons/zoom-fit.png"))); |
69654681 JH |
345 | action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); |
346 | action_view_zoom_fit_->setObjectName( | |
ca46b534 | 347 | QString::fromUtf8("actionViewZoomFit")); |
69654681 | 348 | menu_view->addAction(action_view_zoom_fit_); |
ca46b534 | 349 | |
1c4c23b8 | 350 | action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); |
69654681 | 351 | action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", |
d1e7d82c | 352 | QIcon(":/icons/zoom-original.png"))); |
69654681 JH |
353 | action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); |
354 | action_view_zoom_one_to_one_->setObjectName( | |
d1e7d82c | 355 | QString::fromUtf8("actionViewZoomOneToOne")); |
69654681 | 356 | menu_view->addAction(action_view_zoom_one_to_one_); |
d1e7d82c | 357 | |
ced1cc13 | 358 | menu_view->addSeparator(); |
ab1d13ee | 359 | |
69654681 JH |
360 | action_view_show_cursors_->setCheckable(true); |
361 | action_view_show_cursors_->setChecked(view_->cursors_shown()); | |
587f1d15 JH |
362 | action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", |
363 | QIcon(":/icons/show-cursors.svg"))); | |
69654681 JH |
364 | action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); |
365 | action_view_show_cursors_->setObjectName( | |
ab1d13ee | 366 | QString::fromUtf8("actionViewShowCursors")); |
69654681 JH |
367 | action_view_show_cursors_->setText(tr("Show &Cursors")); |
368 | menu_view->addAction(action_view_show_cursors_); | |
a429590b | 369 | |
7dd093df | 370 | // Decoders Menu |
269528f5 | 371 | #ifdef ENABLE_DECODE |
ced1cc13 | 372 | QMenu *const menu_decoders = new QMenu; |
0a9fdca5 | 373 | menu_decoders->setTitle(tr("&Decoders")); |
7dd093df | 374 | |
0f90452b JH |
375 | menu_decoders_add_->setTitle(tr("&Add")); |
376 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
f0d37dab | 377 | this, SLOT(add_decoder(srd_decoder*))); |
7dd093df | 378 | |
0f90452b | 379 | menu_decoders->addMenu(menu_decoders_add_); |
269528f5 | 380 | #endif |
7dd093df | 381 | |
ab1d13ee | 382 | // Help Menu |
ced1cc13 | 383 | QMenu *const menu_help = new QMenu; |
0a9fdca5 | 384 | menu_help->setTitle(tr("&Help")); |
ab1d13ee | 385 | |
69654681 JH |
386 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); |
387 | action_about_->setText(tr("&About...")); | |
388 | menu_help->addAction(action_about_); | |
7d5425ef | 389 | |
ced1cc13 JH |
390 | menu_bar->addAction(menu_file->menuAction()); |
391 | menu_bar->addAction(menu_view->menuAction()); | |
269528f5 | 392 | #ifdef ENABLE_DECODE |
ced1cc13 | 393 | menu_bar->addAction(menu_decoders->menuAction()); |
269528f5 | 394 | #endif |
ced1cc13 | 395 | menu_bar->addAction(menu_help->menuAction()); |
7d5425ef | 396 | |
ced1cc13 | 397 | setMenuBar(menu_bar); |
7d5425ef JH |
398 | QMetaObject::connectSlotsByName(this); |
399 | ||
d2f13e3e | 400 | // Setup the toolbar |
7c657094 | 401 | main_bar_ = new toolbars::MainBar(session_, *this); |
5ac961e3 JH |
402 | |
403 | // Populate the device list and select the initially selected device | |
107ca6d3 | 404 | update_device_list(); |
5ac961e3 | 405 | |
7c657094 | 406 | addToolBar(main_bar_); |
d4984fe7 | 407 | |
5ac961e3 | 408 | // Set the title |
0a9fdca5 | 409 | setWindowTitle(tr("PulseView")); |
a8d3fb2d | 410 | |
8dbbc7f0 JH |
411 | // Setup session_ events |
412 | connect(&session_, SIGNAL(capture_state_changed(int)), this, | |
6ac96c2e | 413 | SLOT(capture_state_changed(int))); |
20f81a58 JH |
414 | connect(&session_, SIGNAL(device_selected()), this, |
415 | SLOT(device_selected())); | |
d7bed479 | 416 | } |
30236a54 | 417 | |
93f683ad SA |
418 | void MainWindow::save_ui_settings() |
419 | { | |
39eb0d45 | 420 | QSettings settings; |
93f683ad | 421 | |
6842b5fc SA |
422 | map<string, string> dev_info; |
423 | list<string> key_list; | |
424 | ||
93f683ad SA |
425 | settings.beginGroup("MainWindow"); |
426 | settings.setValue("state", saveState()); | |
427 | settings.setValue("geometry", saveGeometry()); | |
428 | settings.endGroup(); | |
6842b5fc | 429 | |
8dbbc7f0 | 430 | if (session_.device()) { |
6842b5fc SA |
431 | settings.beginGroup("Device"); |
432 | key_list.push_back("vendor"); | |
433 | key_list.push_back("model"); | |
434 | key_list.push_back("version"); | |
435 | key_list.push_back("serial_num"); | |
436 | key_list.push_back("connection_id"); | |
437 | ||
8dbbc7f0 JH |
438 | dev_info = device_manager_.get_device_info( |
439 | session_.device()); | |
6842b5fc SA |
440 | |
441 | for (string key : key_list) { | |
442 | ||
443 | if (dev_info.count(key)) | |
444 | settings.setValue(QString::fromUtf8(key.c_str()), | |
445 | QString::fromUtf8(dev_info.at(key).c_str())); | |
446 | else | |
447 | settings.remove(QString::fromUtf8(key.c_str())); | |
448 | } | |
449 | ||
450 | settings.endGroup(); | |
451 | } | |
93f683ad SA |
452 | } |
453 | ||
454 | void MainWindow::restore_ui_settings() | |
455 | { | |
39eb0d45 | 456 | QSettings settings; |
93f683ad | 457 | |
e8d00928 | 458 | shared_ptr<HardwareDevice> device; |
6842b5fc SA |
459 | |
460 | map<string, string> dev_info; | |
461 | list<string> key_list; | |
462 | string value; | |
463 | ||
93f683ad SA |
464 | settings.beginGroup("MainWindow"); |
465 | ||
466 | if (settings.contains("geometry")) { | |
467 | restoreGeometry(settings.value("geometry").toByteArray()); | |
468 | restoreState(settings.value("state").toByteArray()); | |
469 | } else | |
470 | resize(1000, 720); | |
471 | ||
472 | settings.endGroup(); | |
6842b5fc SA |
473 | |
474 | // Re-select last used device if possible. | |
475 | settings.beginGroup("Device"); | |
476 | key_list.push_back("vendor"); | |
477 | key_list.push_back("model"); | |
478 | key_list.push_back("version"); | |
479 | key_list.push_back("serial_num"); | |
480 | key_list.push_back("connection_id"); | |
481 | ||
482 | for (string key : key_list) { | |
483 | if (!settings.contains(QString::fromUtf8(key.c_str()))) | |
484 | continue; | |
485 | ||
486 | value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString(); | |
487 | ||
488 | if (value.size() > 0) | |
489 | dev_info.insert(std::make_pair(key, value)); | |
490 | } | |
491 | ||
8dbbc7f0 | 492 | device = device_manager_.find_device_from_info(dev_info); |
6842b5fc SA |
493 | |
494 | if (device) { | |
51cf49fe | 495 | select_device(device); |
6842b5fc SA |
496 | update_device_list(); |
497 | } | |
498 | ||
499 | settings.endGroup(); | |
93f683ad SA |
500 | } |
501 | ||
f2edb557 JH |
502 | void MainWindow::session_error( |
503 | const QString text, const QString info_text) | |
504 | { | |
505 | QMetaObject::invokeMethod(this, "show_session_error", | |
506 | Qt::QueuedConnection, Q_ARG(QString, text), | |
507 | Q_ARG(QString, info_text)); | |
508 | } | |
509 | ||
d873f4d6 | 510 | void MainWindow::update_device_list() |
107ca6d3 | 511 | { |
079d39ea | 512 | main_bar_->update_device_list(); |
107ca6d3 JH |
513 | } |
514 | ||
93f683ad SA |
515 | void MainWindow::closeEvent(QCloseEvent *event) |
516 | { | |
517 | save_ui_settings(); | |
518 | event->accept(); | |
519 | } | |
520 | ||
1c4c23b8 JH |
521 | void MainWindow::keyReleaseEvent(QKeyEvent *event) |
522 | { | |
523 | if (event->key() == Qt::Key_Alt) { | |
524 | menuBar()->setHidden(!menuBar()->isHidden()); | |
525 | menuBar()->setFocus(); | |
526 | } | |
527 | QMainWindow::keyReleaseEvent(event); | |
528 | } | |
529 | ||
1d478458 JH |
530 | void MainWindow::load_file(QString file_name) |
531 | { | |
f2edb557 JH |
532 | const QString errorMessage( |
533 | QString("Failed to load file %1").arg(file_name)); | |
534 | const QString infoMessage; | |
ae2d1bc5 JH |
535 | |
536 | try { | |
4b5537c5 | 537 | session_.set_session_file(file_name.toStdString()); |
e8d00928 ML |
538 | } catch(Error e) { |
539 | show_session_error(tr("Failed to load ") + file_name, e.what()); | |
8dbbc7f0 | 540 | session_.set_default_device(); |
6fd0b639 JH |
541 | update_device_list(); |
542 | return; | |
ae2d1bc5 JH |
543 | } |
544 | ||
a5ea63c2 JH |
545 | update_device_list(); |
546 | ||
8dbbc7f0 | 547 | session_.start_capture([&, errorMessage, infoMessage](QString) { |
6db73158 | 548 | session_error(errorMessage, infoMessage); }); |
f2edb557 JH |
549 | } |
550 | ||
551 | void MainWindow::show_session_error( | |
552 | const QString text, const QString info_text) | |
553 | { | |
554 | QMessageBox msg(this); | |
555 | msg.setText(text); | |
556 | msg.setInformativeText(info_text); | |
557 | msg.setStandardButtons(QMessageBox::Ok); | |
558 | msg.setIcon(QMessageBox::Warning); | |
559 | msg.exec(); | |
1d478458 JH |
560 | } |
561 | ||
2953961c JH |
562 | void MainWindow::on_actionOpen_triggered() |
563 | { | |
643f65f9 JS |
564 | QSettings settings; |
565 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
566 | ||
1d43d767 | 567 | // Show the dialog |
1d478458 | 568 | const QString file_name = QFileDialog::getOpenFileName( |
643f65f9 | 569 | this, tr("Open File"), dir, tr( |
6866d5de JH |
570 | "Sigrok Sessions (*.sr);;" |
571 | "All Files (*.*)")); | |
643f65f9 JS |
572 | |
573 | if (!file_name.isEmpty()) { | |
92b139d0 | 574 | load_file(file_name); |
643f65f9 JS |
575 | |
576 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
577 | settings.setValue(SettingOpenDirectory, abs_path); | |
578 | } | |
2953961c JH |
579 | } |
580 | ||
0fbda3c2 JH |
581 | void MainWindow::on_actionSaveAs_triggered() |
582 | { | |
998b89fd | 583 | export_file(device_manager_.context()->output_formats()["srzip"]); |
0fbda3c2 JH |
584 | } |
585 | ||
9663c82b JH |
586 | void MainWindow::on_actionConnect_triggered() |
587 | { | |
b99cfc42 | 588 | // Stop any currently running capture session |
8dbbc7f0 | 589 | session_.stop_capture(); |
b99cfc42 | 590 | |
8dbbc7f0 | 591 | dialogs::Connect dlg(this, device_manager_); |
5eb0fa13 | 592 | |
dc0867ff JH |
593 | // If the user selected a device, select it in the device list. Select the |
594 | // current device otherwise. | |
d873f4d6 | 595 | if (dlg.exec()) |
51cf49fe | 596 | select_device(dlg.get_selected_device()); |
dc0867ff | 597 | |
d873f4d6 | 598 | update_device_list(); |
9663c82b JH |
599 | } |
600 | ||
2a032dcb JH |
601 | void MainWindow::on_actionQuit_triggered() |
602 | { | |
603 | close(); | |
604 | } | |
605 | ||
a429590b JH |
606 | void MainWindow::on_actionViewZoomIn_triggered() |
607 | { | |
8dbbc7f0 | 608 | view_->zoom(1); |
a429590b JH |
609 | } |
610 | ||
611 | void MainWindow::on_actionViewZoomOut_triggered() | |
612 | { | |
8dbbc7f0 | 613 | view_->zoom(-1); |
a429590b JH |
614 | } |
615 | ||
ca46b534 JH |
616 | void MainWindow::on_actionViewZoomFit_triggered() |
617 | { | |
8dbbc7f0 | 618 | view_->zoom_fit(); |
ca46b534 JH |
619 | } |
620 | ||
d1e7d82c JH |
621 | void MainWindow::on_actionViewZoomOneToOne_triggered() |
622 | { | |
8dbbc7f0 | 623 | view_->zoom_one_to_one(); |
d1e7d82c JH |
624 | } |
625 | ||
e072efc8 JH |
626 | void MainWindow::on_actionViewShowCursors_triggered() |
627 | { | |
8dbbc7f0 | 628 | assert(view_); |
b4d91e56 | 629 | |
8dbbc7f0 | 630 | const bool show = !view_->cursors_shown(); |
b4d91e56 | 631 | if(show) |
8dbbc7f0 | 632 | view_->centre_cursors(); |
b4d91e56 | 633 | |
8dbbc7f0 | 634 | view_->show_cursors(show); |
e072efc8 JH |
635 | } |
636 | ||
30236a54 JH |
637 | void MainWindow::on_actionAbout_triggered() |
638 | { | |
8dbbc7f0 | 639 | dialogs::About dlg(device_manager_.context(), this); |
40eb2ff4 | 640 | dlg.exec(); |
30236a54 | 641 | } |
274d4f13 | 642 | |
f0d37dab | 643 | void MainWindow::add_decoder(srd_decoder *decoder) |
7dd093df | 644 | { |
269528f5 | 645 | #ifdef ENABLE_DECODE |
f0d37dab | 646 | assert(decoder); |
8dbbc7f0 | 647 | session_.add_decoder(decoder); |
269528f5 JH |
648 | #else |
649 | (void)decoder; | |
650 | #endif | |
7dd093df JH |
651 | } |
652 | ||
6ac96c2e JH |
653 | void MainWindow::capture_state_changed(int state) |
654 | { | |
7c657094 | 655 | main_bar_->set_capture_state((pv::Session::capture_state)state); |
6ac96c2e JH |
656 | } |
657 | ||
20f81a58 JH |
658 | void MainWindow::device_selected() |
659 | { | |
660 | // Set the title to include the device/file name | |
661 | const shared_ptr<sigrok::Device> device = session_.device(); | |
662 | if (!device) | |
663 | return; | |
664 | ||
665 | const string display_name = device_manager_.get_display_name(device); | |
666 | setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str())); | |
667 | } | |
668 | ||
51e77110 | 669 | } // namespace pv |