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