diff --git a/src/ProgressBarInfinite.cpp b/src/ProgressBarInfinite.cpp index 254e6ac..3ecb9c5 100644 --- a/src/ProgressBarInfinite.cpp +++ b/src/ProgressBarInfinite.cpp @@ -1,4 +1,5 @@ #include +#include "Utilities.h" ProgressBarInfinite::ProgressBarInfinite(QString title, QWidget* parent) : ProgressBar{std::move(title), parent} @@ -26,15 +27,12 @@ void ProgressBarInfinite::reset() void ProgressBarInfinite::paintProgressBar(QPainter& painter) { - constexpr int step{45}; - int startAngle{static_cast(std::lround( - progressCounter_ * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))}; - constexpr const int spanAngle{-step * FULL_DEGREE}; - painter.drawArc(getArcRectangle(), startAngle, spanAngle); - startAngle = static_cast(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) diff --git a/src/Utilities.cpp b/src/Utilities.cpp index c09bea7..49286eb 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -1,6 +1,15 @@ #include "Utilities.h" #include +#include + +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 { @@ -11,4 +20,21 @@ bool doublesAreEqual(double left, double right) (qtDoublePrecision * std::max({1.0, std::abs(left), std::abs(right)})); } + +std::pair getStartingAngles(int progressCounter) +{ + int firstAngle{static_cast( + std::lround(progressCounter * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))}; + int secondAngle{static_cast(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 diff --git a/src/Utilities.h b/src/Utilities.h index c5e8ee8..0fd8546 100644 --- a/src/Utilities.h +++ b/src/Utilities.h @@ -1,6 +1,12 @@ #pragma once +#include + namespace utilities { bool doublesAreEqual(double left, double right); + +std::pair getStartingAngles(int progressCounter); + +int getSpanAngle(); } // namespace utilities diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0c75847..092d1fc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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}) diff --git a/tests/Tests.cpp b/tests/Tests.cpp index a21a4af..1076329 100644 --- a/tests/Tests.cpp +++ b/tests/Tests.cpp @@ -7,6 +7,7 @@ #include "FilterStringsTest.h" #include "ProgressBarCounterTest.h" #include "ProgressBarInfiniteTest.h" +#include "UtilitiesTest.h" namespace { @@ -45,5 +46,8 @@ int main(int argc, char* argv[]) ProgressBarInfiniteTest progressBarInfiniteTest; QTest::qExec(&progressBarInfiniteTest); + UtilitiesTest utilitiesTest; + QTest::qExec(&utilitiesTest); + return 0; } diff --git a/tests/UtilitiesTest.cpp b/tests/UtilitiesTest.cpp new file mode 100644 index 0000000..b9654e2 --- /dev/null +++ b/tests/UtilitiesTest.cpp @@ -0,0 +1,26 @@ +#include "UtilitiesTest.h" + +#include + +#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); +} diff --git a/tests/UtilitiesTest.h b/tests/UtilitiesTest.h new file mode 100644 index 0000000..10c246d --- /dev/null +++ b/tests/UtilitiesTest.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class UtilitiesTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + static void testAnglesZeroProgress(); + + static void testAnglesHalfProgress(); + + static void testAnglesFullProgress(); +};