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>
30 #include <pv/prop/binding/deviceoptions.h>
31 #include <pv/sigsession.h>
32 #include <pv/view/signal.h>
34 #include <libsigrok/libsigrok.hpp>
40 using std::shared_ptr;
43 using sigrok::Channel;
44 using sigrok::ChannelGroup;
47 using pv::view::Signal;
52 Channels::Channels(SigSession &session, QWidget *parent) :
55 _updating_channels(false),
56 _enable_all_channels(tr("Enable All"), this),
57 _disable_all_channels(tr("Disable All"), this),
58 _check_box_mapper(this)
63 shared_ptr<sigrok::Device> device = _session.get_device();
66 // Collect a set of signals
67 map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map;
68 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
70 for (const shared_ptr<Signal> &sig : sigs)
71 signal_map[sig->channel()] = sig;
73 // Populate channel groups
74 for (auto entry : device->channel_groups())
76 shared_ptr<ChannelGroup> group = entry.second;
77 // Make a set of signals, and removed this signals from the
79 vector< shared_ptr<Signal> > group_sigs;
80 for (auto channel : group->channels())
82 const auto iter = signal_map.find(channel);
84 if (iter == signal_map.end())
87 group_sigs.push_back((*iter).second);
88 signal_map.erase(iter);
91 populate_group(group, group_sigs);
94 // Make a vector of the remaining channels
95 vector< shared_ptr<Signal> > global_sigs;
96 for (auto channel : device->channels())
98 const map<shared_ptr<Channel>, shared_ptr<Signal> >::
99 const_iterator iter = signal_map.find(channel);
100 if (iter != signal_map.end())
101 global_sigs.push_back((*iter).second);
105 populate_group(NULL, global_sigs);
107 // Create the enable/disable all buttons
108 connect(&_enable_all_channels, SIGNAL(clicked()),
109 this, SLOT(enable_all_channels()));
110 connect(&_disable_all_channels, SIGNAL(clicked()),
111 this, SLOT(disable_all_channels()));
113 _enable_all_channels.setFlat(true);
114 _disable_all_channels.setFlat(true);
116 _buttons_bar.addWidget(&_enable_all_channels);
117 _buttons_bar.addWidget(&_disable_all_channels);
118 _buttons_bar.addStretch(1);
120 _layout.addRow(&_buttons_bar);
122 // Connect the check-box signal mapper
123 connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)),
124 this, SLOT(on_channel_checked(QWidget*)));
127 void Channels::set_all_channels(bool set)
129 _updating_channels = true;
131 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
132 _check_box_signal_map.begin();
133 i != _check_box_signal_map.end(); i++)
135 const shared_ptr<Signal> sig = (*i).second;
139 (*i).first->setChecked(set);
142 _updating_channels = false;
145 void Channels::populate_group(shared_ptr<ChannelGroup> group,
146 const vector< shared_ptr<pv::view::Signal> > sigs)
148 using pv::prop::binding::DeviceOptions;
150 // Only bind options if this is a group. We don't do it for general
151 // options, because these properties are shown in the device config
153 shared_ptr<DeviceOptions> binding;
155 binding = shared_ptr<DeviceOptions>(new DeviceOptions(group));
157 // Create a title if the group is going to have any content
158 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
160 _layout.addRow(new QLabel(
161 QString("<h3>%1</h3>").arg(group->name().c_str())));
163 // Create the channel group grid
164 QGridLayout *const channel_grid =
165 create_channel_group_grid(sigs);
166 _layout.addRow(channel_grid);
168 // Create the channel group options
171 binding->add_properties_to_form(&_layout, true);
172 _group_bindings.push_back(binding);
176 QGridLayout* Channels::create_channel_group_grid(
177 const vector< shared_ptr<pv::view::Signal> > sigs)
179 int row = 0, col = 0;
180 QGridLayout *const grid = new QGridLayout();
182 for (const shared_ptr<pv::view::Signal>& sig : sigs)
186 QCheckBox *const checkbox = new QCheckBox(sig->get_name());
187 _check_box_mapper.setMapping(checkbox, checkbox);
188 connect(checkbox, SIGNAL(toggled(bool)),
189 &_check_box_mapper, SLOT(map()));
191 grid->addWidget(checkbox, row, col);
193 _check_box_signal_map[checkbox] = sig;
202 void Channels::showEvent(QShowEvent *e)
204 pv::widgets::Popup::showEvent(e);
206 _updating_channels = true;
208 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
209 _check_box_signal_map.begin();
210 i != _check_box_signal_map.end(); i++)
212 const shared_ptr<Signal> sig = (*i).second;
215 (*i).first->setChecked(sig->enabled());
218 _updating_channels = false;
221 void Channels::on_channel_checked(QWidget *widget)
223 if (_updating_channels)
226 QCheckBox *const check_box = (QCheckBox*)widget;
229 // Look up the signal of this check-box
230 map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
231 _check_box_signal_map.find((QCheckBox*)check_box);
232 assert(iter != _check_box_signal_map.end());
234 const shared_ptr<pv::view::Signal> s = (*iter).second;
237 s->enable(check_box->isChecked());
240 void Channels::enable_all_channels()
242 set_all_channels(true);
245 void Channels::disable_all_channels()
247 set_all_channels(false);