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