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
24 // Windows: Avoid boost/thread namespace pollution (which includes windows.h).
28 #include <boost/thread/locks.hpp>
29 #include <boost/thread/shared_mutex.hpp>
32 #include <QFormLayout>
33 #include <QGridLayout>
36 #include "channels.hpp"
38 #include <pv/binding/device.hpp>
39 #include <pv/devices/device.hpp>
40 #include <pv/session.hpp>
41 #include <pv/view/signal.hpp>
43 #include <libsigrokcxx/libsigrokcxx.hpp>
47 using boost::shared_lock;
48 using boost::shared_mutex;
49 using std::lock_guard;
53 using std::shared_ptr;
54 using std::unordered_set;
57 using sigrok::Channel;
58 using sigrok::ChannelGroup;
61 using pv::view::Signal;
66 Channels::Channels(Session &session, QWidget *parent) :
69 updating_channels_(false),
70 enable_all_channels_(tr("Enable All"), this),
71 disable_all_channels_(tr("Disable All"), this),
72 check_box_mapper_(this)
77 const shared_ptr<sigrok::Device> device = session_.device()->device();
80 // Collect a set of signals
81 map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map;
83 const unordered_set< shared_ptr<Signal> > sigs(session_.signals());
85 for (const shared_ptr<Signal> &sig : sigs)
86 signal_map[sig->channel()] = sig;
88 // Populate channel groups
89 for (auto entry : device->channel_groups())
91 shared_ptr<ChannelGroup> group = entry.second;
92 // Make a set of signals, and removed this signals from the
94 vector< shared_ptr<Signal> > group_sigs;
95 for (auto channel : group->channels())
97 const auto iter = signal_map.find(channel);
99 if (iter == signal_map.end())
102 group_sigs.push_back((*iter).second);
103 signal_map.erase(iter);
106 populate_group(group, group_sigs);
109 // Make a vector of the remaining channels
110 vector< shared_ptr<Signal> > global_sigs;
111 for (auto channel : device->channels())
113 const map<shared_ptr<Channel>, shared_ptr<Signal> >::
114 const_iterator iter = signal_map.find(channel);
115 if (iter != signal_map.end())
116 global_sigs.push_back((*iter).second);
120 populate_group(nullptr, global_sigs);
122 // Create the enable/disable all buttons
123 connect(&enable_all_channels_, SIGNAL(clicked()),
124 this, SLOT(enable_all_channels()));
125 connect(&disable_all_channels_, SIGNAL(clicked()),
126 this, SLOT(disable_all_channels()));
128 enable_all_channels_.setFlat(true);
129 disable_all_channels_.setFlat(true);
131 buttons_bar_.addWidget(&enable_all_channels_);
132 buttons_bar_.addWidget(&disable_all_channels_);
133 buttons_bar_.addStretch(1);
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 (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
147 check_box_signal_map_.begin();
148 i != check_box_signal_map_.end(); i++)
150 const shared_ptr<Signal> sig = (*i).second;
154 (*i).first->setChecked(set);
157 updating_channels_ = false;
160 void Channels::populate_group(shared_ptr<ChannelGroup> group,
161 const vector< shared_ptr<pv::view::Signal> > sigs)
163 using pv::binding::Device;
165 // Only bind options if this is a group. We don't do it for general
166 // options, because these properties are shown in the device config
168 shared_ptr<Device> binding;
170 binding = shared_ptr<Device>(new Device(group));
172 // Create a title if the group is going to have any content
173 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
175 layout_.addRow(new QLabel(
176 QString("<h3>%1</h3>").arg(group->name().c_str())));
178 // Create the channel group grid
179 QGridLayout *const channel_grid =
180 create_channel_group_grid(sigs);
181 layout_.addRow(channel_grid);
183 // Create the channel group options
186 binding->add_properties_to_form(&layout_, true);
187 group_bindings_.push_back(binding);
191 QGridLayout* Channels::create_channel_group_grid(
192 const vector< shared_ptr<pv::view::Signal> > sigs)
194 int row = 0, col = 0;
195 QGridLayout *const grid = new QGridLayout();
197 for (const shared_ptr<pv::view::Signal>& sig : sigs)
201 QCheckBox *const checkbox = new QCheckBox(sig->name());
202 check_box_mapper_.setMapping(checkbox, checkbox);
203 connect(checkbox, SIGNAL(toggled(bool)),
204 &check_box_mapper_, SLOT(map()));
206 grid->addWidget(checkbox, row, col);
208 check_box_signal_map_[checkbox] = sig;
217 void Channels::showEvent(QShowEvent *e)
219 pv::widgets::Popup::showEvent(e);
221 updating_channels_ = true;
223 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
224 check_box_signal_map_.begin();
225 i != check_box_signal_map_.end(); i++)
227 const shared_ptr<Signal> sig = (*i).second;
230 (*i).first->setChecked(sig->enabled());
233 updating_channels_ = false;
236 void Channels::on_channel_checked(QWidget *widget)
238 if (updating_channels_)
241 QCheckBox *const check_box = (QCheckBox*)widget;
244 // Look up the signal of this check-box
245 map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
246 check_box_signal_map_.find((QCheckBox*)check_box);
247 assert(iter != check_box_signal_map_.end());
249 const shared_ptr<pv::view::Signal> s = (*iter).second;
252 s->enable(check_box->isChecked());
255 void Channels::enable_all_channels()
257 set_all_channels(true);
260 void Channels::disable_all_channels()
262 set_all_channels(false);