]> sigrok.org Git - pulseview.git/blob - pv/dialogs/connect.cpp
Various minor whitespace and consistency fixes.
[pulseview.git] / pv / dialogs / connect.cpp
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
30 using std::list;
31 using std::map;
32 using std::shared_ptr;
33 using std::string;
34
35 using Glib::ustring;
36 using Glib::Variant;
37 using Glib::VariantBase;
38
39 using sigrok::ConfigKey;
40 using sigrok::Driver;
41 using sigrok::Error;
42
43 using pv::devices::HardwareDevice;
44
45 namespace pv {
46 namespace dialogs {
47
48 Connect::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
88 shared_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
97 void 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
121 void 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
130 void 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
138 void 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
145 void 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                 assert(device);
177
178                 QString text = QString::fromStdString(
179                         device->display_name(device_manager_));
180                 text += QString(" with %1 channels").arg(
181                         device->device()->channels().size());
182
183                 QListWidgetItem *const item = new QListWidgetItem(text,
184                         &device_list_);
185                 item->setData(Qt::UserRole, qVariantFromValue(device));
186                 device_list_.addItem(item);
187         }
188
189         device_list_.setCurrentRow(0);
190         button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
191 }
192
193 void Connect::device_selected(int index)
194 {
195         shared_ptr<Driver> driver =
196                 drivers_.itemData(index).value<shared_ptr<Driver>>();
197
198         unset_connection();
199
200         if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
201                 set_serial_connection(driver);
202 }
203
204 } // namespace dialogs
205 } // namespace pv