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