]> sigrok.org Git - pulseview.git/blame - pv/dialogs/connect.cpp
Connect dialog: Fix constructor initialization order
[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>
26
2acdb232 27#include "connect.hpp"
9663c82b 28
da30ecb7
JH
29#include <pv/devicemanager.hpp>
30#include <pv/devices/hardwaredevice.hpp>
9663c82b 31
819f4c25 32using std::list;
e8d00928 33using std::map;
f9abf97e 34using std::shared_ptr;
819f4c25 35using std::string;
107ca6d3 36
e8d00928
ML
37using Glib::ustring;
38using Glib::Variant;
39using Glib::VariantBase;
40
41using sigrok::ConfigKey;
42using sigrok::Driver;
43using sigrok::Error;
da30ecb7
JH
44
45using pv::devices::HardwareDevice;
c9de8b32 46
9663c82b
JH
47namespace pv {
48namespace dialogs {
49
107ca6d3 50Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
9663c82b 51 QDialog(parent),
8dbbc7f0
JH
52 device_manager_(device_manager),
53 layout_(this),
54 form_(this),
55 form_layout_(&form_),
56 drivers_(&form_),
34750abe 57 serial_devices_(&form_),
f21fd40b
SA
58 tcp_endpoint_(&form_),
59 tcp_endpoint_layout_(&tcp_endpoint_),
60 tcp_host_(&tcp_endpoint_),
61 tcp_port_(&tcp_endpoint_),
be7645e8 62 scan_button_(tr("&Scan for devices using driver above"), this),
8dbbc7f0
JH
63 device_list_(this),
64 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
9663c82b
JH
65 Qt::Horizontal, this)
66{
67 setWindowTitle(tr("Connect to Device"));
68
8dbbc7f0
JH
69 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
70 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
9663c82b
JH
71
72 populate_drivers();
8dbbc7f0 73 connect(&drivers_, SIGNAL(activated(int)),
9663c82b
JH
74 this, SLOT(device_selected(int)));
75
8dbbc7f0 76 form_.setLayout(&form_layout_);
a0730b3b 77 form_layout_.addRow(tr("&Driver"), &drivers_);
9663c82b 78
34750abe
AJ
79 form_layout_.addRow(tr("Serial &Port"), &serial_devices_);
80 serial_devices_.setEditable(true);
9663c82b 81
26f209b7 82 tcp_host_.setPlaceholderText("192.168.1.100");
83 tcp_endpoint_layout_.addWidget(&tcp_host_);
84 tcp_endpoint_layout_.addWidget(new QLabel(":"));
85 tcp_port_.setRange(1, 65535);
86 tcp_port_.setValue(5555);
87 tcp_endpoint_layout_.addWidget(&tcp_port_);
88 tcp_endpoint_layout_.setContentsMargins(0, 0, 0, 0);
89 form_layout_.addRow(tr("TCP &Endpoint"), &tcp_endpoint_);
90
9663c82b
JH
91 unset_connection();
92
8dbbc7f0 93 connect(&scan_button_, SIGNAL(pressed()),
c9de8b32
JH
94 this, SLOT(scan_pressed()));
95
8dbbc7f0
JH
96 setLayout(&layout_);
97 layout_.addWidget(&form_);
98 layout_.addWidget(&scan_button_);
99 layout_.addWidget(&device_list_);
100 layout_.addWidget(&button_box_);
9663c82b
JH
101}
102
e8d00928 103shared_ptr<HardwareDevice> Connect::get_selected_device() const
5eb0fa13 104{
8dbbc7f0 105 const QListWidgetItem *const item = device_list_.currentItem();
5eb0fa13 106 if (!item)
e8d00928 107 return shared_ptr<HardwareDevice>();
19adbc2c 108
e8d00928 109 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
5eb0fa13
JH
110}
111
9663c82b
JH
112void Connect::populate_drivers()
113{
8dbbc7f0 114 for (auto entry : device_manager_.context()->drivers()) {
e8d00928
ML
115 auto name = entry.first;
116 auto driver = entry.second;
30938303
JH
117 /**
118 * We currently only support devices that can deliver
119 * samples at a fixed samplerate i.e. oscilloscopes and
120 * logic analysers.
121 * @todo Add support for non-monotonic devices i.e. DMMs
122 * and sensors.
123 */
7bb0fbf4
ML
124 const auto keys = driver->config_keys();
125
126 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
127 keys.count(ConfigKey::OSCILLOSCOPE);
30938303 128
793f8096 129 if (supported_device)
8dbbc7f0 130 drivers_.addItem(QString("%1 (%2)").arg(
744aa24f 131 driver->long_name().c_str(), name.c_str()),
e8d00928 132 qVariantFromValue(driver));
30938303 133 }
9663c82b
JH
134}
135
34750abe
AJ
136void Connect::populate_serials(shared_ptr<Driver> driver)
137{
138 serial_devices_.clear();
139 for (auto serial : device_manager_.context()->serials(driver))
140 serial_devices_.addItem(QString("%1 (%2)").arg(
744aa24f 141 serial.first.c_str(), serial.second.c_str()),
34750abe
AJ
142 QString::fromStdString(serial.first));
143}
144
c9de8b32
JH
145void Connect::unset_connection()
146{
8dbbc7f0 147 device_list_.clear();
34750abe
AJ
148 serial_devices_.hide();
149 form_layout_.labelForField(&serial_devices_)->hide();
26f209b7 150 tcp_endpoint_.hide();
151 form_layout_.labelForField(&tcp_endpoint_)->hide();
8dbbc7f0 152 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
c9de8b32
JH
153}
154
34750abe 155void Connect::set_serial_connection(shared_ptr<Driver> driver)
c9de8b32 156{
34750abe
AJ
157 populate_serials(driver);
158 serial_devices_.show();
159 form_layout_.labelForField(&serial_devices_)->show();
c9de8b32
JH
160}
161
26f209b7 162void Connect::set_tcp_connection(shared_ptr<Driver> driver)
163{
164 (void)driver;
165
166 tcp_endpoint_.show();
167 form_layout_.labelForField(&tcp_endpoint_)->show();
168}
169
c9de8b32
JH
170void Connect::scan_pressed()
171{
8dbbc7f0 172 device_list_.clear();
c9de8b32 173
8dbbc7f0 174 const int index = drivers_.currentIndex();
793f8096 175 if (index == -1)
c9de8b32
JH
176 return;
177
e8d00928 178 shared_ptr<Driver> driver =
8dbbc7f0 179 drivers_.itemData(index).value<shared_ptr<Driver>>();
c9de8b32 180
e8d00928 181 assert(driver);
c9de8b32 182
e8d00928 183 map<const ConfigKey *, VariantBase> drvopts;
c9de8b32 184
34750abe
AJ
185 if (serial_devices_.isVisible()) {
186 QString serial;
187 const int index = serial_devices_.currentIndex();
188 if (index >= 0 && index < serial_devices_.count() &&
189 serial_devices_.currentText() == serial_devices_.itemText(index))
190 serial = serial_devices_.itemData(index).value<QString>();
191 else
192 serial = serial_devices_.currentText();
e8d00928 193 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
34750abe
AJ
194 serial.toUtf8().constData());
195 }
c9de8b32 196
26f209b7 197 if (tcp_endpoint_.isVisible()) {
198 QString host = tcp_host_.text();
199 QString port = tcp_port_.text();
200 if(!host.isEmpty()) {
201 QString conn = QString("tcp-raw/%1/%2").arg(host, port);
202 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
203 conn.toUtf8().constData());
204 }
205 }
206
da30ecb7 207 const list< shared_ptr<HardwareDevice> > devices =
8dbbc7f0 208 device_manager_.driver_scan(driver, drvopts);
c9de8b32 209
2ad82c2e 210 for (shared_ptr<HardwareDevice> device : devices) {
e8d00928 211 assert(device);
19adbc2c 212
e8d00928 213 QString text = QString::fromStdString(
3084ed4b 214 device->display_name(device_manager_));
da30ecb7
JH
215 text += QString(" with %1 channels").arg(
216 device->device()->channels().size());
c9de8b32 217
5eb0fa13 218 QListWidgetItem *const item = new QListWidgetItem(text,
8dbbc7f0 219 &device_list_);
e8d00928 220 item->setData(Qt::UserRole, qVariantFromValue(device));
8dbbc7f0 221 device_list_.addItem(item);
c9de8b32
JH
222 }
223
8dbbc7f0
JH
224 device_list_.setCurrentRow(0);
225 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
c9de8b32
JH
226}
227
9663c82b
JH
228void Connect::device_selected(int index)
229{
e8d00928 230 shared_ptr<Driver> driver =
8dbbc7f0 231 drivers_.itemData(index).value<shared_ptr<Driver>>();
9663c82b
JH
232
233 unset_connection();
234
7bb0fbf4 235 if (driver->scan_options().count(ConfigKey::SERIALCOMM))
34750abe 236 set_serial_connection(driver);
26f209b7 237
238 if (driver->name() == "rigol-ds") // NBNB
239 set_tcp_connection(driver);
9663c82b
JH
240}
241
9663c82b
JH
242} // namespace dialogs
243} // namespace pv