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