Skip to content

Commit

Permalink
Move some logic related to arc calculations to utilities namepsace.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Dec 23, 2024
1 parent a0dc927 commit c68d359
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/ProgressBarInfinite.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <wble/ProgressBarInfinite.h>
#include "Utilities.h"

ProgressBarInfinite::ProgressBarInfinite(QString title, QWidget* parent)
: ProgressBar{std::move(title), parent}
Expand Down Expand Up @@ -26,15 +27,12 @@ void ProgressBarInfinite::reset()

void ProgressBarInfinite::paintProgressBar(QPainter& painter)
{
constexpr int step{45};
int startAngle{static_cast<int>(std::lround(
progressCounter_ * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))};
constexpr const int spanAngle{-step * FULL_DEGREE};
painter.drawArc(getArcRectangle(), startAngle, spanAngle);
startAngle = static_cast<int>(std::lround(
(HALF_CIRCLE_ANGLE + (progressCounter_ * HUNDREDTH_OF_FULL_CIRCLE)) *
FULL_DEGREE));
painter.drawArc(getArcRectangle(), startAngle, spanAngle);
const auto [firstAngle,
secondAngle]{utilities::getStartingAngles(progressCounter_)};
const int spanAngle{utilities::getSpanAngle()};

painter.drawArc(getArcRectangle(), firstAngle, spanAngle);
painter.drawArc(getArcRectangle(), secondAngle, spanAngle);
}

void ProgressBarInfinite::timerEvent([[maybe_unused]] QTimerEvent* event)
Expand Down
26 changes: 26 additions & 0 deletions src/Utilities.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include "Utilities.h"

#include <algorithm>
#include <cmath>

namespace
{
constexpr int FULL_DEGREE{16};
constexpr double HUNDREDTH_OF_FULL_CIRCLE{3.6};
constexpr int QUARTER_CIRCLE_ANGLE{90};
constexpr int HALF_CIRCLE_ANGLE{2 * QUARTER_CIRCLE_ANGLE};
} // namespace

namespace utilities
{
Expand All @@ -11,4 +20,21 @@ bool doublesAreEqual(double left, double right)
(qtDoublePrecision *
std::max({1.0, std::abs(left), std::abs(right)}));
}

std::pair<int, int> getStartingAngles(int progressCounter)
{
int firstAngle{static_cast<int>(
std::lround(progressCounter * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))};
int secondAngle{static_cast<int>(std::lround(
(HALF_CIRCLE_ANGLE + (progressCounter * HUNDREDTH_OF_FULL_CIRCLE)) *
FULL_DEGREE))};
return {firstAngle, secondAngle};
}

int getSpanAngle()
{
constexpr int step{45};
constexpr const int spanAngle{-step * FULL_DEGREE};
return spanAngle;
}
} // namespace utilities
6 changes: 6 additions & 0 deletions src/Utilities.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#pragma once

#include <utility>

namespace utilities
{
bool doublesAreEqual(double left, double right);

std::pair<int, int> getStartingAngles(int progressCounter);

int getSpanAngle();
} // namespace utilities
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ set(${PROJECT_TESTS}_SOURCES
ProgressBarCommon.cpp
ProgressBarInfiniteTest.h
ProgressBarInfiniteTest.cpp
UtilitiesTest.h
UtilitiesTest.cpp
)

qt_add_resources(${PROJECT_TESTS}_SOURCES testResources.qrc)

add_executable(${PROJECT_TESTS} ${${PROJECT_TESTS}_SOURCES})

target_include_directories(${PROJECT_TESTS} PRIVATE ../src)

target_link_libraries(${PROJECT_TESTS} PRIVATE wble Qt6::Core Qt6::Widgets Qt6::Test)

add_test(NAME ${PROJECT_TESTS} COMMAND ${PROJECT_TESTS})
Expand Down
4 changes: 4 additions & 0 deletions tests/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "FilterStringsTest.h"
#include "ProgressBarCounterTest.h"
#include "ProgressBarInfiniteTest.h"
#include "UtilitiesTest.h"

namespace
{
Expand Down Expand Up @@ -45,5 +46,8 @@ int main(int argc, char* argv[])
ProgressBarInfiniteTest progressBarInfiniteTest;
QTest::qExec(&progressBarInfiniteTest);

UtilitiesTest utilitiesTest;
QTest::qExec(&utilitiesTest);

return 0;
}
26 changes: 26 additions & 0 deletions tests/UtilitiesTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "UtilitiesTest.h"

#include <QTest>

#include "Utilities.h"

void UtilitiesTest::testAnglesZeroProgress()
{
auto [firstAngle, secondAngle]{utilities::getStartingAngles(0)};
QCOMPARE(firstAngle, 0);
QCOMPARE(secondAngle, 2880);
}

void UtilitiesTest::testAnglesHalfProgress()
{
auto [firstAngle, secondAngle]{utilities::getStartingAngles(50)};
QCOMPARE(firstAngle, 2880);
QCOMPARE(secondAngle, 5760);
}

void UtilitiesTest::testAnglesFullProgress()
{
auto [firstAngle, secondAngle]{utilities::getStartingAngles(100)};
QCOMPARE(firstAngle, 5760);
QCOMPARE(secondAngle, 8640);
}
14 changes: 14 additions & 0 deletions tests/UtilitiesTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <QObject>

class UtilitiesTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
static void testAnglesZeroProgress();

static void testAnglesHalfProgress();

static void testAnglesFullProgress();
};

0 comments on commit c68d359

Please sign in to comment.