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