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, see <http://www.gnu.org/licenses/>.
22 #include <libsigrokcxx/libsigrokcxx.hpp>
26 #include <QRadioButton>
28 #include "connect.hpp"
30 #include <pv/devicemanager.hpp>
31 #include <pv/devices/hardwaredevice.hpp>
35 using std::shared_ptr;
40 using Glib::VariantBase;
42 using sigrok::ConfigKey;
45 using pv::devices::HardwareDevice;
50 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
52 device_manager_(device_manager),
57 serial_devices_(&form_),
58 scan_button_(tr("&Scan for devices using driver above"), this),
60 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
63 setWindowTitle(tr("Connect to Device"));
65 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
66 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
69 connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
71 form_.setLayout(&form_layout_);
73 QVBoxLayout *vbox_drv = new QVBoxLayout;
74 vbox_drv->addWidget(&drivers_);
75 QGroupBox *groupbox_drv = new QGroupBox(tr("Step 1: Choose the driver"));
76 groupbox_drv->setLayout(vbox_drv);
77 form_layout_.addRow(groupbox_drv);
79 QRadioButton *radiobtn_usb = new QRadioButton(tr("&USB"), this);
80 QRadioButton *radiobtn_serial = new QRadioButton(tr("Serial &Port"), this);
81 QRadioButton *radiobtn_tcp = new QRadioButton(tr("&TCP/IP"), this);
83 radiobtn_usb->setChecked(true);
85 serial_devices_.setEditable(true);
86 serial_devices_.setEnabled(false);
88 tcp_config_ = new QWidget();
89 QHBoxLayout *tcp_config_layout = new QHBoxLayout(tcp_config_);
90 tcp_host_ = new QLineEdit;
91 tcp_host_->setText("192.168.1.100");
92 tcp_config_layout->addWidget(tcp_host_);
93 tcp_config_layout->addWidget(new QLabel(":"));
94 tcp_port_ = new QSpinBox;
95 tcp_port_->setRange(1, 65535);
96 tcp_port_->setValue(5555);
97 tcp_config_layout->addWidget(tcp_port_);
99 tcp_config_layout->addSpacing(30);
100 tcp_config_layout->addWidget(new QLabel(tr("Protocol:")));
101 tcp_protocol_ = new QComboBox();
102 tcp_protocol_->addItem("Raw TCP", QVariant("tcp-raw/%1/%2"));
103 tcp_protocol_->addItem("VXI", QVariant("vxi/%1/%2"));
104 tcp_config_layout->addWidget(tcp_protocol_);
105 tcp_config_layout->setContentsMargins(0, 0, 0, 0);
106 tcp_config_->setEnabled(false);
108 // Let the device list occupy only the minimum space needed
109 device_list_.setMaximumHeight(device_list_.minimumSizeHint().height());
111 QVBoxLayout *vbox_if = new QVBoxLayout;
112 vbox_if->addWidget(radiobtn_usb);
113 vbox_if->addWidget(radiobtn_serial);
114 vbox_if->addWidget(&serial_devices_);
115 vbox_if->addWidget(radiobtn_tcp);
116 vbox_if->addWidget(tcp_config_);
118 QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface"));
119 groupbox_if->setLayout(vbox_if);
120 form_layout_.addRow(groupbox_if);
122 QVBoxLayout *vbox_scan = new QVBoxLayout;
123 vbox_scan->addWidget(&scan_button_);
124 QGroupBox *groupbox_scan = new QGroupBox(tr("Step 3: Scan for devices"));
125 groupbox_scan->setLayout(vbox_scan);
126 form_layout_.addRow(groupbox_scan);
128 QVBoxLayout *vbox_select = new QVBoxLayout;
129 vbox_select->addWidget(&device_list_);
130 QGroupBox *groupbox_select = new QGroupBox(tr("Step 4: Select the device"));
131 groupbox_select->setLayout(vbox_select);
132 form_layout_.addRow(groupbox_select);
136 connect(radiobtn_serial, SIGNAL(toggled(bool)), this, SLOT(serial_toggled(bool)));
137 connect(radiobtn_tcp, SIGNAL(toggled(bool)), this, SLOT(tcp_toggled(bool)));
138 connect(&scan_button_, SIGNAL(pressed()), this, SLOT(scan_pressed()));
142 layout_.addWidget(&form_);
143 layout_.addWidget(&button_box_);
146 shared_ptr<HardwareDevice> Connect::get_selected_device() const
148 const QListWidgetItem *const item = device_list_.currentItem();
150 return shared_ptr<HardwareDevice>();
152 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
155 void Connect::populate_drivers()
157 for (auto& entry : device_manager_.context()->drivers()) {
158 auto name = entry.first;
159 auto driver = entry.second;
161 * We currently only support devices that can deliver
162 * samples at a fixed samplerate i.e. oscilloscopes and
164 * @todo Add support for non-monotonic devices i.e. DMMs
167 const auto keys = driver->config_keys();
169 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
170 keys.count(ConfigKey::OSCILLOSCOPE);
172 if (supported_device)
173 drivers_.addItem(QString("%1 (%2)").arg(
174 driver->long_name().c_str(), name.c_str()),
175 qVariantFromValue(driver));
179 void Connect::populate_serials(shared_ptr<Driver> driver)
181 serial_devices_.clear();
182 for (auto& serial : device_manager_.context()->serials(driver))
183 serial_devices_.addItem(QString("%1 (%2)").arg(
184 serial.first.c_str(), serial.second.c_str()),
185 QString::fromStdString(serial.first));
188 void Connect::unset_connection()
190 device_list_.clear();
191 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
194 void Connect::serial_toggled(bool checked)
196 serial_devices_.setEnabled(checked);
199 void Connect::tcp_toggled(bool checked)
201 tcp_config_->setEnabled(checked);
204 void Connect::scan_pressed()
206 device_list_.clear();
208 const int index = drivers_.currentIndex();
212 shared_ptr<Driver> driver =
213 drivers_.itemData(index).value<shared_ptr<Driver>>();
217 map<const ConfigKey *, VariantBase> drvopts;
219 if (serial_devices_.isEnabled()) {
221 const int index = serial_devices_.currentIndex();
222 if (index >= 0 && index < serial_devices_.count() &&
223 serial_devices_.currentText() == serial_devices_.itemText(index))
224 serial = serial_devices_.itemData(index).toString();
226 serial = serial_devices_.currentText();
227 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
228 serial.toUtf8().constData());
231 if (tcp_config_->isEnabled()) {
232 QString host = tcp_host_->text();
233 QString port = tcp_port_->text();
234 if (!host.isEmpty()) {
236 tcp_protocol_->itemData(tcp_protocol_->currentIndex()).toString();
238 conn = conn.arg(host, port);
240 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
241 conn.toUtf8().constData());
245 const list< shared_ptr<HardwareDevice> > devices =
246 device_manager_.driver_scan(driver, drvopts);
248 for (const shared_ptr<HardwareDevice>& device : devices) {
251 QString text = QString::fromStdString(device->display_name(device_manager_));
252 text += QString(" with %1 channels").arg(device->device()->channels().size());
254 QListWidgetItem *const item = new QListWidgetItem(text, &device_list_);
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