]> sigrok.org Git - pulseview.git/blame - pv/popups/channels.cpp
Implement annotation export from the DecodeTrace context menu
[pulseview.git] / pv / popups / channels.cpp
CommitLineData
cdb50f67
JH
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
cdb50f67
JH
18 */
19
488f068c
JH
20#include <map>
21
b887c4d8 22#include <QApplication>
bfc9f61e 23#include <QCheckBox>
b887c4d8 24#include <QFontMetrics>
488f068c
JH
25#include <QFormLayout>
26#include <QGridLayout>
9fa8a63c 27#include <QHBoxLayout>
bfc9f61e 28#include <QLabel>
488f068c 29
2acdb232 30#include "channels.hpp"
cdb50f67 31
b5d20c6d 32#include <pv/session.hpp>
3cc9ad7b 33#include <pv/binding/device.hpp>
5b35d18c
SA
34#include <pv/data/logic.hpp>
35#include <pv/data/logicsegment.hpp>
bf0edd2b 36#include <pv/data/signalbase.hpp>
da30ecb7 37#include <pv/devices/device.hpp>
488f068c 38
b5d20c6d 39using std::make_shared;
488f068c 40using std::map;
b5d20c6d 41using std::out_of_range;
f9abf97e 42using std::shared_ptr;
78b0af3e 43using std::unordered_set;
819f4c25 44using std::vector;
479bcabe 45
bf0edd2b 46using pv::data::SignalBase;
5b35d18c
SA
47using pv::data::Logic;
48using pv::data::LogicSegment;
bf0edd2b 49
e8d00928
ML
50using sigrok::Channel;
51using sigrok::ChannelGroup;
52using sigrok::Device;
53
cdb50f67 54namespace pv {
51d4a9ab 55namespace popups {
cdb50f67 56
2b81ae46 57Channels::Channels(Session &session, QWidget *parent) :
51d4a9ab 58 Popup(parent),
8dbbc7f0
JH
59 session_(session),
60 updating_channels_(false),
9fa8a63c
UH
61 enable_all_channels_(tr("All"), this),
62 disable_all_channels_(tr("All"), this),
63 enable_all_logic_channels_(tr("Logic"), this),
64 enable_all_analog_channels_(tr("Analog"), this),
65 enable_all_named_channels_(tr("Named"), this),
66 enable_all_changing_channels_(tr("Changing"), this),
8dbbc7f0 67 check_box_mapper_(this)
cdb50f67 68{
488f068c 69 // Create the layout
8dbbc7f0 70 setLayout(&layout_);
9c025ef3 71
da30ecb7 72 const shared_ptr<sigrok::Device> device = session_.device()->device();
e8d00928 73 assert(device);
488f068c
JH
74
75 // Collect a set of signals
bf0edd2b 76 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
c3a740dd 77
bf0edd2b 78 unordered_set< shared_ptr<SignalBase> > sigs;
47e9e7bb
SA
79 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
80 sigs.insert(b);
733eee0e 81
bf0edd2b 82 for (const shared_ptr<SignalBase> &sig : sigs)
6ac6242b 83 signal_map[sig->channel()] = sig;
488f068c 84
2445160a 85 // Populate channel groups
2ad82c2e 86 for (auto entry : device->channel_groups()) {
b5d20c6d
SA
87 const shared_ptr<ChannelGroup> group = entry.second;
88 // Make a set of signals and remove these signals from the signal map
bf0edd2b 89 vector< shared_ptr<SignalBase> > group_sigs;
2ad82c2e 90 for (auto channel : group->channels()) {
6ac6242b 91 const auto iter = signal_map.find(channel);
52a80eb3
SA
92
93 if (iter == signal_map.end())
94 break;
488f068c
JH
95
96 group_sigs.push_back((*iter).second);
97 signal_map.erase(iter);
98 }
99
100 populate_group(group, group_sigs);
101 }
102
6ac6242b 103 // Make a vector of the remaining channels
3b6c4a1f 104 vector< shared_ptr<SignalBase> > global_analog_sigs, global_logic_sigs;
2ad82c2e 105 for (auto channel : device->channels()) {
bf0edd2b 106 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
6ac6242b 107 const_iterator iter = signal_map.find(channel);
3b6c4a1f
SA
108
109 if (iter != signal_map.end()) {
110 const shared_ptr<SignalBase> signal = (*iter).second;
111 if (signal->type() == SignalBase::AnalogChannel)
112 global_analog_sigs.push_back(signal);
113 else
114 global_logic_sigs.push_back(signal);
115 }
488f068c
JH
116 }
117
3b6c4a1f
SA
118 // Create the groups for the ungrouped channels
119 populate_group(nullptr, global_logic_sigs);
120 populate_group(nullptr, global_analog_sigs);
488f068c
JH
121
122 // Create the enable/disable all buttons
5b35d18c
SA
123 connect(&enable_all_channels_, SIGNAL(clicked()), this, SLOT(enable_all_channels()));
124 connect(&disable_all_channels_, SIGNAL(clicked()), this, SLOT(disable_all_channels()));
125 connect(&enable_all_logic_channels_, SIGNAL(clicked()), this, SLOT(enable_all_logic_channels()));
126 connect(&enable_all_analog_channels_, SIGNAL(clicked()), this, SLOT(enable_all_analog_channels()));
127 connect(&enable_all_named_channels_, SIGNAL(clicked()), this, SLOT(enable_all_named_channels()));
128 connect(&enable_all_changing_channels_, SIGNAL(clicked()),
129 this, SLOT(enable_all_changing_channels()));
ed773982 130
9fa8a63c
UH
131 QLabel *label1 = new QLabel(tr("Disable: "));
132 buttons_bar_.addWidget(label1);
133 buttons_bar_.addWidget(&disable_all_channels_);
134 QLabel *label2 = new QLabel(tr("Enable: "));
135 buttons_bar_.addWidget(label2);
136 buttons_bar_.addWidget(&enable_all_channels_);
137 buttons_bar_.addWidget(&enable_all_logic_channels_);
138 buttons_bar_.addWidget(&enable_all_analog_channels_);
139 buttons_bar_.addWidget(&enable_all_named_channels_);
140 buttons_bar_.addWidget(&enable_all_changing_channels_);
141 buttons_bar_.addStretch();
ed773982 142
8dbbc7f0 143 layout_.addRow(&buttons_bar_);
0740907f 144
488f068c 145 // Connect the check-box signal mapper
8dbbc7f0 146 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
6ac6242b 147 this, SLOT(on_channel_checked(QWidget*)));
b7b659aa
JH
148}
149
6ac6242b 150void Channels::set_all_channels(bool set)
b7b659aa 151{
8dbbc7f0 152 updating_channels_ = true;
b7b659aa 153
b5d20c6d
SA
154 for (auto entry : check_box_signal_map_) {
155 QCheckBox *cb = entry.first;
156 const shared_ptr<SignalBase> sig = entry.second;
488f068c 157 assert(sig);
b7b659aa 158
bf0edd2b 159 sig->set_enabled(set);
b5d20c6d 160 cb->setChecked(set);
0740907f 161 }
51d4a9ab 162
8dbbc7f0 163 updating_channels_ = false;
0740907f
JH
164}
165
7ceb2376 166void Channels::enable_channels_conditionally(
5b35d18c
SA
167 function<bool (const shared_ptr<data::SignalBase>)> cond_func)
168{
169 updating_channels_ = true;
170
171 for (auto entry : check_box_signal_map_) {
172 QCheckBox *cb = entry.first;
173 const shared_ptr<SignalBase> sig = entry.second;
174 assert(sig);
175
176 const bool state = cond_func(sig);
7ceb2376
SA
177 if (state) {
178 sig->set_enabled(state);
179 cb->setChecked(state);
180 }
5b35d18c
SA
181 }
182
183 updating_channels_ = false;
184}
185
e8d00928 186void Channels::populate_group(shared_ptr<ChannelGroup> group,
bf0edd2b 187 const vector< shared_ptr<SignalBase> > sigs)
488f068c 188{
3cc9ad7b 189 using pv::binding::Device;
488f068c
JH
190
191 // Only bind options if this is a group. We don't do it for general
192 // options, because these properties are shown in the device config
193 // popup.
3cc9ad7b 194 shared_ptr<Device> binding;
488f068c 195 if (group)
067bb624 196 binding = make_shared<Device>(group);
488f068c
JH
197
198 // Create a title if the group is going to have any content
b5d20c6d
SA
199 if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
200 {
201 QLabel *label = new QLabel(
202 QString("<h3>%1</h3>").arg(group->name().c_str()));
203 layout_.addRow(label);
204 group_label_map_[group] = label;
205 }
488f068c 206
2445160a 207 // Create the channel group grid
c063290a 208 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
8dbbc7f0 209 layout_.addRow(channel_grid);
488f068c 210
2445160a 211 // Create the channel group options
c063290a 212 if (binding) {
8dbbc7f0
JH
213 binding->add_properties_to_form(&layout_, true);
214 group_bindings_.push_back(binding);
488f068c
JH
215 }
216}
217
6ac6242b 218QGridLayout* Channels::create_channel_group_grid(
bf0edd2b 219 const vector< shared_ptr<SignalBase> > sigs)
488f068c
JH
220{
221 int row = 0, col = 0;
222 QGridLayout *const grid = new QGridLayout();
223
bf0edd2b 224 for (const shared_ptr<SignalBase>& sig : sigs) {
488f068c
JH
225 assert(sig);
226
b5d20c6d 227 QCheckBox *const checkbox = new QCheckBox(sig->display_name());
8dbbc7f0 228 check_box_mapper_.setMapping(checkbox, checkbox);
488f068c 229 connect(checkbox, SIGNAL(toggled(bool)),
8dbbc7f0 230 &check_box_mapper_, SLOT(map()));
488f068c
JH
231
232 grid->addWidget(checkbox, row, col);
233
8dbbc7f0 234 check_box_signal_map_[checkbox] = sig;
488f068c 235
f3290553 236 if (++col >= 8)
488f068c
JH
237 col = 0, row++;
238 }
239
240 return grid;
241}
242
d9ea9628 243void Channels::showEvent(QShowEvent *event)
ed773982 244{
d9ea9628 245 pv::widgets::Popup::showEvent(event);
b7b659aa 246
b5d20c6d
SA
247 const shared_ptr<sigrok::Device> device = session_.device()->device();
248 assert(device);
249
250 // Update group labels
251 for (auto entry : device->channel_groups()) {
252 const shared_ptr<ChannelGroup> group = entry.second;
253
254 try {
255 QLabel* label = group_label_map_.at(group);
256 label->setText(QString("<h3>%1</h3>").arg(group->name().c_str()));
30677c13 257 } catch (out_of_range&) {
b5d20c6d
SA
258 // Do nothing
259 }
260 }
261
8dbbc7f0 262 updating_channels_ = true;
b7b659aa 263
b5d20c6d
SA
264 for (auto entry : check_box_signal_map_) {
265 QCheckBox *cb = entry.first;
266 const shared_ptr<SignalBase> sig = entry.second;
488f068c
JH
267 assert(sig);
268
b5d20c6d
SA
269 // Update the check box
270 cb->setChecked(sig->enabled());
271 cb->setText(sig->display_name());
ed773982 272 }
b7b659aa 273
8dbbc7f0 274 updating_channels_ = false;
ed773982
JH
275}
276
6ac6242b 277void Channels::on_channel_checked(QWidget *widget)
51d4a9ab 278{
8dbbc7f0 279 if (updating_channels_)
b7b659aa
JH
280 return;
281
488f068c
JH
282 QCheckBox *const check_box = (QCheckBox*)widget;
283 assert(check_box);
284
285 // Look up the signal of this check-box
bf0edd2b 286 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
8dbbc7f0
JH
287 check_box_signal_map_.find((QCheckBox*)check_box);
288 assert(iter != check_box_signal_map_.end());
488f068c 289
bf0edd2b 290 const shared_ptr<SignalBase> s = (*iter).second;
aca00b1e 291 assert(s);
488f068c 292
bf0edd2b 293 s->set_enabled(check_box->isChecked());
51d4a9ab
JH
294}
295
6ac6242b 296void Channels::enable_all_channels()
ed773982 297{
6ac6242b 298 set_all_channels(true);
ed773982
JH
299}
300
6ac6242b 301void Channels::disable_all_channels()
ed773982 302{
6ac6242b 303 set_all_channels(false);
ed773982
JH
304}
305
5b35d18c
SA
306void Channels::enable_all_logic_channels()
307{
7ceb2376 308 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
5b35d18c
SA
309 { return signal->type() == SignalBase::LogicChannel; });
310}
311
312void Channels::enable_all_analog_channels()
313{
7ceb2376 314 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
5b35d18c
SA
315 { return signal->type() == SignalBase::AnalogChannel; });
316}
317
318void Channels::enable_all_named_channels()
319{
7ceb2376 320 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
5b35d18c
SA
321 { return signal->name() != signal->internal_name(); });
322}
323
324void Channels::enable_all_changing_channels()
325{
7ceb2376 326 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
5b35d18c 327 {
d13d95b3
SA
328 // Never enable channels without sample data
329 if (!signal->has_samples())
330 return false;
331
5b35d18c
SA
332 // Non-logic channels are considered to always have a signal
333 if (signal->type() != SignalBase::LogicChannel)
334 return true;
335
336 const shared_ptr<Logic> logic = signal->logic_data();
337 assert(logic);
338
339 // If any of the segments has edges, enable this channel
340 for (shared_ptr<LogicSegment> segment : logic->logic_segments()) {
341 vector<LogicSegment::EdgePair> edges;
342
343 segment->get_subsampled_edges(edges,
344 0, segment->get_sample_count() - 1,
345 LogicSegment::MipMapScaleFactor,
346 signal->index());
347
348 if (edges.size() > 2)
349 return true;
350 }
351
352 // No edges detected in any of the segments
353 return false;
354 });
355}
356
870ea3db
UH
357} // namespace popups
358} // namespace pv