]> sigrok.org Git - pulseview.git/blame_incremental - pv/dialogs/connect.cpp
Minor whitespace cosmetics.
[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#include <pv/devices/hardwaredevice.hpp>
29
30using std::list;
31using std::map;
32using std::shared_ptr;
33using std::string;
34
35using Glib::ustring;
36using Glib::Variant;
37using Glib::VariantBase;
38
39using sigrok::ConfigKey;
40using sigrok::Driver;
41using sigrok::Error;
42
43using pv::devices::HardwareDevice;
44
45namespace pv {
46namespace dialogs {
47
48Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
49 QDialog(parent),
50 device_manager_(device_manager),
51 layout_(this),
52 form_(this),
53 form_layout_(&form_),
54 drivers_(&form_),
55 serial_devices_(&form_),
56 scan_button_(tr("&Scan for Devices"), this),
57 device_list_(this),
58 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
59 Qt::Horizontal, this)
60{
61 setWindowTitle(tr("Connect to Device"));
62
63 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
64 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
65
66 populate_drivers();
67 connect(&drivers_, SIGNAL(activated(int)),
68 this, SLOT(device_selected(int)));
69
70 form_.setLayout(&form_layout_);
71 form_layout_.addRow(tr("&Driver"), &drivers_);
72
73 form_layout_.addRow(tr("Serial &Port"), &serial_devices_);
74 serial_devices_.setEditable(true);
75
76 unset_connection();
77
78 connect(&scan_button_, SIGNAL(pressed()),
79 this, SLOT(scan_pressed()));
80
81 setLayout(&layout_);
82 layout_.addWidget(&form_);
83 layout_.addWidget(&scan_button_);
84 layout_.addWidget(&device_list_);
85 layout_.addWidget(&button_box_);
86}
87
88shared_ptr<HardwareDevice> Connect::get_selected_device() const
89{
90 const QListWidgetItem *const item = device_list_.currentItem();
91 if (!item)
92 return shared_ptr<HardwareDevice>();
93
94 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
95}
96
97void Connect::populate_drivers()
98{
99 for (auto entry : device_manager_.context()->drivers()) {
100 auto name = entry.first;
101 auto driver = entry.second;
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 */
109 bool supported_device = driver->config_check(
110 ConfigKey::LOGIC_ANALYZER, ConfigKey::DEVICE_OPTIONS) |
111 driver->config_check(
112 ConfigKey::OSCILLOSCOPE, ConfigKey::DEVICE_OPTIONS);
113
114 if (supported_device)
115 drivers_.addItem(QString("%1 (%2)").arg(
116 driver->long_name().c_str()).arg(name.c_str()),
117 qVariantFromValue(driver));
118 }
119}
120
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
130void Connect::unset_connection()
131{
132 device_list_.clear();
133 serial_devices_.hide();
134 form_layout_.labelForField(&serial_devices_)->hide();
135 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
136}
137
138void Connect::set_serial_connection(shared_ptr<Driver> driver)
139{
140 populate_serials(driver);
141 serial_devices_.show();
142 form_layout_.labelForField(&serial_devices_)->show();
143}
144
145void Connect::scan_pressed()
146{
147 device_list_.clear();
148
149 const int index = drivers_.currentIndex();
150 if (index == -1)
151 return;
152
153 shared_ptr<Driver> driver =
154 drivers_.itemData(index).value<shared_ptr<Driver>>();
155
156 assert(driver);
157
158 map<const ConfigKey *, VariantBase> drvopts;
159
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();
168 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
169 serial.toUtf8().constData());
170 }
171
172 const list< shared_ptr<HardwareDevice> > devices =
173 device_manager_.driver_scan(driver, drvopts);
174
175 for (shared_ptr<HardwareDevice> device : devices)
176 {
177 assert(device);
178
179 QString text = QString::fromStdString(
180 device->display_name(device_manager_));
181 text += QString(" with %1 channels").arg(
182 device->device()->channels().size());
183
184 QListWidgetItem *const item = new QListWidgetItem(text,
185 &device_list_);
186 item->setData(Qt::UserRole, qVariantFromValue(device));
187 device_list_.addItem(item);
188 }
189
190 device_list_.setCurrentRow(0);
191 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
192}
193
194void Connect::device_selected(int index)
195{
196 shared_ptr<Driver> driver =
197 drivers_.itemData(index).value<shared_ptr<Driver>>();
198
199 unset_connection();
200
201 if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
202 set_serial_connection(driver);
203}
204
205} // namespace dialogs
206} // namespace pv