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