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/device/devinst.h>
31 #include <pv/prop/binding/deviceoptions.h>
32 #include <pv/sigsession.h>
33 #include <pv/view/signal.h>
39 using std::shared_ptr;
42 using pv::view::Signal;
47 Probes::Probes(SigSession &session, QWidget *parent) :
50 _updating_probes(false),
51 _enable_all_probes(tr("Enable All"), this),
52 _disable_all_probes(tr("Disable All"), this),
53 _check_box_mapper(this)
58 shared_ptr<device::DevInst> dev_inst = _session.get_device();
60 const sr_dev_inst *const sdi = dev_inst->dev_inst();
63 // Collect a set of signals
64 map<const sr_channel*, shared_ptr<Signal> > signal_map;
65 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
66 for (const shared_ptr<Signal> &sig : sigs)
67 signal_map[sig->probe()] = sig;
69 // Populate channel groups
70 for (const GSList *g = sdi->channel_groups; g; g = g->next)
72 const sr_channel_group *const group =
73 (const sr_channel_group*)g->data;
76 // Make a set of signals, and removed this signals from the
78 vector< shared_ptr<Signal> > group_sigs;
79 for (const GSList *p = group->channels; p; p = p->next)
81 const sr_channel *const probe = (const sr_channel*)p->data;
84 const auto iter = signal_map.find(probe);
85 assert(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 probes
95 vector< shared_ptr<Signal> > global_sigs;
96 for (const GSList *p = sdi->channels; p; p = p->next)
98 const sr_channel *const probe = (const sr_channel*)p->data;
101 const map<const sr_channel*, shared_ptr<Signal> >::
102 const_iterator iter = signal_map.find(probe);
103 if (iter != signal_map.end())
104 global_sigs.push_back((*iter).second);
108 populate_group(NULL, global_sigs);
110 // Create the enable/disable all buttons
111 connect(&_enable_all_probes, SIGNAL(clicked()),
112 this, SLOT(enable_all_probes()));
113 connect(&_disable_all_probes, SIGNAL(clicked()),
114 this, SLOT(disable_all_probes()));
116 _enable_all_probes.setFlat(true);
117 _disable_all_probes.setFlat(true);
119 _buttons_bar.addWidget(&_enable_all_probes);
120 _buttons_bar.addWidget(&_disable_all_probes);
121 _buttons_bar.addStretch(1);
123 _layout.addRow(&_buttons_bar);
125 // Connect the check-box signal mapper
126 connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)),
127 this, SLOT(on_probe_checked(QWidget*)));
130 void Probes::set_all_probes(bool set)
132 _updating_probes = true;
134 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
135 _check_box_signal_map.begin();
136 i != _check_box_signal_map.end(); i++)
138 const shared_ptr<Signal> sig = (*i).second;
142 (*i).first->setChecked(set);
145 _updating_probes = false;
148 void Probes::populate_group(const sr_channel_group *group,
149 const vector< shared_ptr<pv::view::Signal> > sigs)
151 using pv::prop::binding::DeviceOptions;
153 // Only bind options if this is a group. We don't do it for general
154 // options, because these properties are shown in the device config
156 shared_ptr<DeviceOptions> binding;
158 binding = shared_ptr<DeviceOptions>(new DeviceOptions(
159 _session.get_device(), group));
161 // Create a title if the group is going to have any content
162 if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
163 group && group->name)
164 _layout.addRow(new QLabel(
165 QString("<h3>%1</h3>").arg(group->name)));
167 // Create the channel group grid
168 QGridLayout *const probe_grid =
169 create_channel_group_grid(sigs);
170 _layout.addRow(probe_grid);
172 // Create the channel group options
175 binding->add_properties_to_form(&_layout, true);
176 _group_bindings.push_back(binding);
180 QGridLayout* Probes::create_channel_group_grid(
181 const vector< shared_ptr<pv::view::Signal> > sigs)
183 int row = 0, col = 0;
184 QGridLayout *const grid = new QGridLayout();
186 for (const shared_ptr<pv::view::Signal>& sig : sigs)
190 QCheckBox *const checkbox = new QCheckBox(sig->get_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 Probes::showEvent(QShowEvent *e)
208 pv::widgets::Popup::showEvent(e);
210 _updating_probes = true;
212 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
213 _check_box_signal_map.begin();
214 i != _check_box_signal_map.end(); i++)
216 const shared_ptr<Signal> sig = (*i).second;
219 (*i).first->setChecked(sig->enabled());
222 _updating_probes = false;
225 void Probes::on_probe_checked(QWidget *widget)
227 if (_updating_probes)
230 QCheckBox *const check_box = (QCheckBox*)widget;
233 // Look up the signal of this check-box
234 map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
235 _check_box_signal_map.find((QCheckBox*)check_box);
236 assert(iter != _check_box_signal_map.end());
238 const shared_ptr<pv::view::Signal> s = (*iter).second;
241 s->enable(check_box->isChecked());
244 void Probes::enable_all_probes()
246 set_all_probes(true);
249 void Probes::disable_all_probes()
251 set_all_probes(false);