]> sigrok.org Git - pulseview.git/blame - pv/dialogs/connect.cpp
Fix #849 by making sure no references to the DecodeTrace instance remain
[pulseview.git] / pv / dialogs / connect.cpp
CommitLineData
9663c82b
JH
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
f9abf97e
JH
21#include <cassert>
22
fe3a1c21 23#include <libsigrokcxx/libsigrokcxx.hpp>
19adbc2c 24
26f209b7 25#include <QLabel>
1ebe875c
SA
26#include <QGroupBox>
27#include <QRadioButton>
26f209b7 28
2acdb232 29#include "connect.hpp"
9663c82b 30
da30ecb7
JH
31#include <pv/devicemanager.hpp>
32#include <pv/devices/hardwaredevice.hpp>
9663c82b 33
819f4c25 34using std::list;
e8d00928 35using std::map;
f9abf97e 36using std::shared_ptr;
819f4c25 37using std::string;
107ca6d3 38
e8d00928
ML
39using Glib::ustring;
40using Glib::Variant;
41using Glib::VariantBase;
42
43using sigrok::ConfigKey;
44using sigrok::Driver;
45using sigrok::Error;
da30ecb7
JH
46
47using pv::devices::HardwareDevice;
c9de8b32 48
9663c82b
JH
49namespace pv {
50namespace dialogs {
51
107ca6d3 52Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
9663c82b 53 QDialog(parent),
8dbbc7f0
JH
54 device_manager_(device_manager),
55 layout_(this),
56 form_(this),
57 form_layout_(&form_),
58 drivers_(&form_),
34750abe 59 serial_devices_(&form_),
be7645e8 60 scan_button_(tr("&Scan for devices using driver above"), this),
8dbbc7f0
JH
61 device_list_(this),
62 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
9663c82b
JH
63 Qt::Horizontal, this)
64{
65 setWindowTitle(tr("Connect to Device"));
66
8dbbc7f0
JH
67 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
68 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
9663c82b
JH
69
70 populate_drivers();
1ebe875c 71 connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
9663c82b 72
8dbbc7f0 73 form_.setLayout(&form_layout_);
9663c82b 74
1ebe875c
SA
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);
80
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);
9663c82b 84
1ebe875c
SA
85 radiobtn_usb->setChecked(true);
86
87 serial_devices_.setEditable(true);
88 serial_devices_.setEnabled(false);
89
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);
106
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_);
113
114 QGroupBox *groupbox_if = new QGroupBox(tr("Step 2: Choose the interface"));
115 groupbox_if->setLayout(vbox_if);
116 form_layout_.addRow(groupbox_if);
117
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);
123
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);
26f209b7 129
9663c82b
JH
130 unset_connection();
131
1ebe875c
SA
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()));
c9de8b32 135
8dbbc7f0 136 setLayout(&layout_);
1ebe875c 137
8dbbc7f0 138 layout_.addWidget(&form_);
8dbbc7f0 139 layout_.addWidget(&button_box_);
9663c82b
JH
140}
141
e8d00928 142shared_ptr<HardwareDevice> Connect::get_selected_device() const
5eb0fa13 143{
8dbbc7f0 144 const QListWidgetItem *const item = device_list_.currentItem();
5eb0fa13 145 if (!item)
e8d00928 146 return shared_ptr<HardwareDevice>();
19adbc2c 147
e8d00928 148 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
5eb0fa13
JH
149}
150
9663c82b
JH
151void Connect::populate_drivers()
152{
8dbbc7f0 153 for (auto entry : device_manager_.context()->drivers()) {
e8d00928
ML
154 auto name = entry.first;
155 auto driver = entry.second;
30938303
JH
156 /**
157 * We currently only support devices that can deliver
158 * samples at a fixed samplerate i.e. oscilloscopes and
159 * logic analysers.
160 * @todo Add support for non-monotonic devices i.e. DMMs
161 * and sensors.
162 */
7bb0fbf4
ML
163 const auto keys = driver->config_keys();
164
165 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
166 keys.count(ConfigKey::OSCILLOSCOPE);
30938303 167
793f8096 168 if (supported_device)
8dbbc7f0 169 drivers_.addItem(QString("%1 (%2)").arg(
744aa24f 170 driver->long_name().c_str(), name.c_str()),
e8d00928 171 qVariantFromValue(driver));
30938303 172 }
9663c82b
JH
173}
174
34750abe
AJ
175void Connect::populate_serials(shared_ptr<Driver> driver)
176{
177 serial_devices_.clear();
178 for (auto serial : device_manager_.context()->serials(driver))
179 serial_devices_.addItem(QString("%1 (%2)").arg(
744aa24f 180 serial.first.c_str(), serial.second.c_str()),
34750abe
AJ
181 QString::fromStdString(serial.first));
182}
183
c9de8b32
JH
184void Connect::unset_connection()
185{
8dbbc7f0 186 device_list_.clear();
8dbbc7f0 187 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
c9de8b32
JH
188}
189
1ebe875c 190void Connect::serial_toggled(bool checked)
c9de8b32 191{
1ebe875c 192 serial_devices_.setEnabled(checked);
c9de8b32
JH
193}
194
1ebe875c 195void Connect::tcp_toggled(bool checked)
26f209b7 196{
1ebe875c 197 tcp_config_->setEnabled(checked);
26f209b7 198}
199
c9de8b32
JH
200void Connect::scan_pressed()
201{
8dbbc7f0 202 device_list_.clear();
c9de8b32 203
8dbbc7f0 204 const int index = drivers_.currentIndex();
793f8096 205 if (index == -1)
c9de8b32
JH
206 return;
207
e8d00928 208 shared_ptr<Driver> driver =
8dbbc7f0 209 drivers_.itemData(index).value<shared_ptr<Driver>>();
c9de8b32 210
e8d00928 211 assert(driver);
c9de8b32 212
e8d00928 213 map<const ConfigKey *, VariantBase> drvopts;
c9de8b32 214
1ebe875c 215 if (serial_devices_.isEnabled()) {
34750abe
AJ
216 QString serial;
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>();
221 else
222 serial = serial_devices_.currentText();
e8d00928 223 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
34750abe
AJ
224 serial.toUtf8().constData());
225 }
c9de8b32 226
1ebe875c
SA
227 if (tcp_config_->isEnabled()) {
228 QString host = tcp_host_->text();
229 QString port = tcp_port_->text();
230 if (!host.isEmpty()) {
231 QString conn;
232 if (tcp_use_vxi_->isChecked())
233 conn = QString("vxi/%1/%2").arg(host, port);
234 else
235 conn = QString("tcp-raw/%1/%2").arg(host, port);
236
26f209b7 237 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
238 conn.toUtf8().constData());
239 }
240 }
241
da30ecb7 242 const list< shared_ptr<HardwareDevice> > devices =
8dbbc7f0 243 device_manager_.driver_scan(driver, drvopts);
c9de8b32 244
2ad82c2e 245 for (shared_ptr<HardwareDevice> device : devices) {
e8d00928 246 assert(device);
19adbc2c 247
e8d00928 248 QString text = QString::fromStdString(
3084ed4b 249 device->display_name(device_manager_));
da30ecb7
JH
250 text += QString(" with %1 channels").arg(
251 device->device()->channels().size());
c9de8b32 252
5eb0fa13 253 QListWidgetItem *const item = new QListWidgetItem(text,
8dbbc7f0 254 &device_list_);
e8d00928 255 item->setData(Qt::UserRole, qVariantFromValue(device));
8dbbc7f0 256 device_list_.addItem(item);
c9de8b32
JH
257 }
258
8dbbc7f0
JH
259 device_list_.setCurrentRow(0);
260 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
c9de8b32
JH
261}
262
1ebe875c 263void Connect::driver_selected(int index)
9663c82b 264{
e8d00928 265 shared_ptr<Driver> driver =
8dbbc7f0 266 drivers_.itemData(index).value<shared_ptr<Driver>>();
9663c82b
JH
267
268 unset_connection();
269
1ebe875c 270 populate_serials(driver);
9663c82b
JH
271}
272
9663c82b
JH
273} // namespace dialogs
274} // namespace pv