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/signalbase.hpp>
32 #include <pv/devices/device.hpp>
34 using std::make_shared;
36 using std::out_of_range;
37 using std::shared_ptr;
38 using std::unordered_set;
41 using pv::data::SignalBase;
43 using sigrok::Channel;
44 using sigrok::ChannelGroup;
50 Channels::Channels(Session &session, QWidget *parent) :
53 updating_channels_(false),
54 enable_all_channels_(tr("Enable All"), this),
55 disable_all_channels_(tr("Disable All"), this),
56 check_box_mapper_(this)
61 const shared_ptr<sigrok::Device> device = session_.device()->device();
64 // Collect a set of signals
65 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
67 unordered_set< shared_ptr<SignalBase> > sigs;
68 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
71 for (const shared_ptr<SignalBase> &sig : sigs)
72 signal_map[sig->channel()] = sig;
74 // Populate channel groups
75 for (auto entry : device->channel_groups()) {
76 const shared_ptr<ChannelGroup> group = entry.second;
77 // Make a set of signals and remove these signals from the signal map
78 vector< shared_ptr<SignalBase> > group_sigs;
79 for (auto channel : group->channels()) {
80 const auto iter = signal_map.find(channel);
82 if (iter == signal_map.end())
85 group_sigs.push_back((*iter).second);
86 signal_map.erase(iter);
89 populate_group(group, group_sigs);
92 // Make a vector of the remaining channels
93 vector< shared_ptr<SignalBase> > global_sigs;
94 for (auto channel : device->channels()) {
95 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
96 const_iterator iter = signal_map.find(channel);
97 if (iter != signal_map.end())
98 global_sigs.push_back((*iter).second);
102 populate_group(nullptr, global_sigs);
104 // Create the enable/disable all buttons
105 connect(&enable_all_channels_, SIGNAL(clicked()),
106 this, SLOT(enable_all_channels()));
107 connect(&disable_all_channels_, SIGNAL(clicked()),
108 this, SLOT(disable_all_channels()));
110 enable_all_channels_.setFlat(true);
111 disable_all_channels_.setFlat(true);
113 buttons_bar_.addWidget(&enable_all_channels_);
114 buttons_bar_.addWidget(&disable_all_channels_);
115 buttons_bar_.addStretch(1);
117 layout_.addRow(&buttons_bar_);
119 // Connect the check-box signal mapper
120 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
121 this, SLOT(on_channel_checked(QWidget*)));
124 void Channels::set_all_channels(bool set)
126 updating_channels_ = true;
128 for (auto entry : check_box_signal_map_) {
129 QCheckBox *cb = entry.first;
130 const shared_ptr<SignalBase> sig = entry.second;
133 sig->set_enabled(set);
137 updating_channels_ = false;
140 void Channels::populate_group(shared_ptr<ChannelGroup> group,
141 const vector< shared_ptr<SignalBase> > sigs)
143 using pv::binding::Device;
145 // Only bind options if this is a group. We don't do it for general
146 // options, because these properties are shown in the device config
148 shared_ptr<Device> binding;
150 binding = make_shared<Device>(group);
152 // Create a title if the group is going to have any content
153 if ((!sigs.empty() || (binding && !binding->properties().empty())) && group)
155 QLabel *label = new QLabel(
156 QString("<h3>%1</h3>").arg(group->name().c_str()));
157 layout_.addRow(label);
158 group_label_map_[group] = label;
161 // Create the channel group grid
162 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
163 layout_.addRow(channel_grid);
165 // Create the channel group options
167 binding->add_properties_to_form(&layout_, true);
168 group_bindings_.push_back(binding);
172 QGridLayout* Channels::create_channel_group_grid(
173 const vector< shared_ptr<SignalBase> > sigs)
175 int row = 0, col = 0;
176 QGridLayout *const grid = new QGridLayout();
178 for (const shared_ptr<SignalBase>& sig : sigs) {
181 QCheckBox *const checkbox = new QCheckBox(sig->display_name());
182 check_box_mapper_.setMapping(checkbox, checkbox);
183 connect(checkbox, SIGNAL(toggled(bool)),
184 &check_box_mapper_, SLOT(map()));
186 grid->addWidget(checkbox, row, col);
188 check_box_signal_map_[checkbox] = sig;
197 void Channels::showEvent(QShowEvent *event)
199 pv::widgets::Popup::showEvent(event);
201 const shared_ptr<sigrok::Device> device = session_.device()->device();
204 // Update group labels
205 for (auto entry : device->channel_groups()) {
206 const shared_ptr<ChannelGroup> group = entry.second;
209 QLabel* label = group_label_map_.at(group);
210 label->setText(QString("<h3>%1</h3>").arg(group->name().c_str()));
211 } catch (out_of_range&) {
216 updating_channels_ = true;
218 for (auto entry : check_box_signal_map_) {
219 QCheckBox *cb = entry.first;
220 const shared_ptr<SignalBase> sig = entry.second;
223 // Update the check box
224 cb->setChecked(sig->enabled());
225 cb->setText(sig->display_name());
228 updating_channels_ = false;
231 void Channels::on_channel_checked(QWidget *widget)
233 if (updating_channels_)
236 QCheckBox *const check_box = (QCheckBox*)widget;
239 // Look up the signal of this check-box
240 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
241 check_box_signal_map_.find((QCheckBox*)check_box);
242 assert(iter != check_box_signal_map_.end());
244 const shared_ptr<SignalBase> s = (*iter).second;
247 s->set_enabled(check_box->isChecked());
250 void Channels::enable_all_channels()
252 set_all_channels(true);
255 void Channels::disable_all_channels()
257 set_all_channels(false);
260 } // namespace popups