fe21c9a42b6ee49fef94125fc62ff0187a3f81fd
[pulseview.git] / pv / dialogs / deviceoptions.cpp
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 "deviceoptions.h"
22
23 #include <boost/foreach.hpp>
24
25 #include <QFormLayout>
26 #include <QListWidget>
27
28 #include <pv/prop/property.h>
29
30 using namespace boost;
31 using namespace std;
32
33 namespace pv {
34 namespace dialogs {
35
36 DeviceOptions::DeviceOptions(QWidget *parent, struct sr_dev_inst *sdi) :
37         QDialog(parent),
38         _sdi(sdi),
39         _layout(this),
40         _probes_box(tr("Probes"), this),
41         _probes(this),
42         _probes_bar(this),
43         _enable_all_probes(this),
44         _disable_all_probes(this),
45         _props_box(tr("Configuration"), this),
46         _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
47                 Qt::Horizontal, this),
48         _device_options_binding(sdi)
49 {
50         setWindowTitle(tr("Configure Device"));
51
52         connect(&_enable_all_probes, SIGNAL(clicked()),
53                 this, SLOT(enable_all_probes()));
54         connect(&_disable_all_probes, SIGNAL(clicked()),
55                 this, SLOT(disable_all_probes()));
56
57         connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept()));
58         connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject()));
59
60         setLayout(&_layout);
61
62         setup_probes();
63         _probes_box.setLayout(&_probes_box_layout);
64         _probes_box_layout.addWidget(&_probes);
65
66         _enable_all_probes.setText(tr("Enable All"));
67         _probes_bar.addWidget(&_enable_all_probes);
68
69         _disable_all_probes.setText(tr("Disable All"));
70         _probes_bar.addWidget(&_disable_all_probes);
71
72         _probes_box_layout.addWidget(&_probes_bar);
73         _layout.addWidget(&_probes_box);
74
75
76         _props_box.setLayout(&_props_box_layout);
77         _props_box_layout.addWidget(
78                 _device_options_binding.get_property_form(this));
79         _layout.addWidget(&_props_box);
80
81         _layout.addWidget(&_button_box);
82 }
83
84 void DeviceOptions::accept()
85 {
86         using namespace Qt;
87
88         QDialog::accept();
89
90         // Commit the probes
91         for (int i = 0; i < _probes.count(); i++) {
92                 const QListWidgetItem *const item = _probes.item(i);
93                 assert(item);
94                 sr_probe *const probe = (sr_probe*)
95                         item->data(UserRole).value<void*>();
96                 assert(probe);
97                 probe->enabled = item->checkState() == Checked;
98         }
99
100         // Commit the properties
101         _device_options_binding.commit();
102 }
103
104 void DeviceOptions::setup_probes()
105 {
106         using namespace Qt;
107
108         for (const GSList *l = _sdi->probes; l; l = l->next) {
109                 sr_probe *const probe = (sr_probe*)l->data;
110                 assert(probe);
111                 QListWidgetItem *const item = new QListWidgetItem(
112                         probe->name, &_probes);
113                 assert(item);
114                 item->setCheckState(probe->enabled ?
115                         Checked : Unchecked);
116                 item->setData(UserRole,
117                         qVariantFromValue((void*)probe));
118                 _probes.addItem(item);
119         }
120 }
121
122 void DeviceOptions::set_all_probes(bool set)
123 {
124         for (int i = 0; i < _probes.count(); i++) {
125                 QListWidgetItem *const item = _probes.item(i);
126                 assert(item);
127                 item->setCheckState(set ? Qt::Checked : Qt::Unchecked);
128         }
129 }
130
131 void DeviceOptions::enable_all_probes()
132 {
133         set_all_probes(true);
134 }
135
136 void DeviceOptions::disable_all_probes()
137 {
138         set_all_probes(false);
139 }
140
141 } // namespace dialogs
142 } // namespace pv