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 #include <QFormLayout>
25 #include <QGridLayout>
28 #include "channels.hpp"
30 #include <pv/binding/device.hpp>
31 #include <pv/session.hpp>
32 #include <pv/view/signal.hpp>
34 #include <libsigrokcxx/libsigrokcxx.hpp>
38 using boost::shared_lock;
39 using boost::shared_mutex;
40 using std::lock_guard;
44 using std::shared_ptr;
47 using sigrok::Channel;
48 using sigrok::ChannelGroup;
51 using pv::view::Signal;
56 Channels::Channels(Session &session, QWidget *parent) :
59 updating_channels_(false),
60 enable_all_channels_(tr("Enable All"), this),
61 disable_all_channels_(tr("Disable All"), this),
62 check_box_mapper_(this)
67 shared_ptr<sigrok::Device> device = session_.device();
70 // Collect a set of signals
71 map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map;
73 shared_lock<shared_mutex> lock(session_.signals_mutex());
74 const vector< shared_ptr<Signal> > &sigs(session_.signals());
76 for (const shared_ptr<Signal> &sig : sigs)
77 signal_map[sig->channel()] = sig;
79 // Populate channel groups
80 for (auto entry : device->channel_groups())
82 shared_ptr<ChannelGroup> group = entry.second;
83 // Make a set of signals, and removed this signals from the
85 vector< shared_ptr<Signal> > group_sigs;
86 for (auto channel : group->channels())
88 const auto iter = signal_map.find(channel);
90 if (iter == signal_map.end())
93 group_sigs.push_back((*iter).second);
94 signal_map.erase(iter);
97 populate_group(group, group_sigs);
100 // Make a vector of the remaining channels
101 vector< shared_ptr<Signal> > global_sigs;
102 for (auto channel : device->channels())
104 const map<shared_ptr<Channel>, shared_ptr<Signal> >::
105 const_iterator iter = signal_map.find(channel);
106 if (iter != signal_map.end())
107 global_sigs.push_back((*iter).second);
111 populate_group(NULL, global_sigs);
113 // Create the enable/disable all buttons
114 connect(&enable_all_channels_, SIGNAL(clicked()),
115 this, SLOT(enable_all_channels()));
116 connect(&disable_all_channels_, SIGNAL(clicked()),
117 this, SLOT(disable_all_channels()));
119 enable_all_channels_.setFlat(true);
120 disable_all_channels_.setFlat(true);
122 buttons_bar_.addWidget(&enable_all_channels_);
123 buttons_bar_.addWidget(&disable_all_channels_);
124 buttons_bar_.addStretch(1);
126 layout_.addRow(&buttons_bar_);
128 // Connect the check-box signal mapper
129 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
130 this, SLOT(on_channel_checked(QWidget*)));
133 void Channels::set_all_channels(bool set)
135 updating_channels_ = true;
137 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
138 check_box_signal_map_.begin();
139 i != check_box_signal_map_.end(); i++)
141 const shared_ptr<Signal> sig = (*i).second;
145 (*i).first->setChecked(set);
148 updating_channels_ = false;
151 void Channels::populate_group(shared_ptr<ChannelGroup> group,
152 const vector< shared_ptr<pv::view::Signal> > sigs)
154 using pv::binding::Device;
156 // Only bind options if this is a group. We don't do it for general
157 // options, because these properties are shown in the device config
159 shared_ptr<Device> binding;
161 binding = shared_ptr<Device>(new Device(group));
163 // Create a title if the group is going to have any content
164 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
166 layout_.addRow(new QLabel(
167 QString("<h3>%1</h3>").arg(group->name().c_str())));
169 // Create the channel group grid
170 QGridLayout *const channel_grid =
171 create_channel_group_grid(sigs);
172 layout_.addRow(channel_grid);
174 // Create the channel group options
177 binding->add_properties_to_form(&layout_, true);
178 group_bindings_.push_back(binding);
182 QGridLayout* Channels::create_channel_group_grid(
183 const vector< shared_ptr<pv::view::Signal> > sigs)
185 int row = 0, col = 0;
186 QGridLayout *const grid = new QGridLayout();
188 for (const shared_ptr<pv::view::Signal>& sig : sigs)
192 QCheckBox *const checkbox = new QCheckBox(sig->name());
193 check_box_mapper_.setMapping(checkbox, checkbox);
194 connect(checkbox, SIGNAL(toggled(bool)),
195 &check_box_mapper_, SLOT(map()));
197 grid->addWidget(checkbox, row, col);
199 check_box_signal_map_[checkbox] = sig;
208 void Channels::showEvent(QShowEvent *e)
210 pv::widgets::Popup::showEvent(e);
212 updating_channels_ = true;
214 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
215 check_box_signal_map_.begin();
216 i != check_box_signal_map_.end(); i++)
218 const shared_ptr<Signal> sig = (*i).second;
221 (*i).first->setChecked(sig->enabled());
224 updating_channels_ = false;
227 void Channels::on_channel_checked(QWidget *widget)
229 if (updating_channels_)
232 QCheckBox *const check_box = (QCheckBox*)widget;
235 // Look up the signal of this check-box
236 map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
237 check_box_signal_map_.find((QCheckBox*)check_box);
238 assert(iter != check_box_signal_map_.end());
240 const shared_ptr<pv::view::Signal> s = (*iter).second;
243 s->enable(check_box->isChecked());
246 void Channels::enable_all_channels()
248 set_all_channels(true);
251 void Channels::disable_all_channels()
253 set_all_channels(false);