]> sigrok.org Git - pulseview.git/blob - pv/widgets/importmenu.cpp
Session: Fix issue #67 by improving error handling
[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 using std::vector;
35
36 using sigrok::Context;
37 using sigrok::InputFormat;
38
39 namespace pv {
40 namespace widgets {
41
42 ImportMenu::ImportMenu(QWidget *parent, shared_ptr<Context> context,
43         vector<QAction *>open_actions) :
44         QMenu(parent),
45         context_(context),
46         mapper_(this)
47 {
48         assert(context);
49
50         if (!open_actions.empty()) {
51                 bool first_action = true;
52                 for (auto open_action : open_actions) {
53                         addAction(open_action);
54
55                         if (first_action) {
56                                 first_action = false;
57                                 setDefaultAction(open_action);
58                         }
59                 }
60                 addSeparator();
61         }
62
63         const map<string, shared_ptr<InputFormat> > formats =
64                 context->input_formats();
65
66         for (const pair<const string, shared_ptr<InputFormat> > &f : formats) {
67                 assert(f.second);
68                 QAction *const action = addAction(tr("Import %1...")
69                         .arg(QString::fromStdString(f.second->description())));
70                 action->setData(QVariant::fromValue((void*)f.second.get()));
71                 mapper_.setMapping(action, action);
72                 connect(action, SIGNAL(triggered()), &mapper_, SLOT(map()));
73         }
74
75 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
76         connect(&mapper_, SIGNAL(mappedObject(QObject*)),
77                 this, SLOT(on_action(QObject*)));
78 #else
79         connect(&mapper_, SIGNAL(mapped(QObject*)),
80                 this, SLOT(on_action(QObject*)));
81 #endif
82 }
83
84 void ImportMenu::on_action(QObject *action)
85 {
86         assert(action);
87
88         const map<string, shared_ptr<InputFormat> > formats =
89                 context_->input_formats();
90         const auto iter = find_if(formats.cbegin(), formats.cend(),
91                 [&](const pair<string, shared_ptr<InputFormat> > &f) {
92                         return f.second.get() ==
93                                 ((QAction*)action)->data().value<void*>(); });
94         if (iter == formats.cend())
95                 return;
96
97         format_selected((*iter).second);
98 }
99
100 }  // namespace widgets
101 }  // namespace pv