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