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