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/>.
23 #include <QFormLayout>
24 #include <QGridLayout>
27 #include "channels.hpp"
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>
36 using std::make_shared;
38 using std::out_of_range;
39 using std::shared_ptr;
40 using std::unordered_set;
43 using pv::data::SignalBase;
44 using pv::data::Logic;
45 using pv::data::LogicSegment;
47 using sigrok::Channel;
48 using sigrok::ChannelGroup;
54 Channels::Channels(Session &session, QWidget *parent) :
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)
69 const shared_ptr<sigrok::Device> device = session_.device()->device();
72 // Collect a set of signals
73 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
75 unordered_set< shared_ptr<SignalBase> > sigs;
76 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
79 for (const shared_ptr<SignalBase> &sig : sigs)
80 signal_map[sig->channel()] = sig;
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);
90 if (iter == signal_map.end())
93 group_sigs.push_back((*iter).second);
94 signal_map.erase(iter);
97 populate_group(group, group_sigs);
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);
110 populate_group(nullptr, global_sigs);
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()));
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);
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);
136 layout_.addRow(&buttons_bar_);
138 // Connect the check-box signal mapper
139 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
140 this, SLOT(on_channel_checked(QWidget*)));
143 void Channels::set_all_channels(bool set)
145 updating_channels_ = true;
147 for (auto entry : check_box_signal_map_) {
148 QCheckBox *cb = entry.first;
149 const shared_ptr<SignalBase> sig = entry.second;
152 sig->set_enabled(set);
156 updating_channels_ = false;
159 void Channels::set_all_channels_conditionally(
160 function<bool (const shared_ptr<data::SignalBase>)> cond_func)
162 updating_channels_ = true;
164 for (auto entry : check_box_signal_map_) {
165 QCheckBox *cb = entry.first;
166 const shared_ptr<SignalBase> sig = entry.second;
169 const bool state = cond_func(sig);
170 sig->set_enabled(state);
171 cb->setChecked(state);
174 updating_channels_ = false;
177 void Channels::populate_group(shared_ptr<ChannelGroup> group,
178 const vector< shared_ptr<SignalBase> > sigs)
180 using pv::binding::Device;
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
185 shared_ptr<Device> binding;
187 binding = make_shared<Device>(group);
189 // Create a title if the group is going to have any content
190 if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
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;
198 // Create the channel group grid
199 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
200 layout_.addRow(channel_grid);
202 // Create the channel group options
204 binding->add_properties_to_form(&layout_, true);
205 group_bindings_.push_back(binding);
209 QGridLayout* Channels::create_channel_group_grid(
210 const vector< shared_ptr<SignalBase> > sigs)
212 int row = 0, col = 0;
213 QGridLayout *const grid = new QGridLayout();
215 for (const shared_ptr<SignalBase>& sig : sigs) {
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()));
223 grid->addWidget(checkbox, row, col);
225 check_box_signal_map_[checkbox] = sig;
234 void Channels::showEvent(QShowEvent *event)
236 pv::widgets::Popup::showEvent(event);
238 const shared_ptr<sigrok::Device> device = session_.device()->device();
241 // Update group labels
242 for (auto entry : device->channel_groups()) {
243 const shared_ptr<ChannelGroup> group = entry.second;
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&) {
253 updating_channels_ = true;
255 for (auto entry : check_box_signal_map_) {
256 QCheckBox *cb = entry.first;
257 const shared_ptr<SignalBase> sig = entry.second;
260 // Update the check box
261 cb->setChecked(sig->enabled());
262 cb->setText(sig->display_name());
265 updating_channels_ = false;
268 void Channels::on_channel_checked(QWidget *widget)
270 if (updating_channels_)
273 QCheckBox *const check_box = (QCheckBox*)widget;
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());
281 const shared_ptr<SignalBase> s = (*iter).second;
284 s->set_enabled(check_box->isChecked());
287 void Channels::enable_all_channels()
289 set_all_channels(true);
292 void Channels::disable_all_channels()
294 set_all_channels(false);
297 void Channels::enable_all_logic_channels()
299 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
300 { return signal->type() == SignalBase::LogicChannel; });
303 void Channels::enable_all_analog_channels()
305 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
306 { return signal->type() == SignalBase::AnalogChannel; });
309 void Channels::enable_all_named_channels()
311 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
312 { return signal->name() != signal->internal_name(); });
315 void Channels::enable_all_changing_channels()
317 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
319 // Non-logic channels are considered to always have a signal
320 if (signal->type() != SignalBase::LogicChannel)
323 const shared_ptr<Logic> logic = signal->logic_data();
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;
330 segment->get_subsampled_edges(edges,
331 0, segment->get_sample_count() - 1,
332 LogicSegment::MipMapScaleFactor,
335 if (edges.size() > 2)
339 // No edges detected in any of the segments
344 } // namespace popups