]> sigrok.org Git - pulseview.git/blame - pv/popups/probes.cpp
pv::data::decode::Annotation: Add pd_index() and set_pd_index().
[pulseview.git] / pv / popups / probes.cpp
CommitLineData
cdb50f67
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
488f068c
JH
21#include <map>
22
bfc9f61e
JH
23#include <boost/foreach.hpp>
24
25#include <QCheckBox>
488f068c
JH
26#include <QFormLayout>
27#include <QGridLayout>
bfc9f61e 28#include <QLabel>
488f068c 29
51d4a9ab 30#include "probes.h"
cdb50f67 31
488f068c 32#include <pv/prop/binding/deviceoptions.h>
aca00b1e
JH
33#include <pv/sigsession.h>
34#include <pv/view/signal.h>
35
51d4a9ab 36using namespace Qt;
488f068c 37
819f4c25 38using boost::shared_ptr;
488f068c
JH
39using std::map;
40using std::set;
819f4c25 41using std::vector;
479bcabe 42
488f068c
JH
43using pv::view::Signal;
44
cdb50f67 45namespace pv {
51d4a9ab 46namespace popups {
cdb50f67 47
aca00b1e 48Probes::Probes(SigSession &session, QWidget *parent) :
51d4a9ab 49 Popup(parent),
aca00b1e 50 _session(session),
b7b659aa 51 _updating_probes(false),
65cbcf46
JH
52 _enable_all_probes(tr("Enable All"), this),
53 _disable_all_probes(tr("Disable All"), this),
488f068c 54 _check_box_mapper(this)
cdb50f67 55{
488f068c 56 // Create the layout
51d4a9ab 57 setLayout(&_layout);
9c025ef3 58
488f068c
JH
59 sr_dev_inst *const sdi = _session.get_device();
60 assert(sdi);
61
62 // Collect a set of signals
63 map<const sr_probe*, shared_ptr<Signal> > signal_map;
64 const vector< shared_ptr<Signal> > sigs = _session.get_signals();
65 BOOST_FOREACH(const shared_ptr<Signal> &sig, sigs)
66 signal_map[sig->probe()] = sig;
67
68 // Populate probe groups
69 for (const GSList *g = sdi->probe_groups; g; g = g->next)
70 {
71 const sr_probe_group *const group =
72 (const sr_probe_group*)g->data;
73 assert(group);
74
75 // Make a set of signals, and removed this signals from the
76 // signal map.
77 vector< shared_ptr<Signal> > group_sigs;
78 for (const GSList *p = group->probes; p; p = p->next)
79 {
80 const sr_probe *const probe = (const sr_probe*)p->data;
81 assert(probe);
82
83 const map<const sr_probe*, shared_ptr<Signal> >::
84 iterator iter = signal_map.find(probe);
85 assert(iter != signal_map.end());
86
87 group_sigs.push_back((*iter).second);
88 signal_map.erase(iter);
89 }
90
91 populate_group(group, group_sigs);
92 }
93
94 // Make a vector of the remaining probes
95 vector< shared_ptr<Signal> > global_sigs;
96 for (const GSList *p = sdi->probes; p; p = p->next)
97 {
98 const sr_probe *const probe = (const sr_probe*)p->data;
99 assert(probe);
100
101 const map<const sr_probe*, shared_ptr<Signal> >::
102 const_iterator iter = signal_map.find(probe);
103 if (iter != signal_map.end())
104 global_sigs.push_back((*iter).second);
105 }
106
107 // Create a group
108 populate_group(NULL, global_sigs);
109
110 // Create the enable/disable all buttons
ed773982
JH
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()));
115
65cbcf46
JH
116 _enable_all_probes.setFlat(true);
117 _disable_all_probes.setFlat(true);
ed773982 118
65cbcf46
JH
119 _buttons_bar.addWidget(&_enable_all_probes);
120 _buttons_bar.addWidget(&_disable_all_probes);
121 _buttons_bar.addStretch(1);
ed773982 122
65cbcf46 123 _layout.addRow(&_buttons_bar);
0740907f 124
488f068c
JH
125 // Connect the check-box signal mapper
126 connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)),
127 this, SLOT(on_probe_checked(QWidget*)));
b7b659aa
JH
128}
129
130void Probes::set_all_probes(bool set)
131{
b7b659aa
JH
132 _updating_probes = true;
133
488f068c
JH
134 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
135 _check_box_signal_map.begin();
136 i != _check_box_signal_map.end(); i++)
aca00b1e 137 {
488f068c
JH
138 const shared_ptr<Signal> sig = (*i).second;
139 assert(sig);
b7b659aa 140
488f068c
JH
141 sig->enable(set);
142 (*i).first->setChecked(set);
0740907f 143 }
51d4a9ab 144
b7b659aa 145 _updating_probes = false;
0740907f
JH
146}
147
488f068c
JH
148void Probes::populate_group(const sr_probe_group *group,
149 const vector< shared_ptr<pv::view::Signal> > sigs)
150{
151 using pv::prop::binding::DeviceOptions;
152
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
155 // popup.
156 shared_ptr<DeviceOptions> binding;
157 if (group)
158 binding = shared_ptr<DeviceOptions>(new DeviceOptions(
159 _session.get_device(), group));
160
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)));
166
167 // Create the probe group grid
168 QGridLayout *const probe_grid =
169 create_probe_group_grid(sigs);
170 _layout.addRow(probe_grid);
171
172 // Create the probe group options
173 if (binding)
174 {
175 binding->add_properties_to_form(&_layout, true);
176 _group_bindings.push_back(binding);
177 }
178}
179
180QGridLayout* Probes::create_probe_group_grid(
181 const vector< shared_ptr<pv::view::Signal> > sigs)
182{
183 int row = 0, col = 0;
184 QGridLayout *const grid = new QGridLayout();
185
186 BOOST_FOREACH(const shared_ptr<pv::view::Signal>& sig, sigs)
187 {
188 assert(sig);
189
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()));
194
195 grid->addWidget(checkbox, row, col);
196
197 _check_box_signal_map[checkbox] = sig;
198
199 if(++col >= 8)
200 col = 0, row++;
201 }
202
203 return grid;
204}
205
b7b659aa 206void Probes::showEvent(QShowEvent *e)
ed773982 207{
b7b659aa
JH
208 pv::widgets::Popup::showEvent(e);
209
210 _updating_probes = true;
211
488f068c
JH
212 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
213 _check_box_signal_map.begin();
214 i != _check_box_signal_map.end(); i++)
aca00b1e 215 {
488f068c
JH
216 const shared_ptr<Signal> sig = (*i).second;
217 assert(sig);
218
219 (*i).first->setChecked(sig->enabled());
ed773982 220 }
b7b659aa
JH
221
222 _updating_probes = false;
ed773982
JH
223}
224
488f068c 225void Probes::on_probe_checked(QWidget *widget)
51d4a9ab 226{
b7b659aa
JH
227 if (_updating_probes)
228 return;
229
488f068c
JH
230 QCheckBox *const check_box = (QCheckBox*)widget;
231 assert(check_box);
232
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());
237
238 const shared_ptr<pv::view::Signal> s = (*iter).second;
aca00b1e 239 assert(s);
488f068c
JH
240
241 s->enable(check_box->isChecked());
51d4a9ab
JH
242}
243
244void Probes::enable_all_probes()
ed773982
JH
245{
246 set_all_probes(true);
247}
248
51d4a9ab 249void Probes::disable_all_probes()
ed773982
JH
250{
251 set_all_probes(false);
252}
253
51d4a9ab
JH
254} // popups
255} // pv