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 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
29 #include <QFormLayout>
30 #include <QGridLayout>
33 #include "channels.hpp"
35 #include <pv/binding/device.hpp>
36 #include <pv/data/signalbase.hpp>
37 #include <pv/devices/device.hpp>
38 #include <pv/session.hpp>
39 #include <pv/view/signal.hpp>
41 #include <libsigrokcxx/libsigrokcxx.hpp>
46 using std::shared_ptr;
47 using std::make_shared;
48 using std::unordered_set;
51 using pv::data::SignalBase;
53 using sigrok::Channel;
54 using sigrok::ChannelGroup;
60 Channels::Channels(Session &session, QWidget *parent) :
63 updating_channels_(false),
64 enable_all_channels_(tr("Enable All"), this),
65 disable_all_channels_(tr("Disable All"), this),
66 check_box_mapper_(this)
71 const shared_ptr<sigrok::Device> device = session_.device()->device();
74 // Collect a set of signals
75 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
77 unordered_set< shared_ptr<SignalBase> > sigs;
78 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
81 for (const shared_ptr<SignalBase> &sig : sigs)
82 signal_map[sig->channel()] = sig;
84 // Populate channel groups
85 for (auto entry : device->channel_groups()) {
86 shared_ptr<ChannelGroup> group = entry.second;
87 // Make a set of signals, and removed this signals from the
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_sigs;
105 for (auto channel : device->channels()) {
106 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
107 const_iterator iter = signal_map.find(channel);
108 if (iter != signal_map.end())
109 global_sigs.push_back((*iter).second);
113 populate_group(nullptr, global_sigs);
115 // Create the enable/disable all buttons
116 connect(&enable_all_channels_, SIGNAL(clicked()),
117 this, SLOT(enable_all_channels()));
118 connect(&disable_all_channels_, SIGNAL(clicked()),
119 this, SLOT(disable_all_channels()));
121 enable_all_channels_.setFlat(true);
122 disable_all_channels_.setFlat(true);
124 buttons_bar_.addWidget(&enable_all_channels_);
125 buttons_bar_.addWidget(&disable_all_channels_);
126 buttons_bar_.addStretch(1);
128 layout_.addRow(&buttons_bar_);
130 // Connect the check-box signal mapper
131 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
132 this, SLOT(on_channel_checked(QWidget*)));
135 void Channels::set_all_channels(bool set)
137 updating_channels_ = true;
139 for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
140 check_box_signal_map_.begin();
141 i != check_box_signal_map_.end(); i++) {
142 const shared_ptr<SignalBase> sig = (*i).second;
145 sig->set_enabled(set);
146 (*i).first->setChecked(set);
149 updating_channels_ = false;
152 void Channels::populate_group(shared_ptr<ChannelGroup> group,
153 const vector< shared_ptr<SignalBase> > sigs)
155 using pv::binding::Device;
157 // Only bind options if this is a group. We don't do it for general
158 // options, because these properties are shown in the device config
160 shared_ptr<Device> binding;
162 binding = make_shared<Device>(group);
164 // Create a title if the group is going to have any content
165 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
167 layout_.addRow(new QLabel(
168 QString("<h3>%1</h3>").arg(group->name().c_str())));
170 // Create the channel group grid
171 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
172 layout_.addRow(channel_grid);
174 // Create the channel group options
176 binding->add_properties_to_form(&layout_, true);
177 group_bindings_.push_back(binding);
181 QGridLayout* Channels::create_channel_group_grid(
182 const vector< shared_ptr<SignalBase> > sigs)
184 int row = 0, col = 0;
185 QGridLayout *const grid = new QGridLayout();
187 for (const shared_ptr<SignalBase>& sig : sigs) {
190 QCheckBox *const checkbox = new QCheckBox(sig->name());
191 check_box_mapper_.setMapping(checkbox, checkbox);
192 connect(checkbox, SIGNAL(toggled(bool)),
193 &check_box_mapper_, SLOT(map()));
195 grid->addWidget(checkbox, row, col);
197 check_box_signal_map_[checkbox] = sig;
206 void Channels::showEvent(QShowEvent *event)
208 pv::widgets::Popup::showEvent(event);
210 updating_channels_ = true;
212 for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
213 check_box_signal_map_.begin();
214 i != check_box_signal_map_.end(); i++) {
215 const shared_ptr<SignalBase> sig = (*i).second;
218 (*i).first->setChecked(sig->enabled());
221 updating_channels_ = false;
224 void Channels::on_channel_checked(QWidget *widget)
226 if (updating_channels_)
229 QCheckBox *const check_box = (QCheckBox*)widget;
232 // Look up the signal of this check-box
233 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
234 check_box_signal_map_.find((QCheckBox*)check_box);
235 assert(iter != check_box_signal_map_.end());
237 const shared_ptr<SignalBase> s = (*iter).second;
240 s->set_enabled(check_box->isChecked());
243 void Channels::enable_all_channels()
245 set_all_channels(true);
248 void Channels::disable_all_channels()
250 set_all_channels(false);
253 } // namespace popups