]> sigrok.org Git - pulseview.git/blame - pv/dialogs/connect.cpp
Use libsigrok C++ bindings (patch version 7).
[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
e8d00928 23#include <libsigrok/libsigrok.hpp>
19adbc2c 24
9663c82b
JH
25#include "connect.h"
26
107ca6d3 27#include "pv/devicemanager.h"
9663c82b 28
819f4c25 29using std::list;
e8d00928 30using std::map;
f9abf97e 31using std::shared_ptr;
819f4c25 32using std::string;
107ca6d3 33
e8d00928
ML
34using Glib::ustring;
35using Glib::Variant;
36using Glib::VariantBase;
37
38using sigrok::ConfigKey;
39using sigrok::Driver;
40using sigrok::Error;
41using sigrok::HardwareDevice;
c9de8b32 42
9663c82b
JH
43namespace pv {
44namespace dialogs {
45
107ca6d3 46Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
9663c82b 47 QDialog(parent),
107ca6d3 48 _device_manager(device_manager),
9663c82b
JH
49 _layout(this),
50 _form(this),
51 _form_layout(&_form),
52 _drivers(&_form),
53 _serial_device(&_form),
c9de8b32
JH
54 _scan_button(tr("Scan for Devices"), this),
55 _device_list(this),
9663c82b
JH
56 _button_box(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
57 Qt::Horizontal, this)
58{
59 setWindowTitle(tr("Connect to Device"));
60
61 connect(&_button_box, SIGNAL(accepted()), this, SLOT(accept()));
62 connect(&_button_box, SIGNAL(rejected()), this, SLOT(reject()));
63
64 populate_drivers();
65 connect(&_drivers, SIGNAL(activated(int)),
66 this, SLOT(device_selected(int)));
67
68 _form.setLayout(&_form_layout);
69 _form_layout.addRow(tr("Driver"), &_drivers);
70
71 _form_layout.addRow(tr("Serial Port"), &_serial_device);
72
73 unset_connection();
74
c9de8b32
JH
75 connect(&_scan_button, SIGNAL(pressed()),
76 this, SLOT(scan_pressed()));
77
9663c82b
JH
78 setLayout(&_layout);
79 _layout.addWidget(&_form);
c9de8b32
JH
80 _layout.addWidget(&_scan_button);
81 _layout.addWidget(&_device_list);
9663c82b
JH
82 _layout.addWidget(&_button_box);
83}
84
e8d00928 85shared_ptr<HardwareDevice> Connect::get_selected_device() const
5eb0fa13
JH
86{
87 const QListWidgetItem *const item = _device_list.currentItem();
88 if (!item)
e8d00928 89 return shared_ptr<HardwareDevice>();
19adbc2c 90
e8d00928 91 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
5eb0fa13
JH
92}
93
9663c82b
JH
94void Connect::populate_drivers()
95{
e8d00928
ML
96 for (auto entry : _device_manager.context()->drivers()) {
97 auto name = entry.first;
98 auto driver = entry.second;
30938303
JH
99 /**
100 * We currently only support devices that can deliver
101 * samples at a fixed samplerate i.e. oscilloscopes and
102 * logic analysers.
103 * @todo Add support for non-monotonic devices i.e. DMMs
104 * and sensors.
105 */
e8d00928
ML
106 bool supported_device = driver->config_check(
107 ConfigKey::SAMPLERATE, ConfigKey::DEVICE_OPTIONS);
30938303 108
793f8096 109 if (supported_device)
30938303 110 _drivers.addItem(QString("%1 (%2)").arg(
e8d00928
ML
111 driver->long_name().c_str()).arg(name.c_str()),
112 qVariantFromValue(driver));
30938303 113 }
9663c82b
JH
114}
115
c9de8b32
JH
116void Connect::unset_connection()
117{
118 _device_list.clear();
119 _serial_device.hide();
120 _form_layout.labelForField(&_serial_device)->hide();
f60ec39f 121 _button_box.button(QDialogButtonBox::Ok)->setDisabled(true);
c9de8b32
JH
122}
123
124void Connect::set_serial_connection()
125{
126 _serial_device.show();
127 _form_layout.labelForField(&_serial_device)->show();
128}
129
130void Connect::scan_pressed()
131{
132 _device_list.clear();
133
134 const int index = _drivers.currentIndex();
793f8096 135 if (index == -1)
c9de8b32
JH
136 return;
137
e8d00928
ML
138 shared_ptr<Driver> driver =
139 _drivers.itemData(index).value<shared_ptr<Driver>>();
c9de8b32 140
e8d00928 141 assert(driver);
c9de8b32 142
e8d00928 143 map<const ConfigKey *, VariantBase> drvopts;
c9de8b32 144
e8d00928
ML
145 if (_serial_device.isVisible())
146 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
147 _serial_device.text().toUtf8().constData());
c9de8b32 148
e8d00928
ML
149 list< shared_ptr<HardwareDevice> > devices =
150 _device_manager.driver_scan(driver, drvopts);
c9de8b32 151
e8d00928 152 for (shared_ptr<HardwareDevice> device : devices)
dd63af74 153 {
e8d00928 154 assert(device);
19adbc2c 155
e8d00928
ML
156 QString text = QString::fromStdString(
157 _device_manager.device_description(device));
158 text += QString(" with %1 channels").arg(device->channels().size());
c9de8b32 159
5eb0fa13
JH
160 QListWidgetItem *const item = new QListWidgetItem(text,
161 &_device_list);
e8d00928 162 item->setData(Qt::UserRole, qVariantFromValue(device));
5eb0fa13 163 _device_list.addItem(item);
c9de8b32
JH
164 }
165
f60ec39f 166 _device_list.setCurrentRow(0);
f4413378 167 _button_box.button(QDialogButtonBox::Ok)->setDisabled(_device_list.count() == 0);
c9de8b32
JH
168}
169
9663c82b
JH
170void Connect::device_selected(int index)
171{
e8d00928
ML
172 shared_ptr<Driver> driver =
173 _drivers.itemData(index).value<shared_ptr<Driver>>();
9663c82b
JH
174
175 unset_connection();
176
e8d00928
ML
177 if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
178 set_serial_connection();
9663c82b
JH
179}
180
9663c82b
JH
181} // namespace dialogs
182} // namespace pv