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