]> sigrok.org Git - pulseview.git/blob - pv/popups/channels.cpp
Backport recent changes from mainline.
[pulseview.git] / pv / popups / channels.cpp
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
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <map>
21
22 #include <QApplication>
23 #include <QCheckBox>
24 #include <QFontMetrics>
25 #include <QFormLayout>
26 #include <QGridLayout>
27 #include <QHBoxLayout>
28 #include <QLabel>
29
30 #include "channels.hpp"
31
32 #include <pv/session.hpp>
33 #include <pv/binding/device.hpp>
34 #include <pv/data/logic.hpp>
35 #include <pv/data/logicsegment.hpp>
36 #include <pv/data/signalbase.hpp>
37 #include <pv/devices/device.hpp>
38
39 using std::make_shared;
40 using std::map;
41 using std::out_of_range;
42 using std::shared_ptr;
43 using std::unordered_set;
44 using std::vector;
45
46 using pv::data::SignalBase;
47 using pv::data::Logic;
48 using pv::data::LogicSegment;
49
50 using sigrok::Channel;
51 using sigrok::ChannelGroup;
52 using sigrok::Device;
53
54 namespace pv {
55 namespace popups {
56
57 Channels::Channels(Session &session, QWidget *parent) :
58         Popup(parent),
59         session_(session),
60         updating_channels_(false),
61         enable_all_channels_(tr("All"), this),
62         disable_all_channels_(tr("All"), this),
63         enable_all_logic_channels_(tr("Logic"), this),
64         disable_all_logic_channels_(tr("Logic"), this),
65         enable_all_analog_channels_(tr("Analog"), this),
66         disable_all_analog_channels_(tr("Analog"), this),
67         enable_all_named_channels_(tr("Named"), this),
68         disable_all_unnamed_channels_(tr("Unnamed"), this),
69         enable_all_changing_channels_(tr("Changing"), this),
70         disable_all_non_changing_channels_(tr("Non-changing"), this),
71         check_box_mapper_(this)
72 {
73         // Create the layout
74         setLayout(&layout_);
75
76         const shared_ptr<sigrok::Device> device = session_.device()->device();
77         assert(device);
78
79         // Collect a set of signals
80         map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
81
82         unordered_set< shared_ptr<SignalBase> > sigs;
83         for (const shared_ptr<data::SignalBase>& b : session_.signalbases())
84                 sigs.insert(b);
85
86         for (const shared_ptr<SignalBase> &sig : sigs)
87                 signal_map[sig->channel()] = sig;
88
89         // Populate channel groups
90         for (auto& entry : device->channel_groups()) {
91                 const shared_ptr<ChannelGroup> group = entry.second;
92                 // Make a set of signals and remove these signals from the signal map
93                 vector< shared_ptr<SignalBase> > group_sigs;
94                 for (auto& channel : group->channels()) {
95                         const auto iter = signal_map.find(channel);
96
97                         if (iter == signal_map.end())
98                                 break;
99
100                         group_sigs.push_back((*iter).second);
101                         signal_map.erase(iter);
102                 }
103
104                 populate_group(group, group_sigs);
105         }
106
107         // Make a vector of the remaining channels
108         vector< shared_ptr<SignalBase> > global_analog_sigs, global_logic_sigs;
109         for (auto& channel : device->channels()) {
110                 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
111                         const_iterator iter = signal_map.find(channel);
112
113                 if (iter != signal_map.end()) {
114                         const shared_ptr<SignalBase> signal = (*iter).second;
115                         if (signal->type() == SignalBase::AnalogChannel)
116                                 global_analog_sigs.push_back(signal);
117                         else
118                                 global_logic_sigs.push_back(signal);
119                 }
120         }
121
122         // Create the groups for the ungrouped channels
123         populate_group(nullptr, global_logic_sigs);
124         populate_group(nullptr, global_analog_sigs);
125
126         // Create the enable/disable all buttons
127         connect(&enable_all_channels_, SIGNAL(clicked()), this, SLOT(enable_all_channels()));
128         connect(&disable_all_channels_, SIGNAL(clicked()), this, SLOT(disable_all_channels()));
129         connect(&enable_all_logic_channels_, SIGNAL(clicked()), this, SLOT(enable_all_logic_channels()));
130         connect(&disable_all_logic_channels_, SIGNAL(clicked()), this, SLOT(disable_all_logic_channels()));
131         connect(&enable_all_analog_channels_, SIGNAL(clicked()), this, SLOT(enable_all_analog_channels()));
132         connect(&disable_all_analog_channels_, SIGNAL(clicked()), this, SLOT(disable_all_analog_channels()));
133         connect(&enable_all_named_channels_, SIGNAL(clicked()), this, SLOT(enable_all_named_channels()));
134         connect(&disable_all_unnamed_channels_, SIGNAL(clicked()), this, SLOT(disable_all_unnamed_channels()));
135         connect(&enable_all_changing_channels_, SIGNAL(clicked()),
136                 this, SLOT(enable_all_changing_channels()));
137         connect(&disable_all_non_changing_channels_, SIGNAL(clicked()),
138                 this, SLOT(disable_all_non_changing_channels()));
139
140         QLabel *label1 = new QLabel(tr("Disable: "));
141         filter_buttons_bar_.addWidget(label1, 0, 0);
142         filter_buttons_bar_.addWidget(&disable_all_channels_, 0, 1);
143         filter_buttons_bar_.addWidget(&disable_all_logic_channels_, 0, 2);
144         filter_buttons_bar_.addWidget(&disable_all_analog_channels_, 0, 3);
145         filter_buttons_bar_.addWidget(&disable_all_unnamed_channels_, 0, 4);
146         filter_buttons_bar_.addWidget(&disable_all_non_changing_channels_, 0, 5);
147
148         QLabel *label2 = new QLabel(tr("Enable: "));
149         filter_buttons_bar_.addWidget(label2, 1, 0);
150         filter_buttons_bar_.addWidget(&enable_all_channels_, 1, 1);
151         filter_buttons_bar_.addWidget(&enable_all_logic_channels_, 1, 2);
152         filter_buttons_bar_.addWidget(&enable_all_analog_channels_, 1, 3);
153         filter_buttons_bar_.addWidget(&enable_all_named_channels_, 1, 4);
154         filter_buttons_bar_.addWidget(&enable_all_changing_channels_, 1, 5);
155
156         layout_.addItem(new QSpacerItem(0, 15, QSizePolicy::Expanding, QSizePolicy::Expanding));
157         layout_.addRow(&filter_buttons_bar_);
158
159         // Connect the check-box signal mapper
160         connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
161                 this, SLOT(on_channel_checked(QWidget*)));
162 }
163
164 void Channels::set_all_channels(bool set)
165 {
166         updating_channels_ = true;
167
168         for (auto& entry : check_box_signal_map_) {
169                 QCheckBox *cb = entry.first;
170                 const shared_ptr<SignalBase> sig = entry.second;
171                 assert(sig);
172
173                 sig->set_enabled(set);
174                 cb->setChecked(set);
175         }
176
177         updating_channels_ = false;
178 }
179
180 void Channels::enable_channels_conditionally(
181         function<bool (const shared_ptr<data::SignalBase>)> cond_func)
182 {
183         updating_channels_ = true;
184
185         for (auto& entry : check_box_signal_map_) {
186                 QCheckBox *cb = entry.first;
187                 const shared_ptr<SignalBase> sig = entry.second;
188                 assert(sig);
189
190                 if (cond_func(sig)) {
191                         sig->set_enabled(true);
192                         cb->setChecked(true);
193                 }
194         }
195
196         updating_channels_ = false;
197 }
198
199 void Channels::disable_channels_conditionally(
200         function<bool (const shared_ptr<data::SignalBase>)> cond_func)
201 {
202         updating_channels_ = true;
203
204         for (auto& entry : check_box_signal_map_) {
205                 QCheckBox *cb = entry.first;
206                 const shared_ptr<SignalBase> sig = entry.second;
207                 assert(sig);
208
209                 if (cond_func(sig)) {
210                         sig->set_enabled(false);
211                         cb->setChecked(false);
212                 }
213         }
214
215         updating_channels_ = false;
216 }
217
218 void Channels::populate_group(shared_ptr<ChannelGroup> group,
219         const vector< shared_ptr<SignalBase> > sigs)
220 {
221         using pv::binding::Device;
222
223         // Only bind options if this is a group. We don't do it for general
224         // options, because these properties are shown in the device config
225         // popup.
226         shared_ptr<Device> binding;
227         if (group)
228                 binding = make_shared<Device>(group);
229
230         // Create a title if the group is going to have any content
231         if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
232         {
233                 QLabel *label = new QLabel(
234                         QString("<h3>%1</h3>").arg(group->name().c_str()));
235                 layout_.addRow(label);
236                 group_label_map_[group] = label;
237         }
238
239         // Create the channel group grid
240         QGridLayout *const channel_grid = create_channel_group_grid(sigs);
241         layout_.addRow(channel_grid);
242
243         // Create the channel group options
244         if (binding) {
245                 binding->add_properties_to_form(&layout_, true);
246                 group_bindings_.push_back(binding);
247         }
248 }
249
250 QGridLayout* Channels::create_channel_group_grid(
251         const vector< shared_ptr<SignalBase> > sigs)
252 {
253         int row = 0, col = 0;
254         QGridLayout *const grid = new QGridLayout();
255
256         for (const shared_ptr<SignalBase>& sig : sigs) {
257                 assert(sig);
258
259                 QCheckBox *const checkbox = new QCheckBox(sig->display_name());
260                 check_box_mapper_.setMapping(checkbox, checkbox);
261                 connect(checkbox, SIGNAL(toggled(bool)),
262                         &check_box_mapper_, SLOT(map()));
263
264                 grid->addWidget(checkbox, row, col);
265
266                 check_box_signal_map_[checkbox] = sig;
267
268                 if (++col >= 8)
269                         col = 0, row++;
270         }
271
272         return grid;
273 }
274
275 void Channels::showEvent(QShowEvent *event)
276 {
277         pv::widgets::Popup::showEvent(event);
278
279         const shared_ptr<sigrok::Device> device = session_.device()->device();
280         assert(device);
281
282         // Update group labels
283         for (auto& entry : device->channel_groups()) {
284                 const shared_ptr<ChannelGroup> group = entry.second;
285
286                 try {
287                         QLabel* label = group_label_map_.at(group);
288                         label->setText(QString("<h3>%1</h3>").arg(group->name().c_str()));
289                 } catch (out_of_range&) {
290                         // Do nothing
291                 }
292         }
293
294         updating_channels_ = true;
295
296         for (auto& entry : check_box_signal_map_) {
297                 QCheckBox *cb = entry.first;
298                 const shared_ptr<SignalBase> sig = entry.second;
299                 assert(sig);
300
301                 // Update the check box
302                 cb->setChecked(sig->enabled());
303                 cb->setText(sig->display_name());
304         }
305
306         updating_channels_ = false;
307 }
308
309 void Channels::on_channel_checked(QWidget *widget)
310 {
311         if (updating_channels_)
312                 return;
313
314         QCheckBox *const check_box = (QCheckBox*)widget;
315         assert(check_box);
316
317         // Look up the signal of this check-box
318         map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
319                 check_box_signal_map_.find((QCheckBox*)check_box);
320         assert(iter != check_box_signal_map_.end());
321
322         const shared_ptr<SignalBase> s = (*iter).second;
323         assert(s);
324
325         s->set_enabled(check_box->isChecked());
326 }
327
328 void Channels::enable_all_channels()
329 {
330         set_all_channels(true);
331 }
332
333 void Channels::disable_all_channels()
334 {
335         set_all_channels(false);
336 }
337
338 void Channels::enable_all_logic_channels()
339 {
340         enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
341                 { return signal->type() == SignalBase::LogicChannel; });
342 }
343
344 void Channels::disable_all_logic_channels()
345 {
346         disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
347                 { return signal->type() == SignalBase::LogicChannel; });
348 }
349
350 void Channels::enable_all_analog_channels()
351 {
352         enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
353                 { return signal->type() == SignalBase::AnalogChannel; });
354 }
355
356 void Channels::disable_all_analog_channels()
357 {
358         disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
359                 { return signal->type() == SignalBase::AnalogChannel; });
360 }
361
362 void Channels::enable_all_named_channels()
363 {
364         enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
365                 { return signal->name() != signal->internal_name(); });
366 }
367
368 void Channels::disable_all_unnamed_channels()
369 {
370         disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
371                 { return signal->name() == signal->internal_name(); });
372 }
373
374 void Channels::enable_all_changing_channels()
375 {
376         enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
377                 {
378                         // Never enable channels without sample data
379                         if (!signal->has_samples())
380                                 return false;
381
382                         // Non-logic channels are considered to always have a signal
383                         if (signal->type() != SignalBase::LogicChannel)
384                                 return true;
385
386                         const shared_ptr<Logic> logic = signal->logic_data();
387                         assert(logic);
388
389                         // If any of the segments has edges, enable this channel
390                         for (shared_ptr<LogicSegment> segment : logic->logic_segments()) {
391                                 vector<LogicSegment::EdgePair> edges;
392
393                                 segment->get_subsampled_edges(edges,
394                                         0, segment->get_sample_count() - 1,
395                                         LogicSegment::MipMapScaleFactor,
396                                         signal->index());
397
398                                 if (edges.size() > 2)
399                                         return true;
400                         }
401
402                         // No edges detected in any of the segments
403                         return false;
404                 });
405 }
406
407 void Channels::disable_all_non_changing_channels()
408 {
409         disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
410                 {
411                         // Always disable channels without sample data
412                         if (!signal->has_samples())
413                                 return true;
414
415                         // Non-logic channels are considered to always have a signal
416                         if (signal->type() != SignalBase::LogicChannel)
417                                 return false;
418
419                         const shared_ptr<Logic> logic = signal->logic_data();
420                         assert(logic);
421
422                         // If any of the segments has edges, leave this channel enabled
423                         for (shared_ptr<LogicSegment> segment : logic->logic_segments()) {
424                                 vector<LogicSegment::EdgePair> edges;
425
426                                 segment->get_subsampled_edges(edges,
427                                         0, segment->get_sample_count() - 1,
428                                         LogicSegment::MipMapScaleFactor,
429                                         signal->index());
430
431                                 if (edges.size() > 2)
432                                         return false;
433                         }
434
435                         // No edges detected in any of the segments
436                         return true;
437                 });
438 }
439
440 }  // namespace popups
441 }  // namespace pv