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