2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
22 #include <QApplication>
24 #include <QFontMetrics>
25 #include <QFormLayout>
26 #include <QGridLayout>
27 #include <QHBoxLayout>
30 #include "channels.hpp"
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>
39 using std::make_shared;
41 using std::out_of_range;
42 using std::shared_ptr;
43 using std::unordered_set;
46 using pv::data::SignalBase;
47 using pv::data::Logic;
48 using pv::data::LogicSegment;
50 using sigrok::Channel;
51 using sigrok::ChannelGroup;
57 Channels::Channels(Session &session, QWidget *parent) :
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)
76 const shared_ptr<sigrok::Device> device = session_.device()->device();
79 // Collect a set of signals
80 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
82 unordered_set< shared_ptr<SignalBase> > sigs;
83 for (const shared_ptr<data::SignalBase>& b : session_.signalbases())
86 for (const shared_ptr<SignalBase> &sig : sigs)
87 signal_map[sig->channel()] = sig;
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);
97 if (iter == signal_map.end())
100 group_sigs.push_back((*iter).second);
101 signal_map.erase(iter);
104 populate_group(group, group_sigs);
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);
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);
118 global_logic_sigs.push_back(signal);
122 // Create the groups for the ungrouped channels
123 populate_group(nullptr, global_logic_sigs);
124 populate_group(nullptr, global_analog_sigs);
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()));
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);
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);
156 layout_.addItem(new QSpacerItem(0, 15, QSizePolicy::Expanding, QSizePolicy::Expanding));
157 layout_.addRow(&filter_buttons_bar_);
159 // Connect the check-box signal mapper
160 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
161 this, SLOT(on_channel_checked(QWidget*)));
164 void Channels::set_all_channels(bool set)
166 updating_channels_ = true;
168 for (auto& entry : check_box_signal_map_) {
169 QCheckBox *cb = entry.first;
170 const shared_ptr<SignalBase> sig = entry.second;
173 sig->set_enabled(set);
177 updating_channels_ = false;
180 void Channels::enable_channels_conditionally(
181 function<bool (const shared_ptr<data::SignalBase>)> cond_func)
183 updating_channels_ = true;
185 for (auto& entry : check_box_signal_map_) {
186 QCheckBox *cb = entry.first;
187 const shared_ptr<SignalBase> sig = entry.second;
190 if (cond_func(sig)) {
191 sig->set_enabled(true);
192 cb->setChecked(true);
196 updating_channels_ = false;
199 void Channels::disable_channels_conditionally(
200 function<bool (const shared_ptr<data::SignalBase>)> cond_func)
202 updating_channels_ = true;
204 for (auto& entry : check_box_signal_map_) {
205 QCheckBox *cb = entry.first;
206 const shared_ptr<SignalBase> sig = entry.second;
209 if (cond_func(sig)) {
210 sig->set_enabled(false);
211 cb->setChecked(false);
215 updating_channels_ = false;
218 void Channels::populate_group(shared_ptr<ChannelGroup> group,
219 const vector< shared_ptr<SignalBase> > sigs)
221 using pv::binding::Device;
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
226 shared_ptr<Device> binding;
228 binding = make_shared<Device>(group);
230 // Create a title if the group is going to have any content
231 if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
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;
239 // Create the channel group grid
240 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
241 layout_.addRow(channel_grid);
243 // Create the channel group options
245 binding->add_properties_to_form(&layout_, true);
246 group_bindings_.push_back(binding);
250 QGridLayout* Channels::create_channel_group_grid(
251 const vector< shared_ptr<SignalBase> > sigs)
253 int row = 0, col = 0;
254 QGridLayout *const grid = new QGridLayout();
256 for (const shared_ptr<SignalBase>& sig : sigs) {
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()));
264 grid->addWidget(checkbox, row, col);
266 check_box_signal_map_[checkbox] = sig;
275 void Channels::showEvent(QShowEvent *event)
277 pv::widgets::Popup::showEvent(event);
279 const shared_ptr<sigrok::Device> device = session_.device()->device();
282 // Update group labels
283 for (auto& entry : device->channel_groups()) {
284 const shared_ptr<ChannelGroup> group = entry.second;
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&) {
294 updating_channels_ = true;
296 for (auto& entry : check_box_signal_map_) {
297 QCheckBox *cb = entry.first;
298 const shared_ptr<SignalBase> sig = entry.second;
301 // Update the check box
302 cb->setChecked(sig->enabled());
303 cb->setText(sig->display_name());
306 updating_channels_ = false;
309 void Channels::on_channel_checked(QWidget *widget)
311 if (updating_channels_)
314 QCheckBox *const check_box = (QCheckBox*)widget;
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());
322 const shared_ptr<SignalBase> s = (*iter).second;
325 s->set_enabled(check_box->isChecked());
328 void Channels::enable_all_channels()
330 set_all_channels(true);
333 void Channels::disable_all_channels()
335 set_all_channels(false);
338 void Channels::enable_all_logic_channels()
340 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
341 { return signal->type() == SignalBase::LogicChannel; });
344 void Channels::disable_all_logic_channels()
346 disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
347 { return signal->type() == SignalBase::LogicChannel; });
350 void Channels::enable_all_analog_channels()
352 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
353 { return signal->type() == SignalBase::AnalogChannel; });
356 void Channels::disable_all_analog_channels()
358 disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
359 { return signal->type() == SignalBase::AnalogChannel; });
362 void Channels::enable_all_named_channels()
364 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
365 { return signal->name() != signal->internal_name(); });
368 void Channels::disable_all_unnamed_channels()
370 disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
371 { return signal->name() == signal->internal_name(); });
374 void Channels::enable_all_changing_channels()
376 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
378 // Never enable channels without sample data
379 if (!signal->has_samples())
382 // Non-logic channels are considered to always have a signal
383 if (signal->type() != SignalBase::LogicChannel)
386 const shared_ptr<Logic> logic = signal->logic_data();
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;
393 segment->get_subsampled_edges(edges,
394 0, segment->get_sample_count() - 1,
395 LogicSegment::MipMapScaleFactor,
398 if (edges.size() > 2)
402 // No edges detected in any of the segments
407 void Channels::disable_all_non_changing_channels()
409 disable_channels_conditionally([](const shared_ptr<SignalBase> signal)
411 // Always disable channels without sample data
412 if (!signal->has_samples())
415 // Non-logic channels are considered to always have a signal
416 if (signal->type() != SignalBase::LogicChannel)
419 const shared_ptr<Logic> logic = signal->logic_data();
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;
426 segment->get_subsampled_edges(edges,
427 0, segment->get_sample_count() - 1,
428 LogicSegment::MipMapScaleFactor,
431 if (edges.size() > 2)
435 // No edges detected in any of the segments
440 } // namespace popups