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