]> sigrok.org Git - pulseview.git/blob - pv/dialogs/connect.cpp
license: remove FSF postal address from boiler plate license text
[pulseview.git] / pv / dialogs / connect.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012-2013 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 <cassert>
21
22 #include <libsigrokcxx/libsigrokcxx.hpp>
23
24 #include <QLabel>
25 #include <QGroupBox>
26 #include <QRadioButton>
27
28 #include "connect.hpp"
29
30 #include <pv/devicemanager.hpp>
31 #include <pv/devices/hardwaredevice.hpp>
32
33 using std::list;
34 using std::map;
35 using std::shared_ptr;
36 using std::string;
37
38 using Glib::ustring;
39 using Glib::Variant;
40 using Glib::VariantBase;
41
42 using sigrok::ConfigKey;
43 using sigrok::Driver;
44 using sigrok::Error;
45
46 using pv::devices::HardwareDevice;
47
48 namespace pv {
49 namespace dialogs {
50
51 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
52         QDialog(parent),
53         device_manager_(device_manager),
54         layout_(this),
55         form_(this),
56         form_layout_(&form_),
57         drivers_(&form_),
58         serial_devices_(&form_),
59         scan_button_(tr("&Scan for devices using driver above"), this),
60         device_list_(this),
61         button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
62                 Qt::Horizontal, this)
63 {
64         setWindowTitle(tr("Connect to Device"));
65
66         connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
67         connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
68
69         populate_drivers();
70         connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
71
72         form_.setLayout(&form_layout_);
73
74         QVBoxLayout *vbox_drv = new QVBoxLayout;
75         vbox_drv->addWidget(&drivers_);
76         QGroupBox *groupbox_drv = new QGroupBox(tr("Step 1: Choose the driver"));
77         groupbox_drv->setLayout(vbox_drv);
78         form_layout_.addRow(groupbox_drv);
79
80         QRadioButton *radiobtn_usb = new QRadioButton(tr("&USB"), this);
81         QRadioButton *radiobtn_serial = new QRadioButton(tr("Serial &Port"), this);
82         QRadioButton *radiobtn_tcp = new QRadioButton(tr("&TCP/IP"), this);
83
84         radiobtn_usb->setChecked(true);
85
86         serial_devices_.setEditable(true);
87         serial_devices_.setEnabled(false);
88
89         tcp_config_ = new QWidget();
90         QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_);
91         tcp_host_ = new QLineEdit;
92         tcp_host_->setText("192.168.1.100");
93         tcp_config_layout->addWidget(tcp_host_);
94         tcp_config_layout->addWidget(new QLabel(":"));
95         tcp_port_ = new QSpinBox;
96         tcp_port_->setRange(1, 65535);
97         tcp_port_->setValue(5555);
98         tcp_config_layout->addWidget(tcp_port_);
99         tcp_use_vxi_ = new QCheckBox();
100         tcp_use_vxi_->setText(tr("Use VXI"));
101         tcp_config_layout->addSpacing(30);
102         tcp_config_layout->addWidget(tcp_use_vxi_);
103         tcp_config_layout->setContentsMargins(0, 0, 0, 0);
104         tcp_config_->setEnabled(false);
105
106         QVBoxLayout *vbox_if = new QVBoxLayout;
107         vbox_if->addWidget(radiobtn_usb);
108         vbox_if->addWidget(radiobtn_serial);
109         vbox_if->addWidget(&serial_devices_);
110         vbox_if->addWidget(radiobtn_tcp);
111         vbox_if->addWidget(tcp_config_);
112
113         QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface"));
114         groupbox_if->setLayout(vbox_if);
115         form_layout_.addRow(groupbox_if);
116
117         QVBoxLayout *vbox_scan = new QVBoxLayout;
118         vbox_scan->addWidget(&scan_button_);
119         QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices"));
120         groupbox_scan->setLayout(vbox_scan);
121         form_layout_.addRow(groupbox_scan);
122
123         QVBoxLayout *vbox_select = new QVBoxLayout;
124         vbox_select->addWidget(&device_list_);
125         QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device"));
126         groupbox_select->setLayout(vbox_select);
127         form_layout_.addRow(groupbox_select);
128
129         unset_connection();
130
131         connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool)));
132         connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool)));
133         connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed()));
134
135         setLayout(&layout_);
136
137         layout_.addWidget(&form_);
138         layout_.addWidget(&button_box_);
139 }
140
141 shared_ptr<HardwareDevice> Connect::get_selected_device() const
142 {
143         const QListWidgetItem *const item = device_list_.currentItem();
144         if (!item)
145                 return shared_ptr<HardwareDevice>();
146
147         return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
148 }
149
150 void Connect::populate_drivers()
151 {
152         for (auto entry : device_manager_.context()->drivers()) {
153                 auto name = entry.first;
154                 auto driver = entry.second;
155                 /**
156                  * We currently only support devices that can deliver
157                  * samples at a fixed samplerate i.e. oscilloscopes and
158                  * logic analysers.
159                  * @todo Add support for non-monotonic devices i.e. DMMs
160                  * and sensors.
161                  */
162                 const auto keys = driver->config_keys();
163
164                 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
165                         keys.count(ConfigKey::OSCILLOSCOPE);
166
167                 if (supported_device)
168                         drivers_.addItem(QString("%1 (%2)").arg(
169                                 driver->long_name().c_str(), name.c_str()),
170                                 qVariantFromValue(driver));
171         }
172 }
173
174 void Connect::populate_serials(shared_ptr<Driver> driver)
175 {
176         serial_devices_.clear();
177         for (auto serial : device_manager_.context()->serials(driver))
178                 serial_devices_.addItem(QString("%1 (%2)").arg(
179                         serial.first.c_str(), serial.second.c_str()),
180                         QString::fromStdString(serial.first));
181 }
182
183 void Connect::unset_connection()
184 {
185         device_list_.clear();
186         button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
187 }
188
189 void Connect::serial_toggled(bool checked)
190 {
191         serial_devices_.setEnabled(checked);
192 }
193
194 void Connect::tcp_toggled(bool checked)
195 {
196         tcp_config_->setEnabled(checked);
197 }
198
199 void Connect::scan_pressed()
200 {
201         device_list_.clear();
202
203         const int index = drivers_.currentIndex();
204         if (index == -1)
205                 return;
206
207         shared_ptr<Driver> driver =
208                 drivers_.itemData(index).value<shared_ptr<Driver>>();
209
210         assert(driver);
211
212         map<const ConfigKey *, VariantBase> drvopts;
213
214         if (serial_devices_.isEnabled()) {
215                 QString serial;
216                 const int index = serial_devices_.currentIndex();
217                 if (index >= 0 && index < serial_devices_.count() &&
218                     serial_devices_.currentText() == serial_devices_.itemText(index))
219                         serial = serial_devices_.itemData(index).value<QString>();
220                 else
221                         serial = serial_devices_.currentText();
222                 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
223                         serial.toUtf8().constData());
224         }
225
226         if (tcp_config_->isEnabled()) {
227                 QString host = tcp_host_->text();
228                 QString port = tcp_port_->text();
229                 if (!host.isEmpty()) {
230                         QString conn;
231                         if (tcp_use_vxi_->isChecked())
232                                 conn = QString("vxi/%1/%2").arg(host, port);
233                         else
234                                 conn = QString("tcp-raw/%1/%2").arg(host, port);
235
236                         drvopts[ConfigKey::CONN] = Variant<ustring>::create(
237                                 conn.toUtf8().constData());
238                 }
239         }
240
241         const list< shared_ptr<HardwareDevice> > devices =
242                 device_manager_.driver_scan(driver, drvopts);
243
244         for (shared_ptr<HardwareDevice> device : devices) {
245                 assert(device);
246
247                 QString text = QString::fromStdString(
248                         device->display_name(device_manager_));
249                 text += QString(" with %1 channels").arg(
250                         device->device()->channels().size());
251
252                 QListWidgetItem *const item = new QListWidgetItem(text,
253                         &device_list_);
254                 item->setData(Qt::UserRole, qVariantFromValue(device));
255                 device_list_.addItem(item);
256         }
257
258         device_list_.setCurrentRow(0);
259         button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
260 }
261
262 void Connect::driver_selected(int index)
263 {
264         shared_ptr<Driver> driver =
265                 drivers_.itemData(index).value<shared_ptr<Driver>>();
266
267         unset_connection();
268
269         populate_serials(driver);
270 }
271
272 } // namespace dialogs
273 } // namespace pv