]> sigrok.org Git - pulseview.git/blame - pv/widgets/exportmenu.cpp
SignalBase: Add internal name
[pulseview.git] / pv / widgets / exportmenu.cpp
CommitLineData
ce6001b8
JH
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
30using std::find_if;
31using std::map;
32using std::pair;
33using std::string;
34using std::shared_ptr;
35
36using sigrok::Context;
37using sigrok::OutputFormat;
38
39namespace pv {
40namespace widgets {
41
42ExportMenu::ExportMenu(QWidget *parent, shared_ptr<Context> context,
686d9151 43 std::vector<QAction *>open_actions) :
ce6001b8
JH
44 QMenu(parent),
45 context_(context),
46 mapper_(this)
47{
48 assert(context);
49
686d9151
SA
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 }
ce6001b8
JH
60 addSeparator();
61 }
62
63 const map<string, shared_ptr<OutputFormat> > formats =
64 context->output_formats();
65
66 for (const pair<string, shared_ptr<OutputFormat> > &f : formats) {
67 if (f.first == "srzip")
68 continue;
69
70 assert(f.second);
71 QAction *const action = addAction(tr("Export %1...")
72 .arg(QString::fromStdString(f.second->description())));
73 action->setData(qVariantFromValue((void*)f.second.get()));
74 mapper_.setMapping(action, action);
75 connect(action, SIGNAL(triggered()), &mapper_, SLOT(map()));
76 }
77
78 connect(&mapper_, SIGNAL(mapped(QObject*)),
79 this, SLOT(on_action(QObject*)));
80}
81
82void ExportMenu::on_action(QObject *action)
83{
84 assert(action);
85
86 const map<string, shared_ptr<OutputFormat> > formats =
87 context_->output_formats();
88 const auto iter = find_if(formats.cbegin(), formats.cend(),
89 [&](const pair<string, shared_ptr<OutputFormat> > &f) {
90 return f.second.get() ==
91 ((QAction*)action)->data().value<void*>(); });
92 if (iter == formats.cend())
93 return;
94
95 format_selected((*iter).second);
96}
97
98} // widgets
99} // pv