DateSelector.qml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import Qt 4.7
  2. Item {
  3. // api variables: these are updated to represent the selected item
  4. property int selectedDate
  5. property int selectedMonth
  6. property int selectedYear
  7. // internal variables, not updated after initial load completed
  8. property int previousDaysToPopulate: 21
  9. property int nextDaysToPopulate: 0
  10. property int todayIndex: previousDaysToPopulate
  11. id: daySelector
  12. width: screenWidth
  13. function populate() {
  14. var currentDate = new Date();
  15. var dayInMsecs = 1000 * 60 * 60 * 24;
  16. var tmpDay = new Date();
  17. // populate previous days
  18. for (var i = previousDaysToPopulate; i > 0; i--) {
  19. tmpDay.setTime(currentDate.getTime() - dayInMsecs * i);
  20. dateModel.append({
  21. "date": tmpDay.getDate(),
  22. "month": tmpDay.getMonth() + 1,
  23. "year": tmpDay.getFullYear()
  24. });
  25. }
  26. // populate next days
  27. for (var i = 0; i < nextDaysToPopulate + 1; i++) {
  28. tmpDay.setTime(currentDate.getTime() + dayInMsecs * i);
  29. dateModel.append( {
  30. "date": tmpDay.getDate(),
  31. "month": tmpDay.getMonth() + 1,
  32. "year": tmpDay.getFullYear()
  33. });
  34. }
  35. selectedDate = currentDate.getDate();
  36. selectedMonth = currentDate.getMonth();
  37. selectedYear = currentDate.getFullYear();
  38. }
  39. function setApiVariables(dateModelIndex) {
  40. selectedDate = dateModel.get(dateModelIndex).date
  41. selectedMonth = dateModel.get(dateModelIndex).month
  42. selectedYear = dateModel.get(dateModelIndex).year
  43. console.debug("selectedDate = " + selectedDate)
  44. console.debug("selectedMonth = " + selectedMonth)
  45. console.debug("selectedYear = " + selectedYear)
  46. }
  47. Component {
  48. id: dateSelectorComponent
  49. Item {
  50. property bool today: index == todayIndex
  51. property bool current: ListView.isCurrentItem
  52. height: 64
  53. width: 64
  54. anchors.margins: 4
  55. MouseArea {
  56. anchors.fill: parent
  57. z: 64
  58. onClicked: dateListView.currentIndex = index
  59. }
  60. Text {
  61. id: textItem
  62. font.pixelSize: applicationFontSize - 4
  63. text: today ? qsTr("Today") : model.date + "." + model.month + "."
  64. color: today ? heiaYellow : heiaLightGrey
  65. anchors.horizontalCenter: parent.horizontalCenter
  66. }
  67. Rectangle {
  68. anchors.bottom: parent.bottom
  69. anchors.horizontalCenter: parent.horizontalCenter
  70. height: 32
  71. width: 32
  72. radius: 16
  73. smooth: true
  74. color: current ? heiaFocusColorOuter : heiaLightGrey
  75. Rectangle {
  76. anchors.horizontalCenter: parent.horizontalCenter
  77. anchors.verticalCenter: parent.verticalCenter
  78. height: parent.height - heiaBorderWidth
  79. width: parent.width - heiaBorderWidth
  80. radius: 16
  81. smooth: true
  82. color: current ? heiaFocusColorInner : heiaDarkGrey
  83. }
  84. }
  85. }
  86. }
  87. ListModel {
  88. id: dateModel
  89. }
  90. ListView {
  91. id: dateListView
  92. anchors.fill: parent
  93. model: dateModel
  94. delegate: dateSelectorComponent
  95. onCurrentIndexChanged: setApiVariables(currentIndex)
  96. orientation: ListView.Horizontal
  97. Component.onCompleted: {
  98. populate()
  99. dateListView.currentIndex = todayIndex
  100. positionViewAtIndex(todayIndex, ListView.Center);
  101. }
  102. }
  103. }