-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserialportcombobox.cpp
More file actions
226 lines (205 loc) · 8.18 KB
/
serialportcombobox.cpp
File metadata and controls
226 lines (205 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/****************************************************************************
**
** Copyright (C) 2018 Miklos Marton <martonmiklosqdev@gmail.com>
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
****************************************************************************/
#include "serialportcombobox.h"
#include <QSerialPort>
#include <QSerialPortInfo>
SerialPortComboBox::SerialPortComboBox(QWidget *parent) :
QComboBox(parent)
{
refreshPorts();
}
SerialPortComboBox::SerialPortComboBox(SerialPortComboBox::DisplayMode displayMode, QWidget *parent) :
QComboBox(parent),
m_displayMode(displayMode)
{
refreshPorts();
}
/**
* @brief SerialPortComboBox::refreshPorts
* Refresh the combobox's items to match the available ports in the system.
*/
void SerialPortComboBox::refreshPorts()
{
QList<QSerialPortInfo> availablePorts = QSerialPortInfo::availablePorts();
/* Loop through the current combobox items and remove the non present ports */
for (int i = 0; i<count(); i++) {
bool leaveExistingPort = false;
foreach (const QSerialPortInfo &info, availablePorts) {
if (info.portName() == itemData(i).toString()) {
leaveExistingPort = true;
if (
(!m_vidFilterInverted && m_vidFilterSet && info.vendorIdentifier() != m_vidFilter) ||
(!m_pidFilterInverted && m_pidFilterSet && info.productIdentifier() != m_pidFilter) ||
(m_vidFilterInverted && m_vidFilterSet && info.vendorIdentifier() == m_vidFilter) ||
(m_pidFilterInverted && m_pidFilterSet && info.productIdentifier() == m_pidFilter)
) {
// an existing port filtered out by the PID/VID filter -> remove it
leaveExistingPort = false;
}
break;
}
}
if (!leaveExistingPort) {
removeItem(i);
i--;
}
}
/* Loop through the available ports and check that they are present in the combobox */
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
/* If a port is not present add it to the combobox */
if (findText(info.portName()) == -1) {
if ((m_vidFilterSet &&
(((!m_vidFilterInverted && (info.vendorIdentifier() != m_vidFilter)) || (m_vidFilterInverted && (info.vendorIdentifier() == m_vidFilter))))) ||
(m_pidFilterSet &&
((!m_pidFilterInverted && (info.productIdentifier() != m_pidFilter)) || (m_pidFilterInverted && (info.productIdentifier() == m_pidFilter))))
) {
// skip this port if it filtered out
continue;
}
if (m_serialFilterSet) {
auto matches = m_serialFilterRe.match(info.serialNumber());
if (m_serialFilterInverted) {
if (matches.hasMatch())
continue;
} else {
if (!matches.hasMatch())
continue;
}
}
QString displayedText, portName;
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
displayedText = info.systemLocation();
#else
displayedText = info.portName();
#endif
portName = displayedText;
switch (m_displayMode) {
case PortNameOnly:
break;
case PortNameAndSerial:
if (info.serialNumber().isEmpty())
displayedText.append(tr(" (SN Unsupported)"));
else
displayedText.append(QStringLiteral(" (%1)").arg(info.serialNumber()));
break;
case PortNameAndVIDPID:
if (info.vendorIdentifier() == 0 && info.productIdentifier() == 0)
displayedText.append(tr(" (no VID,PID)"));
else
displayedText.append(QString(" (%1:%2)")
.arg(info.vendorIdentifier(), 4, 16, QChar('0'))
.arg(info.productIdentifier(), 4, 16, QChar('0')));
break;
case PortNameAndDescription:
if (info.description().isEmpty())
displayedText.append(tr(" (no description)"));
else
displayedText.append(QStringLiteral(" (%1)").arg(info.description()));
break;
}
addItem(displayedText, info.systemLocation());
setItemData(count() - 1, portName, PortName);
setItemData(count() - 1, info.description(), Description);
setItemData(count() - 1, info.manufacturer(), Manufacturer);
setItemData(count() - 1, info.serialNumber(), Serialnumber);
setItemData(count() - 1, info.systemLocation(), Location);
if (info.hasVendorIdentifier()) {
setItemData(count() - 1,
QString("0x%1").arg((uint)info.vendorIdentifier(), 4, 16, QChar('0')).toUpper(),
VendorIdentifier);
} else {
setItemData(count() - 1, QString(), VendorIdentifier);
}
if (info.hasProductIdentifier()) {
setItemData(count() - 1,
QString("0x%1").arg((uint)info.productIdentifier(), 4, 16, QChar('0')).toUpper(),
ProductIdentifier);
} else {
setItemData(count() - 1, QString(), ProductIdentifier);
}
}
}
}
SerialPortComboBox::DisplayMode SerialPortComboBox::displayMode() const
{
return m_displayMode;
}
void SerialPortComboBox::setDisplayMode(const DisplayMode &displayMode)
{
m_displayMode = displayMode;
}
bool SerialPortComboBox::selectPort(const QString & portName)
{
int itemIndex = findText(portName);
if (itemIndex == -1)
return false;
setCurrentIndex(itemIndex);
return true;
}
void SerialPortComboBox::showPopup()
{
refreshPorts();
QComboBox::showPopup();
}
void SerialPortComboBox::setProductIdentifierFilter(quint16 pidFilter, bool invertedFilter)
{
m_pidFilter = pidFilter;
m_pidFilterSet = true;
m_pidFilterInverted = invertedFilter;
refreshPorts();
}
void SerialPortComboBox::setVendorIdentifierFilter(quint16 vidFilter, bool invertedFilter)
{
m_vidFilter = vidFilter;
m_vidFilterSet = true;
m_vidFilterInverted = invertedFilter;
refreshPorts();
}
void SerialPortComboBox::setSerialFilter(const QString &serialFilter, bool invertedFilter)
{
m_serialFilterSet = !serialFilter.isEmpty();
if (m_serialFilterSet) {
m_serialFilterInverted = invertedFilter;
auto pattern = serialFilter;
pattern.replace("*", ".*");
pattern.replace("?", ".{1}");
m_serialFilterRe.setPattern(pattern);
}
}
void SerialPortComboBox::clearProductIdentifierFilter()
{
m_pidFilterSet = false;
refreshPorts();
}
void SerialPortComboBox::clearVendorIdentifierFilter()
{
m_vidFilterSet = false;
refreshPorts();
}
QString SerialPortComboBox::currentPortName() const
{
return itemData(currentIndex(), SerialPortComboBox::PortName).toString();
}
void SerialPortComboBox::setCurrentPortName(const QString &portName)
{
setCurrentIndex(findData(portName, SerialPortComboBox::PortName));
}