forked from UAVGCSTeam/GCS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
89 lines (73 loc) · 3.17 KB
/
mainwindow.cpp
File metadata and controls
89 lines (73 loc) · 3.17 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QDebug>
#include <QProcess>
#include <QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, pythonProcess(nullptr)
{
ui->setupUi(this);
ui->quickWidget_MapView->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
// Reference to the rootObject of quickWidget_MapView which is our map
auto Obje = ui->quickWidget_MapView->rootObject();
// When setCenterPosition is emitted from the current object, the setCenterPosition method in Obje will be called with the same parameters
connect(this, SIGNAL(setCenterPosition(QVariant,QVariant)), Obje, SLOT(setCenterPosition(QVariant,QVariant)));
connect(this, SIGNAL(setLocationMarking(QVariant,QVariant)), Obje, SLOT(setLocationMarking(QVariant,QVariant)));
// Start the Python Xbee Process
if (startXbeeProcess()) {
xbeeStatusLabel->setText("XBee: Starting...");
xbeeStatusLabel->setStyleSheet("color: orange;");
// Initialize XBee monitoring in DroneController
// This assumes DroneController is already created and available
QTimer::singleShot(1000, [this]() {
// This call should be adapted to how you access your DroneController
// droneController->startXbeeMonitoring();
});
}
}
MainWindow::~MainWindow()
{
// Terminate the Python process
if (pythonProcess && pythonProcess->state() == QProcess::Running) {
pythonProcess->terminate();
if (!pythonProcess->waitForFinished(3000)) {
pythonProcess->kill();
}
}
delete ui;
}
bool MainWindow::startXbeeProcess()
{
pythonProcess = new QProcess(this);
// Connect signals to handle process output and errors
connect(pythonProcess, &QProcess::readyReadStandardOutput, this, [this]() {
QByteArray output = pythonProcess->readAllStandardOutput();
qDebug() << "Python output:" << output;
});
connect(pythonProcess, &QProcess::readyReadStandardError, this, [this]() {
QByteArray error = pythonProcess->readAllStandardError();
qWarning() << "Python error:" << error;
});
connect(pythonProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << "Python process finished with code" << exitCode;
xbeeStatusLabel->setText("XBee: Disconnected");
xbeeStatusLabel->setStyleSheet("color: red;");
});
// Set the working directory to where the script is located
pythonProcess->setWorkingDirectory(QCoreApplication::applicationDirPath());
QStringList args;
args << "xbeeHandler.py";
#ifdef QT_DEBUG
args << "--simulate"; // Use simulation mode in debug builds IMPLEMENT THIS
#endif
pythonProcess->start("python", args);
// Wait for it to start
if (!pythonProcess->waitForStarted(5000)) {
qWarning() << "Failed to start Python process:" << pythonProcess->errorString();
return false;
}
return true;
}