]>
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 | |
aca64cac JH |
38 | using boost::shared_lock; |
39 | using boost::shared_mutex; | |
c3a740dd | 40 | using std::lock_guard; |
488f068c | 41 | using std::map; |
c3a740dd | 42 | using std::mutex; |
488f068c | 43 | using std::set; |
f9abf97e | 44 | using std::shared_ptr; |
819f4c25 | 45 | using std::vector; |
479bcabe | 46 | |
e8d00928 ML |
47 | using sigrok::Channel; |
48 | using sigrok::ChannelGroup; | |
49 | using sigrok::Device; | |
50 | ||
488f068c JH |
51 | using pv::view::Signal; |
52 | ||
cdb50f67 | 53 | namespace pv { |
51d4a9ab | 54 | namespace popups { |
cdb50f67 | 55 | |
6ac6242b | 56 | Channels::Channels(SigSession &session, QWidget *parent) : |
51d4a9ab | 57 | Popup(parent), |
aca00b1e | 58 | _session(session), |
6ac6242b ML |
59 | _updating_channels(false), |
60 | _enable_all_channels(tr("Enable All"), this), | |
61 | _disable_all_channels(tr("Disable All"), this), | |
488f068c | 62 | _check_box_mapper(this) |
cdb50f67 | 63 | { |
488f068c | 64 | // Create the layout |
51d4a9ab | 65 | setLayout(&_layout); |
9c025ef3 | 66 | |
d260d425 | 67 | shared_ptr<sigrok::Device> device = _session.device(); |
e8d00928 | 68 | assert(device); |
488f068c JH |
69 | |
70 | // Collect a set of signals | |
e8d00928 | 71 | map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map; |
c3a740dd | 72 | |
aca64cac | 73 | shared_lock<shared_mutex> lock(_session.signals_mutex()); |
c3a740dd | 74 | const vector< shared_ptr<Signal> > &sigs(_session.signals()); |
733eee0e | 75 | |
d9aecf1f | 76 | for (const shared_ptr<Signal> &sig : sigs) |
6ac6242b | 77 | signal_map[sig->channel()] = sig; |
488f068c | 78 | |
2445160a | 79 | // Populate channel groups |
e8d00928 | 80 | for (auto entry : device->channel_groups()) |
488f068c | 81 | { |
e8d00928 ML |
82 | shared_ptr<ChannelGroup> group = entry.second; |
83 | // Make a set of signals, and removed this signals from the | |
488f068c JH |
84 | // signal map. |
85 | vector< shared_ptr<Signal> > group_sigs; | |
e8d00928 | 86 | for (auto channel : group->channels()) |
488f068c | 87 | { |
6ac6242b | 88 | const auto iter = signal_map.find(channel); |
52a80eb3 SA |
89 | |
90 | if (iter == signal_map.end()) | |
91 | break; | |
488f068c JH |
92 | |
93 | group_sigs.push_back((*iter).second); | |
94 | signal_map.erase(iter); | |
95 | } | |
96 | ||
97 | populate_group(group, group_sigs); | |
98 | } | |
99 | ||
6ac6242b | 100 | // Make a vector of the remaining channels |
488f068c | 101 | vector< shared_ptr<Signal> > global_sigs; |
e8d00928 | 102 | for (auto channel : device->channels()) |
488f068c | 103 | { |
e8d00928 | 104 | const map<shared_ptr<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 | 133 | void 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 | ||
e8d00928 | 151 | void Channels::populate_group(shared_ptr<ChannelGroup> 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) | |
e8d00928 | 161 | binding = shared_ptr<DeviceOptions>(new DeviceOptions(group)); |
488f068c JH |
162 | |
163 | // Create a title if the group is going to have any content | |
164 | if ((!sigs.empty() || (binding && !binding->properties().empty())) && | |
e8d00928 | 165 | group) |
488f068c | 166 | _layout.addRow(new QLabel( |
e8d00928 | 167 | QString("<h3>%1</h3>").arg(group->name().c_str()))); |
488f068c | 168 | |
2445160a | 169 | // Create the channel group grid |
6ac6242b | 170 | QGridLayout *const channel_grid = |
2445160a | 171 | create_channel_group_grid(sigs); |
6ac6242b | 172 | _layout.addRow(channel_grid); |
488f068c | 173 | |
2445160a | 174 | // Create the channel group options |
488f068c JH |
175 | if (binding) |
176 | { | |
177 | binding->add_properties_to_form(&_layout, true); | |
178 | _group_bindings.push_back(binding); | |
179 | } | |
180 | } | |
181 | ||
6ac6242b | 182 | QGridLayout* Channels::create_channel_group_grid( |
488f068c JH |
183 | const vector< shared_ptr<pv::view::Signal> > sigs) |
184 | { | |
185 | int row = 0, col = 0; | |
186 | QGridLayout *const grid = new QGridLayout(); | |
187 | ||
d9aecf1f | 188 | for (const shared_ptr<pv::view::Signal>& sig : sigs) |
488f068c JH |
189 | { |
190 | assert(sig); | |
191 | ||
0a47889b | 192 | QCheckBox *const checkbox = new QCheckBox(sig->name()); |
488f068c JH |
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 | ||
6ac6242b | 208 | void Channels::showEvent(QShowEvent *e) |
ed773982 | 209 | { |
b7b659aa JH |
210 | pv::widgets::Popup::showEvent(e); |
211 | ||
6ac6242b | 212 | _updating_channels = true; |
b7b659aa | 213 | |
488f068c JH |
214 | for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i = |
215 | _check_box_signal_map.begin(); | |
216 | i != _check_box_signal_map.end(); i++) | |
aca00b1e | 217 | { |
488f068c JH |
218 | const shared_ptr<Signal> sig = (*i).second; |
219 | assert(sig); | |
220 | ||
221 | (*i).first->setChecked(sig->enabled()); | |
ed773982 | 222 | } |
b7b659aa | 223 | |
6ac6242b | 224 | _updating_channels = false; |
ed773982 JH |
225 | } |
226 | ||
6ac6242b | 227 | void Channels::on_channel_checked(QWidget *widget) |
51d4a9ab | 228 | { |
6ac6242b | 229 | if (_updating_channels) |
b7b659aa JH |
230 | return; |
231 | ||
488f068c JH |
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; | |
aca00b1e | 241 | assert(s); |
488f068c JH |
242 | |
243 | s->enable(check_box->isChecked()); | |
51d4a9ab JH |
244 | } |
245 | ||
6ac6242b | 246 | void Channels::enable_all_channels() |
ed773982 | 247 | { |
6ac6242b | 248 | set_all_channels(true); |
ed773982 JH |
249 | } |
250 | ||
6ac6242b | 251 | void Channels::disable_all_channels() |
ed773982 | 252 | { |
6ac6242b | 253 | set_all_channels(false); |
ed773982 JH |
254 | } |
255 | ||
51d4a9ab JH |
256 | } // popups |
257 | } // pv |