Calendar.qml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. import QtQuick 2.9
  40. import QtQuick.Controls 1.5
  41. import QtQuick.Controls.Styles 1.1
  42. import QtQuick.Controls.Private 1.0
  43. /*!
  44. \qmltype Calendar
  45. \inqmlmodule QtQuick.Controls
  46. \since 5.3
  47. \ingroup controls
  48. \brief Provides a way to select dates from a calendar.
  49. \image calendar.png
  50. Calendar allows selection of dates from a grid of days, similar to
  51. QCalendarWidget.
  52. The dates on the calendar can be selected with the mouse, or navigated
  53. with the keyboard.
  54. The selected date can be set through \l selectedDate.
  55. A minimum and maximum date can be set through \l minimumDate and
  56. \l maximumDate. The earliest minimum date that can be set is 1 January, 1
  57. AD. The latest maximum date that can be set is 25 October, 275759 AD.
  58. \code
  59. Calendar {
  60. minimumDate: new Date(2017, 0, 1)
  61. maximumDate: new Date(2018, 0, 1)
  62. }
  63. \endcode
  64. The selected date is displayed using the format in the application's
  65. default locale.
  66. Week numbers can be displayed by setting the weekNumbersVisible property to
  67. \c true.
  68. \qml
  69. Calendar {
  70. weekNumbersVisible: true
  71. }
  72. \endqml
  73. You can create a custom appearance for Calendar by assigning a
  74. \l {CalendarStyle}.
  75. */
  76. Control {
  77. id: calendar
  78. /*!
  79. \qmlproperty date Calendar::selectedDate
  80. The date that has been selected by the user.
  81. This property is subject to the following validation:
  82. \list
  83. \li If selectedDate is outside the range of \l minimumDate and
  84. \l maximumDate, it will be clamped to be within that range.
  85. \li selectedDate will not be changed if \c undefined or some other
  86. invalid value is assigned.
  87. \li If there are hours, minutes, seconds or milliseconds set, they
  88. will be removed.
  89. \endlist
  90. The default value is the current date, which is equivalent to:
  91. \code
  92. new Date()
  93. \endcode
  94. */
  95. property alias selectedDate: rangedDate.date
  96. /*!
  97. \qmlproperty date Calendar::minimumDate
  98. The earliest date that this calendar will accept.
  99. By default, this property is set to the earliest minimum date
  100. (1 January, 1 AD).
  101. */
  102. property alias minimumDate: rangedDate.minimumDate
  103. /*!
  104. \qmlproperty date Calendar::maximumDate
  105. The latest date that this calendar will accept.
  106. By default, this property is set to the latest maximum date
  107. (25 October, 275759 AD).
  108. */
  109. property alias maximumDate: rangedDate.maximumDate
  110. /*!
  111. This property determines which month in visibleYear is shown on the
  112. calendar.
  113. The month is from \c 0 to \c 11 to be consistent with the JavaScript
  114. Date object.
  115. \sa visibleYear
  116. */
  117. property int visibleMonth: selectedDate.getMonth()
  118. /*!
  119. This property determines which year is shown on the
  120. calendar.
  121. \sa visibleMonth
  122. */
  123. property int visibleYear: selectedDate.getFullYear()
  124. onSelectedDateChanged: {
  125. // When the selected date changes, the view should move back to that date.
  126. visibleMonth = selectedDate.getMonth();
  127. visibleYear = selectedDate.getFullYear();
  128. }
  129. RangedDate {
  130. id: rangedDate
  131. date: new Date()
  132. minimumDate: CalendarUtils.minimumCalendarDate
  133. maximumDate: CalendarUtils.maximumCalendarDate
  134. }
  135. /*!
  136. This property determines the visibility of the frame
  137. surrounding the calendar.
  138. The default value is \c true.
  139. */
  140. property bool frameVisible: true
  141. /*!
  142. This property determines the visibility of week numbers.
  143. The default value is \c false.
  144. */
  145. property bool weekNumbersVisible: false
  146. /*!
  147. This property determines the visibility of the navigation bar.
  148. \since QtQuick.Controls 1.3
  149. The default value is \c true.
  150. */
  151. property bool navigationBarVisible: true
  152. /*!
  153. \qmlproperty enum Calendar::dayOfWeekFormat
  154. The format in which the days of the week (in the header) are displayed.
  155. \c Locale.ShortFormat is the default and recommended format, as
  156. \c Locale.NarrowFormat may not be fully supported by each locale (see
  157. \l {Locale String Format Types}) and
  158. \c Locale.LongFormat may not fit within the header cells.
  159. */
  160. property int dayOfWeekFormat: Locale.ShortFormat
  161. /*!
  162. \qmlproperty object Calendar::locale
  163. \since QtQuick.Controls 1.6
  164. This property controls the locale that this calendar uses to display
  165. itself.
  166. The locale affects how dates and day names are localized, as well as
  167. which day is considered the first in a week.
  168. The following example sets an Australian locale:
  169. \code
  170. locale: Qt.locale("en_AU")
  171. \endcode
  172. The default value is equivalent to \c Qt.locale().
  173. */
  174. property var locale: Qt.locale()
  175. // left for compatibility reasons; can be removed in next minor version/Qt 6
  176. property alias __locale: calendar.locale
  177. /*!
  178. \internal
  179. This property holds the model that will be used by the Calendar to
  180. populate the dates available to the user.
  181. */
  182. property CalendarModel __model: CalendarModel {
  183. locale: calendar.locale
  184. // TODO: don't set the hour when QTBUG-56787 is fixed
  185. visibleDate: new Date(visibleYear, visibleMonth, 1, 12)
  186. }
  187. style: Settings.styleComponent(Settings.style, "CalendarStyle.qml", calendar)
  188. /*!
  189. \qmlsignal Calendar::hovered(date date)
  190. Emitted when the mouse hovers over a valid date in the calendar.
  191. \e date is the date that was hovered over.
  192. The corresponding handler is \c onHovered.
  193. */
  194. signal hovered(date date)
  195. /*!
  196. \qmlsignal Calendar::pressed(date date)
  197. Emitted when the mouse is pressed on a valid date in the calendar.
  198. This is also emitted when dragging the mouse to another date while it is pressed.
  199. \e date is the date that the mouse was pressed on.
  200. The corresponding handler is \c onPressed.
  201. */
  202. signal pressed(date date)
  203. /*!
  204. \qmlsignal Calendar::released(date date)
  205. Emitted when the mouse is released over a valid date in the calendar.
  206. \e date is the date that the mouse was released over.
  207. The corresponding handler is \c onReleased.
  208. */
  209. signal released(date date)
  210. /*!
  211. \qmlsignal Calendar::clicked(date date)
  212. Emitted when the mouse is clicked on a valid date in the calendar.
  213. \e date is the date that the mouse was clicked on.
  214. The corresponding handler is \c onClicked.
  215. */
  216. signal clicked(date date)
  217. /*!
  218. \qmlsignal Calendar::doubleClicked(date date)
  219. Emitted when the mouse is double-clicked on a valid date in the calendar.
  220. \e date is the date that the mouse was double-clicked on.
  221. The corresponding handler is \c onDoubleClicked.
  222. */
  223. signal doubleClicked(date date)
  224. /*!
  225. \qmlsignal Calendar::pressAndHold(date date)
  226. \since QtQuick.Controls 1.3
  227. Emitted when the mouse is pressed and held on a valid date in the calendar.
  228. \e date is the date that the mouse was pressed on.
  229. The corresponding handler is \c onPressAndHold.
  230. */
  231. signal pressAndHold(date date)
  232. /*!
  233. \qmlmethod void Calendar::showPreviousMonth()
  234. Sets visibleMonth to the previous month.
  235. */
  236. function showPreviousMonth() {
  237. if (visibleMonth === 0) {
  238. visibleMonth = CalendarUtils.monthsInAYear - 1;
  239. --visibleYear;
  240. } else {
  241. --visibleMonth;
  242. }
  243. }
  244. /*!
  245. \qmlmethod void Calendar::showNextMonth()
  246. Sets visibleMonth to the next month.
  247. */
  248. function showNextMonth() {
  249. if (visibleMonth === CalendarUtils.monthsInAYear - 1) {
  250. visibleMonth = 0;
  251. ++visibleYear;
  252. } else {
  253. ++visibleMonth;
  254. }
  255. }
  256. /*!
  257. \qmlmethod void Calendar::showPreviousYear()
  258. Sets visibleYear to the previous year.
  259. */
  260. function showPreviousYear() {
  261. if (visibleYear - 1 >= minimumDate.getFullYear()) {
  262. --visibleYear;
  263. }
  264. }
  265. /*!
  266. \qmlmethod void Calendar::showNextYear()
  267. Sets visibleYear to the next year.
  268. */
  269. function showNextYear() {
  270. if (visibleYear + 1 <= maximumDate.getFullYear()) {
  271. ++visibleYear;
  272. }
  273. }
  274. /*!
  275. Selects the month before the current month in \l selectedDate.
  276. */
  277. function __selectPreviousMonth() {
  278. calendar.selectedDate = CalendarUtils.setMonth(calendar.selectedDate, calendar.selectedDate.getMonth() - 1);
  279. }
  280. /*!
  281. Selects the month after the current month in \l selectedDate.
  282. */
  283. function __selectNextMonth() {
  284. calendar.selectedDate = CalendarUtils.setMonth(calendar.selectedDate, calendar.selectedDate.getMonth() + 1);
  285. }
  286. /*!
  287. Selects the week before the current week in \l selectedDate.
  288. */
  289. function __selectPreviousWeek() {
  290. var newDate = new Date(calendar.selectedDate);
  291. newDate.setDate(newDate.getDate() - CalendarUtils.daysInAWeek);
  292. calendar.selectedDate = newDate;
  293. }
  294. /*!
  295. Selects the week after the current week in \l selectedDate.
  296. */
  297. function __selectNextWeek() {
  298. var newDate = new Date(calendar.selectedDate);
  299. newDate.setDate(newDate.getDate() + CalendarUtils.daysInAWeek);
  300. calendar.selectedDate = newDate;
  301. }
  302. /*!
  303. Selects the first day of the current month in \l selectedDate.
  304. */
  305. function __selectFirstDayOfMonth() {
  306. var newDate = new Date(calendar.selectedDate);
  307. newDate.setDate(1);
  308. calendar.selectedDate = newDate;
  309. }
  310. /*!
  311. Selects the last day of the current month in \l selectedDate.
  312. */
  313. function __selectLastDayOfMonth() {
  314. var newDate = new Date(calendar.selectedDate);
  315. newDate.setDate(CalendarUtils.daysInMonth(newDate));
  316. calendar.selectedDate = newDate;
  317. }
  318. /*!
  319. Selects the day before the current day in \l selectedDate.
  320. */
  321. function __selectPreviousDay() {
  322. var newDate = new Date(calendar.selectedDate);
  323. newDate.setDate(newDate.getDate() - 1);
  324. calendar.selectedDate = newDate;
  325. }
  326. /*!
  327. Selects the day after the current day in \l selectedDate.
  328. */
  329. function __selectNextDay() {
  330. var newDate = new Date(calendar.selectedDate);
  331. newDate.setDate(newDate.getDate() + 1);
  332. calendar.selectedDate = newDate;
  333. }
  334. Keys.onLeftPressed: {
  335. calendar.__selectPreviousDay();
  336. }
  337. Keys.onUpPressed: {
  338. calendar.__selectPreviousWeek();
  339. }
  340. Keys.onDownPressed: {
  341. calendar.__selectNextWeek();
  342. }
  343. Keys.onRightPressed: {
  344. calendar.__selectNextDay();
  345. }
  346. Keys.onPressed: {
  347. if (event.key === Qt.Key_Home) {
  348. calendar.__selectFirstDayOfMonth();
  349. event.accepted = true;
  350. } else if (event.key === Qt.Key_End) {
  351. calendar.__selectLastDayOfMonth();
  352. event.accepted = true;
  353. } else if (event.key === Qt.Key_PageUp) {
  354. calendar.__selectPreviousMonth();
  355. event.accepted = true;
  356. } else if (event.key === Qt.Key_PageDown) {
  357. calendar.__selectNextMonth();
  358. event.accepted = true;
  359. }
  360. }
  361. }