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