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