Fix #404 by only saving enabled signals
[pulseview.git] / pv / widgets / exportmenu.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 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 <algorithm>
22 #include <cassert>
23 #include <string>
24 #include <utility>
25
26 #include <libsigrokcxx/libsigrokcxx.hpp>
27
28 #include "exportmenu.hpp"
29
30 using std::find_if;
31 using std::map;
32 using std::pair;
33 using std::string;
34 using std::shared_ptr;
35
36 using sigrok::Context;
37 using sigrok::OutputFormat;
38
39 namespace pv {
40 namespace widgets {
41
42 ExportMenu::ExportMenu(QWidget *parent, shared_ptr<Context> context,
43         QAction *open_action) :
44         QMenu(parent),
45         context_(context),
46         mapper_(this)
47 {
48         assert(context);
49
50         if (open_action) {
51                 addAction(open_action);
52                 setDefaultAction(open_action);
53                 addSeparator();
54         }
55
56         const map<string, shared_ptr<OutputFormat> > formats =
57                 context->output_formats();
58
59         for (const pair<string, shared_ptr<OutputFormat> > &f : formats) {
60                 if (f.first == "srzip")
61                         continue;
62
63                 assert(f.second);
64                 QAction *const action = addAction(tr("Export %1...")
65                         .arg(QString::fromStdString(f.second->description())));
66                 action->setData(qVariantFromValue((void*)f.second.get()));
67                 mapper_.setMapping(action, action);
68                 connect(action, SIGNAL(triggered()), &mapper_, SLOT(map()));
69         }
70
71         connect(&mapper_, SIGNAL(mapped(QObject*)),
72                 this, SLOT(on_action(QObject*)));
73 }
74
75 void ExportMenu::on_action(QObject *action)
76 {
77         assert(action);
78
79         const map<string, shared_ptr<OutputFormat> > formats =
80                 context_->output_formats();
81         const auto iter = find_if(formats.cbegin(), formats.cend(),
82                 [&](const pair<string, shared_ptr<OutputFormat> > &f) {
83                         return f.second.get() ==
84                                 ((QAction*)action)->data().value<void*>(); });
85         if (iter == formats.cend())
86                 return;
87
88         format_selected((*iter).second);
89 }
90
91 } // widgets
92 } // pv