]> sigrok.org Git - pulseview.git/blob - pv/widgets/importmenu.cpp
1a45aa6207dec2cd493719bfa86f5eb376a80afb
[pulseview.git] / pv / widgets / importmenu.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <algorithm>
21 #include <cassert>
22 #include <string>
23 #include <utility>
24
25 #include <libsigrokcxx/libsigrokcxx.hpp>
26
27 #include "importmenu.hpp"
28
29 using std::find_if;
30 using std::map;
31 using std::pair;
32 using std::string;
33 using std::shared_ptr;
34
35 using sigrok::Context;
36 using sigrok::InputFormat;
37
38 namespace pv {
39 namespace widgets {
40
41 ImportMenu::ImportMenu(QWidget *parent, shared_ptr<Context> context,
42         QAction *open_action) :
43         QMenu(parent),
44         context_(context),
45         mapper_(this)
46 {
47         assert(context);
48
49         if (open_action) {
50                 addAction(open_action);
51                 setDefaultAction(open_action);
52                 addSeparator();
53         }
54
55         const map<string, shared_ptr<InputFormat> > formats =
56                 context->input_formats();
57
58         for (const pair<string, shared_ptr<InputFormat> > &f : formats) {
59                 assert(f.second);
60                 QAction *const action = addAction(tr("Import %1...")
61                         .arg(QString::fromStdString(f.second->description())));
62                 action->setData(qVariantFromValue((void*)f.second.get()));
63                 mapper_.setMapping(action, action);
64                 connect(action, SIGNAL(triggered()), &mapper_, SLOT(map()));
65         }
66
67         connect(&mapper_, SIGNAL(mapped(QObject*)),
68                 this, SLOT(on_action(QObject*)));
69 }
70
71 void ImportMenu::on_action(QObject *action)
72 {
73         assert(action);
74
75         const map<string, shared_ptr<InputFormat> > formats =
76                 context_->input_formats();
77         const auto iter = find_if(formats.cbegin(), formats.cend(),
78                 [&](const pair<string, shared_ptr<InputFormat> > &f) {
79                         return f.second.get() ==
80                                 ((QAction*)action)->data().value<void*>(); });
81         if (iter == formats.cend())
82                 return;
83
84         format_selected((*iter).second);
85 }
86
87 }  // namespace widgets
88 }  // namespace pv