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