]> sigrok.org Git - pulseview.git/blob - pv/popups/probes.cpp
Implemented support for probe groups
[pulseview.git] / pv / popups / probes.cpp
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
21 #include <map>
22
23 #include <QFormLayout>
24 #include <QGridLayout>
25
26 #include "probes.h"
27
28 #include <pv/prop/binding/deviceoptions.h>
29 #include <pv/sigsession.h>
30 #include <pv/view/signal.h>
31
32 using namespace Qt;
33
34 using boost::shared_ptr;
35 using std::map;
36 using std::set;
37 using std::vector;
38
39 using pv::view::Signal;
40
41 namespace pv {
42 namespace popups {
43
44 Probes::Probes(SigSession &session, QWidget *parent) :
45         Popup(parent),
46         _session(session),
47         _layout(this),
48         _updating_probes(false),
49         _probes_bar(this),
50         _enable_all_probes(this),
51         _disable_all_probes(this),
52         _check_box_mapper(this)
53 {
54         // Create the layout
55         setLayout(&_layout);
56
57         sr_dev_inst *const sdi = _session.get_device();
58         assert(sdi);
59
60         // Collect a set of signals
61         map<const sr_probe*, shared_ptr<Signal> > signal_map;
62         const vector< shared_ptr<Signal> > sigs = _session.get_signals();
63         BOOST_FOREACH(const shared_ptr<Signal> &sig, sigs)
64                 signal_map[sig->probe()] = sig;
65
66         // Populate probe groups
67         for (const GSList *g = sdi->probe_groups; g; g = g->next)
68         {
69                 const sr_probe_group *const group =
70                         (const sr_probe_group*)g->data;
71                 assert(group);
72
73                 // Make a set of signals, and removed this signals from the
74                 // signal map.
75                 vector< shared_ptr<Signal> > group_sigs;
76                 for (const GSList *p = group->probes; p; p = p->next)
77                 {
78                         const sr_probe *const probe = (const sr_probe*)p->data;
79                         assert(probe);
80
81                         const map<const sr_probe*, shared_ptr<Signal> >::
82                                 iterator iter = signal_map.find(probe);
83                         assert(iter != signal_map.end());
84
85                         group_sigs.push_back((*iter).second);
86                         signal_map.erase(iter);
87                 }
88
89                 populate_group(group, group_sigs);
90         }
91
92         // Make a vector of the remaining probes
93         vector< shared_ptr<Signal> > global_sigs;
94         for (const GSList *p = sdi->probes; p; p = p->next)
95         {
96                 const sr_probe *const probe = (const sr_probe*)p->data;
97                 assert(probe);
98
99                 const map<const sr_probe*, shared_ptr<Signal> >::
100                         const_iterator iter = signal_map.find(probe);
101                 if (iter != signal_map.end())
102                         global_sigs.push_back((*iter).second);
103         }
104
105         // Create a group
106         populate_group(NULL, global_sigs);
107
108         // Create the enable/disable all buttons
109         connect(&_enable_all_probes, SIGNAL(clicked()),
110                 this, SLOT(enable_all_probes()));
111         connect(&_disable_all_probes, SIGNAL(clicked()),
112                 this, SLOT(disable_all_probes()));
113
114         _enable_all_probes.setText(tr("Enable All"));
115         _probes_bar.addWidget(&_enable_all_probes);
116
117         _disable_all_probes.setText(tr("Disable All"));
118         _probes_bar.addWidget(&_disable_all_probes);
119
120         _layout.addRow(&_probes_bar);
121
122         // Connect the check-box signal mapper
123         connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)),
124                 this, SLOT(on_probe_checked(QWidget*)));
125 }
126
127 void Probes::set_all_probes(bool set)
128 {
129         _updating_probes = true;
130
131         for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
132                 _check_box_signal_map.begin();
133                 i != _check_box_signal_map.end(); i++)
134         {
135                 const shared_ptr<Signal> sig = (*i).second;
136                 assert(sig);
137
138                 sig->enable(set);
139                 (*i).first->setChecked(set);
140         }
141
142         _updating_probes = false;
143 }
144
145 void Probes::populate_group(const sr_probe_group *group,
146         const vector< shared_ptr<pv::view::Signal> > sigs)
147 {
148         using pv::prop::binding::DeviceOptions;
149
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
152         // popup.
153         shared_ptr<DeviceOptions> binding;
154         if (group)
155                 binding = shared_ptr<DeviceOptions>(new DeviceOptions(
156                         _session.get_device(), group));
157
158         // Create a title if the group is going to have any content
159         if ((!sigs.empty() || (binding && !binding->properties().empty())) &&
160                 group && group->name)
161                 _layout.addRow(new QLabel(
162                         QString("<h3>%1</h3>").arg(group->name)));
163
164         // Create the probe group grid
165         QGridLayout *const probe_grid =
166                 create_probe_group_grid(sigs);
167         _layout.addRow(probe_grid);
168
169         // Create the probe group options
170         if (binding)
171         {
172                 binding->add_properties_to_form(&_layout, true);
173                 _group_bindings.push_back(binding);
174         }
175 }
176
177 QGridLayout* Probes::create_probe_group_grid(
178         const vector< shared_ptr<pv::view::Signal> > sigs)
179 {
180         int row = 0, col = 0;
181         QGridLayout *const grid = new QGridLayout();
182
183         BOOST_FOREACH(const shared_ptr<pv::view::Signal>& sig, sigs)
184         {
185                 assert(sig);
186
187                 QCheckBox *const checkbox = new QCheckBox(sig->get_name());
188                 _check_box_mapper.setMapping(checkbox, checkbox);
189                 connect(checkbox, SIGNAL(toggled(bool)),
190                         &_check_box_mapper, SLOT(map()));
191
192                 grid->addWidget(checkbox, row, col);
193
194                 _check_box_signal_map[checkbox] = sig;
195
196                 if(++col >= 8)
197                         col = 0, row++;
198         }
199
200         return grid;
201 }
202
203 void Probes::showEvent(QShowEvent *e)
204 {
205         pv::widgets::Popup::showEvent(e);
206
207         _updating_probes = true;
208
209         for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
210                 _check_box_signal_map.begin();
211                 i != _check_box_signal_map.end(); i++)
212         {
213                 const shared_ptr<Signal> sig = (*i).second;
214                 assert(sig);
215
216                 (*i).first->setChecked(sig->enabled());
217         }
218
219         _updating_probes = false;
220 }
221
222 void Probes::on_probe_checked(QWidget *widget)
223 {
224         if (_updating_probes)
225                 return;
226
227         QCheckBox *const check_box = (QCheckBox*)widget;
228         assert(check_box);
229
230         // Look up the signal of this check-box
231         map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter =
232                 _check_box_signal_map.find((QCheckBox*)check_box);
233         assert(iter != _check_box_signal_map.end());
234
235         const shared_ptr<pv::view::Signal> s = (*iter).second;
236         assert(s);
237
238         s->enable(check_box->isChecked());
239 }
240
241 void Probes::enable_all_probes()
242 {
243         set_all_probes(true);
244 }
245
246 void Probes::disable_all_probes()
247 {
248         set_all_probes(false);
249 }
250
251 } // popups
252 } // pv