]> sigrok.org Git - pulseview.git/blame - pv/popups/channels.cpp
Moved pv::prop:bindings classes into pv::bindings namespace
[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
61703a01 30#include <pv/binding/deviceoptions.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;
819f4c25 45using std::vector;
479bcabe 46
e8d00928
ML
47using sigrok::Channel;
48using sigrok::ChannelGroup;
49using sigrok::Device;
50
488f068c
JH
51using pv::view::Signal;
52
cdb50f67 53namespace pv {
51d4a9ab 54namespace popups {
cdb50f67 55
2b81ae46 56Channels::Channels(Session &session, QWidget *parent) :
51d4a9ab 57 Popup(parent),
8dbbc7f0
JH
58 session_(session),
59 updating_channels_(false),
60 enable_all_channels_(tr("Enable All"), this),
61 disable_all_channels_(tr("Disable All"), this),
62 check_box_mapper_(this)
cdb50f67 63{
488f068c 64 // Create the layout
8dbbc7f0 65 setLayout(&layout_);
9c025ef3 66
8dbbc7f0 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
8dbbc7f0
JH
73 shared_lock<shared_mutex> lock(session_.signals_mutex());
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
8dbbc7f0 114 connect(&enable_all_channels_, SIGNAL(clicked()),
6ac6242b 115 this, SLOT(enable_all_channels()));
8dbbc7f0 116 connect(&disable_all_channels_, SIGNAL(clicked()),
6ac6242b 117 this, SLOT(disable_all_channels()));
ed773982 118
8dbbc7f0
JH
119 enable_all_channels_.setFlat(true);
120 disable_all_channels_.setFlat(true);
ed773982 121
8dbbc7f0
JH
122 buttons_bar_.addWidget(&enable_all_channels_);
123 buttons_bar_.addWidget(&disable_all_channels_);
124 buttons_bar_.addStretch(1);
ed773982 125
8dbbc7f0 126 layout_.addRow(&buttons_bar_);
0740907f 127
488f068c 128 // Connect the check-box signal mapper
8dbbc7f0 129 connect(&check_box_mapper_, SIGNAL(mapped(QWidget*)),
6ac6242b 130 this, SLOT(on_channel_checked(QWidget*)));
b7b659aa
JH
131}
132
6ac6242b 133void Channels::set_all_channels(bool set)
b7b659aa 134{
8dbbc7f0 135 updating_channels_ = true;
b7b659aa 136
488f068c 137 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
8dbbc7f0
JH
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
8dbbc7f0 148 updating_channels_ = false;
0740907f
JH
149}
150
e8d00928 151void Channels::populate_group(shared_ptr<ChannelGroup> group,
488f068c
JH
152 const vector< shared_ptr<pv::view::Signal> > sigs)
153{
61703a01 154 using pv::binding::DeviceOptions;
488f068c
JH
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)
8dbbc7f0 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);
8dbbc7f0 172 layout_.addRow(channel_grid);
488f068c 173
2445160a 174 // Create the channel group options
488f068c
JH
175 if (binding)
176 {
8dbbc7f0
JH
177 binding->add_properties_to_form(&layout_, true);
178 group_bindings_.push_back(binding);
488f068c
JH
179 }
180}
181
6ac6242b 182QGridLayout* 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());
8dbbc7f0 193 check_box_mapper_.setMapping(checkbox, checkbox);
488f068c 194 connect(checkbox, SIGNAL(toggled(bool)),
8dbbc7f0 195 &check_box_mapper_, SLOT(map()));
488f068c
JH
196
197 grid->addWidget(checkbox, row, col);
198
8dbbc7f0 199 check_box_signal_map_[checkbox] = sig;
488f068c
JH
200
201 if(++col >= 8)
202 col = 0, row++;
203 }
204
205 return grid;
206}
207
6ac6242b 208void Channels::showEvent(QShowEvent *e)
ed773982 209{
b7b659aa
JH
210 pv::widgets::Popup::showEvent(e);
211
8dbbc7f0 212 updating_channels_ = true;
b7b659aa 213
488f068c 214 for (map<QCheckBox*, shared_ptr<Signal> >::const_iterator i =
8dbbc7f0
JH
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
8dbbc7f0 224 updating_channels_ = false;
ed773982
JH
225}
226
6ac6242b 227void Channels::on_channel_checked(QWidget *widget)
51d4a9ab 228{
8dbbc7f0 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 =
8dbbc7f0
JH
237 check_box_signal_map_.find((QCheckBox*)check_box);
238 assert(iter != check_box_signal_map_.end());
488f068c
JH
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 246void Channels::enable_all_channels()
ed773982 247{
6ac6242b 248 set_all_channels(true);
ed773982
JH
249}
250
6ac6242b 251void Channels::disable_all_channels()
ed773982 252{
6ac6242b 253 set_all_channels(false);
ed773982
JH
254}
255
51d4a9ab
JH
256} // popups
257} // pv