LCOV - code coverage report
Current view: top level - src - battery.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 91 91
Test Date: 2025-07-25 14:54:22 Functions: 100.0 % 9 9

            Line data    Source code
       1              : #include "../include/battery.h"
       2              : 
       3            7 : Battery::Battery(QWidget *parent)
       4            7 :     : QWidget(parent), current(20), max(100), test_painter(nullptr)
       5              : {
       6            7 :     if (parent)
       7              :     {
       8            7 :         setMinimumSize(parent->width() * 0.5, parent->height() * 0.7); 
       9            7 :         setMaximumSize(parent->width() * 0.5, parent->height() * 0.7);
      10              :     }
      11            7 : }
      12              : 
      13           14 : Battery::~Battery()
      14              : {
      15            7 :     std::cout << "Remove Battery" << std::endl;
      16           14 : }
      17              : 
      18            1 : void Battery::set_current(int n)
      19              : {
      20            1 :     current = n;
      21            1 :     update();
      22            1 : }
      23              : 
      24            1 : int Battery::get_current()
      25              : {
      26            1 :     return current;
      27              : }
      28              : 
      29            4 : void Battery::paintEvent(QPaintEvent *event)
      30              : {
      31            4 :     TestPainter* painter = test_painter;
      32            4 :     QPainter real_painter(this);
      33            4 :     if (!painter)
      34              :     {
      35            3 :         painter = new QPainterCaller(&real_painter);
      36            3 :         flag = 1;
      37              :     }
      38            4 :     if (test_painter)
      39              :     {
      40            1 :         painter->begin(this);
      41              :     }
      42            4 :     painter->setRenderHint(QPainter::Antialiasing, true);
      43            4 :     draw_arcs(painter);
      44            4 :     draw_pixmap(painter);
      45            4 :     if (flag == 1 && painter)
      46              :     {
      47            3 :         delete painter;  // Clean up if we allocated it
      48              :     }
      49            8 : }
      50              : 
      51              : 
      52              : //fading arcs
      53            4 : void Battery::draw_arcs(TestPainter *painter)
      54              : {
      55            4 :     int radius = qMin(width(), height()) / 2.5;  
      56            4 :     int segments = 300; 
      57            4 :     int centerX = width() / 2;
      58            4 :     int centerY = height() / 2;
      59            4 :     float segment_angle = 270.0f / segments; 
      60         1204 :     for (int i = 0; i < segments; ++i)
      61              :     {
      62         1200 :         float t = static_cast<float>(i) / (segments);
      63         1200 :         int alpha = static_cast<int>(120 * (1 - std::abs(2 * t - 1))); 
      64         1200 :         QColor color(0, 52, 50, alpha); 
      65         1200 :         QPen pen(color, width() / 50);
      66         1200 :         painter->setPen(pen);
      67         1200 :         float overlap = 1.1f; 
      68         1200 :         painter->drawArc(centerX - radius, centerY - radius, radius * 2, radius * 2, (180 - i * segment_angle) * 16, -segment_angle * 16 * overlap);
      69         1200 :     }
      70            4 :     float angle_progress = (static_cast<float>(current) * 270.0f) / max;
      71            4 :     segment_angle = angle_progress / segments;
      72            4 :     QColor start_color(0, 65, 74); 
      73            4 :     QColor end_color(0, 255, 200);
      74            4 :     if (current < 40)
      75              :     {
      76            3 :         segments = 200;
      77            3 :         start_color = QColor(0, 65, 74); 
      78            3 :         end_color = QColor(0, 140, 150, 255); 
      79              :     }
      80          904 :     for (int i = 0; i < segments; ++i)
      81              :     {
      82          900 :         float t = static_cast<float>(i) / segments; //factor (0 to 1)
      83          900 :         QColor color = QColor::fromRgbF(
      84          900 :             (1 - t) * start_color.redF() + t * end_color.redF(),  
      85          900 :             (1 - t) * start_color.greenF() + t * end_color.greenF(), 
      86          900 :             (1 - t) * start_color.blueF() + t * end_color.blueF()  
      87         2700 :         );
      88          900 :         int alpha = static_cast<int>(255 * (1 - std::abs(2 * t - 1)));
      89          900 :         color.setAlpha(alpha);
      90          900 :         QPen pen(color, width() / 50);
      91          900 :         painter->setPen(pen);
      92          900 :         painter->drawArc(centerX - radius, centerY - radius, radius * 2, radius * 2, (270 + i * segment_angle) * 16, segment_angle * 16);
      93          900 :     }
      94            4 : }
      95              : 
      96            4 : void Battery::draw_pixmap(TestPainter *painter)
      97              : {
      98            4 :     painter->setPen(QPen(QColor(0, 250, 195)));
      99            4 :     painter->setFont(QFont("Digital-7", width() / 5, QFont::Bold));
     100            4 :     QRect currentTextRect = painter->boundingRect(rect(), Qt::AlignCenter, QString::number(current));
     101            4 :     painter->drawText(currentTextRect, Qt::AlignCenter, QString::number(current));
     102              :     
     103            4 :     QString path = QCoreApplication::applicationDirPath();
     104            4 :     QString digital_path = QDir(path).filePath("../fonts_icon/battery.png");
     105            4 :     digital_path = QDir::cleanPath(digital_path);
     106            4 :     QPixmap pixmap(digital_path);  
     107            4 :     pixmap = pixmap.scaled(width() / 10, width() / 10, Qt::KeepAspectRatio);
     108            4 :     QRect rectBottom = this->rect();
     109            4 :     int xIcon = (width() - pixmap.width()) / 2;  
     110            4 :     QRect bottomRect = QRect(xIcon, currentTextRect.bottom() + 10, pixmap.width(), pixmap.height());
     111            4 :     painter->drawPixmap(bottomRect, pixmap);
     112            4 :     draw_text(painter, bottomRect);
     113            8 : }
     114              : 
     115            4 : void Battery::draw_text(TestPainter *painter, QRect bottomRect)
     116              : {
     117            4 :     QFont font("Calculator", width() / 16);
     118            4 :     painter->setFont(font);
     119            4 :     painter->setPen(QPen(QColor(0, 120, 100)));
     120            4 :     QRectF textRect(bottomRect.right() + 5, bottomRect.bottom() - 23, 30, 30);  // Adjust size as needed
     121            4 :     int flags = Qt::AlignLeft | Qt::AlignVCenter;  // Adjust alignment flags as needed
     122            4 :     QString text = "%";
     123            4 :     painter->drawText(textRect, flags, text);
     124            8 : }
     125              : 
        

Generated by: LCOV version 2.0-1