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