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