-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialchooser.cpp
More file actions
166 lines (134 loc) · 5.68 KB
/
Copy pathserialchooser.cpp
File metadata and controls
166 lines (134 loc) · 5.68 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
/*MIT License
Copyright (c) 2021 Starstone Software Systems, Inc.
Copyright (c) 2021 Richard S. Wright Jr.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
#include <QTreeWidget>
#include <QStringList>
#include <QMessageBox>
#include "dlgabout.h"
#include "serialchooser.h"
#include "ui_serialchooser.h"
SerialChooser::SerialChooser(QWidget *parent) :
QDialog(parent),
ui(new Ui::SerialChooser)
{
ui->setupUi(this);
setWindowFlags(Qt::Widget);
ui->pushButtonFind->hide();
connect(ui->pushButtonAbout, SIGNAL(pressed()), this, SLOT(pressedAbout()));
connect(ui->treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelected()));
connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(itemDoubleClicked(QTreeWidgetItem*, int)));
connect(ui->pushButtonRefresh, SIGNAL(pressed()), this, SLOT(refreshPortList()));
connect(ui->pushButtonUseSelected, SIGNAL(pressed()), this, SLOT(attemptOneConnection()));
refreshPortList();
this->show();
}
SerialChooser::~SerialChooser()
{
delete ui;
}
void SerialChooser::pressedAbout()
{
DlgAbout aboutBox(this);
aboutBox.exec();
}
//////////////////////////////////////////////////////////////////////////////////////
void SerialChooser::itemSelected()
{
ui->pushButtonUseSelected->setEnabled(true);
}
/////////////////////////////////////////////////////////////////////////////////////
void SerialChooser::refreshPortList(void)
{
listOfPorts.clear();
listOfPorts = QSerialPortInfo::availablePorts();
ui->treeWidget->clear();
ui->treeWidget->setColumnCount(3);
QStringList headers = { "Port Name", "Description", "Manufacturer" };
ui->treeWidget->setHeaderLabels(headers);
for(int i = 0; i < listOfPorts.size(); i++) {
// On macOS, filter out tty.Bluetooth and other tty. listings.
// CU is for callinging out, tty is for calling in. We are going
// out...
if(listOfPorts[i].portName().contains("tty.")) {
listOfPorts.removeAt(i);
i--;
continue;
}
// Build the string to identify the serial port
QStringList line;
line << listOfPorts[i].portName();
line << listOfPorts[i].description();
line << listOfPorts[i].manufacturer();
// Add to the tree widget
QTreeWidgetItem *pItem = new QTreeWidgetItem(static_cast<QTreeWidget *>(nullptr), line);
ui->treeWidget->addTopLevelItem(pItem);
// If this port is busy, it is in use and should be disqualified. Display it anyway
// so the end user can see it is at least detected, and it could be a device that is
// holding onto the port that they would need to know about.
if(listOfPorts[i].isBusy())
pItem->setDisabled(true);
}
ui->treeWidget->resizeColumnToContents(0);
ui->treeWidget->resizeColumnToContents(1);
ui->treeWidget->resizeColumnToContents(2);
ui->pushButtonUseSelected->setEnabled(false);
}
///////////////////////////////////////////////////////////////////////
// Go try a connection with the selected device
void SerialChooser::attemptOneConnection(void)
{
QTreeWidgetItem *pSelectedItem = ui->treeWidget->currentItem();
Q_ASSERT(pSelectedItem != nullptr);
int iItem = ui->treeWidget->indexOfTopLevelItem(pSelectedItem);
Q_ASSERT(iItem >= 0);
QApplication::setOverrideCursor(Qt::WaitCursor);
QuantumDevice *pConnect = new QuantumDevice(nullptr, listOfPorts[iItem]);
connect(pConnect, SIGNAL(connectedToQuantum(QuantumDevice*)), this, SLOT(gotConnected(QuantumDevice*)), Qt::QueuedConnection);
connect(pConnect, SIGNAL(couldNotOpen(QuantumDevice*)), this, SLOT(failedConnection(QuantumDevice*)), Qt::QueuedConnection);
pConnect->start();
}
void SerialChooser::itemDoubleClicked(QTreeWidgetItem *item, int column)
{
(void)item;
(void)column;
attemptOneConnection();
}
///////////////////////////////////////////////////////////////////////
// We got a connection! Pass the info on to the parent window,
// and close the serial chooser.
void SerialChooser::gotConnected(QuantumDevice* pDevice)
{
// This slot catches the signal from the device and is
// forwarding it up to the main window
emit connectedToQuantum(pDevice);
}
///////////////////////////////////////////////////////////////////////
// Connection failed
void SerialChooser::failedConnection(QuantumDevice* pDevice)
{
QApplication::restoreOverrideCursor();
// Tell the threads event loop to quit
pDevice->quit();
QMessageBox msgBox;
msgBox.setText(tr("Could not connect to a Quantum on the selected serial port"));
msgBox.exec();
// Delete it, but make sure event loop has stopped
pDevice->wait();
delete pDevice;
}