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