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