LCOV - code coverage report
Current view: top level - src - mainwindow.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 82.8 % 122 101
Test Date: 2025-09-04 13:51:42 Functions: 90.0 % 20 18

            Line data    Source code
       1              : #include "../include/mainwindow.h"
       2              : 
       3              : //adding layouts and widgets to main window
       4            8 : MainWindow::MainWindow(QWidget *parent)
       5            8 :     : QMainWindow(parent), client(new QMqttClient(this))
       6              : {
       7            8 :     left_dial = new Speed(this);
       8            8 :     right_dial = new Battery(this);
       9            8 :     object = new Object(this);
      10            8 :     temp = new Temperature(this);
      11            8 :     autonomy = new Autonomy(this);
      12            8 :     center_dial = new Lane(this);
      13            8 :     QVBoxLayout* mainlayout = new QVBoxLayout();
      14            8 :     QHBoxLayout* layout = new QHBoxLayout(); 
      15            8 :     QVBoxLayout* lane_layout = new QVBoxLayout();
      16            8 :     QHBoxLayout* centerbar = new QHBoxLayout();
      17            8 :     QWidget* centralWidget = new QWidget(this);
      18              :     
      19            8 :     layout->addWidget(left_dial, 1,  Qt::AlignTop | Qt::AlignLeft); 
      20            8 :     lane_layout->addWidget(center_dial, 0, Qt::AlignCenter);
      21            8 :     lane_layout->setStretch(0, 1);
      22            8 :     layout->addLayout(lane_layout, 1);
      23            8 :     layout->addWidget(right_dial, 1, Qt::AlignTop | Qt::AlignRight);
      24              :     
      25              :     // Create layouts
      26            8 :     object->setFixedSize(60, 60);
      27              : 
      28              :     // Create a main container for the entire bar
      29            8 :     QWidget *barContainer = new QWidget();
      30            8 :     QHBoxLayout *barLayout = new QHBoxLayout(barContainer);
      31            8 :     barLayout->setContentsMargins(20, 0, 80, 0); // Left/right margins
      32              : 
      33              :     // Left section with object
      34            8 :     barLayout->addWidget(object, 0, Qt::AlignLeft);
      35              : 
      36              :     // Center section with temp and autonomy
      37            8 :     QWidget *centerWidget = new QWidget();
      38            8 :     QHBoxLayout *centerLayout = new QHBoxLayout(centerWidget);
      39            8 :     centerLayout->setContentsMargins(0, 0, 0, 0);
      40            8 :     centerLayout->addStretch(1);
      41            8 :     temp->setFixedHeight(80);       // or whatever looks right
      42            8 :     autonomy->setFixedHeight(80);   // match temp
      43              :     // temp->setStyleSheet("border: 1px solid red;");
      44              :     // autonomy->setStyleSheet("border: 1px solid blue;");
      45              : 
      46            8 :     centerLayout->addWidget(temp, 0, Qt::AlignTop);
      47            8 :     centerLayout->addSpacing(30);
      48            8 :     centerLayout->addWidget(autonomy, 0, Qt::AlignTop);
      49            8 :     centerLayout->addStretch(1);
      50              : 
      51            8 :     barLayout->addWidget(centerWidget, 1); // Take remaining space
      52              : 
      53            8 :     mainlayout->addLayout(layout, 2);
      54            8 :     mainlayout->addWidget(barContainer, 1); // Add the container widget
      55              : 
      56            8 :     centralWidget->setLayout(mainlayout);
      57            8 :     setCentralWidget(centralWidget);
      58            8 :     setStyleSheet("background-color: rgb(0, 0, 20);");
      59            8 :     init_mqtt();
      60            8 : }
      61              : 
      62              : //close main window at destruction
      63           16 : MainWindow::~MainWindow()
      64              : {
      65            8 :     std::cout << "Remove Window" << std::endl;
      66            8 :     close();
      67           16 : }
      68              : 
      69              : //connecting to mqtt via cloud or localhost or to jetracer via network
      70            8 : void    MainWindow::init_mqtt()
      71              : {
      72              :     // client->setHostname("972e24210b544ba49bfb9c1d3164d02b.s1.eu.hivemq.cloud"); //cloud
      73              :     // client->setPort(8883);
      74            8 :     QString user = qgetenv("user");
      75            8 :     client->setUsername(user); 
      76            8 :     QString pass = qgetenv("password");
      77            8 :     client->setPassword(pass); 
      78            8 :     client->setHostname("10.21.221.67"); //when on the same network
      79            8 :     client->setPort(1883); //cross compiling
      80              :     // client->setHostname("127.0.0.1"); //when cross-compiling with jetracer
      81              : 
      82            8 :     connect(client, &QMqttClient::connected, this, &MainWindow::connected);
      83            8 :     connect(client, &QMqttClient::messageReceived, this, &MainWindow::message_received);
      84            8 :     connect(client, &QMqttClient::errorChanged, this, [](QMqttClient::ClientError error) {
      85            0 :         qDebug() << "MQTT Client error:" << error;
      86            0 :     });
      87            8 :     client->connectToHostEncrypted(); //for cloud needs to be encrypted, for jetracer network or localhost its not encrypted
      88            8 : }
      89              : 
      90              : //subscribing to topic of mqtt
      91            0 : void    MainWindow::connected()
      92              : {
      93            0 :     QMqttTopicFilter topic("jetracer/speed");
      94            0 :     auto speed_sub = client->subscribe(topic);
      95            0 :     QMqttTopicFilter battery("jetracer/battery");
      96            0 :     auto bat_sub = client->subscribe(battery);
      97            0 :     QMqttTopicFilter temp("jetracer/temperature");
      98            0 :     auto temp_sub = client->subscribe(temp);
      99            0 :     QMqttTopicFilter autono("jetracer/autonomy");
     100            0 :     auto autono_sub = client->subscribe(autono);
     101            0 :     QMqttTopicFilter lane("jetracer/lane_touch");
     102            0 :     auto lane_sub = client->subscribe(lane);
     103            0 :     QMqttTopicFilter speed50("jetracer/speed_50");
     104            0 :     auto speed50_sub = client->subscribe(speed50);
     105            0 :     QMqttTopicFilter speed80("jetracer/speed_80");
     106            0 :     auto speed80_sub = client->subscribe(speed80);
     107            0 :     if (!speed_sub || !bat_sub | !autono_sub || !temp_sub || !lane_sub || !speed50_sub || !speed80_sub) {
     108            0 :         qDebug() << "Failed to subscribe to topic";
     109              :     } 
     110            0 : }
     111              : 
     112              : //receiving message and updating current
     113            7 : void    MainWindow::message_received(const QByteArray &message, const QMqttTopicName &topic)
     114              : {
     115            7 :     qDebug() << topic.name() << ":" << message;
     116              :     bool ok;
     117            7 :     double msg = message.toDouble(&ok); 
     118            7 :     if (ok) {
     119            7 :         if (topic.name() == "jetracer/speed") {
     120            1 :             QMetaObject::invokeMethod(this, [this, msg]() {
     121            1 :                 left_dial->set_current(static_cast<float>(msg));
     122            1 :             }, Qt::AutoConnection);
     123              :         }
     124            6 :         else if (topic.name() == "jetracer/battery") {
     125            1 :             QMetaObject::invokeMethod(this, [this, msg]() {
     126            1 :                 right_dial->set_current(msg);
     127            1 :             }, Qt::AutoConnection);
     128              :         }
     129            5 :         else if (topic.name() == "jetracer/temperature") {
     130            1 :             QMetaObject::invokeMethod(this, [this, msg]() {
     131            1 :                 temp->set_temperature(msg);
     132            1 :             }, Qt::AutoConnection);
     133              :         }
     134            4 :         else if (topic.name() == "jetracer/autonomy") {
     135            1 :             QMetaObject::invokeMethod(this, [this, msg]() {
     136            1 :                 autonomy->set_autonomy(msg);
     137            1 :             }, Qt::AutoConnection);
     138              :         }
     139            3 :         else if (topic.name() == "jetracer/lane_touch") {
     140            1 :             QMetaObject::invokeMethod(this, [this, msg]() {
     141            1 :                 center_dial->set_lane(msg);
     142            1 :             }, Qt::AutoConnection);
     143              :         }
     144            2 :         else if (topic.name() == "jetracer/speed_50" || topic.name() == "jetracer/speed_80") {
     145            2 :             QMetaObject::invokeMethod(this, [this, msg, topic]() {
     146            2 :                 object->set_object(msg, topic.name());
     147            2 :             }, Qt::AutoConnection);
     148              :         }
     149              :     } else {
     150            0 :         qDebug() << "Invalid data received";
     151              :     }
     152            7 : }
     153              : 
     154            8 : QMqttClient*    MainWindow::get_client()
     155              : {
     156            8 :     return client;
     157              : }
     158              : 
     159            2 : Battery*    MainWindow::get_battery()
     160              : {
     161            2 :     return right_dial;
     162              : }
     163              : 
     164            1 : Speed*    MainWindow::get_speed()
     165              : {
     166            1 :     return left_dial;
     167              : }
     168              : 
     169            2 : Autonomy*   MainWindow::get_autonomy()
     170              : {
     171            2 :     std::cout << "Getting autonomy\n";
     172            2 :     return autonomy;
     173              : }
     174              : 
     175            2 : Temperature*   MainWindow::get_temperature()
     176              : {
     177            2 :     return temp;
     178              : }
     179              : 
     180            4 : Lane*   MainWindow::get_lane()
     181              : {
     182            4 :     return center_dial;
     183              : }
     184              : 
     185            3 : Object*   MainWindow::get_object()
     186              : {
     187            3 :     return object;
     188              : }
        

Generated by: LCOV version 2.0-1