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