ContactsModel.qml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2012 Nokia Corporation.
  3. */
  4. import QtQuick 1.1
  5. import "../Constants.js" as Constants
  6. QtObject{
  7. id: model
  8. property list<Contact> foundContactsModel
  9. property list<Contact> contactsList: [
  10. Contact {
  11. name: "Amy Anderson"
  12. groups: ["1", "2", "3"]
  13. },
  14. Contact {
  15. name: "Anders"
  16. groups: ["1", "3"]
  17. },
  18. Contact {
  19. name: "Barbara Brown"
  20. groups: ["2", "3"]
  21. },
  22. Contact {
  23. name: "Charlie Mickelson"
  24. groups: ["3"]
  25. },
  26. Contact {
  27. name: "Danny"
  28. groups: ["2"]
  29. },
  30. Contact {
  31. name: "Emily"
  32. groups: ["1", "3"]
  33. },
  34. Contact {
  35. name: "Gloria"
  36. groups: ["1"]
  37. },
  38. Contact {
  39. name: "Jerome Walton"
  40. groups: ["1", "2", "3"]
  41. },
  42. Contact {
  43. name: "Joan"
  44. groups: ["1", "3"]
  45. },
  46. Contact {
  47. name: "Lynne"
  48. groups: ["1", "2", "3"]
  49. },
  50. Contact {
  51. name: "Mike Sparks"
  52. groups: ["2", "3"]
  53. },
  54. Contact {
  55. name: "Nataly Grant"
  56. groups: ["1", "3"]
  57. },
  58. Contact {
  59. name: "Peter"
  60. groups: ["2"]
  61. },
  62. Contact {
  63. name: "Patricia"
  64. groups: ["1", "2", "3"]
  65. },
  66. Contact {
  67. name: "Randy Allen"
  68. groups: ["1", "3"]
  69. },
  70. Contact {
  71. name: "Steve"
  72. groups: ["2"]
  73. },
  74. Contact {
  75. name: "Stephanie Baker"
  76. groups: ["1", "2"]
  77. },
  78. Contact {
  79. name: "William Cooper"
  80. groups: ["1", "3"]
  81. }
  82. ]
  83. function getFoundContactsModel(text, group) {
  84. // assing javascript array to QML list property
  85. foundContactsModel = getFoundContactsArray(text, group)
  86. return foundContactsModel
  87. }
  88. function getFoundContactsArray(text, group) {
  89. // temporary javascript array that is about to be fille with found contacts
  90. // this due to the fact QML list can't be modified dynamically
  91. // so we operate on this temporary array
  92. // and at the end we assign it to QML list property (it's legal)
  93. var foundContactsArray = new Array()
  94. for (var i = 0; i< contactsList.length ;i++) {
  95. var matchingGroup = false
  96. if (group == "")
  97. matchingGroup = true
  98. else {
  99. for (var j=0; j<contactsList[i].groups.length; j++) {
  100. if (contactsList[i].groups[j] == group) {
  101. matchingGroup = true
  102. break
  103. }
  104. }
  105. }
  106. if (contactsList[i].name.toLowerCase().indexOf(text.toLowerCase()) != -1 && matchingGroup) {
  107. var contact = Qt.createQmlObject("import QtQuick 1.1; Contact{}", model);
  108. contact.name = contactsList[i].name
  109. contact.groups = contactsList[i].groups
  110. foundContactsArray.push(contact)
  111. }
  112. }
  113. return foundContactsArray
  114. }
  115. }