]>
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 | ||
35750e4d UH |
23 | #ifdef _WIN32 |
24 | // Windows: Avoid boost/thread namespace pollution (which includes windows.h). | |
25 | #define NOGDI | |
26 | #define NORESOURCE | |
27 | #endif | |
e71eb81c JH |
28 | #include <boost/thread/locks.hpp> |
29 | #include <boost/thread/shared_mutex.hpp> | |
30 | ||
bfc9f61e | 31 | #include <QCheckBox> |
488f068c JH |
32 | #include <QFormLayout> |
33 | #include <QGridLayout> | |
bfc9f61e | 34 | #include <QLabel> |
488f068c | 35 | |
2acdb232 | 36 | #include "channels.hpp" |
cdb50f67 | 37 | |
3cc9ad7b | 38 | #include <pv/binding/device.hpp> |
da30ecb7 | 39 | #include <pv/devices/device.hpp> |
f65cd27b | 40 | #include <pv/session.hpp> |
2acdb232 | 41 | #include <pv/view/signal.hpp> |
aca00b1e | 42 | |
fe3a1c21 | 43 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e8d00928 | 44 | |
51d4a9ab | 45 | using namespace Qt; |
488f068c | 46 | |
aca64cac JH |
47 | using boost::shared_lock; |
48 | using boost::shared_mutex; | |
c3a740dd | 49 | using std::lock_guard; |
488f068c | 50 | using std::map; |
c3a740dd | 51 | using std::mutex; |
488f068c | 52 | using std::set; |
f9abf97e | 53 | using std::shared_ptr; |
78b0af3e | 54 | using std::unordered_set; |
819f4c25 | 55 | using std::vector; |
479bcabe | 56 | |
e8d00928 ML |
57 | using sigrok::Channel; |
58 | using sigrok::ChannelGroup; | |
59 | using sigrok::Device; | |
60 | ||
488f068c JH |
61 | using pv::view::Signal; |
62 | ||
cdb50f67 | 63 | namespace pv { |
51d4a9ab | 64 | namespace popups { |
cdb50f67 | 65 | |
2b81ae46 | 66 | Channels::Channels(Session &session, QWidget *parent) : |
51d4a9ab | 67 | Popup(parent), |
8dbbc7f0 JH |
68 | session_(session), |
69 | updating_channels_(false), | |
70 | enable_all_channels_(tr("Enable All"), this), | |
71 | disable_all_channels_(tr("Disable All"), this), | |
72 | check_box_mapper_(this) | |
cdb50f67 | 73 | { |
488f068c | 74 | // Create the layout |
8dbbc7f0 | 75 | setLayout(&layout_); |
9c025ef3 | 76 | |
da30ecb7 | 77 | const shared_ptr<sigrok::Device> device = session_.device()->device(); |
e8d00928 | 78 | assert(device); |
488f068c JH |
79 | |
80 | // Collect a set of signals | |
e8d00928 | 81 | map<shared_ptr<Channel>, shared_ptr<Signal> > signal_map; |
c3a740dd | 82 | |
bf914698 | 83 | const unordered_set< shared_ptr<Signal> > sigs(session_.signals()); |
733eee0e | 84 | |
d9aecf1f | 85 | for (const shared_ptr<Signal> &sig : sigs) |
6ac6242b | 86 | signal_map[sig->channel()] = sig; |
488f068c | 87 | |
2445160a | 88 | // Populate channel groups |
e8d00928 | 89 | for (auto entry : device->channel_groups()) |
488f068c | 90 | { |
e8d00928 ML |
91 | shared_ptr<ChannelGroup> group = entry.second; |
92 | // Make a set of signals, and removed this signals from the | |
488f068c JH |
93 | // signal map. |
94 | vector< shared_ptr<Signal> > group_sigs; | |
e8d00928 | 95 | for (auto channel : group->channels()) |
488f068c | 96 | { |
6ac6242b | 97 | const auto iter = signal_map.find(channel); |
52a80eb3 SA |
98 | |
99 | if (iter == signal_map.end()) | |
100 | break; | |
488f068c JH |
101 | |
102 | group_sigs.push_back((*iter).second); | |
103 | signal_map.erase(iter); | |
104 | } | |
105 | ||
106 | populate_group(group, group_sigs); | |
107 | } | |
108 | ||
6ac6242b | 109 | // Make a vector of the remaining channels |
488f068c | 110 | vector< shared_ptr<Signal> > global_sigs; |
e8d00928 | 111 | for (auto channel : device->channels()) |
488f068c | 112 | { |
e8d00928 | 113 | const map<shared_ptr<Channel>, shared_ptr<Signal> >:: |
6ac6242b | 114 | const_iterator iter = signal_map.find(channel); |
488f068c JH |
115 | if (iter != signal_map.end()) |
116 | global_sigs.push_back((*iter).second); | |
117 | } | |
118 | ||
119 | // Create a group | |
4c60462b | 120 | populate_group(nullptr, global_sigs); |
488f068c JH |
121 | |
122 | // Create the enable/disable all buttons | |
8dbbc7f0 | 123 | connect(&enable_all_channels_, SIGNAL(clicked()), |
6ac6242b | 124 | this, SLOT(enable_all_channels())); |
8dbbc7f0 | 125 | connect(&disable_all_channels_, SIGNAL(clicked()), |
6ac6242b | 126 | this, SLOT(disable_all_channels())); |
ed773982 | 127 | |
8dbbc7f0 JH |
128 | enable_all_channels_.setFlat(true); |
129 | disable_all_channels_.setFlat(true); | |
ed773982 | 130 | |
8dbbc7f0 JH |
131 | buttons_bar_.addWidget(&enable_all_channels_); |
132 | buttons_bar_.addWidget(&disable_all_channels_); | |
133 | buttons_bar_.addStretch(1); | |
ed773982 | 134 | |
8dbbc7f0 | 135 | layout_.addRow(&buttons_bar_); |
0740907f | 136 | |
488f068c | 137 | // Connect the check-box signal mapper |
8dbbc7f0 | 138 | connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)), |
6ac6242b | 139 | this, SLOT(on_channel_checked(QWidget*))); |
b7b659aa JH |
140 | } |
141 | ||
6ac6242b | 142 | void Channels::set_all_channels(bool set) |
b7b659aa | 143 | { |
8dbbc7f0 | 144 | updating_channels_ = true; |
b7b659aa | 145 | |
488f068c | 146 | for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i = |
8dbbc7f0 JH |
147 | check_box_signal_map_.begin(); |
148 | i != check_box_signal_map_.end(); i++) | |
aca00b1e | 149 | { |
488f068c JH |
150 | const shared_ptr<Signal> sig = (*i).second; |
151 | assert(sig); | |
b7b659aa | 152 | |
488f068c JH |
153 | sig->enable(set); |
154 | (*i).first->setChecked(set); | |
0740907f | 155 | } |
51d4a9ab | 156 | |
8dbbc7f0 | 157 | updating_channels_ = false; |
0740907f JH |
158 | } |
159 | ||
e8d00928 | 160 | void Channels::populate_group(shared_ptr<ChannelGroup> group, |
488f068c JH |
161 | const vector< shared_ptr<pv::view::Signal> > sigs) |
162 | { | |
3cc9ad7b | 163 | using pv::binding::Device; |
488f068c JH |
164 | |
165 | // Only bind options if this is a group. We don't do it for general | |
166 | // options, because these properties are shown in the device config | |
167 | // popup. | |
3cc9ad7b | 168 | shared_ptr<Device> binding; |
488f068c | 169 | if (group) |
3cc9ad7b | 170 | binding = shared_ptr<Device>(new Device(group)); |
488f068c JH |
171 | |
172 | // Create a title if the group is going to have any content | |
173 | if ((!sigs.empty() || (binding && !binding->properties().empty())) && | |
e8d00928 | 174 | group) |
8dbbc7f0 | 175 | layout_.addRow(new QLabel( |
e8d00928 | 176 | QString("<h3>%1</h3>").arg(group->name().c_str()))); |
488f068c | 177 | |
2445160a | 178 | // Create the channel group grid |
6ac6242b | 179 | QGridLayout *const channel_grid = |
2445160a | 180 | create_channel_group_grid(sigs); |
8dbbc7f0 | 181 | layout_.addRow(channel_grid); |
488f068c | 182 | |
2445160a | 183 | // Create the channel group options |
488f068c JH |
184 | if (binding) |
185 | { | |
8dbbc7f0 JH |
186 | binding->add_properties_to_form(&layout_, true); |
187 | group_bindings_.push_back(binding); | |
488f068c JH |
188 | } |
189 | } | |
190 | ||
6ac6242b | 191 | QGridLayout* Channels::create_channel_group_grid( |
488f068c JH |
192 | const vector< shared_ptr<pv::view::Signal> > sigs) |
193 | { | |
194 | int row = 0, col = 0; | |
195 | QGridLayout *const grid = new QGridLayout(); | |
196 | ||
d9aecf1f | 197 | for (const shared_ptr<pv::view::Signal>& sig : sigs) |
488f068c JH |
198 | { |
199 | assert(sig); | |
200 | ||
0a47889b | 201 | QCheckBox *const checkbox = new QCheckBox(sig->name()); |
8dbbc7f0 | 202 | check_box_mapper_.setMapping(checkbox, checkbox); |
488f068c | 203 | connect(checkbox, SIGNAL(toggled(bool)), |
8dbbc7f0 | 204 | &check_box_mapper_, SLOT(map())); |
488f068c JH |
205 | |
206 | grid->addWidget(checkbox, row, col); | |
207 | ||
8dbbc7f0 | 208 | check_box_signal_map_[checkbox] = sig; |
488f068c | 209 | |
f3290553 | 210 | if (++col >= 8) |
488f068c JH |
211 | col = 0, row++; |
212 | } | |
213 | ||
214 | return grid; | |
215 | } | |
216 | ||
6ac6242b | 217 | void Channels::showEvent(QShowEvent *e) |
ed773982 | 218 | { |
b7b659aa JH |
219 | pv::widgets::Popup::showEvent(e); |
220 | ||
8dbbc7f0 | 221 | updating_channels_ = true; |
b7b659aa | 222 | |
488f068c | 223 | for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i = |
8dbbc7f0 JH |
224 | check_box_signal_map_.begin(); |
225 | i != check_box_signal_map_.end(); i++) | |
aca00b1e | 226 | { |
488f068c JH |
227 | const shared_ptr<Signal> sig = (*i).second; |
228 | assert(sig); | |
229 | ||
230 | (*i).first->setChecked(sig->enabled()); | |
ed773982 | 231 | } |
b7b659aa | 232 | |
8dbbc7f0 | 233 | updating_channels_ = false; |
ed773982 JH |
234 | } |
235 | ||
6ac6242b | 236 | void Channels::on_channel_checked(QWidget *widget) |
51d4a9ab | 237 | { |
8dbbc7f0 | 238 | if (updating_channels_) |
b7b659aa JH |
239 | return; |
240 | ||
488f068c JH |
241 | QCheckBox *const check_box = (QCheckBox*)widget; |
242 | assert(check_box); | |
243 | ||
244 | // Look up the signal of this check-box | |
245 | map< QCheckBox*, shared_ptr<Signal> >::const_iterator iter = | |
8dbbc7f0 JH |
246 | check_box_signal_map_.find((QCheckBox*)check_box); |
247 | assert(iter != check_box_signal_map_.end()); | |
488f068c JH |
248 | |
249 | const shared_ptr<pv::view::Signal> s = (*iter).second; | |
aca00b1e | 250 | assert(s); |
488f068c JH |
251 | |
252 | s->enable(check_box->isChecked()); | |
51d4a9ab JH |
253 | } |
254 | ||
6ac6242b | 255 | void Channels::enable_all_channels() |
ed773982 | 256 | { |
6ac6242b | 257 | set_all_channels(true); |
ed773982 JH |
258 | } |
259 | ||
6ac6242b | 260 | void Channels::disable_all_channels() |
ed773982 | 261 | { |
6ac6242b | 262 | set_all_channels(false); |
ed773982 JH |
263 | } |
264 | ||
51d4a9ab JH |
265 | } // popups |
266 | } // pv |