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