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 enable_all_analog_channels_(tr("Analog"), this),
65 enable_all_named_channels_(tr("Named"), this),
66 enable_all_changing_channels_(tr("Changing"), this),
67 check_box_mapper_(this)
72 const shared_ptr<sigrok::Device> device = session_.device()->device();
75 // Collect a set of signals
76 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
78 unordered_set< shared_ptr<SignalBase> > sigs;
79 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
82 for (const shared_ptr<SignalBase> &sig : sigs)
83 signal_map[sig->channel()] = sig;
85 // Populate channel groups
86 for (auto entry : device->channel_groups()) {
87 const shared_ptr<ChannelGroup> group = entry.second;
88 // Make a set of signals and remove these signals from the signal map
89 vector< shared_ptr<SignalBase> > group_sigs;
90 for (auto channel : group->channels()) {
91 const auto iter = signal_map.find(channel);
93 if (iter == signal_map.end())
96 group_sigs.push_back((*iter).second);
97 signal_map.erase(iter);
100 populate_group(group, group_sigs);
103 // Make a vector of the remaining channels
104 vector< shared_ptr<SignalBase> > global_analog_sigs, global_logic_sigs;
105 for (auto channel : device->channels()) {
106 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
107 const_iterator iter = signal_map.find(channel);
109 if (iter != signal_map.end()) {
110 const shared_ptr<SignalBase> signal = (*iter).second;
111 if (signal->type() == SignalBase::AnalogChannel)
112 global_analog_sigs.push_back(signal);
114 global_logic_sigs.push_back(signal);
118 // Create the groups for the ungrouped channels
119 populate_group(nullptr, global_logic_sigs);
120 populate_group(nullptr, global_analog_sigs);
122 // Create the enable/disable all buttons
123 connect(&enable_all_channels_, SIGNAL(clicked()), this, SLOT(enable_all_channels()));
124 connect(&disable_all_channels_, SIGNAL(clicked()), this, SLOT(disable_all_channels()));
125 connect(&enable_all_logic_channels_, SIGNAL(clicked()), this, SLOT(enable_all_logic_channels()));
126 connect(&enable_all_analog_channels_, SIGNAL(clicked()), this, SLOT(enable_all_analog_channels()));
127 connect(&enable_all_named_channels_, SIGNAL(clicked()), this, SLOT(enable_all_named_channels()));
128 connect(&enable_all_changing_channels_, SIGNAL(clicked()),
129 this, SLOT(enable_all_changing_channels()));
131 QLabel *label1 = new QLabel(tr("Disable: "));
132 buttons_bar_.addWidget(label1);
133 buttons_bar_.addWidget(&disable_all_channels_);
134 QLabel *label2 = new QLabel(tr("Enable: "));
135 buttons_bar_.addWidget(label2);
136 buttons_bar_.addWidget(&enable_all_channels_);
137 buttons_bar_.addWidget(&enable_all_logic_channels_);
138 buttons_bar_.addWidget(&enable_all_analog_channels_);
139 buttons_bar_.addWidget(&enable_all_named_channels_);
140 buttons_bar_.addWidget(&enable_all_changing_channels_);
141 buttons_bar_.addStretch();
143 layout_.addRow(&buttons_bar_);
145 // Connect the check-box signal mapper
146 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
147 this, SLOT(on_channel_checked(QWidget*)));
150 void Channels::set_all_channels(bool set)
152 updating_channels_ = true;
154 for (auto entry : check_box_signal_map_) {
155 QCheckBox *cb = entry.first;
156 const shared_ptr<SignalBase> sig = entry.second;
159 sig->set_enabled(set);
163 updating_channels_ = false;
166 void Channels::enable_channels_conditionally(
167 function<bool (const shared_ptr<data::SignalBase>)> cond_func)
169 updating_channels_ = true;
171 for (auto entry : check_box_signal_map_) {
172 QCheckBox *cb = entry.first;
173 const shared_ptr<SignalBase> sig = entry.second;
176 const bool state = cond_func(sig);
178 sig->set_enabled(state);
179 cb->setChecked(state);
183 updating_channels_ = false;
186 void Channels::populate_group(shared_ptr<ChannelGroup> group,
187 const vector< shared_ptr<SignalBase> > sigs)
189 using pv::binding::Device;
191 // Only bind options if this is a group. We don't do it for general
192 // options, because these properties are shown in the device config
194 shared_ptr<Device> binding;
196 binding = make_shared<Device>(group);
198 // Create a title if the group is going to have any content
199 if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
201 QLabel *label = new QLabel(
202 QString("<h3>%1</h3>").arg(group->name().c_str()));
203 layout_.addRow(label);
204 group_label_map_[group] = label;
207 // Create the channel group grid
208 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
209 layout_.addRow(channel_grid);
211 // Create the channel group options
213 binding->add_properties_to_form(&layout_, true);
214 group_bindings_.push_back(binding);
218 QGridLayout* Channels::create_channel_group_grid(
219 const vector< shared_ptr<SignalBase> > sigs)
221 int row = 0, col = 0;
222 QGridLayout *const grid = new QGridLayout();
224 for (const shared_ptr<SignalBase>& sig : sigs) {
227 QCheckBox *const checkbox = new QCheckBox(sig->display_name());
228 check_box_mapper_.setMapping(checkbox, checkbox);
229 connect(checkbox, SIGNAL(toggled(bool)),
230 &check_box_mapper_, SLOT(map()));
232 grid->addWidget(checkbox, row, col);
234 check_box_signal_map_[checkbox] = sig;
243 void Channels::showEvent(QShowEvent *event)
245 pv::widgets::Popup::showEvent(event);
247 const shared_ptr<sigrok::Device> device = session_.device()->device();
250 // Update group labels
251 for (auto entry : device->channel_groups()) {
252 const shared_ptr<ChannelGroup> group = entry.second;
255 QLabel* label = group_label_map_.at(group);
256 label->setText(QString("<h3>%1</h3>").arg(group->name().c_str()));
257 } catch (out_of_range&) {
262 updating_channels_ = true;
264 for (auto entry : check_box_signal_map_) {
265 QCheckBox *cb = entry.first;
266 const shared_ptr<SignalBase> sig = entry.second;
269 // Update the check box
270 cb->setChecked(sig->enabled());
271 cb->setText(sig->display_name());
274 updating_channels_ = false;
277 void Channels::on_channel_checked(QWidget *widget)
279 if (updating_channels_)
282 QCheckBox *const check_box = (QCheckBox*)widget;
285 // Look up the signal of this check-box
286 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
287 check_box_signal_map_.find((QCheckBox*)check_box);
288 assert(iter != check_box_signal_map_.end());
290 const shared_ptr<SignalBase> s = (*iter).second;
293 s->set_enabled(check_box->isChecked());
296 void Channels::enable_all_channels()
298 set_all_channels(true);
301 void Channels::disable_all_channels()
303 set_all_channels(false);
306 void Channels::enable_all_logic_channels()
308 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
309 { return signal->type() == SignalBase::LogicChannel; });
312 void Channels::enable_all_analog_channels()
314 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
315 { return signal->type() == SignalBase::AnalogChannel; });
318 void Channels::enable_all_named_channels()
320 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
321 { return signal->name() != signal->internal_name(); });
324 void Channels::enable_all_changing_channels()
326 enable_channels_conditionally([](const shared_ptr<SignalBase> signal)
328 // Never enable channels without sample data
329 if (!signal->has_samples())
332 // Non-logic channels are considered to always have a signal
333 if (signal->type() != SignalBase::LogicChannel)
336 const shared_ptr<Logic> logic = signal->logic_data();
339 // If any of the segments has edges, enable this channel
340 for (shared_ptr<LogicSegment> segment : logic->logic_segments()) {
341 vector<LogicSegment::EdgePair> edges;
343 segment->get_subsampled_edges(edges,
344 0, segment->get_sample_count() - 1,
345 LogicSegment::MipMapScaleFactor,
348 if (edges.size() > 2)
352 // No edges detected in any of the segments
357 } // namespace popups