]>
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 | |
819f4c25 | 71 | using std::list; |
6842b5fc | 72 | using std::map; |
f9abf97e | 73 | using std::shared_ptr; |
6842b5fc | 74 | using std::string; |
ea2dbfea JH |
75 | using std::vector; |
76 | ||
77 | using boost::algorithm::join; | |
30236a54 | 78 | |
e8d00928 | 79 | using sigrok::Error; |
998b89fd | 80 | using sigrok::OutputFormat; |
dd3fd4df | 81 | using sigrok::InputFormat; |
e8d00928 | 82 | |
51e77110 JH |
83 | namespace pv { |
84 | ||
b1264f56 | 85 | namespace view { |
26e3af6b | 86 | class ViewItem; |
b1264f56 JH |
87 | } |
88 | ||
643f65f9 JS |
89 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; |
90 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
91 | ||
107ca6d3 JH |
92 | MainWindow::MainWindow(DeviceManager &device_manager, |
93 | const char *open_file_name, | |
1d478458 | 94 | QWidget *parent) : |
107ca6d3 | 95 | QMainWindow(parent), |
8dbbc7f0 | 96 | device_manager_(device_manager), |
69654681 JH |
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)), | |
e79171dc BG |
107 | action_about_(new QAction(this)) |
108 | #ifdef ENABLE_DECODE | |
109 | , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) | |
19be0af1 | 110 | #endif |
d7bed479 | 111 | { |
7d5425ef | 112 | setup_ui(); |
93f683ad | 113 | restore_ui_settings(); |
1d478458 JH |
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 | } | |
7d5425ef JH |
120 | } |
121 | ||
69654681 JH |
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 | ||
e79171dc | 172 | #ifdef ENABLE_DECODE |
0f90452b JH |
173 | QMenu* MainWindow::menu_decoder_add() const |
174 | { | |
175 | return menu_decoders_add_; | |
176 | } | |
19be0af1 | 177 | #endif |
0f90452b | 178 | |
4e7f5ba8 JH |
179 | void MainWindow::run_stop() |
180 | { | |
181 | switch(session_.get_capture_state()) { | |
2b81ae46 | 182 | case Session::Stopped: |
4e7f5ba8 JH |
183 | session_.start_capture([&](QString message) { |
184 | session_error("Capture failed", message); }); | |
185 | break; | |
186 | ||
2b81ae46 JH |
187 | case Session::AwaitingTrigger: |
188 | case Session::Running: | |
4e7f5ba8 JH |
189 | session_.stop_capture(); |
190 | break; | |
191 | } | |
192 | } | |
193 | ||
da30ecb7 | 194 | void MainWindow::select_device(shared_ptr<devices::Device> device) |
51cf49fe JH |
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 | ||
998b89fd JH |
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 | ||
ea2dbfea JH |
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 | ||
e93f5538 | 230 | // Show the file dialog |
998b89fd | 231 | const QString file_name = QFileDialog::getSaveFileName( |
ea2dbfea | 232 | this, tr("Save File"), dir, filter); |
998b89fd JH |
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 | ||
e93f5538 JH |
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, | |
998b89fd JH |
253 | session_, this); |
254 | dlg->run(); | |
255 | } | |
256 | ||
ed43ef2e JH |
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 | ||
53d45c55 JH |
277 | if (file_name.isEmpty()) |
278 | return; | |
ed43ef2e | 279 | |
53d45c55 JH |
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(); | |
ed43ef2e | 290 | } |
53d45c55 JH |
291 | |
292 | load_file(file_name, format, options); | |
293 | ||
294 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
295 | settings.setValue(SettingOpenDirectory, abs_path); | |
ed43ef2e JH |
296 | } |
297 | ||
7d5425ef JH |
298 | void MainWindow::setup_ui() |
299 | { | |
300 | setObjectName(QString::fromUtf8("MainWindow")); | |
301 | ||
7d5425ef JH |
302 | // Set the window icon |
303 | QIcon icon; | |
4523c509 | 304 | icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); |
7d5425ef JH |
305 | setWindowIcon(icon); |
306 | ||
e072efc8 | 307 | // Setup the central widget |
8dbbc7f0 JH |
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_); | |
e072efc8 | 313 | |
8dbbc7f0 | 314 | view_ = new pv::view::View(session_, this); |
b1264f56 | 315 | |
8dbbc7f0 | 316 | vertical_layout_->addWidget(view_); |
e072efc8 | 317 | |
ab1d13ee | 318 | // Setup the menu bar |
1c4c23b8 JH |
319 | pv::widgets::HidingMenuBar *const menu_bar = |
320 | new pv::widgets::HidingMenuBar(this); | |
a429590b | 321 | |
ab1d13ee | 322 | // File Menu |
ced1cc13 | 323 | QMenu *const menu_file = new QMenu; |
0a9fdca5 | 324 | menu_file->setTitle(tr("&File")); |
e072efc8 | 325 | |
69654681 JH |
326 | action_open_->setText(tr("&Open...")); |
327 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
362eea96 | 328 | QIcon(":/icons/document-open.png"))); |
69654681 JH |
329 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); |
330 | action_open_->setObjectName(QString::fromUtf8("actionOpen")); | |
331 | menu_file->addAction(action_open_); | |
009e1503 | 332 | |
69654681 JH |
333 | action_save_as_->setText(tr("&Save As...")); |
334 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
0fbda3c2 | 335 | QIcon(":/icons/document-save-as.png"))); |
69654681 JH |
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_); | |
0fbda3c2 | 339 | |
ced1cc13 | 340 | menu_file->addSeparator(); |
2a032dcb | 341 | |
998b89fd JH |
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 | ||
ed43ef2e JH |
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 | ||
998b89fd JH |
358 | menu_file->addSeparator(); |
359 | ||
69654681 JH |
360 | action_connect_->setText(tr("&Connect to Device...")); |
361 | action_connect_->setObjectName(QString::fromUtf8("actionConnect")); | |
362 | menu_file->addAction(action_connect_); | |
9663c82b | 363 | |
ced1cc13 | 364 | menu_file->addSeparator(); |
9663c82b | 365 | |
69654681 JH |
366 | action_quit_->setText(tr("&Quit")); |
367 | action_quit_->setIcon(QIcon::fromTheme("application-exit", | |
2a032dcb | 368 | QIcon(":/icons/application-exit.png"))); |
69654681 JH |
369 | action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
370 | action_quit_->setObjectName(QString::fromUtf8("actionQuit")); | |
371 | menu_file->addAction(action_quit_); | |
2a032dcb | 372 | |
ab1d13ee | 373 | // View Menu |
ced1cc13 | 374 | QMenu *menu_view = new QMenu; |
0a9fdca5 | 375 | menu_view->setTitle(tr("&View")); |
ab1d13ee | 376 | |
69654681 JH |
377 | action_view_zoom_in_->setText(tr("Zoom &In")); |
378 | action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", | |
ab1d13ee | 379 | QIcon(":/icons/zoom-in.png"))); |
d2899ad5 | 380 | // simply using Qt::Key_Plus shows no + in the menu |
69654681 JH |
381 | action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); |
382 | action_view_zoom_in_->setObjectName( | |
ab1d13ee | 383 | QString::fromUtf8("actionViewZoomIn")); |
69654681 | 384 | menu_view->addAction(action_view_zoom_in_); |
ab1d13ee | 385 | |
69654681 JH |
386 | action_view_zoom_out_->setText(tr("Zoom &Out")); |
387 | action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", | |
ab1d13ee | 388 | QIcon(":/icons/zoom-out.png"))); |
69654681 JH |
389 | action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); |
390 | action_view_zoom_out_->setObjectName( | |
ab1d13ee | 391 | QString::fromUtf8("actionViewZoomOut")); |
69654681 | 392 | menu_view->addAction(action_view_zoom_out_); |
ab1d13ee | 393 | |
69654681 JH |
394 | action_view_zoom_fit_->setText(tr("Zoom to &Fit")); |
395 | action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", | |
ca46b534 | 396 | QIcon(":/icons/zoom-fit.png"))); |
69654681 JH |
397 | action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); |
398 | action_view_zoom_fit_->setObjectName( | |
ca46b534 | 399 | QString::fromUtf8("actionViewZoomFit")); |
69654681 | 400 | menu_view->addAction(action_view_zoom_fit_); |
ca46b534 | 401 | |
1c4c23b8 | 402 | action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); |
69654681 | 403 | action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", |
d1e7d82c | 404 | QIcon(":/icons/zoom-original.png"))); |
69654681 JH |
405 | action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); |
406 | action_view_zoom_one_to_one_->setObjectName( | |
d1e7d82c | 407 | QString::fromUtf8("actionViewZoomOneToOne")); |
69654681 | 408 | menu_view->addAction(action_view_zoom_one_to_one_); |
d1e7d82c | 409 | |
ced1cc13 | 410 | menu_view->addSeparator(); |
ab1d13ee | 411 | |
69654681 JH |
412 | action_view_show_cursors_->setCheckable(true); |
413 | action_view_show_cursors_->setChecked(view_->cursors_shown()); | |
587f1d15 JH |
414 | action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", |
415 | QIcon(":/icons/show-cursors.svg"))); | |
69654681 JH |
416 | action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); |
417 | action_view_show_cursors_->setObjectName( | |
ab1d13ee | 418 | QString::fromUtf8("actionViewShowCursors")); |
69654681 JH |
419 | action_view_show_cursors_->setText(tr("Show &Cursors")); |
420 | menu_view->addAction(action_view_show_cursors_); | |
a429590b | 421 | |
7dd093df | 422 | // Decoders Menu |
269528f5 | 423 | #ifdef ENABLE_DECODE |
ced1cc13 | 424 | QMenu *const menu_decoders = new QMenu; |
0a9fdca5 | 425 | menu_decoders->setTitle(tr("&Decoders")); |
7dd093df | 426 | |
0f90452b JH |
427 | menu_decoders_add_->setTitle(tr("&Add")); |
428 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
f0d37dab | 429 | this, SLOT(add_decoder(srd_decoder*))); |
7dd093df | 430 | |
0f90452b | 431 | menu_decoders->addMenu(menu_decoders_add_); |
269528f5 | 432 | #endif |
7dd093df | 433 | |
ab1d13ee | 434 | // Help Menu |
ced1cc13 | 435 | QMenu *const menu_help = new QMenu; |
0a9fdca5 | 436 | menu_help->setTitle(tr("&Help")); |
ab1d13ee | 437 | |
69654681 JH |
438 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); |
439 | action_about_->setText(tr("&About...")); | |
440 | menu_help->addAction(action_about_); | |
7d5425ef | 441 | |
ced1cc13 JH |
442 | menu_bar->addAction(menu_file->menuAction()); |
443 | menu_bar->addAction(menu_view->menuAction()); | |
269528f5 | 444 | #ifdef ENABLE_DECODE |
ced1cc13 | 445 | menu_bar->addAction(menu_decoders->menuAction()); |
269528f5 | 446 | #endif |
ced1cc13 | 447 | menu_bar->addAction(menu_help->menuAction()); |
7d5425ef | 448 | |
ced1cc13 | 449 | setMenuBar(menu_bar); |
7d5425ef JH |
450 | QMetaObject::connectSlotsByName(this); |
451 | ||
d2f13e3e | 452 | // Setup the toolbar |
7c657094 | 453 | main_bar_ = new toolbars::MainBar(session_, *this); |
5ac961e3 JH |
454 | |
455 | // Populate the device list and select the initially selected device | |
107ca6d3 | 456 | update_device_list(); |
5ac961e3 | 457 | |
7c657094 | 458 | addToolBar(main_bar_); |
d4984fe7 | 459 | |
5ac961e3 | 460 | // Set the title |
0a9fdca5 | 461 | setWindowTitle(tr("PulseView")); |
a8d3fb2d | 462 | |
8dbbc7f0 JH |
463 | // Setup session_ events |
464 | connect(&session_, SIGNAL(capture_state_changed(int)), this, | |
6ac96c2e | 465 | SLOT(capture_state_changed(int))); |
20f81a58 JH |
466 | connect(&session_, SIGNAL(device_selected()), this, |
467 | SLOT(device_selected())); | |
d7bed479 | 468 | } |
30236a54 | 469 | |
93f683ad SA |
470 | void MainWindow::save_ui_settings() |
471 | { | |
39eb0d45 | 472 | QSettings settings; |
93f683ad | 473 | |
6842b5fc SA |
474 | map<string, string> dev_info; |
475 | list<string> key_list; | |
476 | ||
93f683ad SA |
477 | settings.beginGroup("MainWindow"); |
478 | settings.setValue("state", saveState()); | |
479 | settings.setValue("geometry", saveGeometry()); | |
480 | settings.endGroup(); | |
6842b5fc | 481 | |
8dbbc7f0 | 482 | if (session_.device()) { |
6842b5fc SA |
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 | ||
8dbbc7f0 JH |
490 | dev_info = device_manager_.get_device_info( |
491 | session_.device()); | |
6842b5fc SA |
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 | } | |
93f683ad SA |
504 | } |
505 | ||
506 | void MainWindow::restore_ui_settings() | |
507 | { | |
39eb0d45 | 508 | QSettings settings; |
93f683ad | 509 | |
6842b5fc SA |
510 | map<string, string> dev_info; |
511 | list<string> key_list; | |
6842b5fc | 512 | |
93f683ad SA |
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(); | |
6842b5fc SA |
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) { | |
951b5ae7 JH |
532 | const QString k = QString::fromStdString(key); |
533 | if (!settings.contains(k)) | |
6842b5fc SA |
534 | continue; |
535 | ||
951b5ae7 JH |
536 | const string value = settings.value(k).toString().toStdString(); |
537 | if (!value.empty()) | |
6842b5fc SA |
538 | dev_info.insert(std::make_pair(key, value)); |
539 | } | |
540 | ||
da30ecb7 JH |
541 | const shared_ptr<devices::HardwareDevice> device = |
542 | device_manager_.find_device_from_info(dev_info); | |
6842b5fc | 543 | if (device) { |
51cf49fe | 544 | select_device(device); |
6842b5fc SA |
545 | update_device_list(); |
546 | } | |
547 | ||
548 | settings.endGroup(); | |
93f683ad SA |
549 | } |
550 | ||
f2edb557 JH |
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 | ||
d873f4d6 | 559 | void MainWindow::update_device_list() |
107ca6d3 | 560 | { |
079d39ea | 561 | main_bar_->update_device_list(); |
107ca6d3 JH |
562 | } |
563 | ||
93f683ad SA |
564 | void MainWindow::closeEvent(QCloseEvent *event) |
565 | { | |
566 | save_ui_settings(); | |
567 | event->accept(); | |
568 | } | |
569 | ||
1c4c23b8 JH |
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 | ||
dd3fd4df JH |
579 | void MainWindow::load_file(QString file_name, |
580 | std::shared_ptr<sigrok::InputFormat> format, | |
581 | const std::map<std::string, Glib::VariantBase> &options) | |
1d478458 | 582 | { |
f2edb557 JH |
583 | const QString errorMessage( |
584 | QString("Failed to load file %1").arg(file_name)); | |
585 | const QString infoMessage; | |
ae2d1bc5 JH |
586 | |
587 | try { | |
dd3fd4df JH |
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()))); | |
e8d00928 ML |
599 | } catch(Error e) { |
600 | show_session_error(tr("Failed to load ") + file_name, e.what()); | |
8dbbc7f0 | 601 | session_.set_default_device(); |
6fd0b639 JH |
602 | update_device_list(); |
603 | return; | |
ae2d1bc5 JH |
604 | } |
605 | ||
a5ea63c2 JH |
606 | update_device_list(); |
607 | ||
8dbbc7f0 | 608 | session_.start_capture([&, errorMessage, infoMessage](QString) { |
6db73158 | 609 | session_error(errorMessage, infoMessage); }); |
f2edb557 JH |
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(); | |
1d478458 JH |
621 | } |
622 | ||
2953961c JH |
623 | void MainWindow::on_actionOpen_triggered() |
624 | { | |
643f65f9 JS |
625 | QSettings settings; |
626 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
627 | ||
1d43d767 | 628 | // Show the dialog |
1d478458 | 629 | const QString file_name = QFileDialog::getOpenFileName( |
643f65f9 | 630 | this, tr("Open File"), dir, tr( |
6866d5de JH |
631 | "Sigrok Sessions (*.sr);;" |
632 | "All Files (*.*)")); | |
643f65f9 JS |
633 | |
634 | if (!file_name.isEmpty()) { | |
92b139d0 | 635 | load_file(file_name); |
643f65f9 JS |
636 | |
637 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
638 | settings.setValue(SettingOpenDirectory, abs_path); | |
639 | } | |
2953961c JH |
640 | } |
641 | ||
0fbda3c2 JH |
642 | void MainWindow::on_actionSaveAs_triggered() |
643 | { | |
998b89fd | 644 | export_file(device_manager_.context()->output_formats()["srzip"]); |
0fbda3c2 JH |
645 | } |
646 | ||
9663c82b JH |
647 | void MainWindow::on_actionConnect_triggered() |
648 | { | |
b99cfc42 | 649 | // Stop any currently running capture session |
8dbbc7f0 | 650 | session_.stop_capture(); |
b99cfc42 | 651 | |
8dbbc7f0 | 652 | dialogs::Connect dlg(this, device_manager_); |
5eb0fa13 | 653 | |
dc0867ff JH |
654 | // If the user selected a device, select it in the device list. Select the |
655 | // current device otherwise. | |
d873f4d6 | 656 | if (dlg.exec()) |
51cf49fe | 657 | select_device(dlg.get_selected_device()); |
dc0867ff | 658 | |
d873f4d6 | 659 | update_device_list(); |
9663c82b JH |
660 | } |
661 | ||
2a032dcb JH |
662 | void MainWindow::on_actionQuit_triggered() |
663 | { | |
664 | close(); | |
665 | } | |
666 | ||
a429590b JH |
667 | void MainWindow::on_actionViewZoomIn_triggered() |
668 | { | |
8dbbc7f0 | 669 | view_->zoom(1); |
a429590b JH |
670 | } |
671 | ||
672 | void MainWindow::on_actionViewZoomOut_triggered() | |
673 | { | |
8dbbc7f0 | 674 | view_->zoom(-1); |
a429590b JH |
675 | } |
676 | ||
ca46b534 JH |
677 | void MainWindow::on_actionViewZoomFit_triggered() |
678 | { | |
8dbbc7f0 | 679 | view_->zoom_fit(); |
ca46b534 JH |
680 | } |
681 | ||
d1e7d82c JH |
682 | void MainWindow::on_actionViewZoomOneToOne_triggered() |
683 | { | |
8dbbc7f0 | 684 | view_->zoom_one_to_one(); |
d1e7d82c JH |
685 | } |
686 | ||
e072efc8 JH |
687 | void MainWindow::on_actionViewShowCursors_triggered() |
688 | { | |
8dbbc7f0 | 689 | assert(view_); |
b4d91e56 | 690 | |
8dbbc7f0 | 691 | const bool show = !view_->cursors_shown(); |
b4d91e56 | 692 | if(show) |
8dbbc7f0 | 693 | view_->centre_cursors(); |
b4d91e56 | 694 | |
8dbbc7f0 | 695 | view_->show_cursors(show); |
e072efc8 JH |
696 | } |
697 | ||
30236a54 JH |
698 | void MainWindow::on_actionAbout_triggered() |
699 | { | |
8dbbc7f0 | 700 | dialogs::About dlg(device_manager_.context(), this); |
40eb2ff4 | 701 | dlg.exec(); |
30236a54 | 702 | } |
274d4f13 | 703 | |
f0d37dab | 704 | void MainWindow::add_decoder(srd_decoder *decoder) |
7dd093df | 705 | { |
269528f5 | 706 | #ifdef ENABLE_DECODE |
f0d37dab | 707 | assert(decoder); |
8dbbc7f0 | 708 | session_.add_decoder(decoder); |
269528f5 JH |
709 | #else |
710 | (void)decoder; | |
711 | #endif | |
7dd093df JH |
712 | } |
713 | ||
6ac96c2e JH |
714 | void MainWindow::capture_state_changed(int state) |
715 | { | |
7c657094 | 716 | main_bar_->set_capture_state((pv::Session::capture_state)state); |
6ac96c2e JH |
717 | } |
718 | ||
20f81a58 JH |
719 | void MainWindow::device_selected() |
720 | { | |
721 | // Set the title to include the device/file name | |
da30ecb7 | 722 | const shared_ptr<devices::Device> device = session_.device(); |
20f81a58 JH |
723 | if (!device) |
724 | return; | |
725 | ||
3084ed4b | 726 | const string display_name = device->display_name(device_manager_); |
20f81a58 JH |
727 | setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str())); |
728 | } | |
729 | ||
51e77110 | 730 | } // namespace pv |