CalendarUtils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. .pragma library
  40. var daysInAWeek = 7;
  41. var monthsInAYear = 12;
  42. // Not the number of weeks per month, but the number of weeks that are
  43. // shown on a typical calendar.
  44. var weeksOnACalendarMonth = 6;
  45. // Can't create year 1 directly...
  46. var minimumCalendarDate = new Date(-1, 0, 1);
  47. minimumCalendarDate.setFullYear(minimumCalendarDate.getFullYear() + 2);
  48. var maximumCalendarDate = new Date(275759, 9, 25);
  49. function daysInMonth(date) {
  50. // Passing 0 as the day will give us the previous month, which will be
  51. // date.getMonth() since we added 1 to it.
  52. return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
  53. }
  54. /*!
  55. Returns a copy of \a date with its month set to \a month, keeping the same
  56. day if possible. Does not modify \a date.
  57. */
  58. function setMonth(date, month) {
  59. var oldDay = date.getDate();
  60. var newDate = new Date(date);
  61. // Set the day first, because setting the month could cause it to skip ahead
  62. // a month if the day is larger than the latest day in that month.
  63. newDate.setDate(1);
  64. newDate.setMonth(month);
  65. // We'd like to have the previous day still selected when we change
  66. // months, but it might not be possible, so use the smallest of the two.
  67. newDate.setDate(Math.min(oldDay, daysInMonth(newDate)));
  68. return newDate;
  69. }
  70. /*!
  71. Returns the cell rectangle for the cell at the given \a index, assuming
  72. that the grid has a number of columns equal to \a columns and rows
  73. equal to \a rows, with an available width of \a availableWidth and height
  74. of \a availableHeight.
  75. If \a gridLineWidth is greater than \c 0, the cell rectangle will be
  76. calculated under the assumption that there is a grid between the cells:
  77. 31 | 1 | 2 | 3 | 4 | 5 | 6
  78. --------------------------------
  79. 7 | 8 | 9 | 10 | 11 | 12 | 13
  80. --------------------------------
  81. 14 | 15 | 16 | 17 | 18 | 19 | 20
  82. --------------------------------
  83. 21 | 22 | 23 | 24 | 25 | 26 | 27
  84. --------------------------------
  85. 28 | 29 | 30 | 31 | 1 | 2 | 3
  86. --------------------------------
  87. 4 | 5 | 6 | 7 | 8 | 9 | 10
  88. */
  89. function cellRectAt(index, columns, rows, availableWidth, availableHeight, gridLineWidth) {
  90. var col = Math.floor(index % columns);
  91. var row = Math.floor(index / columns);
  92. var availableWidthMinusGridLines = availableWidth - ((columns - 1) * gridLineWidth);
  93. var availableHeightMinusGridLines = availableHeight - ((rows - 1) * gridLineWidth);
  94. var remainingHorizontalSpace = Math.floor(availableWidthMinusGridLines % columns);
  95. var remainingVerticalSpace = Math.floor(availableHeightMinusGridLines % rows);
  96. var baseCellWidth = Math.floor(availableWidthMinusGridLines / columns);
  97. var baseCellHeight = Math.floor(availableHeightMinusGridLines / rows);
  98. var rect = Qt.rect(0, 0, 0, 0);
  99. rect.x = baseCellWidth * col;
  100. rect.width = baseCellWidth;
  101. if (remainingHorizontalSpace > 0) {
  102. if (col < remainingHorizontalSpace) {
  103. ++rect.width;
  104. }
  105. // This cell's x position should be increased by 1 for every column above it.
  106. rect.x += Math.min(remainingHorizontalSpace, col);
  107. }
  108. rect.y = baseCellHeight * row;
  109. rect.height = baseCellHeight;
  110. if (remainingVerticalSpace > 0) {
  111. if (row < remainingVerticalSpace) {
  112. ++rect.height;
  113. }
  114. // This cell's y position should be increased by 1 for every row above it.
  115. rect.y += Math.min(remainingVerticalSpace, row);
  116. }
  117. rect.x += col * gridLineWidth;
  118. rect.y += row * gridLineWidth;
  119. return rect;
  120. }