levelmodel.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. #include "levelmodel.h"
  5. LevelModel::LevelModel(QObject *parent) :
  6. QAbstractListModel(parent)
  7. {
  8. /***********************************************************************************************
  9. *Making the custom roles usable via qml. *
  10. ***********************************************************************************************/
  11. QHash<int, QByteArray> roles;
  12. roles[Qt::DisplayRole] = "display";
  13. roles[Qt::EditRole] = "edit";
  14. roles[LevelIdRole] = "level_id";
  15. roles[LevelNameRole] = "level_name";
  16. roles[LevelPriceRole] = "level_price";
  17. roles[LevelUnlockedRole] = "level_unlocked";
  18. roles[LevelShortDescriptionRole] = "level_short_desc";
  19. roles[LevelLongDescriptionRole] = "level_long_desc";
  20. roles[LevelType] = "level_type";
  21. setRoleNames(roles);
  22. }
  23. /***************************************************************************************************
  24. *Standard Qt model view methods. *
  25. ***************************************************************************************************/
  26. int LevelModel::rowCount(const QModelIndex &parent) const
  27. {
  28. Q_UNUSED(parent)
  29. return modelData.count();
  30. }
  31. QVariant LevelModel::data(const QModelIndex &index, int role) const
  32. {
  33. QVariant result;
  34. if(index.row() < modelData.count() && index.row() >= 0){
  35. if(role == Qt::DisplayRole || role == Qt::EditRole || role == LevelNameRole)
  36. result = QVariant::fromValue(modelData.at(index.row()).name);
  37. else if(role == LevelIdRole)
  38. result = QVariant::fromValue(modelData.at(index.row()).id);
  39. #if defined(IAP_ENABLED)
  40. else if(role == LevelPriceRole)
  41. result = QVariant::fromValue(modelData.at(index.row()).data.price);
  42. #endif
  43. else if(role == LevelUnlockedRole)
  44. result = QVariant::fromValue(modelData.at(index.row()).unlocked);
  45. #if defined(IAP_ENABLED)
  46. else if(role == LevelShortDescriptionRole)
  47. result = QVariant::fromValue(modelData.at(index.row()).data.shortDescription);
  48. else if(role == LevelLongDescriptionRole)
  49. result = QVariant::fromValue(modelData.at(index.row()).data.longDescription);
  50. else if(role == LevelType)
  51. #endif
  52. result = QVariant::fromValue(modelData.at(index.row()).type);
  53. }
  54. return result;
  55. }
  56. bool LevelModel::setData(const QModelIndex &index, const QVariant &value, int role)
  57. {
  58. if(index.row() < modelData.count() && index.row() >= 0){
  59. if(role == Qt::DisplayRole || role == Qt::EditRole || role == LevelNameRole
  60. && value.type() == QVariant::String)
  61. modelData[index.row()].name = value.toString();
  62. else if(role = LevelIdRole && value.type() == QVariant::String)
  63. modelData[index.row()].id = value.toString();
  64. else if(role == LevelUnlockedRole && value.type() == QVariant::Bool)
  65. modelData[index.row()].unlocked = value.toBool();
  66. }
  67. return false;
  68. }
  69. Qt::ItemFlags LevelModel::flags(const QModelIndex &index) const
  70. {
  71. return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
  72. }
  73. bool LevelModel::insertRows(int row, int count, const QModelIndex &parent)
  74. {
  75. QAbstractListModel::beginInsertRows(parent,row,row + count - 1);
  76. for(int i = 0; i < count; ++i)
  77. modelData.insert(row + i,LevelData());
  78. QAbstractListModel::endInsertRows();
  79. return true;
  80. }
  81. bool LevelModel::removeRows(int row, int count, const QModelIndex &parent)
  82. {
  83. if(row < modelData.count()){
  84. QAbstractListModel::beginRemoveRows(parent,row,row + count - 1);
  85. for(int i = 0; i < count; ++i)
  86. modelData.removeAt(row);
  87. QAbstractListModel::endRemoveRows();
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**************************************************************************************************/
  93. void LevelModel::setModelData(const QList<LevelData>& data)
  94. {
  95. /***********************************************************************************************
  96. *Check the relative data set size and adjust accordingly(add/remove rows). *
  97. ***********************************************************************************************/
  98. int diff = modelData.count() - data.count();
  99. if(diff > 0)
  100. removeRows(modelData.count() - 1 - diff,diff);
  101. else if(diff < 0)
  102. insertRows(modelData.count(),-1 * diff);
  103. /***********************************************************************************************
  104. *Set the new model data and signalize that the entire model has changed. *
  105. ***********************************************************************************************/
  106. modelData = data;
  107. emit dataChanged(index(0),index(modelData.count() - 1));
  108. }
  109. void LevelModel::setLevelData(const LevelData& data)
  110. {
  111. /***********************************************************************************************
  112. *Chceck if the model contains an item with the same id. *
  113. ***********************************************************************************************/
  114. if(modelData.contains(data)){
  115. /*******************************************************************************************
  116. *Overite item witn new data. *
  117. *******************************************************************************************/
  118. int i = modelData.indexOf(data);
  119. modelData[i] = data;
  120. emit dataChanged(index(i),index(i));
  121. }else{
  122. /*******************************************************************************************
  123. *Insert a new item and set its data. *
  124. *******************************************************************************************/
  125. if(insertRows(modelData.count(),1)){
  126. int i = modelData.count() - 1;
  127. modelData[i] = data;
  128. emit dataChanged(index(i),index(i));
  129. }
  130. }
  131. }
  132. LevelData LevelModel::getById(QString itemId) const
  133. {
  134. /***********************************************************************************************
  135. *Find and return the item with the given id. *
  136. ***********************************************************************************************/
  137. LevelData result;
  138. for(int i = 0; i < modelData.count(); ++i)
  139. {
  140. if(modelData.at(i).id == itemId){
  141. result = modelData.at(i);
  142. break;
  143. }
  144. }
  145. return result;
  146. }