]>
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 | 47 | #include "devicemanager.hpp" |
da30ecb7 | 48 | #include "devices/hardwaredevice.hpp" |
dd3fd4df | 49 | #include "devices/inputfile.hpp" |
da30ecb7 | 50 | #include "devices/sessionfile.hpp" |
2acdb232 JH |
51 | #include "dialogs/about.hpp" |
52 | #include "dialogs/connect.hpp" | |
e93f5538 | 53 | #include "dialogs/inputoutputoptions.hpp" |
2acdb232 | 54 | #include "dialogs/storeprogress.hpp" |
7c657094 | 55 | #include "toolbars/mainbar.hpp" |
2acdb232 JH |
56 | #include "view/logicsignal.hpp" |
57 | #include "view/view.hpp" | |
998b89fd | 58 | #include "widgets/exportmenu.hpp" |
ed43ef2e | 59 | #include "widgets/importmenu.hpp" |
269528f5 | 60 | #ifdef ENABLE_DECODE |
2acdb232 | 61 | #include "widgets/decodermenu.hpp" |
269528f5 | 62 | #endif |
1c4c23b8 | 63 | #include "widgets/hidingmenubar.hpp" |
d7bed479 | 64 | |
30236a54 JH |
65 | #include <inttypes.h> |
66 | #include <stdint.h> | |
67 | #include <stdarg.h> | |
68 | #include <glib.h> | |
fe3a1c21 | 69 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e82fd481 | 70 | |
e3c79b07 JH |
71 | using std::cerr; |
72 | using std::endl; | |
819f4c25 | 73 | using std::list; |
6842b5fc | 74 | using std::map; |
e3c79b07 | 75 | using std::pair; |
f9abf97e | 76 | using std::shared_ptr; |
6842b5fc | 77 | using std::string; |
ea2dbfea JH |
78 | using std::vector; |
79 | ||
80 | using boost::algorithm::join; | |
30236a54 | 81 | |
e8d00928 | 82 | using sigrok::Error; |
998b89fd | 83 | using sigrok::OutputFormat; |
dd3fd4df | 84 | using sigrok::InputFormat; |
e8d00928 | 85 | |
51e77110 JH |
86 | namespace pv { |
87 | ||
b1264f56 | 88 | namespace view { |
26e3af6b | 89 | class ViewItem; |
b1264f56 JH |
90 | } |
91 | ||
643f65f9 JS |
92 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; |
93 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
94 | ||
107ca6d3 | 95 | MainWindow::MainWindow(DeviceManager &device_manager, |
e3c79b07 | 96 | string open_file_name, string open_file_format, |
1d478458 | 97 | QWidget *parent) : |
107ca6d3 | 98 | QMainWindow(parent), |
8dbbc7f0 | 99 | device_manager_(device_manager), |
69654681 JH |
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)), | |
e79171dc BG |
110 | action_about_(new QAction(this)) |
111 | #ifdef ENABLE_DECODE | |
112 | , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) | |
19be0af1 | 113 | #endif |
d7bed479 | 114 | { |
7d5425ef | 115 | setup_ui(); |
93f683ad | 116 | restore_ui_settings(); |
e3c79b07 JH |
117 | if (open_file_name.empty()) |
118 | select_init_device(); | |
119 | else | |
120 | load_init_file(open_file_name, open_file_format); | |
7d5425ef JH |
121 | } |
122 | ||
69654681 JH |
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 | ||
e79171dc | 173 | #ifdef ENABLE_DECODE |
0f90452b JH |
174 | QMenu* MainWindow::menu_decoder_add() const |
175 | { | |
176 | return menu_decoders_add_; | |
177 | } | |
19be0af1 | 178 | #endif |
0f90452b | 179 | |
4e7f5ba8 JH |
180 | void MainWindow::run_stop() |
181 | { | |
182 | switch(session_.get_capture_state()) { | |
2b81ae46 | 183 | case Session::Stopped: |
4e7f5ba8 JH |
184 | session_.start_capture([&](QString message) { |
185 | session_error("Capture failed", message); }); | |
186 | break; | |
187 | ||
2b81ae46 JH |
188 | case Session::AwaitingTrigger: |
189 | case Session::Running: | |
4e7f5ba8 JH |
190 | session_.stop_capture(); |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
da30ecb7 | 195 | void MainWindow::select_device(shared_ptr<devices::Device> device) |
51cf49fe JH |
196 | { |
197 | try { | |
859ca307 JH |
198 | if (device) |
199 | session_.set_device(device); | |
200 | else | |
201 | session_.set_default_device(); | |
51cf49fe JH |
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 | ||
998b89fd JH |
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 | ||
ea2dbfea JH |
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 | ||
e93f5538 | 234 | // Show the file dialog |
998b89fd | 235 | const QString file_name = QFileDialog::getSaveFileName( |
ea2dbfea | 236 | this, tr("Save File"), dir, filter); |
998b89fd JH |
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 | ||
e93f5538 JH |
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, | |
998b89fd JH |
257 | session_, this); |
258 | dlg->run(); | |
259 | } | |
260 | ||
ed43ef2e JH |
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 | ||
53d45c55 JH |
281 | if (file_name.isEmpty()) |
282 | return; | |
ed43ef2e | 283 | |
53d45c55 JH |
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(); | |
ed43ef2e | 294 | } |
53d45c55 JH |
295 | |
296 | load_file(file_name, format, options); | |
297 | ||
298 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
299 | settings.setValue(SettingOpenDirectory, abs_path); | |
ed43ef2e JH |
300 | } |
301 | ||
7d5425ef JH |
302 | void MainWindow::setup_ui() |
303 | { | |
304 | setObjectName(QString::fromUtf8("MainWindow")); | |
305 | ||
7d5425ef JH |
306 | // Set the window icon |
307 | QIcon icon; | |
4523c509 | 308 | icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); |
7d5425ef JH |
309 | setWindowIcon(icon); |
310 | ||
e072efc8 | 311 | // Setup the central widget |
8dbbc7f0 JH |
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_); | |
e072efc8 | 317 | |
8dbbc7f0 | 318 | view_ = new pv::view::View(session_, this); |
b1264f56 | 319 | |
8dbbc7f0 | 320 | vertical_layout_->addWidget(view_); |
e072efc8 | 321 | |
ab1d13ee | 322 | // Setup the menu bar |
1c4c23b8 JH |
323 | pv::widgets::HidingMenuBar *const menu_bar = |
324 | new pv::widgets::HidingMenuBar(this); | |
a429590b | 325 | |
ab1d13ee | 326 | // File Menu |
ced1cc13 | 327 | QMenu *const menu_file = new QMenu; |
0a9fdca5 | 328 | menu_file->setTitle(tr("&File")); |
e072efc8 | 329 | |
69654681 JH |
330 | action_open_->setText(tr("&Open...")); |
331 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
362eea96 | 332 | QIcon(":/icons/document-open.png"))); |
69654681 JH |
333 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); |
334 | action_open_->setObjectName(QString::fromUtf8("actionOpen")); | |
335 | menu_file->addAction(action_open_); | |
009e1503 | 336 | |
69654681 JH |
337 | action_save_as_->setText(tr("&Save As...")); |
338 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
0fbda3c2 | 339 | QIcon(":/icons/document-save-as.png"))); |
69654681 JH |
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_); | |
0fbda3c2 | 343 | |
ced1cc13 | 344 | menu_file->addSeparator(); |
2a032dcb | 345 | |
998b89fd JH |
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 | ||
ed43ef2e JH |
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 | ||
998b89fd JH |
362 | menu_file->addSeparator(); |
363 | ||
69654681 JH |
364 | action_connect_->setText(tr("&Connect to Device...")); |
365 | action_connect_->setObjectName(QString::fromUtf8("actionConnect")); | |
366 | menu_file->addAction(action_connect_); | |
9663c82b | 367 | |
ced1cc13 | 368 | menu_file->addSeparator(); |
9663c82b | 369 | |
69654681 JH |
370 | action_quit_->setText(tr("&Quit")); |
371 | action_quit_->setIcon(QIcon::fromTheme("application-exit", | |
2a032dcb | 372 | QIcon(":/icons/application-exit.png"))); |
69654681 JH |
373 | action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
374 | action_quit_->setObjectName(QString::fromUtf8("actionQuit")); | |
375 | menu_file->addAction(action_quit_); | |
2a032dcb | 376 | |
ab1d13ee | 377 | // View Menu |
ced1cc13 | 378 | QMenu *menu_view = new QMenu; |
0a9fdca5 | 379 | menu_view->setTitle(tr("&View")); |
ab1d13ee | 380 | |
69654681 JH |
381 | action_view_zoom_in_->setText(tr("Zoom &In")); |
382 | action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", | |
ab1d13ee | 383 | QIcon(":/icons/zoom-in.png"))); |
d2899ad5 | 384 | // simply using Qt::Key_Plus shows no + in the menu |
69654681 JH |
385 | action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); |
386 | action_view_zoom_in_->setObjectName( | |
ab1d13ee | 387 | QString::fromUtf8("actionViewZoomIn")); |
69654681 | 388 | menu_view->addAction(action_view_zoom_in_); |
ab1d13ee | 389 | |
69654681 JH |
390 | action_view_zoom_out_->setText(tr("Zoom &Out")); |
391 | action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", | |
ab1d13ee | 392 | QIcon(":/icons/zoom-out.png"))); |
69654681 JH |
393 | action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); |
394 | action_view_zoom_out_->setObjectName( | |
ab1d13ee | 395 | QString::fromUtf8("actionViewZoomOut")); |
69654681 | 396 | menu_view->addAction(action_view_zoom_out_); |
ab1d13ee | 397 | |
69654681 JH |
398 | action_view_zoom_fit_->setText(tr("Zoom to &Fit")); |
399 | action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", | |
ca46b534 | 400 | QIcon(":/icons/zoom-fit.png"))); |
69654681 JH |
401 | action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); |
402 | action_view_zoom_fit_->setObjectName( | |
ca46b534 | 403 | QString::fromUtf8("actionViewZoomFit")); |
69654681 | 404 | menu_view->addAction(action_view_zoom_fit_); |
ca46b534 | 405 | |
1c4c23b8 | 406 | action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); |
69654681 | 407 | action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", |
d1e7d82c | 408 | QIcon(":/icons/zoom-original.png"))); |
69654681 JH |
409 | action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); |
410 | action_view_zoom_one_to_one_->setObjectName( | |
d1e7d82c | 411 | QString::fromUtf8("actionViewZoomOneToOne")); |
69654681 | 412 | menu_view->addAction(action_view_zoom_one_to_one_); |
d1e7d82c | 413 | |
ced1cc13 | 414 | menu_view->addSeparator(); |
ab1d13ee | 415 | |
69654681 JH |
416 | action_view_show_cursors_->setCheckable(true); |
417 | action_view_show_cursors_->setChecked(view_->cursors_shown()); | |
587f1d15 JH |
418 | action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", |
419 | QIcon(":/icons/show-cursors.svg"))); | |
69654681 JH |
420 | action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); |
421 | action_view_show_cursors_->setObjectName( | |
ab1d13ee | 422 | QString::fromUtf8("actionViewShowCursors")); |
69654681 JH |
423 | action_view_show_cursors_->setText(tr("Show &Cursors")); |
424 | menu_view->addAction(action_view_show_cursors_); | |
a429590b | 425 | |
7dd093df | 426 | // Decoders Menu |
269528f5 | 427 | #ifdef ENABLE_DECODE |
ced1cc13 | 428 | QMenu *const menu_decoders = new QMenu; |
0a9fdca5 | 429 | menu_decoders->setTitle(tr("&Decoders")); |
7dd093df | 430 | |
0f90452b JH |
431 | menu_decoders_add_->setTitle(tr("&Add")); |
432 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
f0d37dab | 433 | this, SLOT(add_decoder(srd_decoder*))); |
7dd093df | 434 | |
0f90452b | 435 | menu_decoders->addMenu(menu_decoders_add_); |
269528f5 | 436 | #endif |
7dd093df | 437 | |
ab1d13ee | 438 | // Help Menu |
ced1cc13 | 439 | QMenu *const menu_help = new QMenu; |
0a9fdca5 | 440 | menu_help->setTitle(tr("&Help")); |
ab1d13ee | 441 | |
69654681 JH |
442 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); |
443 | action_about_->setText(tr("&About...")); | |
444 | menu_help->addAction(action_about_); | |
7d5425ef | 445 | |
ced1cc13 JH |
446 | menu_bar->addAction(menu_file->menuAction()); |
447 | menu_bar->addAction(menu_view->menuAction()); | |
269528f5 | 448 | #ifdef ENABLE_DECODE |
ced1cc13 | 449 | menu_bar->addAction(menu_decoders->menuAction()); |
269528f5 | 450 | #endif |
ced1cc13 | 451 | menu_bar->addAction(menu_help->menuAction()); |
7d5425ef | 452 | |
ced1cc13 | 453 | setMenuBar(menu_bar); |
7d5425ef JH |
454 | QMetaObject::connectSlotsByName(this); |
455 | ||
b4543062 SA |
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 | ||
d2f13e3e | 460 | // Setup the toolbar |
7c657094 | 461 | main_bar_ = new toolbars::MainBar(session_, *this); |
5ac961e3 JH |
462 | |
463 | // Populate the device list and select the initially selected device | |
107ca6d3 | 464 | update_device_list(); |
5ac961e3 | 465 | |
7c657094 | 466 | addToolBar(main_bar_); |
d4984fe7 | 467 | |
5ac961e3 | 468 | // Set the title |
0a9fdca5 | 469 | setWindowTitle(tr("PulseView")); |
a8d3fb2d | 470 | |
8dbbc7f0 JH |
471 | // Setup session_ events |
472 | connect(&session_, SIGNAL(capture_state_changed(int)), this, | |
6ac96c2e | 473 | SLOT(capture_state_changed(int))); |
20f81a58 JH |
474 | connect(&session_, SIGNAL(device_selected()), this, |
475 | SLOT(device_selected())); | |
d7bed479 | 476 | } |
30236a54 | 477 | |
e3c79b07 JH |
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 | ||
93f683ad SA |
531 | void MainWindow::save_ui_settings() |
532 | { | |
39eb0d45 | 533 | QSettings settings; |
93f683ad | 534 | |
6842b5fc SA |
535 | map<string, string> dev_info; |
536 | list<string> key_list; | |
537 | ||
93f683ad SA |
538 | settings.beginGroup("MainWindow"); |
539 | settings.setValue("state", saveState()); | |
540 | settings.setValue("geometry", saveGeometry()); | |
541 | settings.endGroup(); | |
6842b5fc | 542 | |
8dbbc7f0 | 543 | if (session_.device()) { |
6842b5fc SA |
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 | ||
8dbbc7f0 JH |
551 | dev_info = device_manager_.get_device_info( |
552 | session_.device()); | |
6842b5fc SA |
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 | } | |
93f683ad SA |
565 | } |
566 | ||
567 | void MainWindow::restore_ui_settings() | |
568 | { | |
39eb0d45 | 569 | QSettings settings; |
93f683ad SA |
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 | ||
f2edb557 JH |
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 | ||
d873f4d6 | 590 | void MainWindow::update_device_list() |
107ca6d3 | 591 | { |
079d39ea | 592 | main_bar_->update_device_list(); |
107ca6d3 JH |
593 | } |
594 | ||
dd3fd4df JH |
595 | void MainWindow::load_file(QString file_name, |
596 | std::shared_ptr<sigrok::InputFormat> format, | |
597 | const std::map<std::string, Glib::VariantBase> &options) | |
1d478458 | 598 | { |
f2edb557 JH |
599 | const QString errorMessage( |
600 | QString("Failed to load file %1").arg(file_name)); | |
601 | const QString infoMessage; | |
ae2d1bc5 JH |
602 | |
603 | try { | |
dd3fd4df JH |
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()))); | |
e8d00928 ML |
615 | } catch(Error e) { |
616 | show_session_error(tr("Failed to load ") + file_name, e.what()); | |
8dbbc7f0 | 617 | session_.set_default_device(); |
6fd0b639 JH |
618 | update_device_list(); |
619 | return; | |
ae2d1bc5 JH |
620 | } |
621 | ||
a5ea63c2 JH |
622 | update_device_list(); |
623 | ||
8dbbc7f0 | 624 | session_.start_capture([&, errorMessage, infoMessage](QString) { |
6db73158 | 625 | session_error(errorMessage, infoMessage); }); |
f2edb557 JH |
626 | } |
627 | ||
e3c79b07 JH |
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 | ||
f2edb557 JH |
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(); | |
1d478458 JH |
652 | } |
653 | ||
2953961c JH |
654 | void MainWindow::on_actionOpen_triggered() |
655 | { | |
643f65f9 JS |
656 | QSettings settings; |
657 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
658 | ||
1d43d767 | 659 | // Show the dialog |
1d478458 | 660 | const QString file_name = QFileDialog::getOpenFileName( |
643f65f9 | 661 | this, tr("Open File"), dir, tr( |
6866d5de JH |
662 | "Sigrok Sessions (*.sr);;" |
663 | "All Files (*.*)")); | |
643f65f9 JS |
664 | |
665 | if (!file_name.isEmpty()) { | |
92b139d0 | 666 | load_file(file_name); |
643f65f9 JS |
667 | |
668 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
669 | settings.setValue(SettingOpenDirectory, abs_path); | |
670 | } | |
2953961c JH |
671 | } |
672 | ||
0fbda3c2 JH |
673 | void MainWindow::on_actionSaveAs_triggered() |
674 | { | |
998b89fd | 675 | export_file(device_manager_.context()->output_formats()["srzip"]); |
0fbda3c2 JH |
676 | } |
677 | ||
9663c82b JH |
678 | void MainWindow::on_actionConnect_triggered() |
679 | { | |
b99cfc42 | 680 | // Stop any currently running capture session |
8dbbc7f0 | 681 | session_.stop_capture(); |
b99cfc42 | 682 | |
8dbbc7f0 | 683 | dialogs::Connect dlg(this, device_manager_); |
5eb0fa13 | 684 | |
dc0867ff JH |
685 | // If the user selected a device, select it in the device list. Select the |
686 | // current device otherwise. | |
d873f4d6 | 687 | if (dlg.exec()) |
51cf49fe | 688 | select_device(dlg.get_selected_device()); |
dc0867ff | 689 | |
d873f4d6 | 690 | update_device_list(); |
9663c82b JH |
691 | } |
692 | ||
2a032dcb JH |
693 | void MainWindow::on_actionQuit_triggered() |
694 | { | |
695 | close(); | |
696 | } | |
697 | ||
a429590b JH |
698 | void MainWindow::on_actionViewZoomIn_triggered() |
699 | { | |
8dbbc7f0 | 700 | view_->zoom(1); |
a429590b JH |
701 | } |
702 | ||
703 | void MainWindow::on_actionViewZoomOut_triggered() | |
704 | { | |
8dbbc7f0 | 705 | view_->zoom(-1); |
a429590b JH |
706 | } |
707 | ||
ca46b534 JH |
708 | void MainWindow::on_actionViewZoomFit_triggered() |
709 | { | |
8dbbc7f0 | 710 | view_->zoom_fit(); |
ca46b534 JH |
711 | } |
712 | ||
d1e7d82c JH |
713 | void MainWindow::on_actionViewZoomOneToOne_triggered() |
714 | { | |
8dbbc7f0 | 715 | view_->zoom_one_to_one(); |
d1e7d82c JH |
716 | } |
717 | ||
e072efc8 JH |
718 | void MainWindow::on_actionViewShowCursors_triggered() |
719 | { | |
8dbbc7f0 | 720 | assert(view_); |
b4d91e56 | 721 | |
8dbbc7f0 | 722 | const bool show = !view_->cursors_shown(); |
b4d91e56 | 723 | if(show) |
8dbbc7f0 | 724 | view_->centre_cursors(); |
b4d91e56 | 725 | |
8dbbc7f0 | 726 | view_->show_cursors(show); |
e072efc8 JH |
727 | } |
728 | ||
30236a54 JH |
729 | void MainWindow::on_actionAbout_triggered() |
730 | { | |
8dbbc7f0 | 731 | dialogs::About dlg(device_manager_.context(), this); |
40eb2ff4 | 732 | dlg.exec(); |
30236a54 | 733 | } |
274d4f13 | 734 | |
f0d37dab | 735 | void MainWindow::add_decoder(srd_decoder *decoder) |
7dd093df | 736 | { |
269528f5 | 737 | #ifdef ENABLE_DECODE |
f0d37dab | 738 | assert(decoder); |
8dbbc7f0 | 739 | session_.add_decoder(decoder); |
269528f5 JH |
740 | #else |
741 | (void)decoder; | |
742 | #endif | |
7dd093df JH |
743 | } |
744 | ||
6ac96c2e JH |
745 | void MainWindow::capture_state_changed(int state) |
746 | { | |
7c657094 | 747 | main_bar_->set_capture_state((pv::Session::capture_state)state); |
6ac96c2e JH |
748 | } |
749 | ||
20f81a58 JH |
750 | void MainWindow::device_selected() |
751 | { | |
752 | // Set the title to include the device/file name | |
da30ecb7 | 753 | const shared_ptr<devices::Device> device = session_.device(); |
20f81a58 JH |
754 | if (!device) |
755 | return; | |
756 | ||
3084ed4b | 757 | const string display_name = device->display_name(device_manager_); |
20f81a58 JH |
758 | setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str())); |
759 | } | |
760 | ||
51e77110 | 761 | } // namespace pv |