]> sigrok.org Git - pulseview.git/blame - pv/popups/channels.cpp
Random simplifications, cosmetics/whitespace/consistency fixes.
[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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
cdb50f67
JH
18 */
19
488f068c
JH
20#include <map>
21
35750e4d
UH
22#ifdef _WIN32
23// Windows: Avoid boost/thread namespace pollution (which includes windows.h).
24#define NOGDI
25#define NORESOURCE
26#endif
e71eb81c 27
bfc9f61e 28#include <QCheckBox>
488f068c
JH
29#include <QFormLayout>
30#include <QGridLayout>
bfc9f61e 31#include <QLabel>
488f068c 32
2acdb232 33#include "channels.hpp"
cdb50f67 34
3cc9ad7b 35#include <pv/binding/device.hpp>
bf0edd2b 36#include <pv/data/signalbase.hpp>
da30ecb7 37#include <pv/devices/device.hpp>
f65cd27b 38#include <pv/session.hpp>
2acdb232 39#include <pv/view/signal.hpp>
aca00b1e 40
fe3a1c21 41#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 42
51d4a9ab 43using namespace Qt;
488f068c 44
488f068c 45using std::map;
f9abf97e 46using std::shared_ptr;
067bb624 47using std::make_shared;
78b0af3e 48using std::unordered_set;
819f4c25 49using std::vector;
479bcabe 50
bf0edd2b
SA
51using pv::data::SignalBase;
52
e8d00928
ML
53using sigrok::Channel;
54using sigrok::ChannelGroup;
55using sigrok::Device;
56
cdb50f67 57namespace pv {
51d4a9ab 58namespace popups {
cdb50f67 59
2b81ae46 60Channels::Channels(Session &session, QWidget *parent) :
51d4a9ab 61 Popup(parent),
8dbbc7f0
JH
62 session_(session),
63 updating_channels_(false),
64 enable_all_channels_(tr("Enable All"), this),
65 disable_all_channels_(tr("Disable All"), this),
66 check_box_mapper_(this)
cdb50f67 67{
488f068c 68 // Create the layout
8dbbc7f0 69 setLayout(&layout_);
9c025ef3 70
da30ecb7 71 const shared_ptr<sigrok::Device> device = session_.device()->device();
e8d00928 72 assert(device);
488f068c
JH
73
74 // Collect a set of signals
bf0edd2b 75 map<shared_ptr<Channel>, shared_ptr<SignalBase> > signal_map;
c3a740dd 76
bf0edd2b 77 unordered_set< shared_ptr<SignalBase> > sigs;
47e9e7bb
SA
78 for (const shared_ptr<data::SignalBase> b : session_.signalbases())
79 sigs.insert(b);
733eee0e 80
bf0edd2b 81 for (const shared_ptr<SignalBase> &sig : sigs)
6ac6242b 82 signal_map[sig->channel()] = sig;
488f068c 83
2445160a 84 // Populate channel groups
2ad82c2e 85 for (auto entry : device->channel_groups()) {
e8d00928
ML
86 shared_ptr<ChannelGroup> group = entry.second;
87 // Make a set of signals, and removed this signals from the
488f068c 88 // signal map.
bf0edd2b 89 vector< shared_ptr<SignalBase> > group_sigs;
2ad82c2e 90 for (auto channel : group->channels()) {
6ac6242b 91 const auto iter = signal_map.find(channel);
52a80eb3
SA
92
93 if (iter == signal_map.end())
94 break;
488f068c
JH
95
96 group_sigs.push_back((*iter).second);
97 signal_map.erase(iter);
98 }
99
100 populate_group(group, group_sigs);
101 }
102
6ac6242b 103 // Make a vector of the remaining channels
bf0edd2b 104 vector< shared_ptr<SignalBase> > global_sigs;
2ad82c2e 105 for (auto channel : device->channels()) {
bf0edd2b 106 const map<shared_ptr<Channel>, shared_ptr<SignalBase> >::
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
bf0edd2b 139 for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
2ad82c2e
UH
140 check_box_signal_map_.begin();
141 i != check_box_signal_map_.end(); i++) {
bf0edd2b 142 const shared_ptr<SignalBase> sig = (*i).second;
488f068c 143 assert(sig);
b7b659aa 144
bf0edd2b 145 sig->set_enabled(set);
488f068c 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,
bf0edd2b 153 const vector< shared_ptr<SignalBase> > sigs)
488f068c 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)
067bb624 162 binding = make_shared<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
c063290a 171 QGridLayout *const channel_grid = create_channel_group_grid(sigs);
8dbbc7f0 172 layout_.addRow(channel_grid);
488f068c 173
2445160a 174 // Create the channel group options
c063290a 175 if (binding) {
8dbbc7f0
JH
176 binding->add_properties_to_form(&layout_, true);
177 group_bindings_.push_back(binding);
488f068c
JH
178 }
179}
180
6ac6242b 181QGridLayout* Channels::create_channel_group_grid(
bf0edd2b 182 const vector< shared_ptr<SignalBase> > sigs)
488f068c
JH
183{
184 int row = 0, col = 0;
185 QGridLayout *const grid = new QGridLayout();
186
bf0edd2b 187 for (const shared_ptr<SignalBase>& sig : sigs) {
488f068c
JH
188 assert(sig);
189
0a47889b 190 QCheckBox *const checkbox = new QCheckBox(sig->name());
8dbbc7f0 191 check_box_mapper_.setMapping(checkbox, checkbox);
488f068c 192 connect(checkbox, SIGNAL(toggled(bool)),
8dbbc7f0 193 &check_box_mapper_, SLOT(map()));
488f068c
JH
194
195 grid->addWidget(checkbox, row, col);
196
8dbbc7f0 197 check_box_signal_map_[checkbox] = sig;
488f068c 198
f3290553 199 if (++col >= 8)
488f068c
JH
200 col = 0, row++;
201 }
202
203 return grid;
204}
205
d9ea9628 206void Channels::showEvent(QShowEvent *event)
ed773982 207{
d9ea9628 208 pv::widgets::Popup::showEvent(event);
b7b659aa 209
8dbbc7f0 210 updating_channels_ = true;
b7b659aa 211
bf0edd2b 212 for (map<QCheckBox*, shared_ptr<SignalBase> >::const_iterator i =
2ad82c2e
UH
213 check_box_signal_map_.begin();
214 i != check_box_signal_map_.end(); i++) {
bf0edd2b 215 const shared_ptr<SignalBase> sig = (*i).second;
488f068c
JH
216 assert(sig);
217
218 (*i).first->setChecked(sig->enabled());
ed773982 219 }
b7b659aa 220
8dbbc7f0 221 updating_channels_ = false;
ed773982
JH
222}
223
6ac6242b 224void Channels::on_channel_checked(QWidget *widget)
51d4a9ab 225{
8dbbc7f0 226 if (updating_channels_)
b7b659aa
JH
227 return;
228
488f068c
JH
229 QCheckBox *const check_box = (QCheckBox*)widget;
230 assert(check_box);
231
232 // Look up the signal of this check-box
bf0edd2b 233 map< QCheckBox*, shared_ptr<SignalBase> >::const_iterator iter =
8dbbc7f0
JH
234 check_box_signal_map_.find((QCheckBox*)check_box);
235 assert(iter != check_box_signal_map_.end());
488f068c 236
bf0edd2b 237 const shared_ptr<SignalBase> s = (*iter).second;
aca00b1e 238 assert(s);
488f068c 239
bf0edd2b 240 s->set_enabled(check_box->isChecked());
51d4a9ab
JH
241}
242
6ac6242b 243void Channels::enable_all_channels()
ed773982 244{
6ac6242b 245 set_all_channels(true);
ed773982
JH
246}
247
6ac6242b 248void Channels::disable_all_channels()
ed773982 249{
6ac6242b 250 set_all_channels(false);
ed773982
JH
251}
252
51d4a9ab
JH
253} // popups
254} // pv