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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <boost/thread/locks.hpp>
24 #include <boost/thread/shared_mutex.hpp>
27 #include <QFormLayout>
28 #include <QGridLayout>
31 #include "channels.hpp"
33 #include <pv/binding/device.hpp>
34 #include <pv/devices/device.hpp>
35 #include <pv/session.hpp>
36 #include <pv/view/signal.hpp>
38 #include <libsigrokcxx/libsigrokcxx.hpp>
42 using boost::shared_lock;
43 using boost::shared_mutex;
44 using std::lock_guard;
48 using std::shared_ptr;
49 using std::unordered_set;
52 using sigrok::Channel;
53 using sigrok::ChannelGroup;
56 using pv::view::Signal;
61 Channels::Channels(Session &session, QWidget *parent) :
64 updating_channels_(false),
65 enable_all_channels_(tr("Enable All"), this),
66 disable_all_channels_(tr("Disable All"), 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<Signal> > signal_map;
78 shared_lock<shared_mutex> lock(session_.signals_mutex());
79 const unordered_set< shared_ptr<Signal> > &sigs(session_.signals());
81 for (const shared_ptr<Signal> &sig : sigs)
82 signal_map[sig->channel()] = sig;
84 // Populate channel groups
85 for (auto entry : device->channel_groups())
87 shared_ptr<ChannelGroup> group = entry.second;
88 // Make a set of signals, and removed this signals from the
90 vector< shared_ptr<Signal> > group_sigs;
91 for (auto channel : group->channels())
93 const auto iter = signal_map.find(channel);
95 if (iter == signal_map.end())
98 group_sigs.push_back((*iter).second);
99 signal_map.erase(iter);
102 populate_group(group, group_sigs);
105 // Make a vector of the remaining channels
106 vector< shared_ptr<Signal> > global_sigs;
107 for (auto channel : device->channels())
109 const map<shared_ptr<Channel>, shared_ptr<Signal> >::
110 const_iterator iter = signal_map.find(channel);
111 if (iter != signal_map.end())
112 global_sigs.push_back((*iter).second);
116 populate_group(nullptr, global_sigs);
118 // Create the enable/disable all buttons
119 connect(&enable_all_channels_, SIGNAL(clicked()),
120 this, SLOT(enable_all_channels()));
121 connect(&disable_all_channels_, SIGNAL(clicked()),
122 this, SLOT(disable_all_channels()));
124 enable_all_channels_.setFlat(true);
125 disable_all_channels_.setFlat(true);
127 buttons_bar_.addWidget(&enable_all_channels_);
128 buttons_bar_.addWidget(&disable_all_channels_);
129 buttons_bar_.addStretch(1);
131 layout_.addRow(&buttons_bar_);
133 // Connect the check-box signal mapper
134 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
135 this, SLOT(on_channel_checked(QWidget*)));
138 void Channels::set_all_channels(bool set)
140 updating_channels_ = true;
142 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
143 check_box_signal_map_.begin();
144 i != check_box_signal_map_.end(); i++)
146 const shared_ptr<Signal> sig = (*i).second;
150 (*i).first->setChecked(set);
153 updating_channels_ = false;
156 void Channels::populate_group(shared_ptr<ChannelGroup> group,
157 const vector< shared_ptr<pv::view::Signal> > sigs)
159 using pv::binding::Device;
161 // Only bind options if this is a group. We don't do it for general
162 // options, because these properties are shown in the device config
164 shared_ptr<Device> binding;
166 binding = shared_ptr<Device>(new Device(group));
168 // Create a title if the group is going to have any content
169 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
171 layout_.addRow(new QLabel(
172 QString("<h3>%1</h3>").arg(group->name().c_str())));
174 // Create the channel group grid
175 QGridLayout *const channel_grid =
176 create_channel_group_grid(sigs);
177 layout_.addRow(channel_grid);
179 // Create the channel group options
182 binding->add_properties_to_form(&layout_, true);
183 group_bindings_.push_back(binding);
187 QGridLayout* Channels::create_channel_group_grid(
188 const vector< shared_ptr<pv::view::Signal> > sigs)
190 int row = 0, col = 0;
191 QGridLayout *const grid = new QGridLayout();
193 for (const shared_ptr<pv::view::Signal>& sig : sigs)
197 QCheckBox *const checkbox = new QCheckBox(sig->name());
198 check_box_mapper_.setMapping(checkbox, checkbox);
199 connect(checkbox, SIGNAL(toggled(bool)),
200 &check_box_mapper_, SLOT(map()));
202 grid->addWidget(checkbox, row, col);
204 check_box_signal_map_[checkbox] = sig;
213 void Channels::showEvent(QShowEvent *e)
215 pv::widgets::Popup::showEvent(e);
217 updating_channels_ = true;
219 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
220 check_box_signal_map_.begin();
221 i != check_box_signal_map_.end(); i++)
223 const shared_ptr<Signal> sig = (*i).second;
226 (*i).first->setChecked(sig->enabled());
229 updating_channels_ = false;
232 void Channels::on_channel_checked(QWidget *widget)
234 if (updating_channels_)
237 QCheckBox *const check_box = (QCheckBox*)widget;
240 // Look up the signal of this check-box
241 map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
242 check_box_signal_map_.find((QCheckBox*)check_box);
243 assert(iter != check_box_signal_map_.end());
245 const shared_ptr<pv::view::Signal> s = (*iter).second;
248 s->enable(check_box->isChecked());
251 void Channels::enable_all_channels()
253 set_all_channels(true);
256 void Channels::disable_all_channels()
258 set_all_channels(false);