diff --git a/source/ResultWidget/CMakeLists.txt b/source/ResultWidget/CMakeLists.txt index f401415..b85d0c1 100644 --- a/source/ResultWidget/CMakeLists.txt +++ b/source/ResultWidget/CMakeLists.txt @@ -13,6 +13,7 @@ qt5_wrap_ui(GENUS_UI add_library(${PROJECT_NAME} ResultWidget.cpp ResultModel.cpp + PRMap.cpp ${GENUS_UI} ) diff --git a/source/ResultWidget/PRMap.cpp b/source/ResultWidget/PRMap.cpp new file mode 100644 index 0000000..ee86854 --- /dev/null +++ b/source/ResultWidget/PRMap.cpp @@ -0,0 +1,41 @@ +#include "PRMap.h" + +unsigned int PRMap::lookup(const Age &age, const unsigned int &points) +{ + if (points >= m_PRs.size()) + { + return 0; + } + + auto ageIndex = [&]() -> size_t { + if (m_ages.empty()) + { + return 0; + } + + if (age < m_ages.front()) + { + return 0; + } + + if (m_ages.back() < age) + { + return m_ages.size() - 1; + } + + for (size_t index = 1; index < m_ages.size(); ++index) + { + if (age < m_ages.at(index)) + { + return index - 1; + } + } + }(); + + if (ageIndex >= m_PRs.at(points).size()) + { + return 0; + } + + return m_PRs.at(points).at(ageIndex); +} diff --git a/source/ResultWidget/PRMap.h b/source/ResultWidget/PRMap.h new file mode 100644 index 0000000..5ad5bcf --- /dev/null +++ b/source/ResultWidget/PRMap.h @@ -0,0 +1,14 @@ +#pragma once + +#include "../Age.h" +#include + +class PRMap +{ +protected: + std::vector m_ages; + std::vector> m_PRs; + +public: + unsigned int lookup(const Age &age, const unsigned int &points); +}; diff --git a/source/ResultWidget/ResultModel.cpp b/source/ResultWidget/ResultModel.cpp index e750760..a320691 100644 --- a/source/ResultWidget/ResultModel.cpp +++ b/source/ResultWidget/ResultModel.cpp @@ -21,71 +21,49 @@ int ResultModel::columnCount(const QModelIndex &parent) const QVariant ResultModel::data(const QModelIndex &index, int role) const { - if (role == Qt::DisplayRole) + if (role == Qt::DisplayRole && index.column() < m_results.size()) { switch (index.row()) { case 0: - if (index.column() < m_results.size()) + { + auto points = m_results[index.column()].points(); + if (points != 0) { - size_t points = m_results[index.column()].points(); - if (points != 0) - { - return static_cast(points); - } + return static_cast(points); } break; + } case 1: - switch (index.column()) + { + auto pr = m_results[index.column()].pr(); + if (pr >= 84) { - case 8: - { - auto pR = getPluralPR(); - if (pR >= 84) - { - return pR; - } - } - break; - default: - break; + return static_cast(pr); } break; + } case 2: - switch (index.column()) + { + auto pr = m_results[index.column()].pr(); + if (pr < 84 && pr > 16) { - case 8: - { - auto pR = getPluralPR(); - if (pR < 84 && pR > 16) - { - return pR; - } - } - break; - default: - break; + return static_cast(pr); } break; + } case 3: - switch (index.column()) + { + auto pr = m_results[index.column()].pr(); + if (pr <= 16) { - case 8: - { - auto pR = getPluralPR(); - if (pR <= 16) - { - return pR; - } - } - break; - default: - break; + return static_cast(pr); } break; + } default: break; - } + }; return "-"; } @@ -140,17 +118,9 @@ void ResultModel::setPluralResult(size_t points) { if (m_results[8].points() != points) { - m_results[8] = points; + m_results[8].setPoints(points); + m_results[8].setPR(PluralPR().lookup(m_age, points)); emit dataChanged(index(0, 8), index(4, 8)); } } -unsigned int ResultModel::getPluralPoints() const -{ - return m_results[8].points(); -} - -unsigned int ResultModel::getPluralPR() const -{ - return PluralPR().lookup(m_age, getPluralPoints()); -} diff --git a/source/ResultWidget/ResultModel.h b/source/ResultWidget/ResultModel.h index 178ce0a..c88f4af 100644 --- a/source/ResultWidget/ResultModel.h +++ b/source/ResultWidget/ResultModel.h @@ -1,72 +1,35 @@ #pragma once #include "../Age.h" -#include +#include "PRMap.h" #include -class PluralPR +class PluralPR : public PRMap { - // clang-format off - const std::vector m_ages = { - { 4, 0 }, - { 4, 6 }, - { 5, 6 }, - { 9, 0 } - }; - - const std::vector> m_PRs = { - { 0, 0, 0 }, - { 0, 1, 0 }, - { 0, 1, 0 }, - { 1, 1, 0 }, - { 7, 2, 1 }, - { 10, 4, 1}, - { 26, 10, 2 }, - { 57, 25, 7 }, - { 79, 56, 27 }, - { 100, 100, 100 } - }; - // clang-format on - public: - unsigned int lookup(const Age &age, const unsigned int &points) + PluralPR() { - if (points >= m_PRs.size()) - { - return 0; - } + // clang-format off + m_ages = { + { 4, 0 }, + { 4, 6 }, + { 5, 6 }, + { 9, 0 } + }; - auto ageIndex = [&]() -> size_t { - if (m_ages.empty()) - { - return 0; - } - - if (age < m_ages.front()) - { - return 0; - } - - if (m_ages.back() < age) - { - return m_ages.size() - 1; - } - - for (size_t index = 1; index < m_ages.size(); ++index) - { - if (age < m_ages.at(index)) - { - return index - 1; - } - } - }(); - - if (ageIndex >= m_PRs.at(points).size()) - { - return 0; - } - - return m_PRs.at(points).at(ageIndex); + m_PRs = { + { 0, 0, 0 }, + { 0, 1, 0 }, + { 0, 1, 0 }, + { 1, 1, 0 }, + { 7, 2, 1 }, + { 10, 4, 1}, + { 26, 10, 2 }, + { 57, 25, 7 }, + { 79, 56, 27 }, + { 100, 100, 100 } + }; + // clang-format on } }; @@ -74,20 +37,25 @@ class TestResult { private: QString m_name; - size_t m_points; + size_t m_points = 0; + size_t m_pr = 0; public: TestResult(const char *name) : m_name(name) - , m_points(0) { } - void operator=(const size_t &points) + void setPoints(const size_t &points) { m_points = points; } + void setPR(const unsigned int &pr) + { + m_pr = pr; + } + const QString &name() const { return m_name; @@ -97,6 +65,11 @@ public: { return m_points; } + + const size_t pr() const + { + return m_pr; + } }; class ResultModel : public QAbstractTableModel @@ -121,8 +94,4 @@ public: void setAge(const Age &age); void setPluralResult(size_t points); - -private: - unsigned int getPluralPoints() const; - unsigned int getPluralPR() const; };