-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
279 lines (230 loc) · 9.54 KB
/
mainwindow.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "mainwindow.h"
#include "./ui_mainwindow.h" // todo check why?
#include <QMessageBox>
#include <QSerialPortInfo>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
workerThread(nullptr)
{
ui->setupUi(this);
this->setFocusPolicy(Qt::StrongFocus);
// SetIcon
QIcon icon(":/img/icon.ico");
this->setWindowIcon(icon);
connect(ui->connectButton, &QPushButton::clicked, this, &MainWindow::on_connectButton_clicked);
connect(ui->stopButton, &QPushButton::clicked, this, &MainWindow::on_stopButton_clicked);
connect(ui->actionExit, &QAction::triggered, this, &MainWindow::close);
connect(ui->actionSave_Config_As, &QAction::triggered, this, &MainWindow::on_saveConfigAsButton_clicked);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::on_saveButton_clicked);
connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::on_aboutButton_clicked);
connect(ui->actionCheck_Updates, &QAction::triggered, this, &MainWindow::on_checkUpdatesButton_clicked);
connect(ui->actionAdvanced_View, &QAction::triggered, this, &MainWindow::on_advancedViewButton_clicked);
connect(ui->actionHelp, &QAction::triggered, this, [this]() { QMessageBox::information(this, "Help",
"More information at: github.com/mcagriaksoy"); });
connect(ui->actionClear_cache, &QAction::triggered, this, [this]() { ui->textBrowser->clear(); });
// PT Movements
connect(ui->upButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010008101029"); });
connect(ui->downButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010010101031"); });
connect(ui->leftButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010004121027"); });
connect(ui->rightButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010002132036"); });
connect(ui->noneButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010000000001"); });
connect(ui->stopAllButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010000000001"); });
connect(ui->zoomInButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010020000021"); });
connect(ui->zoomOutButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010040000041"); });
connect(ui->focusInButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010080000081"); });
connect(ui->focusOutButton, &QPushButton::clicked, this, [this]() { on_movement_handler("FF010100000103"); });
// Checkboxes
connect(ui->keyboardCheckBox, &QCheckBox::stateChanged, this, &MainWindow::on_keyboard_handler);
connect(ui->presetEnableCheckBox, &QCheckBox::stateChanged, this, &MainWindow::on_set_limit_handler);
// Detect serial ports in beginning
detectSerialPorts();
}
MainWindow::~MainWindow()
{
// If worker thread is running, stop it
if (workerThread) {
workerThread->quit();
workerThread->wait();
delete workerThread;
}
delete ui;
}
void MainWindow::on_movement_handler(QString data)
{
// Send data to COM port
if (workerThread) {
QByteArray dataBytes = QByteArray::fromHex(data.toLatin1());
worker->writeDataToCom(dataBytes);
ui->textBrowser->append("Sent: " + data);
}
}
void MainWindow::detectSerialPorts()
{
QStringList portList;
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
portList << info.portName() + " - " + info.description();
}
if (portList.isEmpty()) {
QMessageBox::information(this, "Serial Ports", "No serial ports available.");
}
// Put the list of ports in a combobox
ui->PortComboBox->addItems(portList);
}
void MainWindow::on_connectButton_clicked()
{
// Disable Stop button
ui->stopButton->setEnabled(true);
// Get the data from comboboxes
// Extract port from all text detect COM text and extract port number
QString port = ui->PortComboBox->currentText().split(" - ")[0];
QString baudrate = ui->BaudrateComboBox->currentText();
// Start worker thread to listen to the COM port
// If there is already a worker thread, delete it
if (workerThread) {
workerThread->quit();
workerThread->wait();
delete workerThread;
}
// Create a new worker thread
workerThread = new QThread(this);
worker = new SerialPortWorker(port, baudrate.toInt());
worker->moveToThread(workerThread);
// Connect signals and slots
connect(workerThread, &QThread::started, worker, &SerialPortWorker::start);
connect(workerThread, &QThread::finished, worker, &SerialPortWorker::deleteLater);
connect(worker, &SerialPortWorker::errorOccurred, this, [this](const QString &error) {
QMessageBox::critical(this, "Serial Port Error", error);
});
workerThread->start();
ui->textBrowser->append("Connected to " + port + " with baudrate " + baudrate);
// Enable the PT buttons
ui->upButton->setEnabled(true);
ui->downButton->setEnabled(true);
ui->leftButton->setEnabled(true);
ui->rightButton->setEnabled(true);
ui->noneButton->setEnabled(true);
// Set text on label with green color
ui->statusLabel->setStyleSheet("QLabel { color : green; }");
ui->statusLabel->setText("Connected!");
// Disable Comboboxes
ui->PortComboBox->setEnabled(false);
ui->BaudrateComboBox->setEnabled(false);
}
void MainWindow::on_stopButton_clicked()
{
// Enable Start button
ui->connectButton->setEnabled(true);
// Stop the COM port listener and worker thread
if (workerThread) {
workerThread->quit();
workerThread->wait();
delete workerThread;
workerThread = nullptr;
}
// Set text on label with red color
ui->statusLabel->setStyleSheet("QLabel { color : red; }");
ui->statusLabel->setText("Not Connected Yet");
// Disable the PT buttons
ui->upButton->setEnabled(false);
ui->downButton->setEnabled(false);
ui->leftButton->setEnabled(false);
ui->rightButton->setEnabled(false);
ui->noneButton->setEnabled(false);
// Enable Comboboxes
ui->PortComboBox->setEnabled(true);
ui->BaudrateComboBox->setEnabled(true);
}
void MainWindow::on_saveConfigAsButton_clicked()
{
// Save the configuration to a file
// Todo: Implement this feature
}
void MainWindow::on_saveButton_clicked()
{
// Save the configuration to the same file
// Todo: Implement this feature
}
void MainWindow::on_aboutButton_clicked()
{
QMessageBox aboutBox;
// Create a popup with information about the application
QString aboutText = "<p>PTZ Control is a simple application to control PTZ cameras.</p>"
"<p>GNU LESSER GENERAL PUBLIC LICENSE - GNU LGPL v3.0 </p>"
"<p>Version: " + QString(APP_VERSION) + " </p>"
"<p>Written by Mehmet Cagri Aksoy - 2024 "
"<a href=\"https://github.com/mcagriaksoy/PTZControl\">GitHub Repository</a></p>";
aboutBox.setTextFormat(Qt::RichText);
aboutBox.about(this, "About PTZ Control", aboutText);
}
void MainWindow::on_checkUpdatesButton_clicked()
{
// Check for updates
// Get hhtp request to the github page and check the latest version
// Todo: Implement this feature
QMessageBox::information(this, "Check Updates", "No updates available. ");
}
void MainWindow::on_advancedViewButton_clicked()
{
// Show advanced view
// Todo: Implement this feature
QMessageBox::information(this, "Advanced View", "This feature is not available yet.");
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch(event->key()) {
case Qt::Key_Up:
on_movement_handler("FF010008101029");
break;
case Qt::Key_Down:
on_movement_handler("FF010010101031");
break;
case Qt::Key_Left:
on_movement_handler("FF010004121027");
break;
case Qt::Key_Right:
on_movement_handler("FF010002132036");
break;
default:
on_movement_handler("FF010000000001");
break;
}
}
void MainWindow::on_keyboard_handler(void)
{
// Enable or disable keyboard control
if (ui->keyboardCheckBox->isChecked()) {
// Enable keyboard control
ui->textBrowser->append("Keyboard control enabled.");
// Connect keyboard events
} else {
// Disable keyboard control
ui->textBrowser->append("Keyboard control disabled.");
}
}
void MainWindow::on_set_limit_handler(void)
{
// Enable or disable preset limit control
if (ui->presetEnableCheckBox->isChecked()) {
// Enable preset limit control
ui->textBrowser->append("Preset limit control enabled.");
// Enable LineEdits
ui->leftLineEdit->setEnabled(true);
ui->rightLineEdit->setEnabled(true);
// Read the values from LineEdits
int leftLimit = ui->leftLineEdit->text().toInt();
int rightLimit = ui->rightLineEdit->text().toInt();
// Check if the values are valid
if (leftLimit < 0 || leftLimit > 360 || rightLimit < 0 || rightLimit > 360) {
QMessageBox::warning(this, "Warning", "Please enter a valid degree value between 0 and 360.");
return;
}
// Todo add preset 202,203,204 functionality.
} else {
// Disable preset limit control
ui->textBrowser->append("Preset limit control disabled.");
// Disable LineEdits
ui->leftLineEdit->setEnabled(false);
ui->rightLineEdit->setEnabled(false);
}
}