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_, 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);
135 layout_.addRow(&buttons_bar_);
137 // Connect the check-box signal mapper
138 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
139 this, SLOT(on_channel_checked(QWidget*)));
142 void Channels::set_all_channels(bool set)
144 updating_channels_ = true;
146 for (auto entry : check_box_signal_map_) {
147 QCheckBox *cb = entry.first;
148 const shared_ptr<SignalBase> sig = entry.second;
151 sig->set_enabled(set);
155 updating_channels_ = false;
158 void Channels::set_all_channels_conditionally(
159 function<bool (const shared_ptr<data::SignalBase>)> cond_func)
161 updating_channels_ = true;
163 for (auto entry : check_box_signal_map_) {
164 QCheckBox *cb = entry.first;
165 const shared_ptr<SignalBase> sig = entry.second;
168 const bool state = cond_func(sig);
169 sig->set_enabled(state);
170 cb->setChecked(state);
173 updating_channels_ = false;
176 void Channels::populate_group(shared_ptr<ChannelGroup> group,
177 const vector< shared_ptr<SignalBase> > sigs)
179 using pv::binding::Device;
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
184 shared_ptr<Device> binding;
186 binding = make_shared<Device>(group);
188 // Create a title if the group is going to have any content
189 if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
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;
197 // Create the channel group grid
198 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
199 layout_.addRow(channel_grid);
201 // Create the channel group options
203 binding->add_properties_to_form(&layout_, true);
204 group_bindings_.push_back(binding);
208 QGridLayout* Channels::create_channel_group_grid(
209 const vector< shared_ptr<SignalBase> > sigs)
211 int row = 0, col = 0;
212 QGridLayout *const grid = new QGridLayout();
214 for (const shared_ptr<SignalBase>& sig : sigs) {
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()));
222 grid->addWidget(checkbox, row, col);
224 check_box_signal_map_[checkbox] = sig;
233 void Channels::showEvent(QShowEvent *event)
235 pv::widgets::Popup::showEvent(event);
237 const shared_ptr<sigrok::Device> device = session_.device()->device();
240 // Update group labels
241 for (auto entry : device->channel_groups()) {
242 const shared_ptr<ChannelGroup> group = entry.second;
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&) {
252 updating_channels_ = true;
254 for (auto entry : check_box_signal_map_) {
255 QCheckBox *cb = entry.first;
256 const shared_ptr<SignalBase> sig = entry.second;
259 // Update the check box
260 cb->setChecked(sig->enabled());
261 cb->setText(sig->display_name());
264 updating_channels_ = false;
267 void Channels::on_channel_checked(QWidget *widget)
269 if (updating_channels_)
272 QCheckBox *const check_box = (QCheckBox*)widget;
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());
280 const shared_ptr<SignalBase> s = (*iter).second;
283 s->set_enabled(check_box->isChecked());
286 void Channels::enable_all_channels()
288 set_all_channels(true);
291 void Channels::disable_all_channels()
293 set_all_channels(false);
296 void Channels::enable_all_logic_channels()
298 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
299 { return signal->type() == SignalBase::LogicChannel; });
302 void Channels::enable_all_analog_channels()
304 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
305 { return signal->type() == SignalBase::AnalogChannel; });
308 void Channels::enable_all_named_channels()
310 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
311 { return signal->name() != signal->internal_name(); });
314 void Channels::enable_all_changing_channels()
316 set_all_channels_conditionally([](const shared_ptr<SignalBase> signal)
318 // Non-logic channels are considered to always have a signal
319 if (signal->type() != SignalBase::LogicChannel)
322 const shared_ptr<Logic> logic = signal->logic_data();
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;
329 segment->get_subsampled_edges(edges,
330 0, segment->get_sample_count() - 1,
331 LogicSegment::MipMapScaleFactor,
334 if (edges.size() > 2)
338 // No edges detected in any of the segments
343 } // namespace popups