Index: sources/model/MListModel.cpp =================================================================== diff -u -rbae1ae230d8b41f90b1fcd34b9bccdfa87bf3cd9 -rc1e50347c78096b626b2cd1ef32a33b439e88bdd --- sources/model/MListModel.cpp (.../MListModel.cpp) (revision bae1ae230d8b41f90b1fcd34b9bccdfa87bf3cd9) +++ sources/model/MListModel.cpp (.../MListModel.cpp) (revision c1e50347c78096b626b2cd1ef32a33b439e88bdd) @@ -12,21 +12,6 @@ } /*! - * \brief Inserts data into the model at a given position. - * \param[in] pos - The position where the data will be inserted. - * If row is less than 0, then data will be prepended to the model. - * If row is greater than or equal to rowCount(), then data will be appeneded to the model. - * \param[in] data - The data to insert into the model. - */ -void View::MListModel::insertRow(const int pos, const QHash &vData) -{ - const int index = std::clamp(pos, 0, rowCount()); - beginInsertRows(QModelIndex(), index, index); - _data.insert(index, vData); - endInsertRows(); -} - -/*! * \brief Retrieve the role names set for this list model. * \return Role names used for this model. */ @@ -35,18 +20,6 @@ } /*! - * \brief Set the role names for the data in this list model. - * \param[in] roleNames New role names for this list model. - * \note This will clear the list model of any data. - */ -void View::MListModel::setRoleNames(const QHash& vRoleNames) { - if ( _roleNames != vRoleNames ) { - clear(); - _roleNames = vRoleNames; - } -} - -/*! * \brief Get the number of rows in this list model. * \return Number of rows in this list model. */ @@ -82,33 +55,24 @@ } /*! - * \brief Retrieve the data stored at the given index for the specified role and expose to QML - * \param[in] vRow Index of the data in the list model. + * \brief Sets the value of the data in this list model at the given index and for the given role. + * \param[in] index Location of data to update. + * \param[in] value Value to set. + * \param[in] role The role of the data at the given index to set. + * \return Return true if data was set properly, otherwise false. */ -QVariantMap View::MListModel::get(int vRow) const { - QVariantMap map; - QHash roles = roleNames(); +bool View::MListModel::setData(const QModelIndex &vIndex, const QVariant& vValue, int vRole) { + if ( ! vIndex.isValid() || vIndex.row() >= _data.count() ) { + return false; + } - // Row bounds check - if ( vRow < 0 || vRow >= rowCount() ) { goto lOut; } - - // Check if roles are defined - if ( roles.isEmpty() ) { goto lOut; } - - for ( auto it = roles.cbegin(); it != roles.cend(); ++it ) { - const int role = it.key(); - const QByteArray roleName = it.value(); - - if ( roleName.isEmpty() ) { goto lOut; } - - QVariant value = data(index(vRow, 0), role); - if ( ! value.isValid() ) { goto lOut; } - - map.insert( QString::fromUtf8(roleName), value ); + if ( _data[vIndex.row()][vRole] != vValue ) { + _data[vIndex.row()][vRole] = vValue; + // explicitly emit a dataChanged signal to notify anybody bound to this property (vRole) + emit dataChanged(vIndex, vIndex, QVector(1, vRole)); } -lOut: - return map; + return true; } /*! @@ -121,27 +85,51 @@ } /*! - * \brief Sets the value of the data in this list model at the given index and for the given role. - * \param[in] index Location of data to update. - * \param[in] value Value to set. - * \param[in] role The role of the data at the given index to set. - * \return Return true if data was set properly, otherwise false. + * \brief Set the role names for the data in this list model. + * \param[in] roleNames New role names for this list model. + * \note This will clear the list model of any data. */ -bool View::MListModel::setData(const QModelIndex &vIndex, const QVariant& vValue, int vRole) { - if ( ! vIndex.isValid() || vIndex.row() >= _data.count() ) { - return false; +void View::MListModel::setRoleNames(const QHash& vRoleNames) { + if ( _roleNames != vRoleNames ) { + clear(); + _roleNames = vRoleNames; } +} - if ( _data[vIndex.row()][vRole] != vValue ) { - _data[vIndex.row()][vRole] = vValue; - // explicitly emit a dataChanged signal to notify anybody bound to this property (vRole) - emit dataChanged(vIndex, vIndex, QVector(1, vRole)); - } +/*! + * \brief Appends a row of data to the end of the model. + * \param[in] data The data to append. +*/ +void View::MListModel::appendRow(const QHash &vData) { + insertRow(rowCount(), vData); +} - return true; +/*! + * \brief Inserts data into the model at a given position. + * \param[in] vRow - The index where the data will be inserted. + * If row is less than 0, then data will be prepended to the model. + * If row is greater than or equal to rowCount(), then data will be appeneded to the model. + * \param[in] data - The data to insert into the model. + */ +void View::MListModel::insertRow(const int vRow, const QHash &vData) +{ + const int index = std::clamp(vRow, 0, rowCount()); + beginInsertRows(QModelIndex(), index, index); + _data.insert(index, vData); + endInsertRows(); } /*! + * \brief Helper function of setData to update data in the model + * \param[in] vRow row of item to update value + * \param[in] value Value to update. + * \param[in] role The role of the data at the given index to set. + */ +void View::MListModel::updateData (const int vRow, const int vRole, const QVariant& vValue) { + setData(index(vRow,0), vValue, vRole); +} + +/*! * \brief Assignment operator * \return A reference to this object. */ @@ -155,3 +143,33 @@ } return *this; } + +/*! + * \brief Retrieve the data stored at the given index for the specified role and expose to QML + * \param[in] vRow Index of the data in the list model. + */ +QVariantMap View::MListModel::get(int vRow) const { + QVariantMap map; + QHash roles = roleNames(); + + // Row bounds check + if ( vRow < 0 || vRow >= rowCount() ) { goto lOut; } + + // Check if roles are defined + if ( roles.isEmpty() ) { goto lOut; } + + for ( auto it = roles.cbegin(); it != roles.cend(); ++it ) { + const int role = it.key(); + const QByteArray roleName = it.value(); + + if ( roleName.isEmpty() ) { goto lOut; } + + QVariant value = data(index(vRow, 0), role); + if ( ! value.isValid() ) { goto lOut; } + + map.insert( QString::fromUtf8(roleName), value ); + } + +lOut: + return map; +}