]> sigrok.org Git - pulseview.git/blame_incremental - pv/dialogs/connect.cpp
Add "new session" and "new view" toolbar buttons
[pulseview.git] / pv / dialogs / connect.cpp
... / ...
CommitLineData
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
21#include <cassert>
22
23#include <libsigrokcxx/libsigrokcxx.hpp>
24
25#include <QLabel>
26#include <QGroupBox>
27#include <QRadioButton>
28
29#include "connect.hpp"
30
31#include <pv/devicemanager.hpp>
32#include <pv/devices/hardwaredevice.hpp>
33
34using std::list;
35using std::map;
36using std::shared_ptr;
37using std::string;
38
39using Glib::ustring;
40using Glib::Variant;
41using Glib::VariantBase;
42
43using sigrok::ConfigKey;
44using sigrok::Driver;
45using sigrok::Error;
46
47using pv::devices::HardwareDevice;
48
49namespace pv {
50namespace dialogs {
51
52Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
53 QDialog(parent),
54 device_manager_(device_manager),
55 layout_(this),
56 form_(this),
57 form_layout_(&form_),
58 drivers_(&form_),
59 serial_devices_(&form_),
60 scan_button_(tr("&Scan for devices using driver above"), this),
61 device_list_(this),
62 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
63 Qt::Horizontal, this)
64{
65 setWindowTitle(tr("Connect to Device"));
66
67 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
68 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
69
70 populate_drivers();
71 connect(&drivers_, SIGNAL(activated(int)), this, SLOT(driver_selected(int)));
72
73 form_.setLayout(&form_layout_);
74
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);
84
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);
129
130 unset_connection();
131
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()));
135
136 setLayout(&layout_);
137
138 layout_.addWidget(&form_);
139 layout_.addWidget(&button_box_);
140}
141
142shared_ptr<HardwareDevice> Connect::get_selected_device() const
143{
144 const QListWidgetItem *const item = device_list_.currentItem();
145 if (!item)
146 return shared_ptr<HardwareDevice>();
147
148 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
149}
150
151void Connect::populate_drivers()
152{
153 for (auto entry : device_manager_.context()->drivers()) {
154 auto name = entry.first;
155 auto driver = entry.second;
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 */
163 const auto keys = driver->config_keys();
164
165 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
166 keys.count(ConfigKey::OSCILLOSCOPE);
167
168 if (supported_device)
169 drivers_.addItem(QString("%1 (%2)").arg(
170 driver->long_name().c_str(), name.c_str()),
171 qVariantFromValue(driver));
172 }
173}
174
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(
180 serial.first.c_str(), serial.second.c_str()),
181 QString::fromStdString(serial.first));
182}
183
184void Connect::unset_connection()
185{
186 device_list_.clear();
187 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
188}
189
190void Connect::serial_toggled(bool checked)
191{
192 serial_devices_.setEnabled(checked);
193}
194
195void Connect::tcp_toggled(bool checked)
196{
197 tcp_config_->setEnabled(checked);
198}
199
200void Connect::scan_pressed()
201{
202 device_list_.clear();
203
204 const int index = drivers_.currentIndex();
205 if (index == -1)
206 return;
207
208 shared_ptr<Driver> driver =
209 drivers_.itemData(index).value<shared_ptr<Driver>>();
210
211 assert(driver);
212
213 map<const ConfigKey *, VariantBase> drvopts;
214
215 if (serial_devices_.isEnabled()) {
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();
223 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
224 serial.toUtf8().constData());
225 }
226
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
237 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
238 conn.toUtf8().constData());
239 }
240 }
241
242 const list< shared_ptr<HardwareDevice> > devices =
243 device_manager_.driver_scan(driver, drvopts);
244
245 for (shared_ptr<HardwareDevice> device : devices) {
246 assert(device);
247
248 QString text = QString::fromStdString(
249 device->display_name(device_manager_));
250 text += QString(" with %1 channels").arg(
251 device->device()->channels().size());
252
253 QListWidgetItem *const item = new QListWidgetItem(text,
254 &device_list_);
255 item->setData(Qt::UserRole, qVariantFromValue(device));
256 device_list_.addItem(item);
257 }
258
259 device_list_.setCurrentRow(0);
260 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
261}
262
263void Connect::driver_selected(int index)
264{
265 shared_ptr<Driver> driver =
266 drivers_.itemData(index).value<shared_ptr<Driver>>();
267
268 unset_connection();
269
270 populate_serials(driver);
271}
272
273} // namespace dialogs
274} // namespace pv