]> sigrok.org Git - pulseview.git/blob - pv/dialogs/connect.cpp
Make member variable underscores a suffix instead of a prefix
[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 <libsigrok/libsigrok.hpp>
24
25 #include "connect.h"
26
27 #include "pv/devicemanager.h"
28
29 using std::list;
30 using std::map;
31 using std::shared_ptr;
32 using std::string;
33
34 using Glib::ustring;
35 using Glib::Variant;
36 using Glib::VariantBase;
37
38 using sigrok::ConfigKey;
39 using sigrok::Driver;
40 using sigrok::Error;
41 using sigrok::HardwareDevice;
42
43 namespace pv {
44 namespace dialogs {
45
46 Connect::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_device_(&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_device_);
72
73         unset_connection();
74
75         connect(&scan_button_, SIGNAL(pressed()),
76                 this, SLOT(scan_pressed()));
77
78         setLayout(&layout_);
79         layout_.addWidget(&form_);
80         layout_.addWidget(&scan_button_);
81         layout_.addWidget(&device_list_);
82         layout_.addWidget(&button_box_);
83 }
84
85 shared_ptr<HardwareDevice> Connect::get_selected_device() const
86 {
87         const QListWidgetItem *const item = device_list_.currentItem();
88         if (!item)
89                 return shared_ptr<HardwareDevice>();
90
91         return item->data(Qt::UserRole).value<shared_ptr<HardwareDevice>>();
92 }
93
94 void Connect::populate_drivers()
95 {
96         for (auto entry : device_manager_.context()->drivers()) {
97                 auto name = entry.first;
98                 auto driver = entry.second;
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                  */
106                 bool supported_device = driver->config_check(
107                         ConfigKey::SAMPLERATE, ConfigKey::DEVICE_OPTIONS);
108
109                 if (supported_device)
110                         drivers_.addItem(QString("%1 (%2)").arg(
111                                 driver->long_name().c_str()).arg(name.c_str()),
112                                 qVariantFromValue(driver));
113         }
114 }
115
116 void Connect::unset_connection()
117 {
118         device_list_.clear();
119         serial_device_.hide();
120         form_layout_.labelForField(&serial_device_)->hide();
121         button_box_.button(QDialogButtonBox::Ok)->setDisabled(true);
122 }
123
124 void Connect::set_serial_connection()
125 {
126         serial_device_.show();
127         form_layout_.labelForField(&serial_device_)->show();
128 }
129
130 void Connect::scan_pressed()
131 {
132         device_list_.clear();
133
134         const int index = drivers_.currentIndex();
135         if (index == -1)
136                 return;
137
138         shared_ptr<Driver> driver =
139                 drivers_.itemData(index).value<shared_ptr<Driver>>();
140
141         assert(driver);
142
143         map<const ConfigKey *, VariantBase> drvopts;
144
145         if (serial_device_.isVisible())
146                 drvopts[ConfigKey::CONN] = Variant<ustring>::create(
147                         serial_device_.text().toUtf8().constData());
148
149         list< shared_ptr<HardwareDevice> > devices =
150                 device_manager_.driver_scan(driver, drvopts);
151
152         for (shared_ptr<HardwareDevice> device : devices)
153         {
154                 assert(device);
155
156                 QString text = QString::fromStdString(
157                         device_manager_.get_display_name(device));
158                 text += QString(" with %1 channels").arg(device->channels().size());
159
160                 QListWidgetItem *const item = new QListWidgetItem(text,
161                         &device_list_);
162                 item->setData(Qt::UserRole, qVariantFromValue(device));
163                 device_list_.addItem(item);
164         }
165
166         device_list_.setCurrentRow(0);
167         button_box_.button(QDialogButtonBox::Ok)->setDisabled(device_list_.count() == 0);
168 }
169
170 void Connect::device_selected(int index)
171 {
172         shared_ptr<Driver> driver =
173                 drivers_.itemData(index).value<shared_ptr<Driver>>();
174
175         unset_connection();
176
177         if (driver->config_check(ConfigKey::SERIALCOMM, ConfigKey::SCAN_OPTIONS))
178                         set_serial_connection();
179 }
180
181 } // namespace dialogs
182 } // namespace pv