]> sigrok.org Git - pulseview.git/blame - pv/dialogs/connect.cpp
dialogs::Connect: Adds shortcut keys for "Driver", "Serial Port" and "Scan for device...
[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
2acdb232 25#include "connect.hpp"
9663c82b 26
2acdb232 27#include "pv/devicemanager.hpp"
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),
8dbbc7f0
JH
48 device_manager_(device_manager),
49 layout_(this),
50 form_(this),
51 form_layout_(&form_),
52 drivers_(&form_),
53 serial_device_(&form_),
a0730b3b 54 scan_button_(tr("&Scan for Devices"), this),
8dbbc7f0
JH
55 device_list_(this),
56 button_box_(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
9663c82b
JH
57 Qt::Horizontal, this)
58{
59 setWindowTitle(tr("Connect to Device"));
60
8dbbc7f0
JH
61 connect(&button_box_, SIGNAL(accepted()), this, SLOT(accept()));
62 connect(&button_box_, SIGNAL(rejected()), this, SLOT(reject()));
9663c82b
JH
63
64 populate_drivers();
8dbbc7f0 65 connect(&drivers_, SIGNAL(activated(int)),
9663c82b
JH
66 this, SLOT(device_selected(int)));
67
8dbbc7f0 68 form_.setLayout(&form_layout_);
a0730b3b 69 form_layout_.addRow(tr("&Driver"), &drivers_);
9663c82b 70
a0730b3b 71 form_layout_.addRow(tr("Serial &Port"), &serial_device_);
9663c82b
JH
72
73 unset_connection();
74
8dbbc7f0 75 connect(&scan_button_, SIGNAL(pressed()),
c9de8b32
JH
76 this, SLOT(scan_pressed()));
77
8dbbc7f0
JH
78 setLayout(&layout_);
79 layout_.addWidget(&form_);
80 layout_.addWidget(&scan_button_);
81 layout_.addWidget(&device_list_);
82 layout_.addWidget(&button_box_);
9663c82b
JH
83}
84
e8d00928 85shared_ptr<HardwareDevice> Connect::get_selected_device() const
5eb0fa13 86{
8dbbc7f0 87 const QListWidgetItem *const item = device_list_.currentItem();
5eb0fa13 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{
8dbbc7f0 96 for (auto entry : device_manager_.context()->drivers()) {
e8d00928
ML
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 106 bool supported_device = driver->config_check(
55ab5e32
AJ
107 ConfigKey::LOGIC_ANALYZER, ConfigKey::DEVICE_OPTIONS) |
108 driver->config_check(
109 ConfigKey::OSCILLOSCOPE, ConfigKey::DEVICE_OPTIONS);
30938303 110
793f8096 111 if (supported_device)
8dbbc7f0 112 drivers_.addItem(QString("%1 (%2)").arg(
e8d00928
ML
113 driver->long_name().c_str()).arg(name.c_str()),
114 qVariantFromValue(driver));
30938303 115 }
9663c82b
JH
116}
117
c9de8b32
JH
118void Connect::unset_connection()
119{
8dbbc7f0
JH
120 device_list_.clear();
121 serial_device_.hide();
122 form_layout_.labelForField(&serial_device_)->hide();
123 button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
c9de8b32
JH
124}
125
126void Connect::set_serial_connection()
127{
8dbbc7f0
JH
128 serial_device_.show();
129 form_layout_.labelForField(&serial_device_)->show();
c9de8b32
JH
130}
131
132void Connect::scan_pressed()
133{
8dbbc7f0 134 device_list_.clear();
c9de8b32 135
8dbbc7f0 136 const int index = drivers_.currentIndex();
793f8096 137 if (index == -1)
c9de8b32
JH
138 return;
139
e8d00928 140 shared_ptr<Driver> driver =
8dbbc7f0 141 drivers_.itemData(index).value<shared_ptr<Driver>>();
c9de8b32 142
e8d00928 143 assert(driver);
c9de8b32 144
e8d00928 145 map<const ConfigKey *, VariantBase> drvopts;
c9de8b32 146
8dbbc7f0 147 if (serial_device_.isVisible())
e8d00928 148 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
8dbbc7f0 149 serial_device_.text().toUtf8().constData());
c9de8b32 150
e8d00928 151 list< shared_ptr<HardwareDevice> > devices =
8dbbc7f0 152 device_manager_.driver_scan(driver, drvopts);
c9de8b32 153
e8d00928 154 for (shared_ptr<HardwareDevice> device : devices)
dd63af74 155 {
e8d00928 156 assert(device);
19adbc2c 157
e8d00928 158 QString text = QString::fromStdString(
8dbbc7f0 159 device_manager_.get_display_name(device));
e8d00928 160 text += QString(" with %1 channels").arg(device->channels().size());
c9de8b32 161
5eb0fa13 162 QListWidgetItem *const item = new QListWidgetItem(text,
8dbbc7f0 163 &device_list_);
e8d00928 164 item->setData(Qt::UserRole, qVariantFromValue(device));
8dbbc7f0 165 device_list_.addItem(item);
c9de8b32
JH
166 }
167
8dbbc7f0
JH
168 device_list_.setCurrentRow(0);
169 button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
c9de8b32
JH
170}
171
9663c82b
JH
172void Connect::device_selected(int index)
173{
e8d00928 174 shared_ptr<Driver> driver =
8dbbc7f0 175 drivers_.itemData(index).value<shared_ptr<Driver>>();
9663c82b
JH
176
177 unset_connection();
178
e8d00928
ML
179 if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
180 set_serial_connection();
9663c82b
JH
181}
182
9663c82b
JH
183} // namespace dialogs
184} // namespace pv