2 * This file is part of the PulseView project.
4 * Copyright (C) 2012-2013 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
23 #include <libsigrokcxx/libsigrokcxx.hpp>
25 #include "connect.hpp"
27 #include <pv/devicemanager.hpp>
28 #include <pv/devices/hardwaredevice.hpp>
32 using std::shared_ptr;
37 using Glib::VariantBase;
39 using sigrok::ConfigKey;
43 using pv::devices::HardwareDevice;
48 Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) :
50 device_manager_(device_manager),
55 serial_devices_(&form_),
56 scan_button_(tr("&Scan for Devices"), this),
58 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
61 setWindowTitle(tr("Connect to Device"));
63 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
64 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
67 connect(&drivers_, SIGNAL(activated(int)),
68 this, SLOT(device_selected(int)));
70 form_.setLayout(&form_layout_);
71 form_layout_.addRow(tr("&Driver"), &drivers_);
73 form_layout_.addRow(tr("Serial &Port"), &serial_devices_);
74 serial_devices_.setEditable(true);
78 connect(&scan_button_, SIGNAL(pressed()),
79 this, SLOT(scan_pressed()));
82 layout_.addWidget(&form_);
83 layout_.addWidget(&scan_button_);
84 layout_.addWidget(&device_list_);
85 layout_.addWidget(&button_box_);
88 shared_ptr<HardwareDevice> Connect::get_selected_device() const
90 const QListWidgetItem *const item = device_list_.currentItem();
92 return shared_ptr<HardwareDevice>();
94 return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
97 void Connect::populate_drivers()
99 for (auto entry : device_manager_.context()->drivers()) {
100 auto name = entry.first;
101 auto driver = entry.second;
103 * We currently only support devices that can deliver
104 * samples at a fixed samplerate i.e. oscilloscopes and
106 * @todo Add support for non-monotonic devices i.e. DMMs
109 const auto keys = driver->config_keys();
111 bool supported_device = keys.count(ConfigKey::LOGIC_ANALYZER) |
112 keys.count(ConfigKey::OSCILLOSCOPE);
114 if (supported_device)
115 drivers_.addItem(QString("%1 (%2)").arg(
116 driver->long_name().c_str(), name.c_str()),
117 qVariantFromValue(driver));
121 void Connect::populate_serials(shared_ptr<Driver> driver)
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(), serial.second.c_str()),
127 QString::fromStdString(serial.first));
130 void Connect::unset_connection()
132 device_list_.clear();
133 serial_devices_.hide();
134 form_layout_.labelForField(&serial_devices_)->hide();
135 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
138 void Connect::set_serial_connection(shared_ptr<Driver> driver)
140 populate_serials(driver);
141 serial_devices_.show();
142 form_layout_.labelForField(&serial_devices_)->show();
145 void Connect::scan_pressed()
147 device_list_.clear();
149 const int index = drivers_.currentIndex();
153 shared_ptr<Driver> driver =
154 drivers_.itemData(index).value<shared_ptr<Driver>>();
158 map<const ConfigKey *, VariantBase> drvopts;
160 if (serial_devices_.isVisible()) {
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>();
167 serial = serial_devices_.currentText();
168 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
169 serial.toUtf8().constData());
172 const list< shared_ptr<HardwareDevice> > devices =
173 device_manager_.driver_scan(driver, drvopts);
175 for (shared_ptr<HardwareDevice> device : devices) {
178 QString text = QString::fromStdString(
179 device->display_name(device_manager_));
180 text += QString(" with %1 channels").arg(
181 device->device()->channels().size());
183 QListWidgetItem *const item = new QListWidgetItem(text,
185 item->setData(Qt::UserRole, qVariantFromValue(device));
186 device_list_.addItem(item);
189 device_list_.setCurrentRow(0);
190 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
193 void Connect::device_selected(int index)
195 shared_ptr<Driver> driver =
196 drivers_.itemData(index).value<shared_ptr<Driver>>();
200 if (driver->scan_options().count(ConfigKey::SERIALCOMM))
201 set_serial_connection(driver);
204 } // namespace dialogs