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