]>
Commit | Line | Data |
---|---|---|
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 | |
488f068c | 30 | #include <pv/prop/binding/deviceoptions.h> |
aca00b1e JH |
31 | #include <pv/sigsession.h> |
32 | #include <pv/view/signal.h> | |
33 | ||
e8d00928 ML |
34 | #include <libsigrok/libsigrok.hpp> |
35 | ||
51d4a9ab | 36 | using namespace Qt; |
488f068c | 37 | |
488f068c JH |
38 | using std::map; |
39 | using std::set; | |
f9abf97e | 40 | using std::shared_ptr; |
819f4c25 | 41 | using std::vector; |
479bcabe | 42 | |
e8d00928 ML |
43 | using sigrok::Channel; |
44 | using sigrok::ChannelGroup; | |
45 | using sigrok::Device; | |
46 | ||
488f068c JH |
47 | using pv::view::Signal; |
48 | ||
cdb50f67 | 49 | namespace pv { |
51d4a9ab | 50 | namespace popups { |
cdb50f67 | 51 | |
6ac6242b | 52 | Channels::Channels(SigSession &session, QWidget *parent) : |
51d4a9ab | 53 | Popup(parent), |
aca00b1e | 54 | _session(session), |
6ac6242b ML |
55 | _updating_channels(false), |
56 | _enable_all_channels(tr("Enable All"), this), | |
57 | _disable_all_channels(tr("Disable All"), this), | |
488f068c | 58 | _check_box_mapper(this) |
cdb50f67 | 59 | { |
488f068c | 60 | // Create the layout |
51d4a9ab | 61 | setLayout(&_layout); |
9c025ef3 | 62 | |
d260d425 | 63 | shared_ptr<sigrok::Device> device = _session.device(); |
e8d00928 | 64 | assert(device); |
488f068c JH |
65 | |
66 | // Collect a set of signals | |
e8d00928 | 67 | map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map; |
488f068c | 68 | const vector< shared_ptr<Signal> > sigs = _session.get_signals(); |
733eee0e | 69 | |
d9aecf1f | 70 | for (const shared_ptr<Signal> &sig : sigs) |
6ac6242b | 71 | signal_map[sig->channel()] = sig; |
488f068c | 72 | |
2445160a | 73 | // Populate channel groups |
e8d00928 | 74 | for (auto entry : device->channel_groups()) |
488f068c | 75 | { |
e8d00928 ML |
76 | shared_ptr<ChannelGroup> group = entry.second; |
77 | // Make a set of signals, and removed this signals from the | |
488f068c JH |
78 | // signal map. |
79 | vector< shared_ptr<Signal> > group_sigs; | |
e8d00928 | 80 | for (auto channel : group->channels()) |
488f068c | 81 | { |
6ac6242b | 82 | const auto iter = signal_map.find(channel); |
52a80eb3 SA |
83 | |
84 | if (iter == signal_map.end()) | |
85 | break; | |
488f068c JH |
86 | |
87 | group_sigs.push_back((*iter).second); | |
88 | signal_map.erase(iter); | |
89 | } | |
90 | ||
91 | populate_group(group, group_sigs); | |
92 | } | |
93 | ||
6ac6242b | 94 | // Make a vector of the remaining channels |
488f068c | 95 | vector< shared_ptr<Signal> > global_sigs; |
e8d00928 | 96 | for (auto channel : device->channels()) |
488f068c | 97 | { |
e8d00928 | 98 | const map<shared_ptr<Channel>, shared_ptr<Signal> >:: |
6ac6242b | 99 | const_iterator iter = signal_map.find(channel); |
488f068c JH |
100 | if (iter != signal_map.end()) |
101 | global_sigs.push_back((*iter).second); | |
102 | } | |
103 | ||
104 | // Create a group | |
105 | populate_group(NULL, global_sigs); | |
106 | ||
107 | // Create the enable/disable all buttons | |
6ac6242b ML |
108 | connect(&_enable_all_channels, SIGNAL(clicked()), |
109 | this, SLOT(enable_all_channels())); | |
110 | connect(&_disable_all_channels, SIGNAL(clicked()), | |
111 | this, SLOT(disable_all_channels())); | |
ed773982 | 112 | |
6ac6242b ML |
113 | _enable_all_channels.setFlat(true); |
114 | _disable_all_channels.setFlat(true); | |
ed773982 | 115 | |
6ac6242b ML |
116 | _buttons_bar.addWidget(&_enable_all_channels); |
117 | _buttons_bar.addWidget(&_disable_all_channels); | |
65cbcf46 | 118 | _buttons_bar.addStretch(1); |
ed773982 | 119 | |
65cbcf46 | 120 | _layout.addRow(&_buttons_bar); |
0740907f | 121 | |
488f068c JH |
122 | // Connect the check-box signal mapper |
123 | connect(&_check_box_mapper, SIGNAL(mapped(QWidget*)), | |
6ac6242b | 124 | this, SLOT(on_channel_checked(QWidget*))); |
b7b659aa JH |
125 | } |
126 | ||
6ac6242b | 127 | void Channels::set_all_channels(bool set) |
b7b659aa | 128 | { |
6ac6242b | 129 | _updating_channels = true; |
b7b659aa | 130 | |
488f068c JH |
131 | for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i = |
132 | _check_box_signal_map.begin(); | |
133 | i != _check_box_signal_map.end(); i++) | |
aca00b1e | 134 | { |
488f068c JH |
135 | const shared_ptr<Signal> sig = (*i).second; |
136 | assert(sig); | |
b7b659aa | 137 | |
488f068c JH |
138 | sig->enable(set); |
139 | (*i).first->setChecked(set); | |
0740907f | 140 | } |
51d4a9ab | 141 | |
6ac6242b | 142 | _updating_channels = false; |
0740907f JH |
143 | } |
144 | ||
e8d00928 | 145 | void Channels::populate_group(shared_ptr<ChannelGroup> group, |
488f068c JH |
146 | const vector< shared_ptr<pv::view::Signal> > sigs) |
147 | { | |
148 | using pv::prop::binding::DeviceOptions; | |
149 | ||
150 | // Only bind options if this is a group. We don't do it for general | |
151 | // options, because these properties are shown in the device config | |
152 | // popup. | |
153 | shared_ptr<DeviceOptions> binding; | |
154 | if (group) | |
e8d00928 | 155 | binding = shared_ptr<DeviceOptions>(new DeviceOptions(group)); |
488f068c JH |
156 | |
157 | // Create a title if the group is going to have any content | |
158 | if ((!sigs.empty() || (binding && !binding->properties().empty())) && | |
e8d00928 | 159 | group) |
488f068c | 160 | _layout.addRow(new QLabel( |
e8d00928 | 161 | QString("<h3>%1</h3>").arg(group->name().c_str()))); |
488f068c | 162 | |
2445160a | 163 | // Create the channel group grid |
6ac6242b | 164 | QGridLayout *const channel_grid = |
2445160a | 165 | create_channel_group_grid(sigs); |
6ac6242b | 166 | _layout.addRow(channel_grid); |
488f068c | 167 | |
2445160a | 168 | // Create the channel group options |
488f068c JH |
169 | if (binding) |
170 | { | |
171 | binding->add_properties_to_form(&_layout, true); | |
172 | _group_bindings.push_back(binding); | |
173 | } | |
174 | } | |
175 | ||
6ac6242b | 176 | QGridLayout* Channels::create_channel_group_grid( |
488f068c JH |
177 | const vector< shared_ptr<pv::view::Signal> > sigs) |
178 | { | |
179 | int row = 0, col = 0; | |
180 | QGridLayout *const grid = new QGridLayout(); | |
181 | ||
d9aecf1f | 182 | for (const shared_ptr<pv::view::Signal>& sig : sigs) |
488f068c JH |
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 | ||
6ac6242b | 202 | void Channels::showEvent(QShowEvent *e) |
ed773982 | 203 | { |
b7b659aa JH |
204 | pv::widgets::Popup::showEvent(e); |
205 | ||
6ac6242b | 206 | _updating_channels = true; |
b7b659aa | 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 | 217 | |
6ac6242b | 218 | _updating_channels = false; |
ed773982 JH |
219 | } |
220 | ||
6ac6242b | 221 | void Channels::on_channel_checked(QWidget *widget) |
51d4a9ab | 222 | { |
6ac6242b | 223 | if (_updating_channels) |
b7b659aa JH |
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 | ||
6ac6242b | 240 | void Channels::enable_all_channels() |
ed773982 | 241 | { |
6ac6242b | 242 | set_all_channels(true); |
ed773982 JH |
243 | } |
244 | ||
6ac6242b | 245 | void Channels::disable_all_channels() |
ed773982 | 246 | { |
6ac6242b | 247 | set_all_channels(false); |
ed773982 JH |
248 | } |
249 | ||
51d4a9ab JH |
250 | } // popups |
251 | } // pv |