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