]> sigrok.org Git - pulseview.git/blame - pv/popups/probes.cpp
Replaced QToolBar in probes popup
[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
23#include <QFormLayout>
24#include <QGridLayout>
25
51d4a9ab 26#include "probes.h"
cdb50f67 27
488f068c 28#include <pv/prop/binding/deviceoptions.h>
aca00b1e
JH
29#include <pv/sigsession.h>
30#include <pv/view/signal.h>
31
51d4a9ab 32using namespace Qt;
488f068c 33
819f4c25 34using boost::shared_ptr;
488f068c
JH
35using std::map;
36using std::set;
819f4c25 37using std::vector;
479bcabe 38
488f068c
JH
39using pv::view::Signal;
40
cdb50f67 41namespace pv {
51d4a9ab 42namespace popups {
cdb50f67 43
aca00b1e 44Probes::Probes(SigSession &session, QWidget *parent) :
51d4a9ab 45 Popup(parent),
aca00b1e 46 _session(session),
b7b659aa 47 _updating_probes(false),
65cbcf46
JH
48 _enable_all_probes(tr("Enable All"), this),
49 _disable_all_probes(tr("Disable All"), this),
488f068c 50 _check_box_mapper(this)
cdb50f67 51{
488f068c 52 // Create the layout
51d4a9ab 53 setLayout(&_layout);
9c025ef3 54
488f068c
JH
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
ed773982
JH
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
65cbcf46
JH
112 _enable_all_probes.setFlat(true);
113 _disable_all_probes.setFlat(true);
ed773982 114
65cbcf46
JH
115 _buttons_bar.addWidget(&_enable_all_probes);
116 _buttons_bar.addWidget(&_disable_all_probes);
117 _buttons_bar.addStretch(1);
ed773982 118
65cbcf46 119 _layout.addRow(&_buttons_bar);
0740907f 120
488f068c
JH
121 // Connect the check-box signal mapper
122 connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)),
123 this, SLOT(on_probe_checked(QWidget*)));
b7b659aa
JH
124}
125
126void Probes::set_all_probes(bool set)
127{
b7b659aa
JH
128 _updating_probes = true;
129
488f068c
JH
130 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
131 _check_box_signal_map.begin();
132 i != _check_box_signal_map.end(); i++)
aca00b1e 133 {
488f068c
JH
134 const shared_ptr<Signal> sig = (*i).second;
135 assert(sig);
b7b659aa 136
488f068c
JH
137 sig->enable(set);
138 (*i).first->setChecked(set);
0740907f 139 }
51d4a9ab 140
b7b659aa 141 _updating_probes = false;
0740907f
JH
142}
143
488f068c
JH
144void 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
176QGridLayout* 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
b7b659aa 202void Probes::showEvent(QShowEvent *e)
ed773982 203{
b7b659aa
JH
204 pv::widgets::Popup::showEvent(e);
205
206 _updating_probes = true;
207
488f068c
JH
208 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
209 _check_box_signal_map.begin();
210 i != _check_box_signal_map.end(); i++)
aca00b1e 211 {
488f068c
JH
212 const shared_ptr<Signal> sig = (*i).second;
213 assert(sig);
214
215 (*i).first->setChecked(sig->enabled());
ed773982 216 }
b7b659aa
JH
217
218 _updating_probes = false;
ed773982
JH
219}
220
488f068c 221void Probes::on_probe_checked(QWidget *widget)
51d4a9ab 222{
b7b659aa
JH
223 if (_updating_probes)
224 return;
225
488f068c
JH
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;
aca00b1e 235 assert(s);
488f068c
JH
236
237 s->enable(check_box->isChecked());
51d4a9ab
JH
238}
239
240void Probes::enable_all_probes()
ed773982
JH
241{
242 set_all_probes(true);
243}
244
51d4a9ab 245void Probes::disable_all_probes()
ed773982
JH
246{
247 set_all_probes(false);
248}
249
51d4a9ab
JH
250} // popups
251} // pv