]> sigrok.org Git - pulseview.git/blob - pv/popups/channels.cpp
Channels: Re-arrange selector buttons
[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_, 0, 0);
129         buttons_bar_.addWidget(&disable_all_channels_, 0, 1);
130         buttons_bar_.addWidget(&enable_all_logic_channels_, 1, 0);
131         buttons_bar_.addWidget(&enable_all_analog_channels_, 1, 1);
132         buttons_bar_.addWidget(&enable_all_named_channels_, 1, 2);
133         buttons_bar_.addWidget(&enable_all_changing_channels_, 1, 3);
134
135         layout_.addRow(&buttons_bar_);
136
137         // Connect the check-box signal mapper
138         connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
139                 this, SLOT(on_channel_checked(QWidget*)));
140 }
141
142 void Channels::set_all_channels(bool set)
143 {
144         updating_channels_ = true;
145
146         for (auto entry : check_box_signal_map_) {
147                 QCheckBox *cb = entry.first;
148                 const shared_ptr<SignalBase> sig = entry.second;
149                 assert(sig);
150
151                 sig->set_enabled(set);
152                 cb->setChecked(set);
153         }
154
155         updating_channels_ = false;
156 }
157
158 void Channels::set_all_channels_conditionally(
159         function<bool (const shared_ptr<data::SignalBase>)> cond_func)
160 {
161         updating_channels_ = true;
162
163         for (auto entry : check_box_signal_map_) {
164                 QCheckBox *cb = entry.first;
165                 const shared_ptr<SignalBase> sig = entry.second;
166                 assert(sig);
167
168                 const bool state = cond_func(sig);
169                 sig->set_enabled(state);
170                 cb->setChecked(state);
171         }
172
173         updating_channels_ = false;
174 }
175
176 void Channels::populate_group(shared_ptr<ChannelGroup> group,
177         const vector< shared_ptr<SignalBase> > sigs)
178 {
179         using pv::binding::Device;
180
181         // Only bind options if this is a group. We don't do it for general
182         // options, because these properties are shown in the device config
183         // popup.
184         shared_ptr<Device> binding;
185         if (group)
186                 binding = make_shared<Device>(group);
187
188         // Create a title if the group is going to have any content
189         if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
190         {
191                 QLabel *label = new QLabel(
192                         QString("<h3>%1</h3>").arg(group->name().c_str()));
193                 layout_.addRow(label);
194                 group_label_map_[group] = label;
195         }
196
197         // Create the channel group grid
198         QGridLayout *const channel_grid = create_channel_group_grid(sigs);
199         layout_.addRow(channel_grid);
200
201         // Create the channel group options
202         if (binding) {
203                 binding->add_properties_to_form(&layout_, true);
204                 group_bindings_.push_back(binding);
205         }
206 }
207
208 QGridLayout* Channels::create_channel_group_grid(
209         const vector< shared_ptr<SignalBase> > sigs)
210 {
211         int row = 0, col = 0;
212         QGridLayout *const grid = new QGridLayout();
213
214         for (const shared_ptr<SignalBase>& sig : sigs) {
215                 assert(sig);
216
217                 QCheckBox *const checkbox = new QCheckBox(sig->display_name());
218                 check_box_mapper_.setMapping(checkbox, checkbox);
219                 connect(checkbox, SIGNAL(toggled(bool)),
220                         &check_box_mapper_, SLOT(map()));
221
222                 grid->addWidget(checkbox, row, col);
223
224                 check_box_signal_map_[checkbox] = sig;
225
226                 if (++col >= 8)
227                         col = 0, row++;
228         }
229
230         return grid;
231 }
232
233 void Channels::showEvent(QShowEvent *event)
234 {
235         pv::widgets::Popup::showEvent(event);
236
237         const shared_ptr<sigrok::Device> device = session_.device()->device();
238         assert(device);
239
240         // Update group labels
241         for (auto entry : device->channel_groups()) {
242                 const shared_ptr<ChannelGroup> group = entry.second;
243
244                 try {
245                         QLabel* label = group_label_map_.at(group);
246                         label->setText(QString("<h3>%1</h3>").arg(group->name().c_str()));
247                 } catch (out_of_range&) {
248                         // Do nothing
249                 }
250         }
251
252         updating_channels_ = true;
253
254         for (auto entry : check_box_signal_map_) {
255                 QCheckBox *cb = entry.first;
256                 const shared_ptr<SignalBase> sig = entry.second;
257                 assert(sig);
258
259                 // Update the check box
260                 cb->setChecked(sig->enabled());
261                 cb->setText(sig->display_name());
262         }
263
264         updating_channels_ = false;
265 }
266
267 void Channels::on_channel_checked(QWidget *widget)
268 {
269         if (updating_channels_)
270                 return;
271
272         QCheckBox *const check_box = (QCheckBox*)widget;
273         assert(check_box);
274
275         // Look up the signal of this check-box
276         map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
277                 check_box_signal_map_.find((QCheckBox*)check_box);
278         assert(iter != check_box_signal_map_.end());
279
280         const shared_ptr<SignalBase> s = (*iter).second;
281         assert(s);
282
283         s->set_enabled(check_box->isChecked());
284 }
285
286 void Channels::enable_all_channels()
287 {
288         set_all_channels(true);
289 }
290
291 void Channels::disable_all_channels()
292 {
293         set_all_channels(false);
294 }
295
296 void Channels::enable_all_logic_channels()
297 {
298         set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
299                 { return signal->type() == SignalBase::LogicChannel; });
300 }
301
302 void Channels::enable_all_analog_channels()
303 {
304         set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
305                 { return signal->type() == SignalBase::AnalogChannel; });
306 }
307
308 void Channels::enable_all_named_channels()
309 {
310         set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
311                 { return signal->name() != signal->internal_name(); });
312 }
313
314 void Channels::enable_all_changing_channels()
315 {
316         set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
317                 {
318                         // Non-logic channels are considered to always have a signal
319                         if (signal->type() != SignalBase::LogicChannel)
320                                 return true;
321
322                         const shared_ptr<Logic> logic = signal->logic_data();
323                         assert(logic);
324
325                         // If any of the segments has edges, enable this channel
326                         for (shared_ptr<LogicSegment> segment : logic->logic_segments()) {
327                                 vector<LogicSegment::EdgePair> edges;
328
329                                 segment->get_subsampled_edges(edges,
330                                         0, segment->get_sample_count() - 1,
331                                         LogicSegment::MipMapScaleFactor,
332                                         signal->index());
333
334                                 if (edges.size() > 2)
335                                         return true;
336                         }
337
338                         // No edges detected in any of the segments
339                         return false;
340                 });
341 }
342
343 }  // namespace popups
344 }  // namespace pv