Index: sources/view/hd/data/VHDPostSingleResultData.cpp =================================================================== diff -u -r4e708c6e93443b01ac26c71a466708916ede4abf -r83ffaef436920eba534cde428e14d168ea931bea --- sources/view/hd/data/VHDPostSingleResultData.cpp (.../VHDPostSingleResultData.cpp) (revision 4e708c6e93443b01ac26c71a466708916ede4abf) +++ sources/view/hd/data/VHDPostSingleResultData.cpp (.../VHDPostSingleResultData.cpp) (revision 83ffaef436920eba534cde428e14d168ea931bea) @@ -17,8 +17,168 @@ // Project #include "GuiController.h" -VIEW_DEF(VHDPostSingleResult, HDPostSingleResultData) +using namespace View; -void VHDPostSingleResult::onActionReceive(const HDPostSingleResultData &vData) { +VHDPostSingleResult::VHDPostSingleResult(QAbstractListModel *parent) : QAbstractListModel(parent) +{ + initConnections(); +} + +/*! + * \brief VHDPostSingleResult::initConnections + * Initializes the connections + */ +void VHDPostSingleResult::initConnections() +{ + ACTION_VIEW_CONNECTION(HDPostSingleResultData); + ACTION_VIEW_CONNECTION(SettingsData); +} + +/*! + * \brief VHDPostSingleResult::addSelfTest + * Adds a self test + * \param vTest - the post test to add + */ +void VHDPostSingleResult::addSelfTest(const PostTest &vTest) +{ + LOG_DEBUG(QString("Adding HD power on self test %1").arg(vTest.name)); + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + _tests << vTest; + endInsertRows(); +} + +/*! + * \brief VNetworkModel::removeRows + * Removes all rows from the model + */ +void VHDPostSingleResult::removeAllRows() +{ + beginRemoveRows(QModelIndex(), 0, rowCount()); + _tests.clear(); + endRemoveRows(); +} + +/*! + * \brief VHDPostSingleResult::data + * Returns the test properties at the specified index + * \param index (QModelIndex) contains the row of data to lookup + * \param role - (int) the property index to return. See TestDataRole + * \return (QVariant) - the value for the specified test property + */ +QVariant VHDPostSingleResult::data(const QModelIndex & index, int role) const +{ + if (index.row() < 0 || index.row() >= _tests.count()) + return QVariant(); + + switch (role) + { + case TestNameRole: + return _tests[index.row()].name; + case TestResultRole: + return _tests[index.row()].result; + } + + return QVariant(); +} + + +/*! + * \brief VHDPostSingleResult::setData + * \param vIndex - the test number + * \param vValue - the new value for the role + * \param vRole - the role to update + * \return true upon success, false otherwise + */ +bool VHDPostSingleResult::setData(int vIndex, const QVariant &vValue, const TestDataRole &vRole) +{ + if (vIndex < 0 || vIndex >= rowCount()) + return false; + + PostTest &test = _tests[vIndex]; + + if (vRole == TestNameRole) + test.name = vValue.toString(); + else if (vRole == TestResultRole) + { + bool ok = false; + quint32 result = vValue.toInt(&ok); + if (!ok) + return false; + test.result = result; + } + else + return false; + + // FIXME: emit dataChanged... + return true; +} + +/*! + * \brief VNetworkModel::rowCount + * Gets the number of tests + * \param parent - the parent QModelIndex + * \return - the number of networks + */ +int VHDPostSingleResult::rowCount(const QModelIndex & parent) const +{ + Q_UNUSED(parent); + return _tests.count(); +} + +/*! + * \brief VNetworkModel::roleNames + * Translates how to access specific properties of the data for QML from the TestDataRole enum + * \return (QHash) - maps enums to property names + */ +QHash VHDPostSingleResult::roleNames() const +{ + QHash roles; + roles[TestNameRole] = "name"; + roles[TestResultRole] = "result"; + return roles; +} + + +/*! + * \brief VHDPostSingleResult::onActionReceive + * Called when the HD self test has a result + * \param vData - contains the index of the test and its result + */ +void VHDPostSingleResult::onActionReceive(const HDPostSingleResultData &vData) +{ Q_UNUSED(vData) + if (vData.mIndex >= (quint32)rowCount()) + { + LOG_DEBUG("The test index received from FW is greater than number of loaded UI power on self tests."); + return; + } + + if (!setData(vData.mIndex, + QVariant(vData.mResult), + TestResultRole)) + LOG_DEBUG(QString("Could not set data for test index %1 with result %2").arg(vData.mIndex, vData.mResult)); } + +/*! + * \brief VHDPostSingleResult::onActionReceive + * Called when the settings data is read from disk on startup + * \param vData - the settings data + */ +void VHDPostSingleResult::onActionReceive(const SettingsData &) +{ + // POST Settings folder and filename (category): PowerOnSelfTests/PowerOnSelfTests + QStringList tests; + for (const auto &group : _Settings.groups()) + { + if (group == "HDPostSelfTests") + { + tests = _Settings.keys(group); + } + } + for (const QString &test : tests) + { + PostTest posttest; + posttest.name = test; + addSelfTest(posttest); + } +}