2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
23 #include <libsigrokcxx/libsigrokcxx.hpp>
27 #include <QRadioButton>
29 #include "connect.hpp"
31 #include <pv/devicemanager.hpp>
32 #include <pv/devices/hardwaredevice.hpp>
36 using std::shared_ptr;
41 using Glib::VariantBase;
43 using sigrok::ConfigKey;
47 using pv::devices::HardwareDevice;
52 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
54 device_manager_(device_manager),
59 serial_devices_(&form_),
60 scan_button_(tr("&Scan for devices using driver above"), this),
62 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
65 setWindowTitle(tr("Connect to Device"));
67 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
68 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
71 connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
73 form_.setLayout(&form_layout_);
75 QVBoxLayout *vbox_drv = new QVBoxLayout;
76 vbox_drv->addWidget(&drivers_);
77 QGroupBox *groupbox_drv = new QGroupBox(tr("Step 1: Choose the driver"));
78 groupbox_drv->setLayout(vbox_drv);
79 form_layout_.addRow(groupbox_drv);
81 QRadioButton *radiobtn_usb = new QRadioButton(tr("&USB"), this);
82 QRadioButton *radiobtn_serial = new QRadioButton(tr("Serial &Port"), this);
83 QRadioButton *radiobtn_tcp = new QRadioButton(tr("&TCP/IP"), this);
85 radiobtn_usb->setChecked(true);
87 serial_devices_.setEditable(true);
88 serial_devices_.setEnabled(false);
90 tcp_config_ = new QWidget();
91 QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_);
92 tcp_host_ = new QLineEdit;
93 tcp_host_->setText("192.168.1.100");
94 tcp_config_layout->addWidget(tcp_host_);
95 tcp_config_layout->addWidget(new QLabel(":"));
96 tcp_port_ = new QSpinBox;
97 tcp_port_->setRange(1, 65535);
98 tcp_port_->setValue(5555);
99 tcp_config_layout->addWidget(tcp_port_);
100 tcp_use_vxi_ = new QCheckBox();
101 tcp_use_vxi_->setText(tr("Use VXI"));
102 tcp_config_layout->addSpacing(30);
103 tcp_config_layout->addWidget(tcp_use_vxi_);
104 tcp_config_layout->setContentsMargins(0, 0, 0, 0);
105 tcp_config_->setEnabled(false);
107 QVBoxLayout *vbox_if = new QVBoxLayout;
108 vbox_if->addWidget(radiobtn_usb);
109 vbox_if->addWidget(radiobtn_serial);
110 vbox_if->addWidget(&serial_devices_);
111 vbox_if->addWidget(radiobtn_tcp);
112 vbox_if->addWidget(tcp_config_);
114 QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface"));
115 groupbox_if->setLayout(vbox_if);
116 form_layout_.addRow(groupbox_if);
118 QVBoxLayout *vbox_scan = new QVBoxLayout;
119 vbox_scan->addWidget(&scan_button_);
120 QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices"));
121 groupbox_scan->setLayout(vbox_scan);
122 form_layout_.addRow(groupbox_scan);
124 QVBoxLayout *vbox_select = new QVBoxLayout;
125 vbox_select->addWidget(&device_list_);
126 QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device"));
127 groupbox_select->setLayout(vbox_select);
128 form_layout_.addRow(groupbox_select);
132 connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool)));
133 connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool)));
134 connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed()));
138 layout_.addWidget(&form_);
139 layout_.addWidget(&button_box_);
142 shared_ptr<HardwareDevice> Connect::get_selected_device() const
144 const QListWidgetItem *const item = device_list_.currentItem();
146 return shared_ptr<HardwareDevice>();
148 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
151 void Connect::populate_drivers()
153 for (auto entry : device_manager_.context()->drivers()) {
154 auto name = entry.first;
155 auto driver = entry.second;
157 * We currently only support devices that can deliver
158 * samples at a fixed samplerate i.e. oscilloscopes and
160 * @todo Add support for non-monotonic devices i.e. DMMs
163 const auto keys = driver->config_keys();
165 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
166 keys.count(ConfigKey::OSCILLOSCOPE);
168 if (supported_device)
169 drivers_.addItem(QString("%1 (%2)").arg(
170 driver->long_name().c_str(), name.c_str()),
171 qVariantFromValue(driver));
175 void Connect::populate_serials(shared_ptr<Driver> driver)
177 serial_devices_.clear();
178 for (auto serial : device_manager_.context()->serials(driver))
179 serial_devices_.addItem(QString("%1 (%2)").arg(
180 serial.first.c_str(), serial.second.c_str()),
181 QString::fromStdString(serial.first));
184 void Connect::unset_connection()
186 device_list_.clear();
187 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
190 void Connect::serial_toggled(bool checked)
192 serial_devices_.setEnabled(checked);
195 void Connect::tcp_toggled(bool checked)
197 tcp_config_->setEnabled(checked);
200 void Connect::scan_pressed()
202 device_list_.clear();
204 const int index = drivers_.currentIndex();
208 shared_ptr<Driver> driver =
209 drivers_.itemData(index).value<shared_ptr<Driver>>();
213 map<const ConfigKey *, VariantBase> drvopts;
215 if (serial_devices_.isEnabled()) {
217 const int index = serial_devices_.currentIndex();
218 if (index >= 0 && index < serial_devices_.count() &&
219 serial_devices_.currentText() == serial_devices_.itemText(index))
220 serial = serial_devices_.itemData(index).value<QString>();
222 serial = serial_devices_.currentText();
223 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
224 serial.toUtf8().constData());
227 if (tcp_config_->isEnabled()) {
228 QString host = tcp_host_->text();
229 QString port = tcp_port_->text();
230 if (!host.isEmpty()) {
232 if (tcp_use_vxi_->isChecked())
233 conn = QString("vxi/%1/%2").arg(host, port);
235 conn = QString("tcp-raw/%1/%2").arg(host, port);
237 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
238 conn.toUtf8().constData());
242 const list< shared_ptr<HardwareDevice> > devices =
243 device_manager_.driver_scan(driver, drvopts);
245 for (shared_ptr<HardwareDevice> device : devices) {
248 QString text = QString::fromStdString(
249 device->display_name(device_manager_));
250 text += QString(" with %1 channels").arg(
251 device->device()->channels().size());
253 QListWidgetItem *const item = new QListWidgetItem(text,
255 item->setData(Qt::UserRole, qVariantFromValue(device));
256 device_list_.addItem(item);
259 device_list_.setCurrentRow(0);
260 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
263 void Connect::driver_selected(int index)
265 shared_ptr<Driver> driver =
266 drivers_.itemData(index).value<shared_ptr<Driver>>();
270 populate_serials(driver);
273 } // namespace dialogs