]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
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 | ||
21 | #include <cassert> | |
22 | ||
23 | #ifdef ENABLE_DECODE | |
24 | #include <libsigrokdecode/libsigrokdecode.h> | |
25 | #endif | |
26 | ||
27 | #include <algorithm> | |
28 | #include <iterator> | |
29 | ||
30 | #include <QAction> | |
31 | #include <QApplication> | |
32 | #include <QButtonGroup> | |
33 | #include <QCloseEvent> | |
34 | #include <QFileDialog> | |
35 | #include <QMessageBox> | |
36 | #include <QMenu> | |
37 | #include <QMenuBar> | |
38 | #include <QSettings> | |
39 | #include <QStatusBar> | |
40 | #include <QVBoxLayout> | |
41 | #include <QWidget> | |
42 | ||
43 | #include "mainwindow.h" | |
44 | ||
45 | #include "devicemanager.h" | |
46 | #include "dialogs/about.h" | |
47 | #include "dialogs/connect.h" | |
48 | #include "dialogs/storeprogress.h" | |
49 | #include "toolbars/samplingbar.h" | |
50 | #include "view/logicsignal.h" | |
51 | #include "view/view.h" | |
52 | #ifdef ENABLE_DECODE | |
53 | #include "widgets/decodermenu.h" | |
54 | #endif | |
55 | ||
56 | #include <inttypes.h> | |
57 | #include <stdint.h> | |
58 | #include <stdarg.h> | |
59 | #include <glib.h> | |
60 | #include <libsigrok/libsigrok.hpp> | |
61 | ||
62 | using std::list; | |
63 | using std::map; | |
64 | using std::shared_ptr; | |
65 | using std::string; | |
66 | ||
67 | using sigrok::Device; | |
68 | using sigrok::Error; | |
69 | using sigrok::HardwareDevice; | |
70 | ||
71 | namespace pv { | |
72 | ||
73 | namespace view { | |
74 | class SelectableItem; | |
75 | } | |
76 | ||
77 | const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; | |
78 | const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; | |
79 | ||
80 | MainWindow::MainWindow(DeviceManager &device_manager, | |
81 | const char *open_file_name, | |
82 | QWidget *parent) : | |
83 | QMainWindow(parent), | |
84 | _device_manager(device_manager), | |
85 | _session(device_manager) | |
86 | { | |
87 | setup_ui(); | |
88 | restore_ui_settings(); | |
89 | if (open_file_name) { | |
90 | const QString s(QString::fromUtf8(open_file_name)); | |
91 | QMetaObject::invokeMethod(this, "load_file", | |
92 | Qt::QueuedConnection, | |
93 | Q_ARG(QString, s)); | |
94 | } | |
95 | } | |
96 | ||
97 | void MainWindow::setup_ui() | |
98 | { | |
99 | setObjectName(QString::fromUtf8("MainWindow")); | |
100 | ||
101 | // Set the window icon | |
102 | QIcon icon; | |
103 | icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"), | |
104 | QSize(), QIcon::Normal, QIcon::Off); | |
105 | setWindowIcon(icon); | |
106 | ||
107 | // Setup the central widget | |
108 | _central_widget = new QWidget(this); | |
109 | _vertical_layout = new QVBoxLayout(_central_widget); | |
110 | _vertical_layout->setSpacing(6); | |
111 | _vertical_layout->setContentsMargins(0, 0, 0, 0); | |
112 | setCentralWidget(_central_widget); | |
113 | ||
114 | _view = new pv::view::View(_session, this); | |
115 | ||
116 | _vertical_layout->addWidget(_view); | |
117 | ||
118 | // Setup the menu bar | |
119 | QMenuBar *const menu_bar = new QMenuBar(this); | |
120 | menu_bar->setGeometry(QRect(0, 0, 400, 25)); | |
121 | ||
122 | // File Menu | |
123 | QMenu *const menu_file = new QMenu; | |
124 | menu_file->setTitle(tr("&File")); | |
125 | ||
126 | QAction *const action_open = new QAction(this); | |
127 | action_open->setText(tr("&Open...")); | |
128 | action_open->setIcon(QIcon::fromTheme("document-open", | |
129 | QIcon(":/icons/document-open.png"))); | |
130 | action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); | |
131 | action_open->setObjectName(QString::fromUtf8("actionOpen")); | |
132 | menu_file->addAction(action_open); | |
133 | ||
134 | QAction *const action_save_as = new QAction(this); | |
135 | action_save_as->setText(tr("&Save As...")); | |
136 | action_save_as->setIcon(QIcon::fromTheme("document-save-as", | |
137 | QIcon(":/icons/document-save-as.png"))); | |
138 | action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
139 | action_save_as->setObjectName(QString::fromUtf8("actionSaveAs")); | |
140 | menu_file->addAction(action_save_as); | |
141 | ||
142 | menu_file->addSeparator(); | |
143 | ||
144 | QAction *const action_connect = new QAction(this); | |
145 | action_connect->setText(tr("&Connect to Device...")); | |
146 | action_connect->setObjectName(QString::fromUtf8("actionConnect")); | |
147 | menu_file->addAction(action_connect); | |
148 | ||
149 | menu_file->addSeparator(); | |
150 | ||
151 | QAction *action_quit = new QAction(this); | |
152 | action_quit->setText(tr("&Quit")); | |
153 | action_quit->setIcon(QIcon::fromTheme("application-exit", | |
154 | QIcon(":/icons/application-exit.png"))); | |
155 | action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); | |
156 | action_quit->setObjectName(QString::fromUtf8("actionQuit")); | |
157 | menu_file->addAction(action_quit); | |
158 | ||
159 | // View Menu | |
160 | QMenu *menu_view = new QMenu; | |
161 | menu_view->setTitle(tr("&View")); | |
162 | ||
163 | QAction *const action_view_zoom_in = new QAction(this); | |
164 | action_view_zoom_in->setText(tr("Zoom &In")); | |
165 | action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in", | |
166 | QIcon(":/icons/zoom-in.png"))); | |
167 | // simply using Qt::Key_Plus shows no + in the menu | |
168 | action_view_zoom_in->setShortcut(QKeySequence::ZoomIn); | |
169 | action_view_zoom_in->setObjectName( | |
170 | QString::fromUtf8("actionViewZoomIn")); | |
171 | menu_view->addAction(action_view_zoom_in); | |
172 | ||
173 | QAction *const action_view_zoom_out = new QAction(this); | |
174 | action_view_zoom_out->setText(tr("Zoom &Out")); | |
175 | action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out", | |
176 | QIcon(":/icons/zoom-out.png"))); | |
177 | action_view_zoom_out->setShortcut(QKeySequence::ZoomOut); | |
178 | action_view_zoom_out->setObjectName( | |
179 | QString::fromUtf8("actionViewZoomOut")); | |
180 | menu_view->addAction(action_view_zoom_out); | |
181 | ||
182 | QAction *const action_view_zoom_fit = new QAction(this); | |
183 | action_view_zoom_fit->setText(tr("Zoom to &Fit")); | |
184 | action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit", | |
185 | QIcon(":/icons/zoom-fit.png"))); | |
186 | action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F)); | |
187 | action_view_zoom_fit->setObjectName( | |
188 | QString::fromUtf8("actionViewZoomFit")); | |
189 | menu_view->addAction(action_view_zoom_fit); | |
190 | ||
191 | QAction *const action_view_zoom_one_to_one = new QAction(this); | |
192 | action_view_zoom_one_to_one->setText(tr("Zoom to &One-to-One")); | |
193 | action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original", | |
194 | QIcon(":/icons/zoom-original.png"))); | |
195 | action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O)); | |
196 | action_view_zoom_one_to_one->setObjectName( | |
197 | QString::fromUtf8("actionViewZoomOneToOne")); | |
198 | menu_view->addAction(action_view_zoom_one_to_one); | |
199 | ||
200 | menu_view->addSeparator(); | |
201 | ||
202 | QAction *action_view_show_cursors = new QAction(this); | |
203 | action_view_show_cursors->setCheckable(true); | |
204 | action_view_show_cursors->setChecked(_view->cursors_shown()); | |
205 | action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C)); | |
206 | action_view_show_cursors->setObjectName( | |
207 | QString::fromUtf8("actionViewShowCursors")); | |
208 | action_view_show_cursors->setText(tr("Show &Cursors")); | |
209 | menu_view->addAction(action_view_show_cursors); | |
210 | ||
211 | // Decoders Menu | |
212 | #ifdef ENABLE_DECODE | |
213 | QMenu *const menu_decoders = new QMenu; | |
214 | menu_decoders->setTitle(tr("&Decoders")); | |
215 | ||
216 | pv::widgets::DecoderMenu *const menu_decoders_add = | |
217 | new pv::widgets::DecoderMenu(menu_decoders, true); | |
218 | menu_decoders_add->setTitle(tr("&Add")); | |
219 | connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)), | |
220 | this, SLOT(add_decoder(srd_decoder*))); | |
221 | ||
222 | menu_decoders->addMenu(menu_decoders_add); | |
223 | #endif | |
224 | ||
225 | // Help Menu | |
226 | QMenu *const menu_help = new QMenu; | |
227 | menu_help->setTitle(tr("&Help")); | |
228 | ||
229 | QAction *const action_about = new QAction(this); | |
230 | action_about->setObjectName(QString::fromUtf8("actionAbout")); | |
231 | action_about->setText(tr("&About...")); | |
232 | menu_help->addAction(action_about); | |
233 | ||
234 | menu_bar->addAction(menu_file->menuAction()); | |
235 | menu_bar->addAction(menu_view->menuAction()); | |
236 | #ifdef ENABLE_DECODE | |
237 | menu_bar->addAction(menu_decoders->menuAction()); | |
238 | #endif | |
239 | menu_bar->addAction(menu_help->menuAction()); | |
240 | ||
241 | setMenuBar(menu_bar); | |
242 | QMetaObject::connectSlotsByName(this); | |
243 | ||
244 | // Setup the toolbar | |
245 | QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this); | |
246 | toolbar->setObjectName(QString::fromUtf8("MainToolbar")); | |
247 | toolbar->addAction(action_open); | |
248 | toolbar->addAction(action_save_as); | |
249 | toolbar->addSeparator(); | |
250 | toolbar->addAction(action_view_zoom_in); | |
251 | toolbar->addAction(action_view_zoom_out); | |
252 | toolbar->addAction(action_view_zoom_fit); | |
253 | toolbar->addAction(action_view_zoom_one_to_one); | |
254 | addToolBar(toolbar); | |
255 | ||
256 | // Setup the sampling bar | |
257 | _sampling_bar = new toolbars::SamplingBar(_session, this); | |
258 | ||
259 | // Populate the device list and select the initially selected device | |
260 | update_device_list(); | |
261 | ||
262 | connect(_sampling_bar, SIGNAL(run_stop()), this, | |
263 | SLOT(run_stop())); | |
264 | addToolBar(_sampling_bar); | |
265 | ||
266 | // Set the title | |
267 | setWindowTitle(tr("PulseView")); | |
268 | ||
269 | // Setup _session events | |
270 | connect(&_session, SIGNAL(capture_state_changed(int)), this, | |
271 | SLOT(capture_state_changed(int))); | |
272 | } | |
273 | ||
274 | void MainWindow::save_ui_settings() | |
275 | { | |
276 | QSettings settings; | |
277 | ||
278 | map<string, string> dev_info; | |
279 | list<string> key_list; | |
280 | ||
281 | settings.beginGroup("MainWindow"); | |
282 | settings.setValue("state", saveState()); | |
283 | settings.setValue("geometry", saveGeometry()); | |
284 | settings.endGroup(); | |
285 | ||
286 | if (_session.get_device()) { | |
287 | settings.beginGroup("Device"); | |
288 | key_list.push_back("vendor"); | |
289 | key_list.push_back("model"); | |
290 | key_list.push_back("version"); | |
291 | key_list.push_back("serial_num"); | |
292 | key_list.push_back("connection_id"); | |
293 | ||
294 | dev_info = _device_manager.get_device_info( | |
295 | _session.get_device()); | |
296 | ||
297 | for (string key : key_list) { | |
298 | ||
299 | if (dev_info.count(key)) | |
300 | settings.setValue(QString::fromUtf8(key.c_str()), | |
301 | QString::fromUtf8(dev_info.at(key).c_str())); | |
302 | else | |
303 | settings.remove(QString::fromUtf8(key.c_str())); | |
304 | } | |
305 | ||
306 | settings.endGroup(); | |
307 | } | |
308 | } | |
309 | ||
310 | void MainWindow::restore_ui_settings() | |
311 | { | |
312 | QSettings settings; | |
313 | ||
314 | shared_ptr<HardwareDevice> device; | |
315 | ||
316 | map<string, string> dev_info; | |
317 | list<string> key_list; | |
318 | string value; | |
319 | ||
320 | settings.beginGroup("MainWindow"); | |
321 | ||
322 | if (settings.contains("geometry")) { | |
323 | restoreGeometry(settings.value("geometry").toByteArray()); | |
324 | restoreState(settings.value("state").toByteArray()); | |
325 | } else | |
326 | resize(1000, 720); | |
327 | ||
328 | settings.endGroup(); | |
329 | ||
330 | // Re-select last used device if possible. | |
331 | settings.beginGroup("Device"); | |
332 | key_list.push_back("vendor"); | |
333 | key_list.push_back("model"); | |
334 | key_list.push_back("version"); | |
335 | key_list.push_back("serial_num"); | |
336 | key_list.push_back("connection_id"); | |
337 | ||
338 | for (string key : key_list) { | |
339 | if (!settings.contains(QString::fromUtf8(key.c_str()))) | |
340 | continue; | |
341 | ||
342 | value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString(); | |
343 | ||
344 | if (value.size() > 0) | |
345 | dev_info.insert(std::make_pair(key, value)); | |
346 | } | |
347 | ||
348 | device = _device_manager.find_device_from_info(dev_info); | |
349 | ||
350 | if (device) { | |
351 | _session.set_device(device); | |
352 | update_device_list(); | |
353 | } | |
354 | ||
355 | settings.endGroup(); | |
356 | } | |
357 | ||
358 | void MainWindow::session_error( | |
359 | const QString text, const QString info_text) | |
360 | { | |
361 | QMetaObject::invokeMethod(this, "show_session_error", | |
362 | Qt::QueuedConnection, Q_ARG(QString, text), | |
363 | Q_ARG(QString, info_text)); | |
364 | } | |
365 | ||
366 | void MainWindow::update_device_list() | |
367 | { | |
368 | assert(_sampling_bar); | |
369 | ||
370 | shared_ptr<Device> selected_device = _session.get_device(); | |
371 | list< shared_ptr<Device> > devices; | |
372 | ||
373 | if (_device_manager.devices().size() == 0) | |
374 | return; | |
375 | ||
376 | std::copy(_device_manager.devices().begin(), | |
377 | _device_manager.devices().end(), std::back_inserter(devices)); | |
378 | ||
379 | if (std::find(devices.begin(), devices.end(), selected_device) == | |
380 | devices.end()) | |
381 | devices.push_back(selected_device); | |
382 | assert(selected_device); | |
383 | ||
384 | map<shared_ptr<Device>, string> device_names; | |
385 | ||
386 | for (auto device : devices) | |
387 | device_names[device] = _device_manager.get_display_name(device); | |
388 | ||
389 | _sampling_bar->set_device_list(device_names, selected_device); | |
390 | } | |
391 | ||
392 | void MainWindow::closeEvent(QCloseEvent *event) | |
393 | { | |
394 | save_ui_settings(); | |
395 | event->accept(); | |
396 | } | |
397 | ||
398 | void MainWindow::load_file(QString file_name) | |
399 | { | |
400 | const QString errorMessage( | |
401 | QString("Failed to load file %1").arg(file_name)); | |
402 | const QString infoMessage; | |
403 | ||
404 | try { | |
405 | _session.set_file(file_name.toStdString()); | |
406 | } catch(Error e) { | |
407 | show_session_error(tr("Failed to load ") + file_name, e.what()); | |
408 | _session.set_default_device(); | |
409 | update_device_list(); | |
410 | return; | |
411 | } | |
412 | ||
413 | update_device_list(); | |
414 | ||
415 | _session.start_capture([&, errorMessage, infoMessage](QString) { | |
416 | session_error(errorMessage, infoMessage); }); | |
417 | } | |
418 | ||
419 | void MainWindow::show_session_error( | |
420 | const QString text, const QString info_text) | |
421 | { | |
422 | QMessageBox msg(this); | |
423 | msg.setText(text); | |
424 | msg.setInformativeText(info_text); | |
425 | msg.setStandardButtons(QMessageBox::Ok); | |
426 | msg.setIcon(QMessageBox::Warning); | |
427 | msg.exec(); | |
428 | } | |
429 | ||
430 | void MainWindow::on_actionOpen_triggered() | |
431 | { | |
432 | QSettings settings; | |
433 | const QString dir = settings.value(SettingOpenDirectory).toString(); | |
434 | ||
435 | // Show the dialog | |
436 | const QString file_name = QFileDialog::getOpenFileName( | |
437 | this, tr("Open File"), dir, tr( | |
438 | "Sigrok Sessions (*.sr);;" | |
439 | "All Files (*.*)")); | |
440 | ||
441 | if (!file_name.isEmpty()) { | |
442 | load_file(file_name); | |
443 | ||
444 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
445 | settings.setValue(SettingOpenDirectory, abs_path); | |
446 | } | |
447 | } | |
448 | ||
449 | void MainWindow::on_actionSaveAs_triggered() | |
450 | { | |
451 | using pv::dialogs::StoreProgress; | |
452 | ||
453 | // Stop any currently running capture session | |
454 | _session.stop_capture(); | |
455 | ||
456 | QSettings settings; | |
457 | const QString dir = settings.value(SettingSaveDirectory).toString(); | |
458 | ||
459 | // Show the dialog | |
460 | const QString file_name = QFileDialog::getSaveFileName( | |
461 | this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)")); | |
462 | ||
463 | if (file_name.isEmpty()) | |
464 | return; | |
465 | ||
466 | const QString abs_path = QFileInfo(file_name).absolutePath(); | |
467 | settings.setValue(SettingSaveDirectory, abs_path); | |
468 | ||
469 | StoreProgress *dlg = new StoreProgress(file_name, _session, this); | |
470 | dlg->run(); | |
471 | } | |
472 | ||
473 | void MainWindow::on_actionConnect_triggered() | |
474 | { | |
475 | // Stop any currently running capture session | |
476 | _session.stop_capture(); | |
477 | ||
478 | dialogs::Connect dlg(this, _device_manager); | |
479 | ||
480 | // If the user selected a device, select it in the device list. Select the | |
481 | // current device otherwise. | |
482 | if (dlg.exec()) | |
483 | _session.set_device(dlg.get_selected_device()); | |
484 | ||
485 | update_device_list(); | |
486 | } | |
487 | ||
488 | void MainWindow::on_actionQuit_triggered() | |
489 | { | |
490 | close(); | |
491 | } | |
492 | ||
493 | void MainWindow::on_actionViewZoomIn_triggered() | |
494 | { | |
495 | _view->zoom(1); | |
496 | } | |
497 | ||
498 | void MainWindow::on_actionViewZoomOut_triggered() | |
499 | { | |
500 | _view->zoom(-1); | |
501 | } | |
502 | ||
503 | void MainWindow::on_actionViewZoomFit_triggered() | |
504 | { | |
505 | _view->zoom_fit(); | |
506 | } | |
507 | ||
508 | void MainWindow::on_actionViewZoomOneToOne_triggered() | |
509 | { | |
510 | _view->zoom_one_to_one(); | |
511 | } | |
512 | ||
513 | void MainWindow::on_actionViewShowCursors_triggered() | |
514 | { | |
515 | assert(_view); | |
516 | ||
517 | const bool show = !_view->cursors_shown(); | |
518 | if(show) | |
519 | _view->centre_cursors(); | |
520 | ||
521 | _view->show_cursors(show); | |
522 | } | |
523 | ||
524 | void MainWindow::on_actionAbout_triggered() | |
525 | { | |
526 | dialogs::About dlg(_device_manager.context(), this); | |
527 | dlg.exec(); | |
528 | } | |
529 | ||
530 | void MainWindow::add_decoder(srd_decoder *decoder) | |
531 | { | |
532 | #ifdef ENABLE_DECODE | |
533 | assert(decoder); | |
534 | _session.add_decoder(decoder); | |
535 | #else | |
536 | (void)decoder; | |
537 | #endif | |
538 | } | |
539 | ||
540 | void MainWindow::run_stop() | |
541 | { | |
542 | switch(_session.get_capture_state()) { | |
543 | case SigSession::Stopped: | |
544 | _session.start_capture([&](QString message) { | |
545 | session_error("Capture failed", message); }); | |
546 | break; | |
547 | ||
548 | case SigSession::AwaitingTrigger: | |
549 | case SigSession::Running: | |
550 | _session.stop_capture(); | |
551 | break; | |
552 | } | |
553 | } | |
554 | ||
555 | void MainWindow::capture_state_changed(int state) | |
556 | { | |
557 | _sampling_bar->set_capture_state((pv::SigSession::capture_state)state); | |
558 | } | |
559 | ||
560 | } // namespace pv |