]> sigrok.org Git - pulseview.git/blob - pv/popups/probes.cpp
Fixed missing includes and formward declarations hidden by cotire.
[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 <boost/foreach.hpp>
24
25 #include <QCheckBox>
26 #include <QFormLayout>
27 #include <QGridLayout>
28 #include <QLabel>
29
30 #include "probes.h"
31
32 #include <pv/prop/binding/deviceoptions.h>
33 #include <pv/sigsession.h>
34 #include <pv/view/signal.h>
35
36 using namespace Qt;
37
38 using boost::shared_ptr;
39 using std::map;
40 using std::set;
41 using std::vector;
42
43 using pv::view::Signal;
44
45 namespace pv {
46 namespace popups {
47
48 Probes::Probes(SigSession &session, QWidget *parent) :
49         Popup(parent),
50         _session(session),
51         _updating_probes(false),
52         _enable_all_probes(tr("Enable All"), this),
53         _disable_all_probes(tr("Disable All"), this),
54         _check_box_mapper(this)
55 {
56         // Create the layout
57         setLayout(&_layout);
58
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
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
116         _enable_all_probes.setFlat(true);
117         _disable_all_probes.setFlat(true);
118
119         _buttons_bar.addWidget(&_enable_all_probes);
120         _buttons_bar.addWidget(&_disable_all_probes);
121         _buttons_bar.addStretch(1);
122
123         _layout.addRow(&_buttons_bar);
124
125         // Connect the check-box signal mapper
126         connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)),
127                 this, SLOT(on_probe_checked(QWidget*)));
128 }
129
130 void Probes::set_all_probes(bool set)
131 {
132         _updating_probes = true;
133
134         for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
135                 _check_box_signal_map.begin();
136                 i != _check_box_signal_map.end(); i++)
137         {
138                 const shared_ptr<Signal> sig = (*i).second;
139                 assert(sig);
140
141                 sig->enable(set);
142                 (*i).first->setChecked(set);
143         }
144
145         _updating_probes = false;
146 }
147
148 void 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
180 QGridLayout* 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
206 void Probes::showEvent(QShowEvent *e)
207 {
208         pv::widgets::Popup::showEvent(e);
209
210         _updating_probes = true;
211
212         for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
213                 _check_box_signal_map.begin();
214                 i != _check_box_signal_map.end(); i++)
215         {
216                 const shared_ptr<Signal> sig = (*i).second;
217                 assert(sig);
218
219                 (*i).first->setChecked(sig->enabled());
220         }
221
222         _updating_probes = false;
223 }
224
225 void Probes::on_probe_checked(QWidget *widget)
226 {
227         if (_updating_probes)
228                 return;
229
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;
239         assert(s);
240
241         s->enable(check_box->isChecked());
242 }
243
244 void Probes::enable_all_probes()
245 {
246         set_all_probes(true);
247 }
248
249 void Probes::disable_all_probes()
250 {
251         set_all_probes(false);
252 }
253
254 } // popups
255 } // pv