]>
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 { | |
859ca307 JH |
197 | if (device) |
198 | session_.set_device(device); | |
199 | else | |
200 | session_.set_default_device(); | |
51cf49fe JH |
201 | } catch(const QString &e) { |
202 | QMessageBox msg(this); | |
203 | msg.setText(e); | |
204 | msg.setInformativeText(tr("Failed to Select Device")); | |
205 | msg.setStandardButtons(QMessageBox::Ok); | |
206 | msg.setIcon(QMessageBox::Warning); | |
207 | msg.exec(); | |
208 | } | |
209 | } | |
210 | ||
998b89fd JH |
211 | void MainWindow::export_file(shared_ptr<OutputFormat> format) |
212 | { | |
213 | using pv::dialogs::StoreProgress; | |
214 | ||
215 | // Stop any currently running capture session | |
216 | session_.stop_capture(); | |
217 | ||
218 | QSettings settings; | |
219 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
220 | ||
ea2dbfea JH |
221 | // Construct the filter |
222 | const vector<string> exts = format->extensions(); | |
223 | QString filter = tr("%1 files ").arg( | |
224 | QString::fromStdString(format->description())); | |
225 | ||
226 | if (exts.empty()) | |
227 | filter += "(*.*)"; | |
228 | else | |
229 | filter += QString("(*.%1);;%2 (*.*)").arg( | |
230 | QString::fromStdString(join(exts, ", *."))).arg( | |
231 | tr("All Files")); | |
232 | ||
e93f5538 | 233 | // Show the file dialog |
998b89fd | 234 | const QString file_name = QFileDialog::getSaveFileName( |
ea2dbfea | 235 | this, tr("Save File"), dir, filter); |
998b89fd JH |
236 | |
237 | if (file_name.isEmpty()) | |
238 | return; | |
239 | ||
240 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
241 | settings.setValue(SettingSaveDirectory, abs_path); | |
242 | ||
e93f5538 JH |
243 | // Show the options dialog |
244 | map<string, Glib::VariantBase> options; | |
245 | if (!format->options().empty()) { | |
246 | dialogs::InputOutputOptions dlg( | |
247 | tr("Export %1").arg(QString::fromStdString( | |
248 | format->description())), | |
249 | format->options(), this); | |
250 | if (!dlg.exec()) | |
251 | return; | |
252 | options = dlg.options(); | |
253 | } | |
254 | ||
255 | StoreProgress *dlg = new StoreProgress(file_name, format, options, | |
998b89fd JH |
256 | session_, this); |
257 | dlg->run(); | |
258 | } | |
259 | ||
ed43ef2e JH |
260 | void MainWindow::import_file(shared_ptr<InputFormat> format) |
261 | { | |
262 | assert(format); | |
263 | ||
264 | QSettings settings; | |
265 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
266 | ||
267 | // Construct the filter | |
268 | const vector<string> exts = format->extensions(); | |
269 | const QString filter = exts.empty() ? "" : | |
270 | tr("%1 files (*.%2)").arg( | |
271 | QString::fromStdString(format->description())).arg( | |
272 | QString::fromStdString(join(exts, ", *."))); | |
273 | ||
274 | // Show the file dialog | |
275 | const QString file_name = QFileDialog::getOpenFileName( | |
276 | this, tr("Import File"), dir, tr( | |
277 | "%1 files (*.*);;All Files (*.*)").arg( | |
278 | QString::fromStdString(format->description()))); | |
279 | ||
53d45c55 JH |
280 | if (file_name.isEmpty()) |
281 | return; | |
ed43ef2e | 282 | |
53d45c55 JH |
283 | // Show the options dialog |
284 | map<string, Glib::VariantBase> options; | |
285 | if (!format->options().empty()) { | |
286 | dialogs::InputOutputOptions dlg( | |
287 | tr("Import %1").arg(QString::fromStdString( | |
288 | format->description())), | |
289 | format->options(), this); | |
290 | if (!dlg.exec()) | |
291 | return; | |
292 | options = dlg.options(); | |
ed43ef2e | 293 | } |
53d45c55 JH |
294 | |
295 | load_file(file_name, format, options); | |
296 | ||
297 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
298 | settings.setValue(SettingOpenDirectory, abs_path); | |
ed43ef2e JH |
299 | } |
300 | ||
7d5425ef JH |
301 | void MainWindow::setup_ui() |
302 | { | |
303 | setObjectName(QString::fromUtf8("MainWindow")); | |
304 | ||
7d5425ef JH |
305 | // Set the window icon |
306 | QIcon icon; | |
4523c509 | 307 | icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); |
7d5425ef JH |
308 | setWindowIcon(icon); |
309 | ||
e072efc8 | 310 | // Setup the central widget |
8dbbc7f0 JH |
311 | central_widget_ = new QWidget(this); |
312 | vertical_layout_ = new QVBoxLayout(central_widget_); | |
313 | vertical_layout_->setSpacing(6); | |
314 | vertical_layout_->setContentsMargins(0, 0, 0, 0); | |
315 | setCentralWidget(central_widget_); | |
e072efc8 | 316 | |
8dbbc7f0 | 317 | view_ = new pv::view::View(session_, this); |
b1264f56 | 318 | |
8dbbc7f0 | 319 | vertical_layout_->addWidget(view_); |
e072efc8 | 320 | |
ab1d13ee | 321 | // Setup the menu bar |
1c4c23b8 JH |
322 | pv::widgets::HidingMenuBar *const menu_bar = |
323 | new pv::widgets::HidingMenuBar(this); | |
a429590b | 324 | |
ab1d13ee | 325 | // File Menu |
ced1cc13 | 326 | QMenu *const menu_file = new QMenu; |
0a9fdca5 | 327 | menu_file->setTitle(tr("&File")); |
e072efc8 | 328 | |
69654681 JH |
329 | action_open_->setText(tr("&Open...")); |
330 | action_open_->setIcon(QIcon::fromTheme("document-open", | |
362eea96 | 331 | QIcon(":/icons/document-open.png"))); |
69654681 JH |
332 | action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); |
333 | action_open_->setObjectName(QString::fromUtf8("actionOpen")); | |
334 | menu_file->addAction(action_open_); | |
009e1503 | 335 | |
69654681 JH |
336 | action_save_as_->setText(tr("&Save As...")); |
337 | action_save_as_->setIcon(QIcon::fromTheme("document-save-as", | |
0fbda3c2 | 338 | QIcon(":/icons/document-save-as.png"))); |
69654681 JH |
339 | action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); |
340 | action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs")); | |
341 | menu_file->addAction(action_save_as_); | |
0fbda3c2 | 342 | |
ced1cc13 | 343 | menu_file->addSeparator(); |
2a032dcb | 344 | |
998b89fd JH |
345 | widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, |
346 | device_manager_.context()); | |
347 | menu_file_export->setTitle(tr("&Export")); | |
348 | connect(menu_file_export, | |
349 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
350 | this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
351 | menu_file->addAction(menu_file_export->menuAction()); | |
352 | ||
ed43ef2e JH |
353 | widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this, |
354 | device_manager_.context()); | |
355 | menu_file_import->setTitle(tr("&Import")); | |
356 | connect(menu_file_import, | |
357 | SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)), | |
358 | this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>))); | |
359 | menu_file->addAction(menu_file_import->menuAction()); | |
360 | ||
998b89fd JH |
361 | menu_file->addSeparator(); |
362 | ||
69654681 JH |
363 | action_connect_->setText(tr("&Connect to Device...")); |
364 | action_connect_->setObjectName(QString::fromUtf8("actionConnect")); | |
365 | menu_file->addAction(action_connect_); | |
9663c82b | 366 | |
ced1cc13 | 367 | menu_file->addSeparator(); |
9663c82b | 368 | |
69654681 JH |
369 | action_quit_->setText(tr("&Quit")); |
370 | action_quit_->setIcon(QIcon::fromTheme("application-exit", | |
2a032dcb | 371 | QIcon(":/icons/application-exit.png"))); |
69654681 JH |
372 | action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); |
373 | action_quit_->setObjectName(QString::fromUtf8("actionQuit")); | |
374 | menu_file->addAction(action_quit_); | |
2a032dcb | 375 | |
ab1d13ee | 376 | // View Menu |
ced1cc13 | 377 | QMenu *menu_view = new QMenu; |
0a9fdca5 | 378 | menu_view->setTitle(tr("&View")); |
ab1d13ee | 379 | |
69654681 JH |
380 | action_view_zoom_in_->setText(tr("Zoom &In")); |
381 | action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in", | |
ab1d13ee | 382 | QIcon(":/icons/zoom-in.png"))); |
d2899ad5 | 383 | // simply using Qt::Key_Plus shows no + in the menu |
69654681 JH |
384 | action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn); |
385 | action_view_zoom_in_->setObjectName( | |
ab1d13ee | 386 | QString::fromUtf8("actionViewZoomIn")); |
69654681 | 387 | menu_view->addAction(action_view_zoom_in_); |
ab1d13ee | 388 | |
69654681 JH |
389 | action_view_zoom_out_->setText(tr("Zoom &Out")); |
390 | action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out", | |
ab1d13ee | 391 | QIcon(":/icons/zoom-out.png"))); |
69654681 JH |
392 | action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut); |
393 | action_view_zoom_out_->setObjectName( | |
ab1d13ee | 394 | QString::fromUtf8("actionViewZoomOut")); |
69654681 | 395 | menu_view->addAction(action_view_zoom_out_); |
ab1d13ee | 396 | |
69654681 JH |
397 | action_view_zoom_fit_->setText(tr("Zoom to &Fit")); |
398 | action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", | |
ca46b534 | 399 | QIcon(":/icons/zoom-fit.png"))); |
69654681 JH |
400 | action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F)); |
401 | action_view_zoom_fit_->setObjectName( | |
ca46b534 | 402 | QString::fromUtf8("actionViewZoomFit")); |
69654681 | 403 | menu_view->addAction(action_view_zoom_fit_); |
ca46b534 | 404 | |
1c4c23b8 | 405 | action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One")); |
69654681 | 406 | action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original", |
d1e7d82c | 407 | QIcon(":/icons/zoom-original.png"))); |
69654681 JH |
408 | action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O)); |
409 | action_view_zoom_one_to_one_->setObjectName( | |
d1e7d82c | 410 | QString::fromUtf8("actionViewZoomOneToOne")); |
69654681 | 411 | menu_view->addAction(action_view_zoom_one_to_one_); |
d1e7d82c | 412 | |
ced1cc13 | 413 | menu_view->addSeparator(); |
ab1d13ee | 414 | |
69654681 JH |
415 | action_view_show_cursors_->setCheckable(true); |
416 | action_view_show_cursors_->setChecked(view_->cursors_shown()); | |
587f1d15 JH |
417 | action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", |
418 | QIcon(":/icons/show-cursors.svg"))); | |
69654681 JH |
419 | action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); |
420 | action_view_show_cursors_->setObjectName( | |
ab1d13ee | 421 | QString::fromUtf8("actionViewShowCursors")); |
69654681 JH |
422 | action_view_show_cursors_->setText(tr("Show &Cursors")); |
423 | menu_view->addAction(action_view_show_cursors_); | |
a429590b | 424 | |
7dd093df | 425 | // Decoders Menu |
269528f5 | 426 | #ifdef ENABLE_DECODE |
ced1cc13 | 427 | QMenu *const menu_decoders = new QMenu; |
0a9fdca5 | 428 | menu_decoders->setTitle(tr("&Decoders")); |
7dd093df | 429 | |
0f90452b JH |
430 | menu_decoders_add_->setTitle(tr("&Add")); |
431 | connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)), | |
f0d37dab | 432 | this, SLOT(add_decoder(srd_decoder*))); |
7dd093df | 433 | |
0f90452b | 434 | menu_decoders->addMenu(menu_decoders_add_); |
269528f5 | 435 | #endif |
7dd093df | 436 | |
ab1d13ee | 437 | // Help Menu |
ced1cc13 | 438 | QMenu *const menu_help = new QMenu; |
0a9fdca5 | 439 | menu_help->setTitle(tr("&Help")); |
ab1d13ee | 440 | |
69654681 JH |
441 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); |
442 | action_about_->setText(tr("&About...")); | |
443 | menu_help->addAction(action_about_); | |
7d5425ef | 444 | |
ced1cc13 JH |
445 | menu_bar->addAction(menu_file->menuAction()); |
446 | menu_bar->addAction(menu_view->menuAction()); | |
269528f5 | 447 | #ifdef ENABLE_DECODE |
ced1cc13 | 448 | menu_bar->addAction(menu_decoders->menuAction()); |
269528f5 | 449 | #endif |
ced1cc13 | 450 | menu_bar->addAction(menu_help->menuAction()); |
7d5425ef | 451 | |
ced1cc13 | 452 | setMenuBar(menu_bar); |
7d5425ef JH |
453 | QMetaObject::connectSlotsByName(this); |
454 | ||
d2f13e3e | 455 | // Setup the toolbar |
7c657094 | 456 | main_bar_ = new toolbars::MainBar(session_, *this); |
5ac961e3 JH |
457 | |
458 | // Populate the device list and select the initially selected device | |
107ca6d3 | 459 | update_device_list(); |
5ac961e3 | 460 | |
7c657094 | 461 | addToolBar(main_bar_); |
d4984fe7 | 462 | |
5ac961e3 | 463 | // Set the title |
0a9fdca5 | 464 | setWindowTitle(tr("PulseView")); |
a8d3fb2d | 465 | |
8dbbc7f0 JH |
466 | // Setup session_ events |
467 | connect(&session_, SIGNAL(capture_state_changed(int)), this, | |
6ac96c2e | 468 | SLOT(capture_state_changed(int))); |
20f81a58 JH |
469 | connect(&session_, SIGNAL(device_selected()), this, |
470 | SLOT(device_selected())); | |
d7bed479 | 471 | } |
30236a54 | 472 | |
93f683ad SA |
473 | void MainWindow::save_ui_settings() |
474 | { | |
39eb0d45 | 475 | QSettings settings; |
93f683ad | 476 | |
6842b5fc SA |
477 | map<string, string> dev_info; |
478 | list<string> key_list; | |
479 | ||
93f683ad SA |
480 | settings.beginGroup("MainWindow"); |
481 | settings.setValue("state", saveState()); | |
482 | settings.setValue("geometry", saveGeometry()); | |
483 | settings.endGroup(); | |
6842b5fc | 484 | |
8dbbc7f0 | 485 | if (session_.device()) { |
6842b5fc SA |
486 | settings.beginGroup("Device"); |
487 | key_list.push_back("vendor"); | |
488 | key_list.push_back("model"); | |
489 | key_list.push_back("version"); | |
490 | key_list.push_back("serial_num"); | |
491 | key_list.push_back("connection_id"); | |
492 | ||
8dbbc7f0 JH |
493 | dev_info = device_manager_.get_device_info( |
494 | session_.device()); | |
6842b5fc SA |
495 | |
496 | for (string key : key_list) { | |
497 | ||
498 | if (dev_info.count(key)) | |
499 | settings.setValue(QString::fromUtf8(key.c_str()), | |
500 | QString::fromUtf8(dev_info.at(key).c_str())); | |
501 | else | |
502 | settings.remove(QString::fromUtf8(key.c_str())); | |
503 | } | |
504 | ||
505 | settings.endGroup(); | |
506 | } | |
93f683ad SA |
507 | } |
508 | ||
509 | void MainWindow::restore_ui_settings() | |
510 | { | |
39eb0d45 | 511 | QSettings settings; |
93f683ad | 512 | |
6842b5fc SA |
513 | map<string, string> dev_info; |
514 | list<string> key_list; | |
6842b5fc | 515 | |
93f683ad SA |
516 | settings.beginGroup("MainWindow"); |
517 | ||
518 | if (settings.contains("geometry")) { | |
519 | restoreGeometry(settings.value("geometry").toByteArray()); | |
520 | restoreState(settings.value("state").toByteArray()); | |
521 | } else | |
522 | resize(1000, 720); | |
523 | ||
524 | settings.endGroup(); | |
6842b5fc SA |
525 | |
526 | // Re-select last used device if possible. | |
527 | settings.beginGroup("Device"); | |
528 | key_list.push_back("vendor"); | |
529 | key_list.push_back("model"); | |
530 | key_list.push_back("version"); | |
531 | key_list.push_back("serial_num"); | |
532 | key_list.push_back("connection_id"); | |
533 | ||
534 | for (string key : key_list) { | |
951b5ae7 JH |
535 | const QString k = QString::fromStdString(key); |
536 | if (!settings.contains(k)) | |
6842b5fc SA |
537 | continue; |
538 | ||
951b5ae7 JH |
539 | const string value = settings.value(k).toString().toStdString(); |
540 | if (!value.empty()) | |
6842b5fc SA |
541 | dev_info.insert(std::make_pair(key, value)); |
542 | } | |
543 | ||
da30ecb7 JH |
544 | const shared_ptr<devices::HardwareDevice> device = |
545 | device_manager_.find_device_from_info(dev_info); | |
859ca307 JH |
546 | select_device(device); |
547 | update_device_list(); | |
6842b5fc SA |
548 | |
549 | settings.endGroup(); | |
93f683ad SA |
550 | } |
551 | ||
f2edb557 JH |
552 | void MainWindow::session_error( |
553 | const QString text, const QString info_text) | |
554 | { | |
555 | QMetaObject::invokeMethod(this, "show_session_error", | |
556 | Qt::QueuedConnection, Q_ARG(QString, text), | |
557 | Q_ARG(QString, info_text)); | |
558 | } | |
559 | ||
d873f4d6 | 560 | void MainWindow::update_device_list() |
107ca6d3 | 561 | { |
079d39ea | 562 | main_bar_->update_device_list(); |
107ca6d3 JH |
563 | } |
564 | ||
93f683ad SA |
565 | void MainWindow::closeEvent(QCloseEvent *event) |
566 | { | |
567 | save_ui_settings(); | |
568 | event->accept(); | |
569 | } | |
570 | ||
1c4c23b8 JH |
571 | void MainWindow::keyReleaseEvent(QKeyEvent *event) |
572 | { | |
573 | if (event->key() == Qt::Key_Alt) { | |
574 | menuBar()->setHidden(!menuBar()->isHidden()); | |
575 | menuBar()->setFocus(); | |
576 | } | |
577 | QMainWindow::keyReleaseEvent(event); | |
578 | } | |
579 | ||
dd3fd4df JH |
580 | void MainWindow::load_file(QString file_name, |
581 | std::shared_ptr<sigrok::InputFormat> format, | |
582 | const std::map<std::string, Glib::VariantBase> &options) | |
1d478458 | 583 | { |
f2edb557 JH |
584 | const QString errorMessage( |
585 | QString("Failed to load file %1").arg(file_name)); | |
586 | const QString infoMessage; | |
ae2d1bc5 JH |
587 | |
588 | try { | |
dd3fd4df JH |
589 | if (format) |
590 | session_.set_device(shared_ptr<devices::Device>( | |
591 | new devices::InputFile( | |
592 | device_manager_.context(), | |
593 | file_name.toStdString(), | |
594 | format, options))); | |
595 | else | |
596 | session_.set_device(shared_ptr<devices::Device>( | |
597 | new devices::SessionFile( | |
598 | device_manager_.context(), | |
599 | file_name.toStdString()))); | |
e8d00928 ML |
600 | } catch(Error e) { |
601 | show_session_error(tr("Failed to load ") + file_name, e.what()); | |
8dbbc7f0 | 602 | session_.set_default_device(); |
6fd0b639 JH |
603 | update_device_list(); |
604 | return; | |
ae2d1bc5 JH |
605 | } |
606 | ||
a5ea63c2 JH |
607 | update_device_list(); |
608 | ||
8dbbc7f0 | 609 | session_.start_capture([&, errorMessage, infoMessage](QString) { |
6db73158 | 610 | session_error(errorMessage, infoMessage); }); |
f2edb557 JH |
611 | } |
612 | ||
613 | void MainWindow::show_session_error( | |
614 | const QString text, const QString info_text) | |
615 | { | |
616 | QMessageBox msg(this); | |
617 | msg.setText(text); | |
618 | msg.setInformativeText(info_text); | |
619 | msg.setStandardButtons(QMessageBox::Ok); | |
620 | msg.setIcon(QMessageBox::Warning); | |
621 | msg.exec(); | |
1d478458 JH |
622 | } |
623 | ||
2953961c JH |
624 | void MainWindow::on_actionOpen_triggered() |
625 | { | |
643f65f9 JS |
626 | QSettings settings; |
627 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
628 | ||
1d43d767 | 629 | // Show the dialog |
1d478458 | 630 | const QString file_name = QFileDialog::getOpenFileName( |
643f65f9 | 631 | this, tr("Open File"), dir, tr( |
6866d5de JH |
632 | "Sigrok Sessions (*.sr);;" |
633 | "All Files (*.*)")); | |
643f65f9 JS |
634 | |
635 | if (!file_name.isEmpty()) { | |
92b139d0 | 636 | load_file(file_name); |
643f65f9 JS |
637 | |
638 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
639 | settings.setValue(SettingOpenDirectory, abs_path); | |
640 | } | |
2953961c JH |
641 | } |
642 | ||
0fbda3c2 JH |
643 | void MainWindow::on_actionSaveAs_triggered() |
644 | { | |
998b89fd | 645 | export_file(device_manager_.context()->output_formats()["srzip"]); |
0fbda3c2 JH |
646 | } |
647 | ||
9663c82b JH |
648 | void MainWindow::on_actionConnect_triggered() |
649 | { | |
b99cfc42 | 650 | // Stop any currently running capture session |
8dbbc7f0 | 651 | session_.stop_capture(); |
b99cfc42 | 652 | |
8dbbc7f0 | 653 | dialogs::Connect dlg(this, device_manager_); |
5eb0fa13 | 654 | |
dc0867ff JH |
655 | // If the user selected a device, select it in the device list. Select the |
656 | // current device otherwise. | |
d873f4d6 | 657 | if (dlg.exec()) |
51cf49fe | 658 | select_device(dlg.get_selected_device()); |
dc0867ff | 659 | |
d873f4d6 | 660 | update_device_list(); |
9663c82b JH |
661 | } |
662 | ||
2a032dcb JH |
663 | void MainWindow::on_actionQuit_triggered() |
664 | { | |
665 | close(); | |
666 | } | |
667 | ||
a429590b JH |
668 | void MainWindow::on_actionViewZoomIn_triggered() |
669 | { | |
8dbbc7f0 | 670 | view_->zoom(1); |
a429590b JH |
671 | } |
672 | ||
673 | void MainWindow::on_actionViewZoomOut_triggered() | |
674 | { | |
8dbbc7f0 | 675 | view_->zoom(-1); |
a429590b JH |
676 | } |
677 | ||
ca46b534 JH |
678 | void MainWindow::on_actionViewZoomFit_triggered() |
679 | { | |
8dbbc7f0 | 680 | view_->zoom_fit(); |
ca46b534 JH |
681 | } |
682 | ||
d1e7d82c JH |
683 | void MainWindow::on_actionViewZoomOneToOne_triggered() |
684 | { | |
8dbbc7f0 | 685 | view_->zoom_one_to_one(); |
d1e7d82c JH |
686 | } |
687 | ||
e072efc8 JH |
688 | void MainWindow::on_actionViewShowCursors_triggered() |
689 | { | |
8dbbc7f0 | 690 | assert(view_); |
b4d91e56 | 691 | |
8dbbc7f0 | 692 | const bool show = !view_->cursors_shown(); |
b4d91e56 | 693 | if(show) |
8dbbc7f0 | 694 | view_->centre_cursors(); |
b4d91e56 | 695 | |
8dbbc7f0 | 696 | view_->show_cursors(show); |
e072efc8 JH |
697 | } |
698 | ||
30236a54 JH |
699 | void MainWindow::on_actionAbout_triggered() |
700 | { | |
8dbbc7f0 | 701 | dialogs::About dlg(device_manager_.context(), this); |
40eb2ff4 | 702 | dlg.exec(); |
30236a54 | 703 | } |
274d4f13 | 704 | |
f0d37dab | 705 | void MainWindow::add_decoder(srd_decoder *decoder) |
7dd093df | 706 | { |
269528f5 | 707 | #ifdef ENABLE_DECODE |
f0d37dab | 708 | assert(decoder); |
8dbbc7f0 | 709 | session_.add_decoder(decoder); |
269528f5 JH |
710 | #else |
711 | (void)decoder; | |
712 | #endif | |
7dd093df JH |
713 | } |
714 | ||
6ac96c2e JH |
715 | void MainWindow::capture_state_changed(int state) |
716 | { | |
7c657094 | 717 | main_bar_->set_capture_state((pv::Session::capture_state)state); |
6ac96c2e JH |
718 | } |
719 | ||
20f81a58 JH |
720 | void MainWindow::device_selected() |
721 | { | |
722 | // Set the title to include the device/file name | |
da30ecb7 | 723 | const shared_ptr<devices::Device> device = session_.device(); |
20f81a58 JH |
724 | if (!device) |
725 | return; | |
726 | ||
3084ed4b | 727 | const string display_name = device->display_name(device_manager_); |
20f81a58 JH |
728 | setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str())); |
729 | } | |
730 | ||
51e77110 | 731 | } // namespace pv |