/src/qml/QtQuick/Controls/Calendar.qml

https://github.com/yaoliyc/deepin-boot-maker · QML · 412 lines · 127 code · 39 blank · 246 comment · 13 complexity · 1bf1216ccef567d7187428b03450c381 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** You may use this file under the terms of the BSD license as follows:
  10. **
  11. ** "Redistribution and use in source and binary forms, with or without
  12. ** modification, are permitted provided that the following conditions are
  13. ** met:
  14. ** * Redistributions of source code must retain the above copyright
  15. ** notice, this list of conditions and the following disclaimer.
  16. ** * Redistributions in binary form must reproduce the above copyright
  17. ** notice, this list of conditions and the following disclaimer in
  18. ** the documentation and/or other materials provided with the
  19. ** distribution.
  20. ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
  21. ** of its contributors may be used to endorse or promote products derived
  22. ** from this software without specific prior written permission.
  23. **
  24. **
  25. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  36. **
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. import QtQuick 2.2
  41. import QtQuick.Controls 1.2
  42. import QtQuick.Controls.Styles 1.1
  43. import QtQuick.Controls.Private 1.0
  44. /*!
  45. \qmltype Calendar
  46. \inqmlmodule QtQuick.Controls
  47. \since 5.3
  48. \ingroup controls
  49. \brief Provides a way to select dates from a calendar
  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. The selected date is displayed using the format in the application's
  59. default locale.
  60. Week numbers can be displayed by setting the weekNumbersVisible property to
  61. \c true.
  62. You can create a custom appearance for Calendar by assigning a
  63. \l {QtQuick.Controls.Styles::CalendarStyle}{CalendarStyle}.
  64. */
  65. Control {
  66. id: calendar
  67. implicitWidth: 250
  68. implicitHeight: 250
  69. /*!
  70. \qmlproperty date Calendar::selectedDate
  71. The date that has been selected by the user.
  72. This property is subject to the following validation:
  73. \list
  74. \li If selectedDate is outside the range of \l minimumDate and
  75. \l maximumDate, it will be clamped to be within that range.
  76. \li selectedDate will not be changed if \c undefined or some other
  77. invalid value is assigned.
  78. \li If there are hours, minutes, seconds or milliseconds set, they
  79. will be removed.
  80. \endlist
  81. The default value is the current date, which is equivalent to:
  82. \code
  83. new Date()
  84. \endcode
  85. */
  86. property alias selectedDate: rangedDate.date
  87. /*!
  88. \qmlproperty date Calendar::minimumDate
  89. The earliest date that this calendar will accept.
  90. By default, this property is set to the earliest minimum date
  91. (1 January, 1 AD).
  92. */
  93. property alias minimumDate: rangedDate.minimumDate
  94. /*!
  95. \qmlproperty date Calendar::maximumDate
  96. The latest date that this calendar will accept.
  97. By default, this property is set to the latest maximum date
  98. (25 October, 275759 AD).
  99. */
  100. property alias maximumDate: rangedDate.maximumDate
  101. /*!
  102. This property determines which month in visibleYear is shown on the
  103. calendar.
  104. The month is from \c 0 to \c 11 to be consistent with the JavaScript
  105. Date object.
  106. \sa visibleYear
  107. */
  108. property int visibleMonth: selectedDate.getMonth()
  109. /*!
  110. This property determines which year is shown on the
  111. calendar.
  112. \sa visibleMonth
  113. */
  114. property int visibleYear: selectedDate.getFullYear()
  115. onSelectedDateChanged: {
  116. // When the selected date changes, the view should move back to that date.
  117. visibleMonth = selectedDate.getMonth();
  118. visibleYear = selectedDate.getFullYear();
  119. }
  120. RangedDate {
  121. id: rangedDate
  122. date: new Date()
  123. minimumDate: CalendarUtils.minimumCalendarDate
  124. maximumDate: CalendarUtils.maximumCalendarDate
  125. }
  126. /*!
  127. This property determines the visibility of the frame
  128. surrounding the calendar.
  129. The default value is \c true.
  130. */
  131. property bool frameVisible: true
  132. /*!
  133. This property determines the visibility of week numbers.
  134. The default value is \c false.
  135. */
  136. property bool weekNumbersVisible: false
  137. /*!
  138. \qmlproperty enum Calendar::dayOfWeekFormat
  139. The format in which the days of the week (in the header) are displayed.
  140. \c Locale.ShortFormat is the default and recommended format, as
  141. \c Locale.NarrowFormat may not be fully supported by each locale (see
  142. \l {Locale String Format Types}) and
  143. \c Locale.LongFormat may not fit within the header cells.
  144. */
  145. property int dayOfWeekFormat: Locale.ShortFormat
  146. /*!
  147. The locale that this calendar should use to display itself.
  148. Affects how dates and day names are localized, as well as which
  149. day is considered the first in a week.
  150. To set an Australian locale, for example:
  151. \code
  152. locale: Qt.locale("en_AU")
  153. \endcode
  154. The default locale is \c Qt.locale().
  155. */
  156. property var __locale: Qt.locale()
  157. /*!
  158. \internal
  159. This property holds the model that will be used by the Calendar to
  160. populate the dates available to the user.
  161. */
  162. property CalendarModel __model: CalendarModel {
  163. locale: calendar.__locale
  164. visibleDate: new Date(visibleYear, visibleMonth, 1)
  165. }
  166. style: Qt.createComponent(Settings.style + "/CalendarStyle.qml", calendar)
  167. /*!
  168. \qmlsignal Calendar::hovered(date date)
  169. Emitted when the mouse hovers over a valid date in the calendar.
  170. \a date is the date that was hovered over.
  171. The corresponding handler is \c onHovered.
  172. */
  173. signal hovered(date date)
  174. /*!
  175. \qmlsignal Calendar::pressed(date date)
  176. Emitted when the mouse is pressed on a valid date in the calendar.
  177. This is also emitted when dragging the mouse to another date while it is pressed.
  178. \a date is the date that the mouse was pressed on.
  179. The corresponding handler is \c onPressed.
  180. */
  181. signal pressed(date date)
  182. /*!
  183. \qmlsignal Calendar::released(date date)
  184. Emitted when the mouse is released over a valid date in the calendar.
  185. \a date is the date that the mouse was released over.
  186. The corresponding handler is \c onReleased.
  187. */
  188. signal released(date date)
  189. /*!
  190. \qmlsignal Calendar::clicked(date date)
  191. Emitted when the mouse is clicked on a valid date in the calendar.
  192. \a date is the date that the mouse was clicked on.
  193. The corresponding handler is \c onClicked.
  194. */
  195. signal clicked(date date)
  196. /*!
  197. \qmlsignal Calendar::doubleClicked(date date)
  198. Emitted when the mouse is double-clicked on a valid date in the calendar.
  199. \a date is the date that the mouse was double-clicked on.
  200. The corresponding handler is \c onDoubleClicked.
  201. */
  202. signal doubleClicked(date date)
  203. /*!
  204. Sets visibleMonth to the previous month.
  205. */
  206. function showPreviousMonth() {
  207. if (visibleMonth === 0) {
  208. visibleMonth = CalendarUtils.monthsInAYear - 1;
  209. --visibleYear;
  210. } else {
  211. --visibleMonth;
  212. }
  213. }
  214. /*!
  215. Sets visibleMonth to the next month.
  216. */
  217. function showNextMonth() {
  218. if (visibleMonth === CalendarUtils.monthsInAYear - 1) {
  219. visibleMonth = 0;
  220. ++visibleYear;
  221. } else {
  222. ++visibleMonth;
  223. }
  224. }
  225. /*!
  226. Sets visibleYear to the previous year.
  227. */
  228. function showPreviousYear() {
  229. if (visibleYear - 1 >= minimumDate.getFullYear()) {
  230. --visibleYear;
  231. }
  232. }
  233. /*!
  234. Sets visibleYear to the next year.
  235. */
  236. function showNextYear() {
  237. if (visibleYear + 1 <= maximumDate.getFullYear()) {
  238. ++visibleYear;
  239. }
  240. }
  241. /*!
  242. Selects the month before the current month in \l selectedDate.
  243. */
  244. function __selectPreviousMonth() {
  245. calendar.selectedDate = CalendarUtils.setMonth(calendar.selectedDate, calendar.selectedDate.getMonth() - 1);
  246. }
  247. /*!
  248. Selects the month after the current month in \l selectedDate.
  249. */
  250. function __selectNextMonth() {
  251. calendar.selectedDate = CalendarUtils.setMonth(calendar.selectedDate, calendar.selectedDate.getMonth() + 1);
  252. }
  253. /*!
  254. Selects the week before the current week in \l selectedDate.
  255. */
  256. function __selectPreviousWeek() {
  257. var newDate = new Date(calendar.selectedDate);
  258. newDate.setDate(newDate.getDate() - CalendarUtils.daysInAWeek);
  259. calendar.selectedDate = newDate;
  260. }
  261. /*!
  262. Selects the week after the current week in \l selectedDate.
  263. */
  264. function __selectNextWeek() {
  265. var newDate = new Date(calendar.selectedDate);
  266. newDate.setDate(newDate.getDate() + CalendarUtils.daysInAWeek);
  267. calendar.selectedDate = newDate;
  268. }
  269. /*!
  270. Selects the first day of the current month in \l selectedDate.
  271. */
  272. function __selectFirstDayOfMonth() {
  273. var newDate = new Date(calendar.selectedDate);
  274. newDate.setDate(1);
  275. calendar.selectedDate = newDate;
  276. }
  277. /*!
  278. Selects the last day of the current month in \l selectedDate.
  279. */
  280. function __selectLastDayOfMonth() {
  281. var newDate = new Date(calendar.selectedDate);
  282. newDate.setDate(CalendarUtils.daysInMonth(newDate));
  283. calendar.selectedDate = newDate;
  284. }
  285. /*!
  286. Selects the day before the current day in \l selectedDate.
  287. */
  288. function __selectPreviousDay() {
  289. var newDate = new Date(calendar.selectedDate);
  290. newDate.setDate(newDate.getDate() - 1);
  291. calendar.selectedDate = newDate;
  292. }
  293. /*!
  294. Selects the day after the current day in \l selectedDate.
  295. */
  296. function __selectNextDay() {
  297. var newDate = new Date(calendar.selectedDate);
  298. newDate.setDate(newDate.getDate() + 1);
  299. calendar.selectedDate = newDate;
  300. }
  301. Keys.onLeftPressed: {
  302. calendar.__selectPreviousDay();
  303. }
  304. Keys.onUpPressed: {
  305. calendar.__selectPreviousWeek();
  306. }
  307. Keys.onDownPressed: {
  308. calendar.__selectNextWeek();
  309. }
  310. Keys.onRightPressed: {
  311. calendar.__selectNextDay();
  312. }
  313. Keys.onPressed: {
  314. if (event.key === Qt.Key_Home) {
  315. calendar.__selectFirstDayOfMonth();
  316. event.accepted = true;
  317. } else if (event.key === Qt.Key_End) {
  318. calendar.__selectLastDayOfMonth();
  319. event.accepted = true;
  320. } else if (event.key === Qt.Key_PageUp) {
  321. calendar.__selectPreviousMonth();
  322. event.accepted = true;
  323. } else if (event.key === Qt.Key_PageDown) {
  324. calendar.__selectNextMonth();
  325. event.accepted = true;
  326. }
  327. }
  328. }